|
@@ -1,21 +1,24 @@
|
1
|
1
|
package com.yunzhi.shigongli.controller;
|
2
|
2
|
|
3
|
3
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
4
|
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
|
5
|
+import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
4
|
6
|
import com.yunzhi.shigongli.common.*;
|
5
|
7
|
import com.yunzhi.shigongli.entity.TaMiniappQrcode;
|
6
|
8
|
import com.yunzhi.shigongli.entity.TaPerson;
|
|
9
|
+import com.yunzhi.shigongli.entity.TaPersonRole;
|
7
|
10
|
import com.yunzhi.shigongli.service.ITaMiniappQrcodeService;
|
|
11
|
+import com.yunzhi.shigongli.service.ITaPersonRecommenderService;
|
|
12
|
+import com.yunzhi.shigongli.service.ITaPersonRoleService;
|
8
|
13
|
import com.yunzhi.shigongli.service.ITaPersonService;
|
|
14
|
+import com.yunzhi.shigongli.vo.WxMaAuthParam;
|
9
|
15
|
import io.swagger.annotations.Api;
|
10
|
16
|
import io.swagger.annotations.ApiOperation;
|
11
|
|
-import me.chanjar.weixin.common.error.WxErrorException;
|
|
17
|
+import io.swagger.annotations.ApiParam;
|
12
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
19
|
import org.springframework.web.bind.annotation.*;
|
14
|
20
|
|
15
|
|
-import java.util.Arrays;
|
16
|
|
-import java.util.HashMap;
|
17
|
|
-import java.util.Map;
|
18
|
|
-import java.util.Optional;
|
|
21
|
+import java.util.*;
|
19
|
22
|
|
20
|
23
|
|
21
|
24
|
@Api(tags = "微信小程序")
|
|
@@ -32,7 +35,13 @@ public class WxMaController extends BaseController {
|
32
|
35
|
@Autowired
|
33
|
36
|
ITaMiniappQrcodeService miniappQrcodeService;
|
34
|
37
|
|
35
|
|
- @GetMapping("prefetch")
|
|
38
|
+ @Autowired
|
|
39
|
+ ITaPersonRecommenderService recommenderService;
|
|
40
|
+
|
|
41
|
+ @Autowired
|
|
42
|
+ ITaPersonRoleService personRoleService;
|
|
43
|
+
|
|
44
|
+ @GetMapping("/prefetch")
|
36
|
45
|
@ApiOperation(value="微信预拉取数据", notes = "微信预拉取数据", httpMethod = "GET", response = ResponseBean.class)
|
37
|
46
|
public ResponseBean perfetch(String appid,
|
38
|
47
|
String token,
|
|
@@ -44,19 +53,13 @@ public class WxMaController extends BaseController {
|
44
|
53
|
WxMaJscode2SessionResult info = wxUtils.getService().getUserService().getSessionInfo(code);
|
45
|
54
|
String openid = info.getOpenid();
|
46
|
55
|
|
47
|
|
- TaPerson taPerson = iTaPersonService.getByOpenid(openid);
|
48
|
|
- if (null == taPerson) {
|
49
|
|
- taPerson = new TaPerson();
|
50
|
|
- taPerson.setOpenid(openid);
|
51
|
|
- iTaPersonService.save(taPerson);
|
52
|
|
- // 插入过程会有默认字段写入
|
53
|
|
- taPerson = iTaPersonService.getById(taPerson.getPersonId());
|
54
|
|
- }
|
|
56
|
+ TaPerson taPerson = iTaPersonService.getByOpenidOrNot(openid);
|
55
|
57
|
|
56
|
58
|
Map<String, Object> result = new HashMap<>();
|
57
|
|
- result.put("person", taPerson);
|
|
59
|
+ result.put("personId", taPerson.getPersonId());
|
|
60
|
+ result.put("openid", taPerson.getOpenid());
|
|
61
|
+ result.put("sessionKey", info.getSessionKey());
|
58
|
62
|
|
59
|
|
- // 如果是通过扫码打开
|
60
|
63
|
if (!StringUtils.isEmpty(query)) {
|
61
|
64
|
Map<String, String> params = StringUtils.parseQueryString(query);
|
62
|
65
|
|
|
@@ -83,116 +86,108 @@ public class WxMaController extends BaseController {
|
83
|
86
|
}
|
84
|
87
|
|
85
|
88
|
// 如果有推广ID, 比如民宿或者商铺
|
86
|
|
- if (StringUtils.isEmpty(taPerson.getRecommender()) && !StringUtils.isEmpty(recommender)) {
|
87
|
|
- taPerson.setFromType(fromType);
|
88
|
|
- taPerson.setRecommender(recommender);
|
89
|
|
- iTaPersonService.setRecommender(taPerson);
|
90
|
|
- }
|
|
89
|
+ if (!StringUtils.isEmpty(recommender)) {
|
|
90
|
+ if (StringUtils.isEmpty(taPerson.getRecommender())) {
|
|
91
|
+ taPerson.setFromType(fromType);
|
|
92
|
+ taPerson.setRecommender(recommender);
|
|
93
|
+ iTaPersonService.updateById(taPerson);
|
|
94
|
+ }
|
91
|
95
|
|
|
96
|
+ // 写入推荐表
|
|
97
|
+ try {
|
|
98
|
+ recommenderService.addNewRecommender(taPerson.getPersonId(), fromType, recommender);
|
|
99
|
+ } catch (Exception e) {
|
|
100
|
+ e.printStackTrace();;
|
|
101
|
+ }
|
|
102
|
+ }
|
92
|
103
|
}
|
93
|
104
|
|
|
105
|
+ // 生成 token
|
|
106
|
+ Map<String, Object> tokenClaims = new HashMap<>();
|
|
107
|
+ tokenClaims.put("userId", taPerson.getPersonId());
|
|
108
|
+ tokenClaims.put("miniApp", true);
|
|
109
|
+ String authToken = JWTUtils.encode(tokenClaims);
|
|
110
|
+ result.put("token", authToken);
|
|
111
|
+
|
94
|
112
|
return ResponseBean.success(result);
|
95
|
113
|
}
|
96
|
114
|
|
97
|
|
-// @PostMapping("/login")
|
98
|
|
-// @ApiOperation(value="登录", notes = "登录", httpMethod = "POST", response = ResponseBean.class)
|
99
|
|
-// public ResponseBean login(@ApiParam("登录参数") @RequestParam String code) throws Exception {
|
100
|
|
-// WxMaJscode2SessionResult info = wxUtils.getService().getUserService().getSessionInfo(code);
|
101
|
|
-//
|
102
|
|
-// String openid = info.getOpenid();
|
103
|
|
-// TaPerson taPerson = iTaPersonService.getByOpenid(openid);
|
104
|
|
-// if (null == taPerson) {
|
105
|
|
-// taPerson = new TaPerson();
|
106
|
|
-// taPerson.setOpenid(openid);
|
107
|
|
-// iTaPersonService.save(taPerson);
|
108
|
|
-//
|
109
|
|
-// TaPersonData taPersonData = new TaPersonData();
|
110
|
|
-// taPersonData.setPersonId(taPerson.getPersonId());
|
111
|
|
-// iTaPersonDataService.save(taPersonData);
|
112
|
|
-// }
|
113
|
|
-//
|
114
|
|
-// // 查询学生
|
115
|
|
-// TaStudent taStudent = iTaStudentService.getByPersonId(taPerson.getPersonId());
|
116
|
|
-// taPerson.setStudent(taStudent);
|
117
|
|
-//
|
118
|
|
-// Map<String, Object> tokenClaims = new HashMap<>();
|
119
|
|
-// tokenClaims.put("userId", taPerson.getPersonId());
|
120
|
|
-//// tokenClaims.put("sessionKey", info.getSessionKey());
|
121
|
|
-// tokenClaims.put("miniApp", true);
|
122
|
|
-// String token = JWTUtils.encode(tokenClaims);
|
123
|
|
-//
|
124
|
|
-// Map<String, Object> rtn = new HashMap<>();
|
125
|
|
-// rtn.put("person", taPerson);
|
126
|
|
-// rtn.put("sessionKey", info.getSessionKey());
|
127
|
|
-// rtn.put("token", token);
|
128
|
|
-//
|
129
|
|
-// return ResponseBean.success(rtn);
|
130
|
|
-// }
|
131
|
|
-//
|
132
|
|
-// @PutMapping("/auth-user")
|
133
|
|
-// @ApiOperation(value="授权头像", notes = "授权头像", httpMethod = "PUT", response = TaPerson.class)
|
134
|
|
-// public ResponseBean updateUserInfo(@ApiParam("授权头像的参数") @RequestBody WxMaAuthParam params) throws Exception {
|
135
|
|
-// checkAuthParams(params);
|
136
|
|
-//
|
137
|
|
-// // 解密用户信息
|
138
|
|
-// WxMaUserInfo userInfo = wxUtils.getService().getUserService().getUserInfo(params.getSessionKey(), params.getEncryptedData(), params.getIv());
|
139
|
|
-//
|
140
|
|
-// TaPerson taPerson = getCurrentPerson();
|
141
|
|
-// if (null == taPerson) {
|
142
|
|
-// throw new Exception("校验当前人员失败, 请重试");
|
143
|
|
-// }
|
144
|
|
-//
|
145
|
|
-// taPerson.setAvatar(userInfo.getAvatarUrl());
|
146
|
|
-// taPerson.setNickName(userInfo.getNickName());
|
147
|
|
-// taPerson.setSex(Integer.parseInt(userInfo.getGender()));
|
148
|
|
-//
|
149
|
|
-// iTaPersonService.updateById(taPerson);
|
150
|
|
-//
|
151
|
|
-// return ResponseBean.success(taPerson);
|
152
|
|
-// }
|
153
|
|
-//
|
154
|
|
-// @ApiOperation(value="授权手机", notes = "授权手机", httpMethod = "PUT", response = TaPerson.class)
|
155
|
|
-// @PutMapping("/auth-phone")
|
156
|
|
-// public ResponseBean updateUserPhone(@ApiParam("授权手机的参数") @RequestBody WxMaAuthParam params) throws Exception {
|
157
|
|
-//// checkAuthParams(params);
|
158
|
|
-//
|
159
|
|
-// // 解密
|
160
|
|
-// WxMaPhoneNumberInfo phoneNoInfo = wxUtils.getService().getUserService().getPhoneNoInfo(params.getSessionKey(), params.getEncryptedData(), params.getIv());
|
161
|
|
-//
|
162
|
|
-// TaPerson taPerson = getCurrentPerson();
|
163
|
|
-// if (null == taPerson) {
|
164
|
|
-// throw new Exception("校验当前人员失败, 请重试");
|
165
|
|
-// }
|
166
|
|
-//
|
167
|
|
-// taPerson.setPhone(phoneNoInfo.getPhoneNumber());
|
168
|
|
-// iTaPersonService.updateById(taPerson);
|
169
|
|
-//
|
170
|
|
-// // 查询学生
|
171
|
|
-// TaStudent taStudent = iTaStudentService.getByPhone(taPerson.getPhone());
|
172
|
|
-// if (null != taStudent) {
|
173
|
|
-// taStudent.setPersonId(taPerson.getPersonId());
|
174
|
|
-// iTaStudentService.updateById(taStudent);
|
175
|
|
-// }
|
176
|
|
-//
|
177
|
|
-// return ResponseBean.success(taPerson);
|
178
|
|
-// }
|
179
|
|
-//
|
180
|
|
-// private void checkAuthParams(WxMaAuthParam params) throws Exception {
|
181
|
|
-// String signature = params.getSignature();
|
182
|
|
-// String rawData = params.getRawData();
|
183
|
|
-// String encryptedData = params.getEncryptedData();
|
184
|
|
-// String iv = params.getIv();
|
185
|
|
-// String sessionKey = params.getSessionKey();
|
186
|
|
-//
|
187
|
|
-// if (StringUtils.isEmpty(signature) || StringUtils.isEmpty(rawData) || StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv) || StringUtils.isEmpty(sessionKey)) {
|
188
|
|
-// throw new Exception("缺失授权参数");
|
189
|
|
-// }
|
190
|
|
-//
|
191
|
|
-// boolean isValid = wxUtils.getService().getUserService().checkUserInfo(sessionKey, rawData, signature);
|
192
|
|
-// if (!isValid) {
|
193
|
|
-// throw new Exception("校验授权参数失败, 请重试");
|
194
|
|
-// }
|
195
|
|
-//
|
196
|
|
-// return;
|
197
|
|
-// }
|
|
115
|
+ @PostMapping("/login")
|
|
116
|
+ @ApiOperation(value="登录", notes = "登录", httpMethod = "POST", response = ResponseBean.class)
|
|
117
|
+ public ResponseBean login(@ApiParam("登录参数") @RequestParam String code) throws Exception {
|
|
118
|
+ WxMaJscode2SessionResult info = wxUtils.getService().getUserService().getSessionInfo(code);
|
|
119
|
+
|
|
120
|
+ String openid = info.getOpenid();
|
|
121
|
+ TaPerson taPerson = iTaPersonService.getByOpenidOrNot(openid);
|
|
122
|
+
|
|
123
|
+ // 查询人员角色
|
|
124
|
+ List<TaPersonRole> roleList = personRoleService.getListByPerson(taPerson.getPersonId());
|
|
125
|
+
|
|
126
|
+ Map<String, Object> rtn = new HashMap<>();
|
|
127
|
+ rtn.put("person", taPerson);
|
|
128
|
+ rtn.put("personRoleList", roleList);
|
|
129
|
+ rtn.put("sessionKey", info.getSessionKey());
|
|
130
|
+
|
|
131
|
+ return ResponseBean.success(rtn);
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @PutMapping("/auth-user")
|
|
135
|
+ @ApiOperation(value="授权头像", notes = "授权头像", httpMethod = "PUT", response = TaPerson.class)
|
|
136
|
+ public ResponseBean updateUserInfo(@ApiParam("授权头像的参数") @RequestBody WxMaAuthParam params) throws Exception {
|
|
137
|
+ checkAuthParams(params);
|
|
138
|
+
|
|
139
|
+ // 解密用户信息
|
|
140
|
+ WxMaUserInfo userInfo = wxUtils.getService().getUserService().getUserInfo(params.getSessionKey(), params.getEncryptedData(), params.getIv());
|
|
141
|
+
|
|
142
|
+ TaPerson taPerson = getCurrentPerson();
|
|
143
|
+ if (null == taPerson) {
|
|
144
|
+ throw new Exception("校验当前人员失败, 请重试");
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ taPerson.setAvatar(userInfo.getAvatarUrl());
|
|
148
|
+ taPerson.setNickName(userInfo.getNickName());
|
|
149
|
+ taPerson.setSex(Integer.parseInt(userInfo.getGender()));
|
|
150
|
+
|
|
151
|
+ iTaPersonService.updateById(taPerson);
|
|
152
|
+
|
|
153
|
+ return ResponseBean.success(taPerson);
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ @ApiOperation(value="授权手机", notes = "授权手机", httpMethod = "PUT", response = TaPerson.class)
|
|
157
|
+ @PutMapping("/auth-phone")
|
|
158
|
+ public ResponseBean updateUserPhone(@ApiParam("授权手机的参数") @RequestBody WxMaAuthParam params) throws Exception {
|
|
159
|
+ checkAuthParams(params);
|
|
160
|
+
|
|
161
|
+ // 解密
|
|
162
|
+ WxMaPhoneNumberInfo phoneNoInfo = wxUtils.getService().getUserService().getPhoneNoInfo(params.getSessionKey(), params.getEncryptedData(), params.getIv());
|
|
163
|
+
|
|
164
|
+ TaPerson taPerson = getCurrentPerson();
|
|
165
|
+ if (null == taPerson) {
|
|
166
|
+ throw new Exception("校验当前人员失败, 请重试");
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ taPerson.setPhone(phoneNoInfo.getPhoneNumber());
|
|
170
|
+ iTaPersonService.updateById(taPerson);
|
|
171
|
+
|
|
172
|
+ return ResponseBean.success(taPerson);
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ private void checkAuthParams(WxMaAuthParam params) throws Exception {
|
|
176
|
+ String signature = params.getSignature();
|
|
177
|
+ String rawData = params.getRawData();
|
|
178
|
+ String encryptedData = params.getEncryptedData();
|
|
179
|
+ String iv = params.getIv();
|
|
180
|
+ String sessionKey = params.getSessionKey();
|
|
181
|
+
|
|
182
|
+ if (StringUtils.isEmpty(signature) || StringUtils.isEmpty(rawData) || StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv) || StringUtils.isEmpty(sessionKey)) {
|
|
183
|
+ throw new Exception("缺失授权参数");
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ boolean isValid = wxUtils.getService().getUserService().checkUserInfo(sessionKey, rawData, signature);
|
|
187
|
+ if (!isValid) {
|
|
188
|
+ throw new Exception("校验授权参数失败, 请重试");
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ return;
|
|
192
|
+ }
|
198
|
193
|
}
|