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.redis.cluster.operations.serializer.RedisKeyBuilder;
|
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
|
+import com.yoho.service.model.msgcenter.wechat.WechatCustomMsgBO;
|
|
|
10
|
+import com.yoho.yhmessage.constants.RedisCacheKeyConstant;
|
|
|
11
|
+import com.yoho.yhmessage.redis.PushRedisService;
|
6
|
import com.yoho.yhmessage.wechat.common.Consts;
|
12
|
import com.yoho.yhmessage.wechat.common.Consts;
|
7
|
import com.yoho.yhmessage.wechat.service.IWechatService;
|
13
|
import com.yoho.yhmessage.wechat.service.IWechatService;
|
8
|
import com.yoho.yhmessage.wechat.service.IWeixinAccessTokenService;
|
14
|
import com.yoho.yhmessage.wechat.service.IWeixinAccessTokenService;
|
|
@@ -13,6 +19,12 @@ import org.slf4j.Logger; |
|
@@ -13,6 +19,12 @@ import org.slf4j.Logger; |
13
|
import org.slf4j.LoggerFactory;
|
19
|
import org.slf4j.LoggerFactory;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.stereotype.Service;
|
21
|
import org.springframework.stereotype.Service;
|
|
|
22
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
23
|
+import retrofit2.http.PUT;
|
|
|
24
|
+
|
|
|
25
|
+import javax.print.DocFlavor;
|
|
|
26
|
+import java.text.SimpleDateFormat;
|
|
|
27
|
+import java.util.*;
|
16
|
|
28
|
|
17
|
/**
|
29
|
/**
|
18
|
*
|
30
|
*
|
|
@@ -38,6 +50,16 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -38,6 +50,16 @@ public class WechatServiceImpl implements IWechatService { |
38
|
@Autowired
|
50
|
@Autowired
|
39
|
private SendWechatRequest sendWechatRequest;
|
51
|
private SendWechatRequest sendWechatRequest;
|
40
|
|
52
|
|
|
|
53
|
+ @Autowired
|
|
|
54
|
+ private PushRedisService pushRedisService;
|
|
|
55
|
+
|
|
|
56
|
+ private static final String DATE_ID_FORMAT = "yyyyMMdd";
|
|
|
57
|
+
|
|
|
58
|
+ //1s放10个令牌 100ms频率
|
|
|
59
|
+ private static final RateLimiter rateLimiter = RateLimiter.create(10);
|
|
|
60
|
+
|
|
|
61
|
+ private static final int SEVEN_DAY_HOURS = 168;
|
|
|
62
|
+
|
41
|
@Override
|
63
|
@Override
|
42
|
public void sendWechatMsg(McWechatBO wechatBO) throws ServiceException {
|
64
|
public void sendWechatMsg(McWechatBO wechatBO) throws ServiceException {
|
43
|
logger.info("begin sendTemplateMsg param {}", wechatBO);
|
65
|
logger.info("begin sendTemplateMsg param {}", wechatBO);
|
|
@@ -54,7 +76,7 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -54,7 +76,7 @@ public class WechatServiceImpl implements IWechatService { |
54
|
logger.warn("sendWechatMsg with accessToken is null, publicCode is {},wechatBO is {}",wechatBO.getPublicNumberCode(),wechatBO);
|
76
|
logger.warn("sendWechatMsg with accessToken is null, publicCode is {},wechatBO is {}",wechatBO.getPublicNumberCode(),wechatBO);
|
55
|
return;
|
77
|
return;
|
56
|
}
|
78
|
}
|
57
|
- String templateUrl = this.getTemplateUrl(accessToken);
|
79
|
+ String templateUrl = this.getTemplateUrl(accessToken,Consts.SEND_TEMPLATE_URL);
|
58
|
//针对需要展示微信用户昵称的消息,在这里调用微信api查询一把并替换,避免在其他服务调用微信api的复杂行,微信用户昵称统一用${wechatNickname}表示
|
80
|
//针对需要展示微信用户昵称的消息,在这里调用微信api查询一把并替换,避免在其他服务调用微信api的复杂行,微信用户昵称统一用${wechatNickname}表示
|
59
|
String data = this.addUserNickname(wechatBO, accessToken);
|
81
|
String data = this.addUserNickname(wechatBO, accessToken);
|
60
|
// 获取请求参数
|
82
|
// 获取请求参数
|
|
@@ -69,7 +91,7 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -69,7 +91,7 @@ public class WechatServiceImpl implements IWechatService { |
69
|
logger.warn("send wechat template message error, e is {}", JSONObject.toJSONString(result) );
|
91
|
logger.warn("send wechat template message error, e is {}", JSONObject.toJSONString(result) );
|
70
|
// 重新调接口获取access token
|
92
|
// 重新调接口获取access token
|
71
|
AccessToken accessTokenNew = weixinAccessTokenService.getAccessTokenFromUrl(wechatBO.getPublicNumberCode());
|
93
|
AccessToken accessTokenNew = weixinAccessTokenService.getAccessTokenFromUrl(wechatBO.getPublicNumberCode());
|
72
|
- templateUrl = this.getTemplateUrl(accessTokenNew);
|
94
|
+ templateUrl = this.getTemplateUrl(accessTokenNew,Consts.SEND_TEMPLATE_URL);
|
73
|
result = sendWechatRequest.sendWechatRequest(wechatBO.getPublicNumberCode(), templateUrl, "POST", sendMsg.toJSONString());
|
95
|
result = sendWechatRequest.sendWechatRequest(wechatBO.getPublicNumberCode(), templateUrl, "POST", sendMsg.toJSONString());
|
74
|
}
|
96
|
}
|
75
|
sendWechatMsgLog.info("send wechat template message {},result {}, templateUrl is {}", sendMsg.toJSONString(), result, templateUrl);
|
97
|
sendWechatMsgLog.info("send wechat template message {},result {}, templateUrl is {}", sendMsg.toJSONString(), result, templateUrl);
|
|
@@ -103,12 +125,12 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -103,12 +125,12 @@ public class WechatServiceImpl implements IWechatService { |
103
|
* @taskId <br>
|
125
|
* @taskId <br>
|
104
|
* @return <br>
|
126
|
* @return <br>
|
105
|
*/
|
127
|
*/
|
106
|
- private String getTemplateUrl(AccessToken accessToken) {
|
128
|
+ private String getTemplateUrl(AccessToken accessToken,String url) {
|
107
|
if (null == accessToken) {
|
129
|
if (null == accessToken) {
|
108
|
return null;
|
130
|
return null;
|
109
|
}
|
131
|
}
|
110
|
logger.info("accessToken is {}", accessToken.getToken());
|
132
|
logger.info("accessToken is {}", accessToken.getToken());
|
111
|
- StringBuffer templateUrl = new StringBuffer(Consts.SEND_TEMPLATE_URL);
|
133
|
+ StringBuffer templateUrl = new StringBuffer(url);
|
112
|
templateUrl.append(accessToken.getToken());
|
134
|
templateUrl.append(accessToken.getToken());
|
113
|
return templateUrl.toString();
|
135
|
return templateUrl.toString();
|
114
|
}
|
136
|
}
|
|
@@ -116,4 +138,80 @@ public class WechatServiceImpl implements IWechatService { |
|
@@ -116,4 +138,80 @@ public class WechatServiceImpl implements IWechatService { |
116
|
private String getQueryUserUrl(String openId, AccessToken accessToken) {
|
138
|
private String getQueryUserUrl(String openId, AccessToken accessToken) {
|
117
|
return Consts.QUERY_USER_INFO_URL.replace("ACCESS_TOKEN", accessToken.getToken()).replace("OPENID", openId);
|
139
|
return Consts.QUERY_USER_INFO_URL.replace("ACCESS_TOKEN", accessToken.getToken()).replace("OPENID", openId);
|
118
|
}
|
140
|
}
|
|
|
141
|
+
|
|
|
142
|
+
|
|
|
143
|
+ @Override
|
|
|
144
|
+ public void sendWechatCustomMsg(WechatCustomMsgBO customMsgBO) throws ServiceException{
|
|
|
145
|
+ logger.info("enter sendWechatCustomMsg with sceneKey is {},msgBO is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
|
|
|
146
|
+
|
|
|
147
|
+ if(CollectionUtils.isEmpty(customMsgBO.getOpenIdSet())){
|
|
|
148
|
+ logger.warn("sendWechatCustomMsg with sendOpenIds null, sceneKey is {}",customMsgBO.getSendKey());
|
|
|
149
|
+ return;
|
|
|
150
|
+ }
|
|
|
151
|
+ String sceneKey = customMsgBO.getSendKey();
|
|
|
152
|
+ Set<String> openIdSet = new HashSet<>(customMsgBO.getOpenIdSet());
|
|
|
153
|
+ logger.info("sendWechatCustomMsg with sceneKey is {},send origin size is {}",sceneKey,openIdSet.size());
|
|
|
154
|
+ //根据过滤条件进行过滤
|
|
|
155
|
+ if(customMsgBO.getFilterDays() != 0){
|
|
|
156
|
+ getDaysFilter(customMsgBO,openIdSet);
|
|
|
157
|
+ }
|
|
|
158
|
+ //组装发送
|
|
|
159
|
+ List<String> sendSuccessList = customMsgSend(customMsgBO,openIdSet);
|
|
|
160
|
+ logger.info("end sendWechatCustomMsg with sceneKey is {},successSize is {}",customMsgBO.getSendKey(),sendSuccessList.size());
|
|
|
161
|
+ //成功记录存redis
|
|
|
162
|
+ String redisKeyStr = RedisCacheKeyConstant.WECAHT_CUSTOM_SEND_KEY + customMsgBO.getPublicNumberCode() + '_' + nowDate;
|
|
|
163
|
+ //成功记录存7天
|
|
|
164
|
+ pushRedisService.setListValue(redisKeyStr,sendSuccessList,SEVEN_DAY_HOURS);
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ private void getDaysFilter(WechatCustomMsgBO customMsgBO, Set<String> openIdSet) {
|
|
|
168
|
+ int filterDayReal = customMsgBO.getFilterDays() - 1;
|
|
|
169
|
+ //根据间隔天数计算出需要过滤到的最早的一天
|
|
|
170
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
171
|
+ calendar.add(Calendar.DATE, -filterDayReal);
|
|
|
172
|
+ String startDate = DateUtil.dateToString(calendar.getTime(), DATE_ID_FORMAT);
|
|
|
173
|
+ Date date = DateUtil.stringToDate(startDate,"DATE_ID_FORMAT");
|
|
|
174
|
+ Calendar c = Calendar.getInstance();
|
|
|
175
|
+ c.setTime(date);
|
|
|
176
|
+ int day = c.get(Calendar.DATE);
|
|
|
177
|
+ for (int i = 0; i <= filterDayReal; i++){
|
|
|
178
|
+ //从最早日期开始往后累加到每个日期,每天过滤
|
|
|
179
|
+ c.set(Calendar.DATE, day + i);
|
|
|
180
|
+ String nowDate = new SimpleDateFormat(DATE_ID_FORMAT).format(c.getTime());
|
|
|
181
|
+ //根据日期获取当天发送客服消息的记录
|
|
|
182
|
+ String redisKeyStr = RedisCacheKeyConstant.WECAHT_CUSTOM_SEND_KEY + customMsgBO.getPublicNumberCode() + '_' + nowDate;
|
|
|
183
|
+ pushRedisService.getSetFilter(openIdSet,redisKeyStr);
|
|
|
184
|
+ logger.info("sendWechatCustomMsg with day filter,sceneKey is {},redisKeyStr is {},nowSize is {}",customMsgBO.getSendKey(),redisKeyStr,openIdSet.size());
|
|
|
185
|
+ }
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+
|
|
|
189
|
+ private List<String> customMsgSend(WechatCustomMsgBO customMsgBO,Set<String> openIdSet){
|
|
|
190
|
+ logger.info("begin customMsgSend with sceneKey is {},send openId size is {}",customMsgBO.getSendKey(),openIdSet.size());
|
|
|
191
|
+ // 获取token 和接口url
|
|
|
192
|
+ AccessToken accessToken = weixinAccessTokenService.queryWeixinAccessToken(customMsgBO.getPublicNumberCode());
|
|
|
193
|
+ //若仍未获取到 token 则返回
|
|
|
194
|
+ if(accessToken == null){
|
|
|
195
|
+ logger.warn("customMsgSend with accessToken is null,sendKey is {}",customMsgBO.getSendKey());
|
|
|
196
|
+ return new ArrayList<>();
|
|
|
197
|
+ }
|
|
|
198
|
+ List<String> successList = new ArrayList<>();
|
|
|
199
|
+ String templateUrl = this.getTemplateUrl(accessToken, Consts.CUSTOM_MSG_SEND_URL);
|
|
|
200
|
+ //组装消息内容
|
|
|
201
|
+ JSONObject sendDataObject = new JSONObject();
|
|
|
202
|
+ sendDataObject.put("msgtype",customMsgBO.getMsgType());
|
|
|
203
|
+ sendDataObject.put("news",JSONObject.parseObject(customMsgBO.getSendContent()));
|
|
|
204
|
+ openIdSet.forEach(openId -> {
|
|
|
205
|
+ //限制100ms调一次
|
|
|
206
|
+ rateLimiter.acquire();
|
|
|
207
|
+ sendDataObject.put("touser", openId);
|
|
|
208
|
+ JSONObject result = sendWechatRequest.sendWechatRequest(customMsgBO.getPublicNumberCode(), templateUrl, "POST", sendDataObject.toJSONString());
|
|
|
209
|
+ if (result != null && result.getIntValue("errcode") == 0) {
|
|
|
210
|
+ successList.add(openId);
|
|
|
211
|
+ }
|
|
|
212
|
+ sendWechatMsgLog.info("customMsgSend with sceneKey {},openId is {},result {}, content is {}", customMsgBO.getSendKey(), openId, result, sendDataObject.toJSONString());
|
|
|
213
|
+ });
|
|
|
214
|
+ return successList;
|
|
|
215
|
+ }
|
|
|
216
|
+
|
119
|
} |
217
|
} |