1
|
package com.yoho.yhmessage.wechat.service.impl;
|
1
|
package com.yoho.yhmessage.wechat.service.impl;
|
2
|
|
2
|
|
3
|
import com.alibaba.fastjson.JSONObject;
|
3
|
import com.alibaba.fastjson.JSONObject;
|
|
|
4
|
+import com.google.common.util.concurrent.RateLimiter;
|
|
|
5
|
+import com.yoho.core.common.utils.DateUtil;
|
|
|
6
|
+import com.yoho.core.rabbitmq.YhProducer;
|
4
|
import com.yoho.error.exception.ServiceException;
|
7
|
import com.yoho.error.exception.ServiceException;
|
5
|
import com.yoho.service.model.msgcenter.wechat.McWechatBO;
|
8
|
import com.yoho.service.model.msgcenter.wechat.McWechatBO;
|
|
|
9
|
+
|
6
|
import com.yoho.yhmessage.filter.IFilterSensitiveWordService;
|
10
|
import com.yoho.yhmessage.filter.IFilterSensitiveWordService;
|
|
|
11
|
+
|
|
|
12
|
+import com.yoho.service.model.msgcenter.wechat.WechatCustomMsgBO;
|
|
|
13
|
+import com.yoho.yhmessage.constants.RedisCacheKeyConstant;
|
|
|
14
|
+import com.yoho.yhmessage.redis.PushRedisService;
|
|
|
15
|
+
|
7
|
import com.yoho.yhmessage.wechat.common.Consts;
|
16
|
import com.yoho.yhmessage.wechat.common.Consts;
|
8
|
import com.yoho.yhmessage.wechat.service.IWechatService;
|
17
|
import com.yoho.yhmessage.wechat.service.IWechatService;
|
9
|
import com.yoho.yhmessage.wechat.service.IWeixinAccessTokenService;
|
18
|
import com.yoho.yhmessage.wechat.service.IWeixinAccessTokenService;
|
10
|
import com.yoho.yhmessage.wechat.service.model.AccessToken;
|
19
|
import com.yoho.yhmessage.wechat.service.model.AccessToken;
|
11
|
import com.yoho.yhmessage.wechat.util.SendWechatRequest;
|
20
|
import com.yoho.yhmessage.wechat.util.SendWechatRequest;
|
|
|
21
|
+import org.apache.commons.collections.CollectionUtils;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
22
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.slf4j.Logger;
|
23
|
import org.slf4j.Logger;
|
14
|
import org.slf4j.LoggerFactory;
|
24
|
import org.slf4j.LoggerFactory;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
25
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.stereotype.Service;
|
26
|
import org.springframework.stereotype.Service;
|
17
|
|
27
|
|
|
|
28
|
+import javax.annotation.Resource;
|
|
|
29
|
+import java.text.SimpleDateFormat;
|
|
|
30
|
+import java.util.*;
|
|
|
31
|
+
|
18
|
/**
|
32
|
/**
|
19
|
*
|
33
|
*
|
20
|
* <Description> 微信<br>
|
34
|
* <Description> 微信<br>
|
|
@@ -40,8 +54,22 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -40,8 +54,22 @@ public class WechatServiceImpl implements IWechatService { |
40
|
private SendWechatRequest sendWechatRequest;
|
54
|
private SendWechatRequest sendWechatRequest;
|
41
|
|
55
|
|
42
|
@Autowired
|
56
|
@Autowired
|
|
|
57
|
+
|
43
|
private IFilterSensitiveWordService filterSensitiveWordService;
|
58
|
private IFilterSensitiveWordService filterSensitiveWordService;
|
44
|
|
59
|
|
|
|
60
|
+ private PushRedisService pushRedisService;
|
|
|
61
|
+
|
|
|
62
|
+ private static final String DATE_ID_FORMAT = "yyyyMMdd";
|
|
|
63
|
+
|
|
|
64
|
+ //1s放10个令牌 100ms频率
|
|
|
65
|
+ private static final RateLimiter rateLimiter = RateLimiter.create(10);
|
|
|
66
|
+
|
|
|
67
|
+ private static final int SEVEN_DAY_HOURS = 168;
|
|
|
68
|
+
|
|
|
69
|
+ @Resource(name = "yhProducer")
|
|
|
70
|
+ private YhProducer yhProducer;
|
|
|
71
|
+
|
|
|
72
|
+
|
45
|
@Override
|
73
|
@Override
|
46
|
public void sendWechatMsg(McWechatBO wechatBO) throws ServiceException {
|
74
|
public void sendWechatMsg(McWechatBO wechatBO) throws ServiceException {
|
47
|
logger.info("begin sendTemplateMsg param {}", wechatBO);
|
75
|
logger.info("begin sendTemplateMsg param {}", wechatBO);
|
|
@@ -67,7 +95,7 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -67,7 +95,7 @@ public class WechatServiceImpl implements IWechatService { |
67
|
logger.warn("sendWechatMsg with accessToken is null, publicCode is {},wechatBO is {}",wechatBO.getPublicNumberCode(),wechatBO);
|
95
|
logger.warn("sendWechatMsg with accessToken is null, publicCode is {},wechatBO is {}",wechatBO.getPublicNumberCode(),wechatBO);
|
68
|
return;
|
96
|
return;
|
69
|
}
|
97
|
}
|
70
|
- String templateUrl = this.getTemplateUrl(accessToken);
|
98
|
+ String templateUrl = this.getTemplateUrl(accessToken,Consts.SEND_TEMPLATE_URL);
|
71
|
//针对需要展示微信用户昵称的消息,在这里调用微信api查询一把并替换,避免在其他服务调用微信api的复杂行,微信用户昵称统一用${wechatNickname}表示
|
99
|
//针对需要展示微信用户昵称的消息,在这里调用微信api查询一把并替换,避免在其他服务调用微信api的复杂行,微信用户昵称统一用${wechatNickname}表示
|
72
|
String data = this.addUserNickname(wechatBO, accessToken);
|
100
|
String data = this.addUserNickname(wechatBO, accessToken);
|
73
|
// 获取请求参数
|
101
|
// 获取请求参数
|
|
@@ -82,7 +110,7 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -82,7 +110,7 @@ public class WechatServiceImpl implements IWechatService { |
82
|
logger.warn("send wechat template message error, e is {}", JSONObject.toJSONString(result) );
|
110
|
logger.warn("send wechat template message error, e is {}", JSONObject.toJSONString(result) );
|
83
|
// 重新调接口获取access token
|
111
|
// 重新调接口获取access token
|
84
|
AccessToken accessTokenNew = weixinAccessTokenService.getAccessTokenFromUrl(wechatBO.getPublicNumberCode());
|
112
|
AccessToken accessTokenNew = weixinAccessTokenService.getAccessTokenFromUrl(wechatBO.getPublicNumberCode());
|
85
|
- templateUrl = this.getTemplateUrl(accessTokenNew);
|
113
|
+ templateUrl = this.getTemplateUrl(accessTokenNew,Consts.SEND_TEMPLATE_URL);
|
86
|
result = sendWechatRequest.sendWechatRequest(wechatBO.getPublicNumberCode(), templateUrl, "POST", sendMsg.toJSONString());
|
114
|
result = sendWechatRequest.sendWechatRequest(wechatBO.getPublicNumberCode(), templateUrl, "POST", sendMsg.toJSONString());
|
87
|
}
|
115
|
}
|
88
|
sendWechatMsgLog.info("send wechat template message {},result {}, templateUrl is {}", sendMsg.toJSONString(), result, templateUrl);
|
116
|
sendWechatMsgLog.info("send wechat template message {},result {}, templateUrl is {}", sendMsg.toJSONString(), result, templateUrl);
|
|
@@ -116,12 +144,12 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -116,12 +144,12 @@ public class WechatServiceImpl implements IWechatService { |
116
|
* @taskId <br>
|
144
|
* @taskId <br>
|
117
|
* @return <br>
|
145
|
* @return <br>
|
118
|
*/
|
146
|
*/
|
119
|
- private String getTemplateUrl(AccessToken accessToken) {
|
147
|
+ private String getTemplateUrl(AccessToken accessToken,String url) {
|
120
|
if (null == accessToken) {
|
148
|
if (null == accessToken) {
|
121
|
return null;
|
149
|
return null;
|
122
|
}
|
150
|
}
|
123
|
logger.info("accessToken is {}", accessToken.getToken());
|
151
|
logger.info("accessToken is {}", accessToken.getToken());
|
124
|
- StringBuffer templateUrl = new StringBuffer(Consts.SEND_TEMPLATE_URL);
|
152
|
+ StringBuffer templateUrl = new StringBuffer(url);
|
125
|
templateUrl.append(accessToken.getToken());
|
153
|
templateUrl.append(accessToken.getToken());
|
126
|
return templateUrl.toString();
|
154
|
return templateUrl.toString();
|
127
|
}
|
155
|
}
|
|
@@ -129,4 +157,100 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -129,4 +157,100 @@ public class WechatServiceImpl implements IWechatService { |
129
|
private String getQueryUserUrl(String openId, AccessToken accessToken) {
|
157
|
private String getQueryUserUrl(String openId, AccessToken accessToken) {
|
130
|
return Consts.QUERY_USER_INFO_URL.replace("ACCESS_TOKEN", accessToken.getToken()).replace("OPENID", openId);
|
158
|
return Consts.QUERY_USER_INFO_URL.replace("ACCESS_TOKEN", accessToken.getToken()).replace("OPENID", openId);
|
131
|
}
|
159
|
}
|
|
|
160
|
+
|
|
|
161
|
+
|
|
|
162
|
+ @Override
|
|
|
163
|
+ public void sendWechatCustomMsg(WechatCustomMsgBO customMsgBO) throws ServiceException{
|
|
|
164
|
+ logger.info("enter sendWechatCustomMsg with sceneKey is {},msgBO is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
|
|
|
165
|
+
|
|
|
166
|
+ if(CollectionUtils.isEmpty(customMsgBO.getOpenIdList())){
|
|
|
167
|
+ logger.warn("sendWechatCustomMsg with sendOpenIds null, sceneKey is {}", customMsgBO.getSendKey());
|
|
|
168
|
+ return;
|
|
|
169
|
+ }
|
|
|
170
|
+ String sceneKey = customMsgBO.getSendKey();
|
|
|
171
|
+ Set<String> openIdSet = new HashSet<>(customMsgBO.getOpenIdList());
|
|
|
172
|
+ logger.info("sendWechatCustomMsg with sceneKey is {},send origin size is {}",sceneKey,openIdSet.size());
|
|
|
173
|
+ //根据过滤条件进行过滤
|
|
|
174
|
+ if(customMsgBO.getFilterDays() != 0){
|
|
|
175
|
+ getDaysFilter(customMsgBO, openIdSet);
|
|
|
176
|
+ }
|
|
|
177
|
+ //组装发送
|
|
|
178
|
+ List<String> sendSuccessList = customMsgSend(customMsgBO, openIdSet);
|
|
|
179
|
+ if(CollectionUtils.isEmpty(sendSuccessList)){
|
|
|
180
|
+ logger.warn("sendWechatCustomMsg with sendSuccess size is 0,sceneKey is {}",customMsgBO.getSendKey());
|
|
|
181
|
+ return;
|
|
|
182
|
+ }
|
|
|
183
|
+ logger.info("end sendWechatCustomMsg with sceneKey is {},successSize is {}",customMsgBO.getSendKey(),sendSuccessList.size());
|
|
|
184
|
+ //成功记录存redis
|
|
|
185
|
+ String nowDate = DateUtil.getToday(DATE_ID_FORMAT);
|
|
|
186
|
+ String redisKeyStr = RedisCacheKeyConstant.WECAHT_CUSTOM_SEND_KEY + customMsgBO.getPublicNumCode() + '_' + nowDate;
|
|
|
187
|
+ //成功记录存7天
|
|
|
188
|
+ pushRedisService.setListValue(redisKeyStr,sendSuccessList,SEVEN_DAY_HOURS);
|
|
|
189
|
+ logger.info("sendWechatCustomMsg with setRedis end, sceneKey is {},successSize is {}",customMsgBO.getSendKey(),sendSuccessList.size());
|
|
|
190
|
+ //发送成功数给到crm
|
|
|
191
|
+ sendMqToCrm(customMsgBO, sendSuccessList);
|
|
|
192
|
+ }
|
|
|
193
|
+
|
|
|
194
|
+ private void sendMqToCrm(WechatCustomMsgBO customMsgBO, List<String> sendSuccessList) {
|
|
|
195
|
+ JSONObject object = new JSONObject();
|
|
|
196
|
+ object.put("activityId",customMsgBO.getSendKey());
|
|
|
197
|
+ object.put("sendNum",sendSuccessList.size());
|
|
|
198
|
+ yhProducer.send(Consts.CUSTOM_MSG_SEND_NUM_TOPIC,object);
|
|
|
199
|
+ }
|
|
|
200
|
+
|
|
|
201
|
+ private void getDaysFilter(WechatCustomMsgBO customMsgBO, Set<String> openIdSet) {
|
|
|
202
|
+ int filterDayReal = customMsgBO.getFilterDays() - 1;
|
|
|
203
|
+ //根据间隔天数计算出需要过滤到的最早的一天
|
|
|
204
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
205
|
+ calendar.add(Calendar.DATE, -filterDayReal);
|
|
|
206
|
+ Calendar c = Calendar.getInstance();
|
|
|
207
|
+ c.setTime(calendar.getTime());
|
|
|
208
|
+ int day = c.get(Calendar.DATE);
|
|
|
209
|
+ for (int i = 0; i <= filterDayReal; i++){
|
|
|
210
|
+ //从最早日期开始往后累加到每个日期,每天过滤
|
|
|
211
|
+ c.set(Calendar.DATE, day + i);
|
|
|
212
|
+ String nowDate = new SimpleDateFormat(DATE_ID_FORMAT).format(c.getTime());
|
|
|
213
|
+ //根据日期获取当天发送客服消息的记录
|
|
|
214
|
+ String redisKeyStr = RedisCacheKeyConstant.WECAHT_CUSTOM_SEND_KEY + customMsgBO.getPublicNumCode() + '_' + nowDate;
|
|
|
215
|
+ pushRedisService.getSetFilter(openIdSet,redisKeyStr);
|
|
|
216
|
+ logger.info("sendWechatCustomMsg with day filter,sceneKey is {},redisKeyStr is {},nowSize is {}",customMsgBO.getSendKey(),redisKeyStr,openIdSet.size());
|
|
|
217
|
+ if(openIdSet.size() == 0){
|
|
|
218
|
+ break;
|
|
|
219
|
+ }
|
|
|
220
|
+ }
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+
|
|
|
224
|
+ private List<String> customMsgSend(WechatCustomMsgBO customMsgBO,Set<String> openIdSet){
|
|
|
225
|
+ logger.info("begin customMsgSend with sceneKey is {},send openId size is {}",customMsgBO.getSendKey(),openIdSet.size());
|
|
|
226
|
+ if(CollectionUtils.isEmpty(openIdSet)){
|
|
|
227
|
+ logger.warn("customMsgSend with openIdSet is null,sceneKey is {}",customMsgBO.getSendKey());
|
|
|
228
|
+ return new ArrayList<>();
|
|
|
229
|
+ }
|
|
|
230
|
+ // 获取token 和接口url
|
|
|
231
|
+ AccessToken accessToken = weixinAccessTokenService.queryWeixinAccessToken(customMsgBO.getPublicNumCode());
|
|
|
232
|
+ //若仍未获取到 token 则返回
|
|
|
233
|
+ if(accessToken == null){
|
|
|
234
|
+ logger.warn("customMsgSend with accessToken is null,sendKey is {}",customMsgBO.getSendKey());
|
|
|
235
|
+ return new ArrayList<>();
|
|
|
236
|
+ }
|
|
|
237
|
+ List<String> successList = new ArrayList<>();
|
|
|
238
|
+ String templateUrl = this.getTemplateUrl(accessToken, Consts.CUSTOM_MSG_SEND_URL);
|
|
|
239
|
+ //组装消息内容
|
|
|
240
|
+ JSONObject sendDataObject = new JSONObject();
|
|
|
241
|
+ sendDataObject.put("msgtype",customMsgBO.getMsgType());
|
|
|
242
|
+ sendDataObject.put(customMsgBO.getMsgType(),JSONObject.parseObject(customMsgBO.getSendContent()));
|
|
|
243
|
+ openIdSet.forEach(openId -> {
|
|
|
244
|
+ //限制100ms调一次
|
|
|
245
|
+ rateLimiter.acquire();
|
|
|
246
|
+ sendDataObject.put("touser", openId);
|
|
|
247
|
+ JSONObject result = sendWechatRequest.sendWechatRequest(customMsgBO.getPublicNumCode(), templateUrl, "POST", sendDataObject.toJSONString());
|
|
|
248
|
+ if (result != null && result.getIntValue("errcode") == 0) {
|
|
|
249
|
+ successList.add(openId);
|
|
|
250
|
+ }
|
|
|
251
|
+ sendWechatMsgLog.info("customMsgSend with sceneKey {},openId is {},result {}, content is {}", customMsgBO.getSendKey(), openId, result, sendDataObject.toJSONString());
|
|
|
252
|
+ });
|
|
|
253
|
+ return successList;
|
|
|
254
|
+ }
|
|
|
255
|
+
|
132
|
} |
256
|
} |