ApiController.java 4.83 KB
package com.topdraw.dockingapi.controller;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.digital.szzz.gateway.sdk.api.GatewaySender;
import com.digital.szzz.gateway.sdk.bean.GatewayResponse;
import com.topdraw.dockingapi.config.EnvConfiguration;
import com.topdraw.dockingapi.entity.DecodeBody;
import com.topdraw.dockingapi.http.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 对接愉快办接口
 *
 * @author wenxin
 * @version 1.0
 * @date 2024/5/9 下午5:46
 */
@RestController
@Slf4j
@RequestMapping("/api")

@CrossOrigin
public class ApiController {
    @Resource
    EnvConfiguration configuration;
    @Resource
    private
    UserService userService;

    String url;
    String appId;
    String iv;
    String appKey;
    String appSecretKey;
    int readTimeout = 5000;
    int connTimeout = 5000;

    @PostConstruct
    public void init() {
        url = configuration.getUrl();
        appId = configuration.getAppId();
        iv = configuration.getAppIv();
        appKey = configuration.getAppKey();
        appSecretKey = configuration.getAppSecret();
    }

    /**
     * 获取jsToken
     *
     * @return token信息
     */
    @PostMapping("/js/token")
    public GatewayResponse getJsApiToken() {
        String authCode = "";
        String method = "ykb.app.jsapi.getToken";
        JSONObject param = new JSONObject();
        param.put("appId", appId);
        Map<String, String> headerMap = new HashMap<>();
        return GatewaySender.send(url, appId, method,
                iv, appKey, appSecretKey,
                param, authCode, headerMap,
                readTimeout, connTimeout);
    }

    /**
     * 获取authCode
     *
     * @param data 用户id
     * @return authCode信息
     */
    @PostMapping("/auth/code")
    public GatewayResponse getAuthCodeByUserId(@RequestBody JSONObject data) {
        String method = "app.ykb.uc.oauth.get";
        JSONObject param = new JSONObject();
        param.put("appId", appId);
        String userId = data.getString("userId");
        param.put("userId", userId);
        param.put("forceScopes", new JSONArray().fluentAdd("ykb_user_info").fluentAdd("ykb_divide"));
        Map<String, String> headerMap = new HashMap<>();
        return GatewaySender.send(url, appId, method,
                iv, appKey, appSecretKey,
                param, "", headerMap,
                readTimeout, connTimeout);
    }


    private String getPhone(String authCode) {
        String method = "app.ykb.uc.oauth.userInfo";
        JSONObject param = new JSONObject();
        param.put("authCode", authCode);
        Map<String, String> headerMap = new HashMap<>();
        GatewayResponse send = GatewaySender.send(url, appId, method,
                iv, appKey, appSecretKey, param, authCode,
                headerMap, readTimeout, connTimeout);
        log.info("获取手机号返回的结果是:{}", JSONArray.toJSONString(send));
        String data = send.getData();
        return Optional.ofNullable(JSONObject.parseObject(data, JSONObject.class))
                .map(t -> t.getJSONObject("data"))
                .map(t -> t.getJSONObject("userInfo"))
                .map(t -> t.getString("phone"))
                .filter(StrUtil::isNotBlank)
                .orElseThrow(() -> new RuntimeException("获取手机号失败"));

    }


    /**
     * 获取用户脱敏信息
     *
     * @param data 认证code
     * @return 用户信息
     */
    @PostMapping("/user/info")
    public DecodeBody<Object> getUserInfoByAuthCode(@RequestBody JSONObject data) {
        //通过authCode获取用户手机号
        String phone = getPhone(data.getString("authCode"));
        //判断手机号是否已经注册
        DecodeBody<JSONObject> register = userService.checkIsRegisterByPhoneNum(MapBuilder.<String, String>create()
                .put("phoneNum", phone).build());
        String isRegister = register.getData().getString("isRegister");
        if ("0".equals(isRegister)) {
            //注册用户
            DecodeBody<Object> body = userService.privateRegister(MapBuilder.<String, String>create()
                    .put("phoneNum", phone)
                    .put("token", "")
                    .build());
            String errcode = body.getErrcode();
            if (!"0".equals(errcode)) {
                //注册失败直接返回失败信息
                return body;
            }
        }
        //获取用户信息
        return userService.privateLogin(MapBuilder.<String, String>create()
                .put("phoneNum", phone)
                .put("token", "").build());
    }
}