|
@@ -8,10 +8,7 @@ import com.google.common.cache.*;
|
|
|
import com.idiot.operationbackend.config.PlatformProperties;
|
|
|
import com.idiot.operationbackend.entity.*;
|
|
|
import com.idiot.operationbackend.handler.FansInfoHandler;
|
|
|
-import com.idiot.operationbackend.service.facade.AccountFansService;
|
|
|
-import com.idiot.operationbackend.service.facade.AccountService;
|
|
|
-import com.idiot.operationbackend.service.facade.AccountTagService;
|
|
|
-import com.idiot.operationbackend.service.facade.WeChatService;
|
|
|
+import com.idiot.operationbackend.service.facade.*;
|
|
|
import com.idiot.operationbackend.support.Constants;
|
|
|
import com.idiot.operationbackend.support.CustomException;
|
|
|
import com.idiot.operationbackend.support.WxInputStreamResource;
|
|
@@ -33,6 +30,7 @@ import javax.annotation.Resource;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -70,6 +68,13 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
|
|
|
private FansInfoHandler fansInfoHandler;
|
|
|
|
|
|
+ private GroupMsgService groupMsgService;
|
|
|
+
|
|
|
+ private TemplateMsgService templateMsgService;
|
|
|
+
|
|
|
+ private CustomerMsgService customerMsgService;
|
|
|
+
|
|
|
+ private AccountCustomerMsgService accountCustomerMsgService;
|
|
|
|
|
|
@Override
|
|
|
public String notice(Map<String, String> param) {
|
|
@@ -271,7 +276,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
throw new CustomException(500,"当前公众号正在后台同步粉丝数据,请您稍等一会!");
|
|
|
}
|
|
|
cache.put(lockKey,lockKey);
|
|
|
- logger.info("公众号:{}同步粉丝数据---- start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("公众号:{}同步粉丝数据---- start,时间:{}",accountId, LocalDateTime.now());
|
|
|
String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s";
|
|
|
String nextOpenId = "";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
@@ -298,7 +303,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
fansInfoHandler.doSyncUserTask(accountId,openIds);
|
|
|
}while (count < total);
|
|
|
cache.invalidate(lockKey);
|
|
|
- logger.info("公众号:{}同步粉丝数据----end,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("公众号:{}同步粉丝数据----end,时间:{}",accountId, LocalDateTime.now());
|
|
|
return count;
|
|
|
}
|
|
|
|
|
@@ -311,7 +316,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
return;
|
|
|
}
|
|
|
int size = openIds.size();
|
|
|
- logger.info("公众号:{}同步粉丝请求粉丝信息,openId 大小:{}---- start,时间:{}",accountId,size,LocalDateTime.now().toString());
|
|
|
+ logger.info("公众号:{}同步粉丝请求粉丝信息,openId 大小:{}---- start,时间:{}",accountId,size,LocalDateTime.now());
|
|
|
List<AccountFans> accountFans = new ArrayList<>(size);
|
|
|
|
|
|
for (String openId : openIds) {
|
|
@@ -337,7 +342,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
fans.setTags(fans.initTags());
|
|
|
accountFans.add(fans);
|
|
|
}
|
|
|
- logger.info("公众号:{}同步粉丝请求粉丝信息,openId 大小:{}---- end,时间:{}",accountId,size,LocalDateTime.now().toString());
|
|
|
+ logger.info("公众号:{}同步粉丝请求粉丝信息,openId 大小:{}---- end,时间:{}",accountId,size,LocalDateTime.now());
|
|
|
// 保存用户
|
|
|
fansService.saveOrUpdateBatch(accountFans,1000);
|
|
|
}
|
|
@@ -353,7 +358,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
throw new CustomException(500,"");
|
|
|
}
|
|
|
cache.put(lockKey,lockKey);
|
|
|
- logger.info("公众号:{}同步标签 ---- start,时间:{}",accountId,LocalDateTime.now().toString());
|
|
|
+ logger.info("公众号:{}同步标签 ---- start,时间:{}",accountId,LocalDateTime.now());
|
|
|
String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=%s";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
@@ -377,7 +382,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
tempTag.setCreateTime(createTime);
|
|
|
accountTags.add(tempTag);
|
|
|
}
|
|
|
- logger.info("公众号:{}同步标签 ---- end,时间:{}",accountId,LocalDateTime.now().toString());
|
|
|
+ logger.info("公众号:{}同步标签 ---- end,时间:{}",accountId,LocalDateTime.now());
|
|
|
// 新增 多余的 删掉缺少的 俗称覆盖
|
|
|
boolean ifOverlay = tagService.overlayAccountTag(accountId,accountTags);
|
|
|
cache.invalidate(lockKey);
|
|
@@ -386,7 +391,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
|
|
|
@Override
|
|
|
public String getFansInfo(String accountId, String openId) {
|
|
|
- logger.info("查询粉丝信息,openId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("查询粉丝信息,openId:{}----start,时间:{}",accountId, LocalDateTime.now());
|
|
|
String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=OPENID&lang=zh_CN";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
requestUrl = String.format(requestUrl,accessToken);
|
|
@@ -395,7 +400,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (headers);
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.GET,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
- logger.info("查询粉丝信息,openId:{}----end,时间:{},微信返回:{}",accountId, LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("查询粉丝信息,openId:{}----end,时间:{},微信返回:{}",accountId, LocalDateTime.now(),jsonStr);
|
|
|
return jsonStr;
|
|
|
}
|
|
|
|
|
@@ -410,7 +415,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
String requestUrl = "https://api.weixin.qq.com/datacube/getusersummary?access_token=%s";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
requestUrl = String.format(requestUrl,accessToken);
|
|
|
- logger.info("获取微信用户增减数据,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("获取微信用户增减数据,accountId:{}----start,时间:{}",accountId, LocalDateTime.now());
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
MultiValueMap<String,String> params = new LinkedMultiValueMap<>(2);
|
|
@@ -420,7 +425,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
logger.info("获取微信用户增减数据,accountId:{}----end,时间:{},微信返回{}",accountId,
|
|
|
- LocalDateTime.now().toString(),jsonStr);
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
return jsonStr;
|
|
|
}
|
|
|
|
|
@@ -430,7 +435,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
String requestUrl = "https://api.weixin.qq.com/datacube/getusercumulate?access_token=%s";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
requestUrl = String.format(requestUrl,accessToken);
|
|
|
- logger.info("获取微信用户汇总数据,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("获取微信用户汇总数据,accountId:{}----start,时间:{}",accountId, LocalDateTime.now());
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
MultiValueMap<String,String> params = new LinkedMultiValueMap<>(2);
|
|
@@ -440,7 +445,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
logger.info("获取微信用户汇总数据,accountId:{}----end,时间:{},微信返回{}",accountId,
|
|
|
- LocalDateTime.now().toString(),jsonStr);
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -451,7 +456,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
requestUrl = String.format(requestUrl,accessToken);
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- logger.info("获取微信图文阅读数据,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("获取微信图文阅读数据,accountId:{}----start,时间:{}",accountId, LocalDateTime.now());
|
|
|
MultiValueMap<String,String> params = new LinkedMultiValueMap<>(2);
|
|
|
params.add("begin_date",startDate);
|
|
|
params.add("end_date",endDate);
|
|
@@ -459,7 +464,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
logger.info("获取微信图文阅读数据,accountId:{}----end,时间:{},微信返回{}",accountId,
|
|
|
- LocalDateTime.now().toString(),jsonStr);
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
return jsonStr;
|
|
|
}
|
|
|
|
|
@@ -470,14 +475,14 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
requestUrl = String.format(requestUrl,accessToken);
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- logger.info("微信设置备注,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("微信设置备注,accountId:{}----start,时间:{}",accountId, LocalDateTime.now());
|
|
|
MultiValueMap<String,String> params = new LinkedMultiValueMap<>(2);
|
|
|
params.add("openid",openId);
|
|
|
params.add("remark",remark);
|
|
|
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (params,headers);
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
- logger.info("微信设置备注,accountId:{}----end,时间:{},微信返回{}",accountId,LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("微信设置备注,accountId:{}----end,时间:{},微信返回{}",accountId,LocalDateTime.now(),jsonStr);
|
|
|
if (0 ==JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE)) {
|
|
|
return fansService.updateFansRemark(accountId,openId,remark);
|
|
|
}
|
|
@@ -497,10 +502,11 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
map.put("name",tagLabel);
|
|
|
params.add("tag",map);
|
|
|
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<> (params,headers);
|
|
|
- logger.info("微信创建标签,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("微信创建标签,accountId:{}----start,时间:{}",accountId, LocalDateTime.now());
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
- logger.info("微信创建标签,accountId:{}----end,时间:{},微信返回{}",accountId,LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("微信创建标签,accountId:{}----end,时间:{},微信返回{}",accountId,
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
JSONObject temp = JSONObject.parseObject(jsonStr).getJSONObject("tag");
|
|
|
if (null != temp) {
|
|
|
Integer wxId = temp.getInteger("id");
|
|
@@ -523,10 +529,11 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
params.add("tagid",wxId);
|
|
|
int size = openIds.size();
|
|
|
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<> (params,headers);
|
|
|
- logger.info("微信用户打标签,accountId:{},opedId size:{}----start,时间:{}",accountId,size, LocalDateTime.now().toString());
|
|
|
+ logger.info("微信用户打标签,accountId:{},opedId size:{}----start,时间:{}",accountId,size, LocalDateTime.now());
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
- logger.info("微信用户打标签,accountId:{},opedId size:{}----end,时间:{},微信返回:{}",accountId,size, LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("微信用户打标签,accountId:{},opedId size:{}----end,时间:{},微信返回:{}",accountId,size,
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
if (0 ==JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE)) {
|
|
|
return tagService.addTagSize(accountId,wxId,size);
|
|
|
}
|
|
@@ -546,10 +553,11 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
params.add("tagid",wxId);
|
|
|
int size = openIds.size();
|
|
|
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<> (params,headers);
|
|
|
- logger.info("微信用户去除标签,accountId:{},opedId size:{}----start,时间:{}",accountId,size, LocalDateTime.now().toString());
|
|
|
+ logger.info("微信用户去除标签,accountId:{},opedId size:{}----start,时间:{}",accountId,size, LocalDateTime.now());
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
- logger.info("微信用去除标签,accountId:{},opedId size:{}----end,时间:{},微信返回:{}",accountId,size, LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("微信用去除标签,accountId:{},opedId size:{}----end,时间:{},微信返回:{}",accountId,size,
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
if (0 ==JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE)) {
|
|
|
return tagService.subTagSize(accountId,wxId,size);
|
|
|
}
|
|
@@ -571,10 +579,10 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
map.put("id",wxId);
|
|
|
params.add("tag",map);
|
|
|
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<> (params,headers);
|
|
|
- logger.info("微信创建标签,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("微信创建标签,accountId:{}----start,时间:{}",accountId, LocalDateTime.now());
|
|
|
ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
String jsonStr = respStr.getBody();
|
|
|
- logger.info("微信创建标签,accountId:{}----end,时间:{},微信返回{}",accountId,LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("微信创建标签,accountId:{}----end,时间:{},微信返回{}",accountId,LocalDateTime.now(),jsonStr);
|
|
|
int errorCode = JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE);
|
|
|
return 0 == errorCode;
|
|
|
}
|
|
@@ -596,10 +604,10 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
data.add("description", p.toString());
|
|
|
}
|
|
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(data,httpHeaders);
|
|
|
- logger.info("微信上传素材,accountId:{},type:{}----start,时间:{}",accountId,type, LocalDateTime.now().toString());
|
|
|
+ logger.info("微信上传素材,accountId:{},type:{}----start,时间:{}",accountId,type, LocalDateTime.now());
|
|
|
String resultJson = restTemplate.postForObject(requestUrl, requestEntity, String.class);
|
|
|
logger.info("微信上传素材,accountId:{},type:{}----end,时间:{},微信返回结果:{}",accountId,type,
|
|
|
- LocalDateTime.now().toString(),resultJson);
|
|
|
+ LocalDateTime.now(),resultJson);
|
|
|
return resultJson;
|
|
|
}
|
|
|
|
|
@@ -623,10 +631,10 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
HttpEntity<MultiValueMap<String,Object>> httpEntity = new HttpEntity<>(valueMap,httpHeaders);
|
|
|
logger.info("微信上传图文素材,accountId:{},articles:{}----start,时间:{}",accountId,articles.toString(),
|
|
|
- LocalDateTime.now().toString());
|
|
|
+ LocalDateTime.now());
|
|
|
String respJson = restTemplate.postForObject(requestUrl,httpEntity,String.class);
|
|
|
logger.info("微信上传图文素材,accountId:{},articles:{}----end,时间:{},微信返回:{}",accountId,articles.toString(),
|
|
|
- LocalDateTime.now().toString(),respJson);
|
|
|
+ LocalDateTime.now(),respJson);
|
|
|
return respJson;
|
|
|
}
|
|
|
|
|
@@ -648,11 +656,11 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
HttpEntity<String> request = new HttpEntity<String>(parentNode.toJSONString(), headers);
|
|
|
logger.info("微信上生成二维码,accountId:{},QrCode:{}----start,时间:{}",accountId,qrCode.toString(),
|
|
|
- LocalDateTime.now().toString());
|
|
|
+ LocalDateTime.now());
|
|
|
ResponseEntity<String> responseEntity = restTemplate.postForEntity(requestUrl,request,String.class);
|
|
|
String jsonStr = responseEntity.getBody();
|
|
|
logger.info("微信上生成二维码,accountId:{},QrCode:{}----end,时间:{},微信返回:{}",accountId,qrCode.toString(),
|
|
|
- LocalDateTime.now().toString(),jsonStr);
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
return jsonStr;
|
|
|
}
|
|
|
|
|
@@ -669,11 +677,10 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
HttpEntity<String> request = new HttpEntity<String>(param, headers);
|
|
|
- logger.info("微信创建公众号菜单,accountId:{},accountMenus:{}----start,时间:{}",accountId,param,
|
|
|
- LocalDateTime.now().toString());
|
|
|
+ logger.info("微信创建公众号菜单,accountId:{},accountMenus:{}----start,时间:{}",accountId,param, LocalDateTime.now());
|
|
|
String jsonStr = restTemplate.postForObject(requestUrl,request,String.class);
|
|
|
logger.info("微信创建公众号菜单,accountId:{},accountMenus:{}----end,时间:{},微信返回:{}",accountId,param,
|
|
|
- LocalDateTime.now().toString(),jsonStr);
|
|
|
+ LocalDateTime.now(),jsonStr);
|
|
|
int errcode = JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE);
|
|
|
return 0 == errcode;
|
|
|
}
|
|
@@ -684,9 +691,9 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
url = String.format(url,accessToken);
|
|
|
- logger.info("微信删除公众号菜单,accountId:{}----start,时间:{}",accountId,LocalDateTime.now().toString());
|
|
|
+ logger.info("微信删除公众号菜单,accountId:{}----start,时间:{}",accountId,LocalDateTime.now());
|
|
|
String jsonStr = restTemplate.getForObject(url,String.class);
|
|
|
- logger.info("微信创建公众号菜单,accountId:{}----end,时间:{},微信返回:{}",accountId,LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("微信创建公众号菜单,accountId:{}----end,时间:{},微信返回:{}",accountId,LocalDateTime.now(),jsonStr);
|
|
|
int errcode = JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE);
|
|
|
return 0 == errcode;
|
|
|
}
|
|
@@ -697,35 +704,238 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
String requestUrl = " https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=%s";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
requestUrl = String.format(requestUrl,accessToken);
|
|
|
- logger.info("微信请求公众号模板消息列表,accountId:{}----start,时间:{}",accountId,LocalDateTime.now().toString());
|
|
|
+ logger.info("微信请求公众号模板消息列表,accountId:{}----start,时间:{}",accountId,LocalDateTime.now());
|
|
|
String jsonStr = restTemplate.getForObject(requestUrl,String.class);
|
|
|
- logger.info("微信请求公众号模板消息列表,accountId:{}----end,时间:{},微信返回:{}",accountId,LocalDateTime.now().toString(),jsonStr);
|
|
|
+ logger.info("微信请求公众号模板消息列表,accountId:{}----end,时间:{},微信返回:{}",accountId,LocalDateTime.now(),jsonStr);
|
|
|
return jsonStr;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public void groupMsgTask(GroupMsg groupMsg) {
|
|
|
+ public void doGroupMsgTask(GroupMsg groupMsg) {
|
|
|
+
|
|
|
if (Objects.isNull(groupMsg)) {
|
|
|
return ;
|
|
|
}
|
|
|
+ logger.info("微信公众号群发消息任务,groupMsg:{}----start,时间:{}",groupMsg.toString(),LocalDateTime.now());
|
|
|
+ List<String> openIds = null;
|
|
|
String accountId = groupMsg.getAccountId();
|
|
|
Integer sex = null;
|
|
|
String province = null;
|
|
|
String city = null;
|
|
|
String tag = null;
|
|
|
String subscribeTime = null;
|
|
|
- if (Constants.SELECT == groupMsg.getType()) {
|
|
|
+ if (!groupMsg.getType()) {
|
|
|
sex = groupMsg.getSelectSex();
|
|
|
province = groupMsg.getSelectProvince();
|
|
|
city = groupMsg.getSelectCity();
|
|
|
tag = groupMsg.getSelectTag();
|
|
|
subscribeTime = groupMsg.getSelectSubscribeTime();
|
|
|
+ List<AccountFans> fans = fansService.queryAccountFans(accountId,sex,province,city,tag,subscribeTime);
|
|
|
+ openIds = fans.stream().map(AccountFans::getOpenId).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ long count = sendGroupMessage(accountId,groupMsg.getContents(),groupMsg.getType(),groupMsg.getRepeatSend(),openIds);
|
|
|
+ groupMsg.setStatus(count>0?Constants.SUCCESSED:Constants.FAILED);
|
|
|
+ groupMsg.setSendNum(count);
|
|
|
+ groupMsgService.updateById(groupMsg);
|
|
|
+ logger.info("微信公众号群发消息任务,groupMsg:{}----end,时间:{}",groupMsg.toString(),LocalDateTime.now());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doTemplateMsgTask(TemplateMsg templateMsg) {
|
|
|
+ if (Objects.isNull(templateMsg)) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ logger.info("微信公众号模板消息任务,templateMsg:{}----start,时间:{}",templateMsg.toString(),LocalDateTime.now());
|
|
|
+ String accountId = templateMsg.getAccountId();
|
|
|
+ Integer sex = templateMsg.getSelectSex();
|
|
|
+ String province = templateMsg.getSelectProvince();
|
|
|
+ String city = templateMsg.getSelectCity();
|
|
|
+ String tag = templateMsg.getSelectTag();
|
|
|
+ String subscribeTime = templateMsg.getSelectSubscribeTime();
|
|
|
List<AccountFans> fans = fansService.queryAccountFans(accountId,sex,province,city,tag,subscribeTime);
|
|
|
+ long count = sendTemplateMessage(accountId,fans,templateMsg);
|
|
|
+ templateMsg.setStatus(count>0?Constants.SUCCESSED:Constants.FAILED);
|
|
|
+ templateMsg.setSendNum(count);
|
|
|
+ templateMsgService.updateById(templateMsg);
|
|
|
+ logger.info("微信公众号模板消息任务,templateMsg:{}----end,时间:{}",templateMsg.toString(),LocalDateTime.now());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doCustomerMsgTask(CustomerMsg customerMsg) {
|
|
|
+ if (Objects.isNull(customerMsg)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ logger.info("微信公众号客服消息任务,customerMsg:{}----start,时间:{}",customerMsg.toString(),LocalDateTime.now());
|
|
|
+ String contents = customerMsg.getContents();
|
|
|
+ Integer sex = customerMsg.getSelectSex();
|
|
|
+ String province = customerMsg.getSelectProvince();
|
|
|
+ String city = customerMsg.getSelectCity();
|
|
|
+ String tag = customerMsg.getSelectTag();
|
|
|
+ String subscribeTime = customerMsg.getSelectSubscribeTime();
|
|
|
+ String msgId = customerMsg.getId();
|
|
|
+ List<AccountCustomerMsg> accountCustomerMsgs = accountCustomerMsgService.queryByMsgId(msgId);
|
|
|
+ if (CollectionUtils.isEmpty(accountCustomerMsgs)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String accountId = null;
|
|
|
+ long count = 0;
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";
|
|
|
+ for (AccountCustomerMsg accountCustomerMsg : accountCustomerMsgs) {
|
|
|
+ accountId = accountCustomerMsg.getAccountId();
|
|
|
+ List<AccountFans> fans = fansService.queryAccountFans(accountId,sex,province,city,tag,subscribeTime);
|
|
|
+ long temp = sendCustomerMessage(accountId,fans,contents,requestUrl);
|
|
|
+ accountCustomerMsg.setSuccessNum(temp);
|
|
|
+ accountCustomerMsg.setStatus(temp>0?Constants.SUCCESSED:Constants.FAILED);
|
|
|
+ count += temp;
|
|
|
+ }
|
|
|
+ customerMsg.setStatus(count>0?Constants.SUCCESSED:Constants.FAILED);
|
|
|
+ customerMsg.setSuccessNum(count);
|
|
|
+ accountCustomerMsgService.updateBatchById(accountCustomerMsgs);
|
|
|
+ customerMsgService.updateById(customerMsg);
|
|
|
+ logger.info("微信公众号客服消息任务,customerMsg:{}----end,时间:{}",customerMsg.toString(),LocalDateTime.now());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 群发消息
|
|
|
+ * @author wangxiao
|
|
|
+ * @date 13:58 2020/9/24
|
|
|
+ * @param accountId 公众号id
|
|
|
+ * @param contents 群发内容json
|
|
|
+ * @param toAll 是否全部
|
|
|
+ * @param repeatSend 转载继续
|
|
|
+ * @param openIds 发送用户群体
|
|
|
+ * @return java.lang.Boolean
|
|
|
+ */
|
|
|
+ private long sendGroupMessage(String accountId, String contents, boolean toAll, int repeatSend, List<String> openIds) {
|
|
|
+ logger.info("公众号:{}群发消息:{},toAll:{},repeatSend:{},openIds:{}",accountId,contents,toAll,repeatSend,openIds);
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=%s";
|
|
|
+ String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
+ requestUrl = String.format(requestUrl,accessToken);
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(contents);
|
|
|
+ int size = jsonArray.size();
|
|
|
+ if (size == 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int groupCount =0;
|
|
|
+ JSONObject temp = null;
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ temp = jsonArray.getJSONObject(i);
|
|
|
+ logger.info("微信群发消息,accountId:{},openIds:{}----start,时间:{}",accountId,openIds,LocalDateTime.now());
|
|
|
+ String param = parseToJson(temp,openIds,repeatSend,toAll);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> request = new HttpEntity<>(param, headers);
|
|
|
+ String jsonStr = restTemplate.postForObject(requestUrl,request,String.class);
|
|
|
+ logger.info("微信群发消息,accountId:{},openIds:{}----end,时间:{},微信返回:{}",accountId,openIds,LocalDateTime.now(),jsonStr);
|
|
|
+ int errcode = JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE);
|
|
|
+ if (0 == errcode) {
|
|
|
+ groupCount+=1;
|
|
|
+ };
|
|
|
+ logger.info("公众号:{}群发消息第{}条,发送结果:{}",accountId,i,0 == errcode);
|
|
|
+ }
|
|
|
+ return groupCount;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送模板消息
|
|
|
+ * @author wangxiao
|
|
|
+ * @date 14:05 2020/9/24
|
|
|
+ * @param fansList 用户集群
|
|
|
+ * @param templateMsg 模板消息内容
|
|
|
+ * @return boolean 是否成功
|
|
|
+ */
|
|
|
+ private long sendTemplateMessage (String accountId,List<AccountFans> fansList,TemplateMsg templateMsg) {
|
|
|
+ logger.info("公众号:{}发送模板消息:,AccountFans size :{},templateMsg:{}",accountId, fansList.size(),templateMsg.toString());
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
|
|
|
+ String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
+ requestUrl = String.format(requestUrl,accessToken);
|
|
|
+ String templateData = templateMsg.getTemplateData();
|
|
|
+ int templateCount = 0;
|
|
|
+ for (AccountFans fans : fansList) {
|
|
|
+ templateData = templateData.replace("<粉丝昵称>",fans.getNickName());
|
|
|
+ String json = parseJson(fans.getOpenId(),templateData,templateMsg.getTemplateId(),
|
|
|
+ templateMsg.getLinkUrl(),templateMsg.getLinkAppId());
|
|
|
+ logger.info("微信模板消息消息,accountId:{},openId:{}----start,时间:{}",accountId,
|
|
|
+ fans.getOpenId(),LocalDateTime.now());
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> request = new HttpEntity<>(json, headers);
|
|
|
+ String jsonStr = restTemplate.postForObject(requestUrl,request,String.class);
|
|
|
+ logger.info("微信模板消息消息,accountId:{},openId:{}----end,时间:{},微信返回:{}",accountId,
|
|
|
+ fans.getOpenId(),LocalDateTime.now(),jsonStr);
|
|
|
+ int errcode = JSONObject.parseObject(jsonStr).getIntValue(ERROR_CODE);
|
|
|
+ if (0 == errcode) {
|
|
|
+ templateCount+=1;
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return templateCount;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public long sendCustomerMessage (String accountId,List<AccountFans> fansList,String contents,String url) {
|
|
|
+ int result = 0;
|
|
|
+ if (CollectionUtils.isEmpty(fansList)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int count = 0;
|
|
|
+ String nikeName = null;
|
|
|
+ String openId = null;
|
|
|
+ for (AccountFans fans : fansList) {
|
|
|
+ openId = fans.getOpenId();
|
|
|
+ nikeName =fans.getNickName();
|
|
|
+ logger.info("微信客服消息,粉丝逐条发送,accountId:{},openId:{},nikeName:{}",accountId,openId,nikeName);
|
|
|
+ sendCustomerMessage(openId,contents,nikeName,accountId,url);
|
|
|
+ count +=1;
|
|
|
+ }
|
|
|
+ return count;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 单人 发送客服消息消息
|
|
|
+ * @author wangxiao
|
|
|
+ * @date 15:20 2020/9/24
|
|
|
+ * @param openId
|
|
|
+ * @param nikeName
|
|
|
+ * @param contents
|
|
|
+ * @param url
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ private void sendCustomerMessage(String openId,String contents,String nikeName,String accountId,String url) {
|
|
|
+ contents = contents.replace("<粉丝昵称>",nikeName);
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(contents);
|
|
|
+ int size = jsonArray.size();
|
|
|
+ if (size == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
+ url = String.format(url,accessToken);
|
|
|
+ JSONObject temp = null;
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ temp = jsonArray.getJSONObject(i);
|
|
|
+ String param = parseJson(openId,temp);
|
|
|
+ logger.info("微信客服消息,单人第{}条发送,accountId:{},openId:{}----start,时间:{}",i,accountId,openId,LocalDateTime.now());
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> request = new HttpEntity<>(param, headers);
|
|
|
+ String jsonStr = restTemplate.postForObject(url,request,String.class);
|
|
|
+ logger.info("微信客服消息,单人第{}条发送,accountId:{},openId:{}----end,时间:{},微信返回:{}",i,accountId,openId,LocalDateTime.now(),jsonStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 菜单转换 微信json格式
|
|
|
+ * @author wangxiao
|
|
|
+ * @date 10:59 2020/9/24
|
|
|
+ * @param accountMenus accountMenus
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
private String parseToJson (List<AccountMenu> accountMenus) {
|
|
|
int size = accountMenus.size();
|
|
|
JSONObject jsonObject = new JSONObject(1);
|
|
@@ -755,7 +965,135 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
button.add(temp);
|
|
|
}
|
|
|
jsonObject.put("button",button);
|
|
|
- return jsonObject.toJSONString();
|
|
|
+ String result = jsonObject.toJSONString();
|
|
|
+ logger.info("自定义菜单 转换json 格式结果:{}",result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换群发消息得json
|
|
|
+ * @author wangxiao
|
|
|
+ * @date 11:53 2020/9/24
|
|
|
+ * @param jsonObject jsonObject
|
|
|
+ * @param openIds openIds
|
|
|
+ * @param repeatSend 转载是否继续
|
|
|
+ * @param toAll 全部
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ public String parseToJson(JSONObject jsonObject,List<String> openIds,int repeatSend,boolean toAll ) {
|
|
|
+ int type = jsonObject.getIntValue("type");
|
|
|
+ JSONObject parent = new JSONObject();
|
|
|
+ JSONObject filter = new JSONObject();
|
|
|
+ JSONObject target = new JSONObject();
|
|
|
+ filter.put("is_to_all",toAll);
|
|
|
+ if (toAll) {
|
|
|
+ parent.put("filter",filter);
|
|
|
+ }else {
|
|
|
+ parent.put("touser",openIds);
|
|
|
+ }
|
|
|
+ if (Constants.VID == type) {
|
|
|
+ target.put("media_id",jsonObject.getString("media_id"));
|
|
|
+ target.put("title",jsonObject.getString("title"));
|
|
|
+ target.put("description",jsonObject.getString("description"));
|
|
|
+ parent.put("mpvideo",target);
|
|
|
+ parent.put("msgtype","mpvideo");
|
|
|
+ }else if (Constants.VOI == type) {
|
|
|
+ target.put("media_id",jsonObject.getString("media_id"));
|
|
|
+ parent.put("voice",target);
|
|
|
+ parent.put("msgtype","voice");
|
|
|
+ }else if (Constants.TEXT == type) {
|
|
|
+ target.put("content",jsonObject.getString("content"));
|
|
|
+ parent.put("voice",target);
|
|
|
+ parent.put("msgtype","text");
|
|
|
+ }else if (Constants.IMG == type) {
|
|
|
+ target.put("media_ids",Arrays.asList(jsonObject.getString("media_id")));
|
|
|
+ target.put("recommend","分享图片");
|
|
|
+ target.put("need_open_comment",1);
|
|
|
+ target.put("only_fans_can_comment",0);
|
|
|
+ parent.put("images",target);
|
|
|
+ parent.put("msgtype","image");
|
|
|
+ }else if (Constants.NEWS == type) {
|
|
|
+ target.put("media_ids",jsonObject.getString("media_id"));
|
|
|
+ parent.put("mpnews",target);
|
|
|
+ parent.put("msgtype","mpnews");
|
|
|
+ parent.put("send_ignore_reprint",repeatSend);
|
|
|
+ }
|
|
|
+ String result = parent.toJSONString();
|
|
|
+ logger.info("群发消息 转换json 格式结果:{}",result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模板数据转换json
|
|
|
+ * @author wangxiao
|
|
|
+ * @date 14:16 2020/9/24
|
|
|
+ * @param openId openiD
|
|
|
+ * @param templateData 模板数据
|
|
|
+ * @param templateId 模板id
|
|
|
+ * @param linkUrl 链接地址
|
|
|
+ * @param linkAppId 链接小程序appid
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ private String parseJson (String openId,String templateData,String templateId,String linkUrl,String linkAppId) {
|
|
|
+ JSONObject parent = new JSONObject();
|
|
|
+ parent.put("touser",openId);
|
|
|
+ parent.put("template_id",templateId);
|
|
|
+ parent.put("data",JSONArray.parseArray(templateData));
|
|
|
+ if (!StringUtils.isEmpty(linkAppId)) {
|
|
|
+ JSONObject miniprogram = new JSONObject();
|
|
|
+ miniprogram.put("appid",linkAppId);
|
|
|
+ miniprogram.put("pagepath",linkUrl);
|
|
|
+ parent.put("miniprogram",miniprogram);
|
|
|
+ }else if (!StringUtils.isEmpty(linkUrl)) {
|
|
|
+ parent.put("url",linkUrl);
|
|
|
+ }
|
|
|
+ String result = parent.toJSONString();
|
|
|
+ logger.info("模板消息 转换json 格式结果:{}",result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客服消息转换 json
|
|
|
+ * @author wangxiao
|
|
|
+ * @date 15:40 2020/9/24
|
|
|
+ * @param openId openId
|
|
|
+ * @param jsonObject jsonObject
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ private String parseJson(String openId, JSONObject jsonObject) {
|
|
|
+ int type = jsonObject.getIntValue("type");
|
|
|
+ JSONObject parent = new JSONObject();
|
|
|
+ parent.put("touser",openId);
|
|
|
+ JSONObject target = new JSONObject();
|
|
|
+ if (Constants.VID == type) {
|
|
|
+ target.put("media_id",jsonObject.getString("media_id"));
|
|
|
+ target.put("thumb_media_id",jsonObject.getString("thumb_media_id"));
|
|
|
+ target.put("description",jsonObject.getString("description"));
|
|
|
+ parent.put("mpvideo",target);
|
|
|
+ }else if (Constants.VOI == type) {
|
|
|
+ target.put("media_id",jsonObject.getString("media_id"));
|
|
|
+ parent.put("voice",target);
|
|
|
+ parent.put("msgtype","voice");
|
|
|
+ }else if (Constants.TEXT == type) {
|
|
|
+ target.put("content",jsonObject.getString("content"));
|
|
|
+ parent.put("voice",target);
|
|
|
+ parent.put("msgtype","text");
|
|
|
+ }else if (Constants.IMG == type) {
|
|
|
+ target.put("media_ids",jsonObject.getString("media_id"));
|
|
|
+ parent.put("images",target);
|
|
|
+ parent.put("msgtype","image");
|
|
|
+ }else if (Constants.NEWS == type) {
|
|
|
+ target.put("articles",jsonObject.getJSONArray("articles"));
|
|
|
+ parent.put("news",target);
|
|
|
+ parent.put("msgtype","news");
|
|
|
+ }else if (Constants.MNEWS == type){
|
|
|
+ target.put("media_id",jsonObject.getJSONArray("media_id"));
|
|
|
+ parent.put("mpnews",target);
|
|
|
+ parent.put("msgtype","mpnews");
|
|
|
+ }
|
|
|
+ String result = parent.toJSONString();
|
|
|
+ logger.info("客服消息 转换json 格式结果:{}",result);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Resource(name = "rpcRestTemplate")
|
|
@@ -787,4 +1125,24 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
public void setFansInfoHandler(FansInfoHandler fansInfoHandler) {
|
|
|
this.fansInfoHandler = fansInfoHandler;
|
|
|
}
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setGroupMsgService(GroupMsgService groupMsgService) {
|
|
|
+ this.groupMsgService = groupMsgService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setTemplateMsgService(TemplateMsgService templateMsgService) {
|
|
|
+ this.templateMsgService = templateMsgService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setCustomerMsgService(CustomerMsgService customerMsgService) {
|
|
|
+ this.customerMsgService = customerMsgService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setAccountCustomerMsgService(AccountCustomerMsgService accountCustomerMsgService) {
|
|
|
+ this.accountCustomerMsgService = accountCustomerMsgService;
|
|
|
+ }
|
|
|
}
|