|
|
@ -2,12 +2,15 @@ package com.elink.esua.epdc.service.impl; |
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; |
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; |
|
|
|
import com.elink.esua.epdc.commons.tools.exception.RenException; |
|
|
|
import com.elink.esua.epdc.commons.tools.redis.RedisKeys; |
|
|
|
import com.elink.esua.epdc.commons.tools.redis.RedisUtils; |
|
|
|
import com.elink.esua.epdc.commons.tools.utils.Result; |
|
|
|
import com.elink.esua.epdc.dto.TemplateData; |
|
|
|
import com.elink.esua.epdc.dto.epdc.form.EpdcDeleteWxFormIdFormDTO; |
|
|
|
import com.elink.esua.epdc.dto.epdc.result.EpdcUserRegisterAuditMsgResultDTO; |
|
|
|
import com.elink.esua.epdc.dto.epdc.result.UserWorkAndAnalyOpenIdDTO; |
|
|
|
import com.elink.esua.epdc.dto.form.SendMessageFormDTO; |
|
|
|
import com.elink.esua.epdc.enums.AppUserAuditStateEnum; |
|
|
|
import com.elink.esua.epdc.feign.AdminFeignClient; |
|
|
|
import com.elink.esua.epdc.feign.MessageFeignClient; |
|
|
@ -20,11 +23,18 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import me.chanjar.weixin.common.error.WxErrorException; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.Callable; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.concurrent.Future; |
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author yujintao |
|
|
@ -50,6 +60,12 @@ public class MessageServiceImpl implements MessageService { |
|
|
|
@Autowired |
|
|
|
private RedisUtils redisUtils; |
|
|
|
|
|
|
|
@Value("${wx.message.appId}") |
|
|
|
private String appId; |
|
|
|
|
|
|
|
@Value("${wx.message.secret}") |
|
|
|
private String secret; |
|
|
|
|
|
|
|
/** |
|
|
|
* 注册审核失败服务消息模版 |
|
|
|
*/ |
|
|
@ -65,6 +81,9 @@ public class MessageServiceImpl implements MessageService { |
|
|
|
*/ |
|
|
|
private static String MA_PAGE = "pages/index/index"; |
|
|
|
|
|
|
|
//创建固定大小为100 的线程池
|
|
|
|
private static ExecutorService threadPool = Executors.newFixedThreadPool(100); |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送六位短信验证码 |
|
|
|
* |
|
|
@ -156,62 +175,146 @@ public class MessageServiceImpl implements MessageService { |
|
|
|
* @author wanggonfeng |
|
|
|
* @date 2020/05/26 14:18 |
|
|
|
*/ |
|
|
|
public Result sendMessage() { |
|
|
|
// 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
|
|
|
String miniprogram_state = "trial"; |
|
|
|
// 模板ID
|
|
|
|
String template_id = "wagGg4eAeHu1N2N0o8Vhyan41Gr3DqFBD81o5Lv7XnQ"; |
|
|
|
// 用户openID
|
|
|
|
String openId = "oZCeb5TZc2oBwT4jHb0kDmQsN0Xw"; |
|
|
|
// 小程序跳转页
|
|
|
|
String page = "pages/index/index"; |
|
|
|
// 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
|
|
|
|
String lang = "zh_CN"; |
|
|
|
// appid
|
|
|
|
String appId = "wxfa4afaa2b5f9c876"; |
|
|
|
// secret
|
|
|
|
String secret = "7db9f049c78c9a6cafa673deebe8330d"; |
|
|
|
// public Result sendMessage() {
|
|
|
|
// // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
|
|
|
// String miniprogram_state = "trial";
|
|
|
|
// // 模板ID
|
|
|
|
// String template_id = "wagGg4eAeHu1N2N0o8Vhyan41Gr3DqFBD81o5Lv7XnQ";
|
|
|
|
// // 用户openID
|
|
|
|
// String openId = "oZCeb5TZc2oBwT4jHb0kDmQsN0Xw";
|
|
|
|
// // 小程序跳转页
|
|
|
|
// String page = "pages/index/index";
|
|
|
|
// // 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
|
|
|
|
// String lang = "zh_CN";
|
|
|
|
// // appid
|
|
|
|
// String appId = "wxfa4afaa2b5f9c876";
|
|
|
|
// // secret
|
|
|
|
// String secret = "7db9f049c78c9a6cafa673deebe8330d";
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// // 获取access_token
|
|
|
|
// //String access_token = getAccess_token(APPID_NORMAL, jwtTokenProperties.getSecret());
|
|
|
|
// String access_token = WxSendMessageUtils.getAccess_token(appId, secret);
|
|
|
|
// // 拼接URL
|
|
|
|
// String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token;
|
|
|
|
//
|
|
|
|
// // 获取openID
|
|
|
|
// /*Result<UserWorkAndAnalyOpenIdDTO> userIdsResult = adminFeignClient.userRelationWorkAndAnalyOpenId();
|
|
|
|
// if(userIdsResult == null || userIdsResult.getData() == null){
|
|
|
|
// return new Result().error("获取openid失败!");
|
|
|
|
// }
|
|
|
|
// // 调用获取openid接口
|
|
|
|
// UserWorkAndAnalyOpenIdDTO userWorkAndAnalyOpenIdDTO = userIdsResult.getData();
|
|
|
|
// List<String> analyOpenIdList = userWorkAndAnalyOpenIdDTO.getAnalyOpenIds();
|
|
|
|
// List<String> workOpenIdList = userWorkAndAnalyOpenIdDTO.getWorkOpenIds();
|
|
|
|
//
|
|
|
|
// for(int i = 0; i < analyOpenIdList.size() ; i++){
|
|
|
|
// String analysisOpenId = analyOpenIdList.get(i);
|
|
|
|
// sendMessageByOpenId(analysisOpenId,url,miniprogram_state);
|
|
|
|
// }
|
|
|
|
// for(int j = 0; j < workOpenIdList.size() ; j++){
|
|
|
|
// String workOpenId = workOpenIdList.get(j);
|
|
|
|
// sendMessageByOpenId(workOpenId,url,miniprogram_state);
|
|
|
|
// }*/
|
|
|
|
//
|
|
|
|
// // 配置模板内容
|
|
|
|
// Map<String, TemplateData> m = new HashMap<>(5);
|
|
|
|
// TemplateData keyword1 = new TemplateData();
|
|
|
|
// keyword1.setValue("11111测试11111");
|
|
|
|
// m.put("thing1", keyword1);
|
|
|
|
// TemplateData keyword2 = new TemplateData();
|
|
|
|
// keyword2.setValue("2019年10月1日");
|
|
|
|
// m.put("time2", keyword2);
|
|
|
|
//
|
|
|
|
// String resultMsg = WxSendMessageUtils.sendMessageByOpenId(openId,url,miniprogram_state,template_id,m,lang);
|
|
|
|
//
|
|
|
|
// return new Result().ok("消息推送成功!");
|
|
|
|
// }
|
|
|
|
|
|
|
|
@Override |
|
|
|
public Result sendMessage(SendMessageFormDTO formDto) { |
|
|
|
// 模板ID
|
|
|
|
String templateId = ""; |
|
|
|
if(1 == formDto.getType()){//通知
|
|
|
|
templateId = WxSendMessageUtils.notice_template_id; |
|
|
|
}else if(1 == formDto.getType()){//话题
|
|
|
|
templateId = WxSendMessageUtils.topic_template_id; |
|
|
|
}else{ |
|
|
|
throw new RenException("消息推送类型错误!"); |
|
|
|
} |
|
|
|
|
|
|
|
// 获取access_token
|
|
|
|
//String access_token = getAccess_token(APPID_NORMAL, jwtTokenProperties.getSecret());
|
|
|
|
String access_token = WxSendMessageUtils.getAccess_token(appId, secret); |
|
|
|
// 拼接URL
|
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token; |
|
|
|
String url = WxSendMessageUtils.url + access_token; |
|
|
|
|
|
|
|
// 获取openID
|
|
|
|
/*Result<UserWorkAndAnalyOpenIdDTO> userIdsResult = adminFeignClient.userRelationWorkAndAnalyOpenId(); |
|
|
|
Result<UserWorkAndAnalyOpenIdDTO> userIdsResult = adminFeignClient.userRelationWorkAndAnalyOpenId(); |
|
|
|
if(userIdsResult == null || userIdsResult.getData() == null){ |
|
|
|
return new Result().error("获取openid失败!"); |
|
|
|
} |
|
|
|
|
|
|
|
// 调用获取openid接口
|
|
|
|
UserWorkAndAnalyOpenIdDTO userWorkAndAnalyOpenIdDTO = userIdsResult.getData(); |
|
|
|
List<String> analyOpenIdList = userWorkAndAnalyOpenIdDTO.getAnalyOpenIds(); |
|
|
|
List<String> workOpenIdList = userWorkAndAnalyOpenIdDTO.getWorkOpenIds(); |
|
|
|
|
|
|
|
for(int i = 0; i < analyOpenIdList.size() ; i++){ |
|
|
|
String analysisOpenId = analyOpenIdList.get(i); |
|
|
|
sendMessageByOpenId(analysisOpenId,url,miniprogram_state); |
|
|
|
if (null != analyOpenIdList && analyOpenIdList.size() > 0) { |
|
|
|
Integer analysisResult = sendOpenIdListMessage(analyOpenIdList, templateId,url); |
|
|
|
log.info("本次小程序消息推送数据端共【" + analyOpenIdList.size() + "】接收者,发送成功【" + analysisResult + "】条。"); |
|
|
|
} |
|
|
|
if(null != workOpenIdList && workOpenIdList.size() > 0){ |
|
|
|
Integer workResult = sendOpenIdListMessage(workOpenIdList,templateId,url); |
|
|
|
log.info("本次小程序消息推送数据端共【" + workOpenIdList.size() + "】接收者,发送成功【" + workResult + "】条。"); |
|
|
|
} |
|
|
|
for(int j = 0; j < workOpenIdList.size() ; j++){ |
|
|
|
String workOpenId = workOpenIdList.get(j); |
|
|
|
sendMessageByOpenId(workOpenId,url,miniprogram_state); |
|
|
|
}*/ |
|
|
|
|
|
|
|
// 配置模板内容
|
|
|
|
Map<String, TemplateData> m = new HashMap<>(5); |
|
|
|
TemplateData keyword1 = new TemplateData(); |
|
|
|
keyword1.setValue("11111测试11111"); |
|
|
|
m.put("thing1", keyword1); |
|
|
|
TemplateData keyword2 = new TemplateData(); |
|
|
|
keyword2.setValue("2019年10月1日"); |
|
|
|
m.put("time2", keyword2); |
|
|
|
|
|
|
|
String resultMsg = WxSendMessageUtils.sendMessageByOpenId(openId,url,miniprogram_state,template_id,m,lang); |
|
|
|
|
|
|
|
return new Result().ok("消息推送成功!"); |
|
|
|
} |
|
|
|
|
|
|
|
private Integer sendOpenIdListMessage(List<String> openIdList,String templateId,String url){ |
|
|
|
AtomicInteger ai = new AtomicInteger(0); |
|
|
|
List<Future<Integer>> list = new ArrayList<>(); |
|
|
|
for(String openId : openIdList){ |
|
|
|
Future<Integer> future = threadPool.submit(new Callable<Integer>() { |
|
|
|
@Override |
|
|
|
public Integer call() throws Exception { |
|
|
|
// 配置模板内容
|
|
|
|
Map<String, TemplateData> m = new HashMap<>(5); |
|
|
|
TemplateData keyword1 = new TemplateData(); |
|
|
|
keyword1.setValue("11111测试11111"); |
|
|
|
m.put("thing1", keyword1); |
|
|
|
TemplateData keyword2 = new TemplateData(); |
|
|
|
keyword2.setValue("2019年10月1日"); |
|
|
|
m.put("time2", keyword2); |
|
|
|
//发送消息
|
|
|
|
String resultMsg = WxSendMessageUtils.sendMessageByOpenId( |
|
|
|
openId, |
|
|
|
url, |
|
|
|
WxSendMessageUtils.miniprogram_state, |
|
|
|
templateId, |
|
|
|
m, |
|
|
|
WxSendMessageUtils.lang); |
|
|
|
System.out.println("接收者【" + openId + "】,发送结果【" + resultMsg + "】"); |
|
|
|
return 111; |
|
|
|
} |
|
|
|
}); |
|
|
|
list.add(future); |
|
|
|
} |
|
|
|
//循环接收发送结果,相当于一个使线程同步的过程,这个过程是比较耗时的
|
|
|
|
for (int i = 0; i < openIdList.size(); i++) { |
|
|
|
try { |
|
|
|
int resultStatus = list.get(i).get(); |
|
|
|
if (resultStatus == 111) {//发送成功
|
|
|
|
ai.incrementAndGet(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
return ai.get(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|