WeiXinAppletUserParser.java
5.24 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
package com.topdraw.weixin.applet;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.business.process.domian.weixin.WeiXinUserBean;
import com.topdraw.security.AESUtil;
import com.topdraw.utils.StringUtils;
import com.topdraw.weixin.beans.DefaultWeiXinBeanDefinition;
import com.topdraw.weixin.beans.WeiXinUserParser;
import com.topdraw.weixin.beans.config.WeiXinAppListConfig;
import com.topdraw.weixin.util.WeChatConstants;
import com.topdraw.weixin.util.WeiXinRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Component
public class WeiXinAppletUserParser implements WeiXinUserParser {
@Autowired
private WeiXinAppListConfig weiXinAppListConfig;
@Autowired
private WeiXinRequestUtil weixinRequestUtil;
@Value("${uc.service.platform:}")
private String platform;
@Value("${key:}")
private String key;
@Value("${uc.app.subAppId:wx05f35931270014be}")
private String subAppId;
@Value("${uc.app.h5AppId:wxca962918dfeed88c}")
private String h5AppId;
@Value("${uc.app.appletAppid:wxc57d42de3d351cec}")
private String appletAppid;
@Value("${file.upload:upload}")
private String filePath;
@Override
public DefaultWeiXinBeanDefinition parse(WeiXinUserBean resources) {
String key = this.key;
Map<String, String> weixinInfoMap = null;
String appId = resources.getWxAppid();
String code = resources.getWxCode();
String userInfo = null;
if (StringUtils.isNotBlank(appId)) {
String decrypt = AESUtil.decrypt(appId, key);
if (decrypt != null) {
appId = decrypt.substring(16);
}
weixinInfoMap = getWeixinInfoByAppid(appId);
}
if (StringUtils.isNotBlank(code)) {
String decrypt = AESUtil.decrypt(code, key);
if (decrypt != null) {
code = decrypt.substring(16);
}
}
if (StringUtils.isNotBlank(userInfo)) {
String decrypt = AESUtil.decrypt(userInfo, key);
if (decrypt != null) {
userInfo = decrypt.substring(16);
}
}
return this.generateWeiXinBeanDefinition(code,userInfo,weixinInfoMap);
}
/**
*
* @param code
* @param userInfo
* @param weixinInfoMap
* @return
*/
private DefaultWeiXinBeanDefinition generateWeiXinBeanDefinition(String code, String userInfo, Map<String, String> weixinInfoMap) {
String appId = weixinInfoMap.get("appid");
String secret = weixinInfoMap.get("secret");
JSONObject userInfoWxJo = null;
// 链接微信服务器
/*ResponseEntity<String> responseEntity1 = null;*//*restTemplate.exchange(WeChatConstants.CODE2SESSION.replace("APPID", appId)
.replace("SECRET", secret).replace("JSCODE", code),
HttpMethod.GET, null, String.class);*//*
String entityBody1 = responseEntity1.getBody();*/
String url = WeChatConstants.CODE2SESSION.replace("APPID", appId)
.replace("SECRET", secret).replace("JSCODE", code);
String entityBody = HttpUtil.get(url);
JSONObject jsonObject = JSONObject.parseObject(entityBody);
String openId = jsonObject.getString("openid");
String unionId = jsonObject.getString("unionid");
if (StringUtils.isEmpty(unionId)) {
try {
userInfoWxJo = this.weixinRequestUtil.getUserInfo(weixinInfoMap, openId,code);
} catch (Exception e) {
e.printStackTrace();
}
}
if (StringUtils.isNotBlank(userInfo)) {
JSONObject userInfoJo = JSONObject.parseObject(userInfo);
String encryptedData = userInfoJo.getString("encryptedData");
String iv = userInfoJo.getString("iv");
String key = jsonObject.getString("session_key");
String userInfoWeixin = AESUtil.decryptJsUserInfo(encryptedData, iv, key);
if (StringUtils.isNotEmpty(userInfoWeixin))
userInfoWxJo = JSONObject.parseObject(userInfoWeixin);
}
DefaultWeiXinBeanDefinition weiXinBeanDefinition =
new DefaultWeiXinBeanDefinition(appId,code,unionId,openId,userInfoWxJo,null);
return weiXinBeanDefinition;
}
/**
*
* @param appid
* @return
*/
private Map<String, String> getWeixinInfoByAppid(String appid) {
if (StringUtils.isBlank(appid)) {
throw new RuntimeException("wxAppid can not be null");
}
List<Map<String, String>> list = this.weiXinAppListConfig.getList();
Optional<Map<String, String>> weixinInfoOptional = list.stream().filter(o -> o.get("appid").equals(appid)).findFirst();
if (!weixinInfoOptional.isPresent()) {
throw new RuntimeException("wxAppid error, appid is : " + appid);
}
return weixinInfoOptional.get();
}
}