88e030b7
王彪总
init project
|
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
|
package com.java110.job.adapt.wechat;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.factory.WechatFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.system.Business;
import com.java110.dto.wechat.SmallWeChatDto;
import com.java110.dto.wechat.WechatSubscribeDto;
import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
import com.java110.intf.user.IWechatSubscribeV1InnerServiceSMO;
import com.java110.job.adapt.DatabusAdaptImpl;
import com.java110.po.wechat.WechatSubscribePo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@Component(value = "synchronizeWechatSubscribeAdapt")
public class SynchronizeWechatSubscribeAdapt extends DatabusAdaptImpl {
private static Logger logger = LoggerFactory.getLogger(SynchronizeWechatSubscribeAdapt.class);
@Autowired
private IWechatSubscribeV1InnerServiceSMO wechatSubscribeV1InnerServiceSMOImpl;
@Autowired
private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
@Autowired
private RestTemplate outRestTemplate;
private static final String GET_USER = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN";
private static final String GET_BATCH_USER = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
@Override
public void execute(Business business, List<Business> businesses) {
JSONObject data = business.getData();
String communityId = data.getString("communityId");
if (StringUtil.isEmpty(communityId)) {
throw new CmdException("未包含小区");
}
SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
smallWeChatDto.setObjId(communityId);
smallWeChatDto.setWeChatType(SmallWeChatDto.WECHAT_TYPE_PUBLIC);
List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
if (ListUtil.isNull(smallWeChatDtos)) {
throw new CmdException("未配置公众号");
}
String accessToken = WechatFactory.getAccessToken(smallWeChatDtos.get(0).getAppId(), smallWeChatDtos.get(0).getAppSecret());
WechatSubscribePo tmpWechatSubscribePo = new WechatSubscribePo();
tmpWechatSubscribePo.setAppId(smallWeChatDtos.get(0).getAppId());
wechatSubscribeV1InnerServiceSMOImpl.deleteWechatSubscribe(tmpWechatSubscribePo);
getAllOpenId(accessToken, "", smallWeChatDtos.get(0));
}
private void getAllOpenId(String accessToken, String nextOpenid, SmallWeChatDto weChatDto) {
String url = GET_USER.replace("ACCESS_TOKEN", accessToken);
if (!StringUtil.isEmpty(nextOpenid)) {
url += ("&next_openid=" + nextOpenid);
}
ResponseEntity<String> paramOut = outRestTemplate.getForEntity(url, String.class);
logger.info("获取用户返回:{}", paramOut);
if (paramOut.getStatusCode() != HttpStatus.OK) {
throw new IllegalArgumentException(paramOut.getBody());
}
JSONObject paramOutObj = JSONObject.parseObject(paramOut.getBody());
if (paramOutObj.containsKey("errcode")) {
throw new IllegalArgumentException(paramOut.getBody());
}
if (!paramOutObj.containsKey("data")) {
return;
}
JSONObject dataObj = paramOutObj.getJSONObject("data");
JSONArray openids = dataObj.getJSONArray("openid");
nextOpenid = paramOutObj.getString("next_openid");
List<WechatSubscribePo> wechatSubscribePos = new ArrayList<>();
JSONArray user_list = new JSONArray();
JSONObject user = null;
for (int openIndex = 0; openIndex < openids.size(); openIndex++) {
String openId = openids.getString(openIndex);
user = new JSONObject();
user.put("openid", openId);
user.put("lang", "zh_CN");
user_list.add(user);
WechatSubscribePo wechatSubscribePo = new WechatSubscribePo();
wechatSubscribePo.setAppId(weChatDto.getAppId());
wechatSubscribePo.setOpenId(openId);
wechatSubscribePo.setSubId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_file_id));
wechatSubscribePo.setOpenType(WechatSubscribeDto.OPEN_TYPE_WECHAT);
wechatSubscribePos.add(wechatSubscribePo);
if (user_list.size() >= 100) {
getUserUnionId(user_list, wechatSubscribePos, accessToken);
wechatSubscribeV1InnerServiceSMOImpl.saveWechatSubscribes(wechatSubscribePos);
wechatSubscribePos = new ArrayList<>();
user_list = new JSONArray();
}
}
if (!user_list.isEmpty()) {
getUserUnionId(user_list, wechatSubscribePos, accessToken);
}
if (!ListUtil.isNull(wechatSubscribePos)) {
wechatSubscribeV1InnerServiceSMOImpl.saveWechatSubscribes(wechatSubscribePos);
}
//(关注者列表已返回完时,返回next_openid为空)
if (!StringUtil.isEmpty(nextOpenid)) {
getAllOpenId(accessToken, nextOpenid, weChatDto);
}
}
private void getUserUnionId(JSONArray userList, List<WechatSubscribePo> wechatSubscribePos, String accessToken) {
if (ListUtil.isNull(userList)) {
return;
}
String url = GET_BATCH_USER.replace("ACCESS_TOKEN", accessToken);
JSONObject paramIn = new JSONObject();
paramIn.put("user_list",userList);
logger.info("获取用户unionId:{}", paramIn.toJSONString());
ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(url, paramIn.toJSONString(), String.class);
logger.info("获取用户unionId返回:{}", responseEntity);
if (responseEntity.getStatusCode() != HttpStatus.OK) {
throw new IllegalArgumentException(responseEntity.getBody());
}
JSONObject paramOutObj = JSONObject.parseObject(responseEntity.getBody());
if (paramOutObj.containsKey("errcode")) {
throw new IllegalArgumentException(paramOutObj.getString("errmsg"));
}
JSONArray user_info_list = paramOutObj.getJSONArray("user_info_list");
if (ListUtil.isNull(user_info_list)) {
return;
}
JSONObject user = null;
for (int userIndex = 0; userIndex < user_info_list.size(); userIndex++) {
user = user_info_list.getJSONObject(userIndex);
for(WechatSubscribePo wechatSubscribePo : wechatSubscribePos){
if(user.getString("openid").equals(wechatSubscribePo.getOpenId())){
wechatSubscribePo.setUnionId(user.getString("unionid"));
if(StringUtil.isEmpty(wechatSubscribePo.getUnionId())){
wechatSubscribePo.setUnionId("-1");
}
}
}
}
}
}
|