ApiController.java
4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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)
.orElse("");
}
/**
* 获取用户脱敏信息
*
* @param data 认证code
* @return 用户信息
*/
@PostMapping("/user/info")
public Object getUserInfoByAuthCode(@RequestBody JSONObject data) {
//通过authCode获取用户手机号
String phone = getPhone(data.getString("authCode"));
if (StrUtil.isBlank(phone)) {
return DecodeBody.fail("-1", "获取手机号失败");
}
//判断手机号是否已经注册
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());
}
}