WeappUserService.java
15.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package cn.com.glsx.dj.weapp.service;
import java.security.InvalidAlgorithmParameterException;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.oreframework.commons.security.sign.Base64;
import org.oreframework.commons.security.sign.SHA;
import org.oreframework.util.json.JacksonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.glsx.dj.customer.model.MerchantCustomerInfo;
import cn.com.glsx.dj.customer.remote.MerchantCustomerRemote;
import cn.com.glsx.dj.weapp.common.Constants;
import cn.com.glsx.dj.weapp.converter.WeappUserInfoConverter;
import cn.com.glsx.dj.weapp.manager.PlatformAccountManager;
import cn.com.glsx.dj.weapp.util.AES;
import cn.com.glsx.dj.weapp.util.ThreadLocalCache;
import cn.com.glsx.dj.weapp.util.WxPKCS7Encoder;
import cn.com.glsx.dj.weapp.vo.WeappUserInfoVO;
import cn.com.glsx.dj.weapp.vo.WechatAuthSettingVO;
import cn.com.glsx.dj.weapp.vo.WechatPhoneVO;
import cn.com.glsx.dj.weapp.vo.WechatUserInfoVO;
import cn.com.glsx.dj.weapp.web.session.Session;
import cn.com.glsx.dj.wechat.dto.JsCodeSessionDTO;
import cn.com.glsx.dj.wechat.enums.WechatAppTypeEnum;
import cn.com.glsx.dj.wechat.enums.WechatErrorEnum;
import cn.com.glsx.dj.wechat.enums.WechatUserTypeEnum;
import cn.com.glsx.dj.wechat.model.WeappBaseInfo;
import cn.com.glsx.dj.wechat.model.WechatUserInfo;
import cn.com.glsx.dj.wechat.remote.WeappBaseInfoRemote;
import cn.com.glsx.dj.wechat.remote.WechatUserInfoRemote;
import cn.com.glsx.framework.core.beans.ResultEntity;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.StatusEnum;
/**
* @Title: WeappUserService.java
* @Description:
* @author Alvin.zengqi
* @date 2018年3月24日 下午4:20:19
* @version V1.0
* @Company: Didihu.com.cn
* @Copyright Copyright (c) 2015
*/
@Service
public class WeappUserService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private WechatUserInfoRemote wechatUserInfoRemote;
@Autowired
private WeappBaseInfoRemote weappBaseInfoRemote;
@Autowired
private PlatformAccountManager platformAccountManager;
@Autowired
private MerchantCustomerRemote merchantCustomerRemote;
/**
* 根据openId登录
* @param openId
* @return
* @author Alvin
* @date 2019年4月22日 下午8:00:53
*/
public ResultEntity<WeappUserInfoVO> loginByOpenId(String openId){
RpcResponse<WechatUserInfo> response = wechatUserInfoRemote.loadByOpenId(openId);
if(response == null || response.isFailed()){
return ResultEntity.error(WechatErrorEnum.WECHAT_USERINFO_NULL.getValue(), response.getMessage());
}
WechatUserInfo wechatUserInfo = response.getResult();
logger.info("get wechatuserinfo by openId:{} return:{}", openId, wechatUserInfo);
if(wechatUserInfo == null){
return ResultEntity.error(WechatErrorEnum.WECHAT_USERINFO_NULL.getValue(), WechatErrorEnum.WECHAT_USERINFO_NULL.getDisplayName());
}
Session session = ThreadLocalCache.getSession();
session.put(Constants.SessionKey.SESSION_USERINFO_KEY.getValue(), wechatUserInfo);
RpcResponse<WeappBaseInfo> weappInfoResponse = weappBaseInfoRemote.getByAppId(wechatUserInfo.getAppId());
logger.info("loginByOpenId.weappBaseInfoRemote.getByAppId by appId:{} return:{}", wechatUserInfo.getAppId(), weappInfoResponse.getResult());
if(weappInfoResponse.hasResult()){
WeappBaseInfo weappBaseInfo = weappInfoResponse.getResult();
//防止进入其他商户小程序时缓存的merchantId被重置
Integer merchantIdInSession = session.get(Constants.SessionKey.MERCHANT_ID.getValue(), Integer.class);
logger.info("loginByOpenId.merchantIdInSession:{}", merchantIdInSession);
if(merchantIdInSession == null){
session.put(Constants.SessionKey.MERCHANT_ID.getValue(), weappBaseInfo.getMerchantId());
}
session.put(Constants.SessionKey.WEAPP_INFO_ID.getValue(), weappBaseInfo);
RpcResponse<MerchantCustomerInfo> ref = merchantCustomerRemote.getByMerchantIdAndPhone(weappBaseInfo.getMerchantId(), wechatUserInfo.getPhone());
if(ref.isSuccess() && ref.hasResult()){
// 更新用户活跃时间
MerchantCustomerInfo customerInfo = ref.getResult();
customerInfo.setLastLoginTime(new Date());
merchantCustomerRemote.updateRefByPramaryId(customerInfo);
}
}
WeappUserInfoVO weappUserInfo = WeappUserInfoConverter.INSTANCE.converterToSource(wechatUserInfo);
return ResultEntity.success(weappUserInfo, "login by code success");
}
/**
* 授权登录
* @param authSetting 授权配置
* @return
* @author Alvin.zengqi
* @date 2018年3月24日 下午5:08:47
*/
public ResultEntity<WeappUserInfoVO> authLogin(WechatAuthSettingVO authSetting){
Session session = ThreadLocalCache.getSession();
logger.info("wechat auth userinfo settion:{}", authSetting);
if(authSetting == null){
return ResultEntity.error(WechatErrorEnum.PARAMETER_NULL.getValue(), WechatErrorEnum.PARAMETER_NULL.getDisplayName());
}
JsCodeSessionDTO jsCodeSession = session.get(Constants.SessionKey.WEAPP_SESSION_KEY.getValue(), JsCodeSessionDTO.class);
WechatUserInfoVO wechatUserInfoVO = null;
WechatUserInfo wechatUserInfo = null;
// if(StringUtils.isEmpty(authSetting.getEncryptedData())
// || StringUtils.isEmpty(authSetting.getIv())
// || StringUtils.isEmpty(authSetting.getSignature())
// || StringUtils.isEmpty(authSetting.getRawData())){
// return ResultEntity.error(WechatErrorEnum.PARAMETER_NULL.getValue(), WechatErrorEnum.PARAMETER_NULL.getDisplayName());
// }
// // sessionKey 和签名一致
// boolean varifyResult = varify(jsCodeSession.getSessionKey(), authSetting);
// if(!varifyResult){
// return ResultEntity.error(WechatErrorEnum.VALIDATE_FAILED.getValue(), WechatErrorEnum.VALIDATE_FAILED.getDisplayName());
// }
// 获取到用户在小程序授权的信息
wechatUserInfoVO = decryptWechatUserInfo(authSetting, jsCodeSession.getSessionKey());
if(wechatUserInfoVO == null){
return ResultEntity.error(WechatErrorEnum.DECRYPT_WECHAT_USERINFO_FAILED.getValue(), WechatErrorEnum.DECRYPT_WECHAT_USERINFO_FAILED.getDisplayName());
}
wechatUserInfoVO.setOpenId(jsCodeSession.getOpenId());
wechatUserInfoVO.setUnionId(jsCodeSession.getUnionId());
logger.info("decrypt wechat userinfo:{}", wechatUserInfoVO);
session.put(Constants.SessionKey.WEAPP_USERINFO_KEY.getValue(), wechatUserInfoVO);
//解决已授权后loginByOpenId未执行导致merchantId的问题
RpcResponse<WeappBaseInfo> weappInfoResponse = weappBaseInfoRemote.getByAppId(authSetting.getAppId());
logger.info("authLogin.weappBaseInfoRemote.getByAppId by appId:{} return:{}", authSetting.getAppId(), weappInfoResponse.getResult());
if(weappInfoResponse.hasResult()) {
WeappBaseInfo weappBaseInfo = weappInfoResponse.getResult();
if(weappBaseInfo != null){
//防止进入其他商户小程序时缓存的merchantId被重置
Integer merchantIdInSession = session.get(Constants.SessionKey.MERCHANT_ID.getValue(), Integer.class);
logger.info("authLogin.merchantIdInSession:{}", merchantIdInSession);
if (merchantIdInSession == null) {// 为空的话更新商户id
session.put(Constants.SessionKey.MERCHANT_ID.getValue(), weappBaseInfo.getMerchantId());
}
session.put(Constants.SessionKey.WEAPP_INFO_ID.getValue(), weappBaseInfo);
}
}
// 保存小程序微信用户信息
wechatUserInfoVO.setAppId(authSetting.getAppId());
wechatUserInfoVO.setWxId(authSetting.getWxId());
wechatUserInfo = login(wechatUserInfoVO);
session.put(Constants.SessionKey.SESSION_USERINFO_KEY.getValue(), wechatUserInfo);
ThreadLocalCache.setWechatUserInfo(wechatUserInfo);
WeappUserInfoVO weappUserInfo = WeappUserInfoConverter.INSTANCE.converterToSource(wechatUserInfo);
return ResultEntity.success(weappUserInfo, "auth userinfo success");
}
/**
* 授权手机号
* @param authSetting 授权配置
* @return
* @author Alvin.zengqi
* @date 2018年3月24日 下午5:08:47
*/
public ResultEntity<WeappUserInfoVO> authPhone(WechatAuthSettingVO authSetting){
Session session = ThreadLocalCache.getSession();
logger.info("wechat auth phone settion:{}", authSetting);
if(authSetting == null){
return ResultEntity.error(WechatErrorEnum.PARAMETER_NULL.getValue(), WechatErrorEnum.PARAMETER_NULL.getDisplayName());
}
// jsCodeSession
JsCodeSessionDTO jsCodeSession = session.get(Constants.SessionKey.WEAPP_SESSION_KEY.getValue(), JsCodeSessionDTO.class);
WechatPhoneVO wechatPhoneVO = null;
// 获取微信基本授权信息和商户id
WechatUserInfo wechatUserInfo = session.get(Constants.SessionKey.SESSION_USERINFO_KEY.getValue(), WechatUserInfo.class);
if(StringUtils.isEmpty(authSetting.getEncryptedData())
|| StringUtils.isEmpty(authSetting.getIv())){
return ResultEntity.error(WechatErrorEnum.PARAMETER_NULL.getValue(), WechatErrorEnum.PARAMETER_NULL.getDisplayName());
}
logger.info("decryptWechatPhone with jsCodeSession:{}", jsCodeSession);
// 解密手机号
wechatPhoneVO = decryptWechatPhone(authSetting, jsCodeSession.getSessionKey());
if(wechatPhoneVO == null){
return ResultEntity.error(WechatErrorEnum.DECRYPT_WECHAT_PHONE_FAILED.getValue(), WechatErrorEnum.DECRYPT_WECHAT_PHONE_FAILED.getDisplayName());
}
logger.info("decrypt wechat phone:{}", wechatPhoneVO);
wechatUserInfo.setPhone(wechatPhoneVO.getPurePhoneNumber());
// 注册平台账号
Integer accountId = platformAccountManager.regist(wechatUserInfo);
wechatUserInfo.setAccountId(accountId);
logger.info("注册账户wechatUserInfo:{}", wechatUserInfo);
wechatUserInfo.setSceneId(authSetting.getSceneParam());
wechatUserInfo.setUserType(WechatUserTypeEnum.CUSTOMER.getValue());
wechatUserInfo.setIsLogin(StatusEnum.YES.getValue());
RpcResponse<WechatUserInfo> response = wechatUserInfoRemote.updateByOpenId(wechatUserInfo);
wechatUserInfo = response.getResult();
session.put(Constants.SessionKey.SESSION_USERINFO_KEY.getValue(), wechatUserInfo);
// 将重新更新的微信信息放入缓存
ThreadLocalCache.setWechatUserInfo(wechatUserInfo);
WeappUserInfoVO weappUserInfo = WeappUserInfoConverter.INSTANCE.converterToSource(wechatUserInfo);
return ResultEntity.success(weappUserInfo, "auth phone success");
}
/**
* 退出登录
* @return
* @author Alvin
* @date 2020年12月15日 上午11:24:05
*/
public ResultEntity<WeappUserInfoVO> logout(){
Session session = ThreadLocalCache.getSession();
session.remove(Constants.SessionKey.WEAPP_SESSION_KEY.getValue());
WechatUserInfo wechatUserInfo = session.get(Constants.SessionKey.SESSION_USERINFO_KEY.getValue(), WechatUserInfo.class);
wechatUserInfo.setIsLogin(StatusEnum.NO.getValue());
RpcResponse<WechatUserInfo> response = wechatUserInfoRemote.updateByOpenId(wechatUserInfo);
wechatUserInfo = response.getResult();
session.put(Constants.SessionKey.SESSION_USERINFO_KEY.getValue(), wechatUserInfo);
// 将重新更新的微信信息放入缓存
ThreadLocalCache.setWechatUserInfo(wechatUserInfo);
WeappUserInfoVO weappUserInfo = WeappUserInfoConverter.INSTANCE.converterToSource(wechatUserInfo);
return ResultEntity.success(weappUserInfo, "logout success");
}
/**
* 验证签名
* @param sessionKey
* @param authSetting
* @return
* @author Alvin.zengqi
* @date 2018年3月20日 下午9:19:37
*/
private boolean varify(String sessionKey, WechatAuthSettingVO authSetting){
String decript = authSetting.getRawData().concat(sessionKey);
String sign = SHA.sign(decript);
logger.info("varify weapp user info, decript:{} sign:{}, signature:{}", decript, sign, authSetting.getSignature());
return sign.equals(authSetting.getSignature());
}
/**
* 保存小程序用户信息
* @param wechatUserInfoVO
* @return
* @author Alvin.zengqi
* @date 2018年4月14日 上午11:40:00
*/
private WechatUserInfo login(WechatUserInfoVO wechatUserInfoVO){
WechatUserInfo wechatUserInfo = new WechatUserInfo();
wechatUserInfo = copyWeAppUserInfo(wechatUserInfoVO, wechatUserInfo);
wechatUserInfo.setUpdatedBy(wechatUserInfoVO.getUnionId());
wechatUserInfo.setUpdatedDate(new Date());
wechatUserInfo.setUserType(WechatUserTypeEnum.CUSTOMER.getValue());
wechatUserInfo.setAppType(WechatAppTypeEnum.WEAPP.name());
Session session = ThreadLocalCache.getSession();
Integer merchantId = session.get(Constants.SessionKey.MERCHANT_ID.getValue(), Integer.class);
wechatUserInfo.setMerchantId(merchantId);
// 登录成功后,不返回数据库数据(可能有账号ID和手机号),避免小程序未授权手机号直接登录成功
// RpcResponse<WechatUserInfo> response = wechatUserInfoRemote.login(wechatUserInfo);
// return response.getResult();
wechatUserInfoRemote.login(wechatUserInfo);
return wechatUserInfo;
}
/**
* 拷贝小程序用户信息
* @param source
* @param target
* @return
* @author Alvin.zengqi
* @date 2018年3月24日 下午5:18:17
*/
private WechatUserInfo copyWeAppUserInfo(WechatUserInfoVO source, WechatUserInfo target){
target.setUnionId(source.getUnionId());
target.setOpenId(source.getOpenId());
target.setNickName(source.getNickName());
target.setCity(source.getCity());
target.setProvince(source.getProvince());
target.setLogo(source.getAvatarUrl());
target.setSex(source.getGender());
target.setAppId(source.getAppId());
target.setWxId(source.getWxId());
return target;
}
/**
* 解密用户信息
* @param authSetting
* @param sessionKey
* @return
* @author Alvin.zengqi
* @date 2018年3月20日 下午6:38:48
*/
private WechatUserInfoVO decryptWechatUserInfo(WechatAuthSettingVO authSetting, String sessionKey){
WechatUserInfoVO wechatUserInfoVO = authSetting.getUserInfo();
// AES aes = new AES();
// try {
// byte[] resultByte = aes.decrypt(Base64.decode(authSetting.getEncryptedData()), Base64.decode(sessionKey), Base64.decode(authSetting.getIv()));
// if(null != resultByte && resultByte.length > 0){
// String userInfo = new String(WxPKCS7Encoder.decode(resultByte));
// logger.info("decrypt wechat userinfo return:{}", userInfo);
// wechatUserInfoVO = JacksonUtils.jsonCastBean(userInfo, WechatUserInfoVO.class);
// }
// } catch (InvalidAlgorithmParameterException e) {
// e.printStackTrace();
// }
return wechatUserInfoVO;
}
/**
* 解密微信绑定手机号
* @param authSetting
* @param sessionKey
* @return
* @author Alvin.zengqi
* @date 2018年3月20日 下午6:38:48
*/
private WechatPhoneVO decryptWechatPhone(WechatAuthSettingVO authSetting, String sessionKey){
WechatPhoneVO wechatPhoneVO = null;
AES aes = new AES();
try {
byte[] resultByte = aes.decrypt(Base64.decode(authSetting.getEncryptedData()), Base64.decode(sessionKey), Base64.decode(authSetting.getIv()));
if(null != resultByte && resultByte.length > 0){
String phoneInfo = new String(WxPKCS7Encoder.decode(resultByte));
logger.info("decrypt wechat phone return:{}", phoneInfo);
wechatPhoneVO = JacksonUtils.jsonCastBean(phoneInfo, WechatPhoneVO.class);
}
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return wechatPhoneVO;
}
}