|
|
|
@ -2,11 +2,14 @@ package com.elink.esua.epdc.service.impl; |
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; |
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
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.WxAccessTokenBean; |
|
|
|
import com.elink.esua.epdc.dto.WxMssVo; |
|
|
|
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; |
|
|
|
@ -19,12 +22,17 @@ import com.elink.esua.epdc.service.MessageService; |
|
|
|
import com.elink.esua.epdc.utils.WxMaServiceUtils; |
|
|
|
import com.elink.esua.epdc.utils.WxSendMessageUtils; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import com.google.gson.Gson; |
|
|
|
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.http.ResponseEntity; |
|
|
|
import org.springframework.http.client.ClientHttpRequestFactory; |
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
@ -60,6 +68,20 @@ public class MessageServiceImpl implements MessageService { |
|
|
|
@Autowired |
|
|
|
private RedisUtils redisUtils; |
|
|
|
|
|
|
|
private RestTemplate restTemplate; |
|
|
|
private ClientHttpRequestFactory factory; |
|
|
|
@Autowired |
|
|
|
public void setFactory() { |
|
|
|
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); |
|
|
|
factory.setReadTimeout(5000);//ms
|
|
|
|
factory.setConnectTimeout(15000);//ms
|
|
|
|
this.factory = factory; |
|
|
|
} |
|
|
|
@Autowired |
|
|
|
public void setRestTemplate() { |
|
|
|
this.restTemplate = new RestTemplate(this.factory); |
|
|
|
} |
|
|
|
|
|
|
|
@Value("${wx.message.appId}") |
|
|
|
private String appId; |
|
|
|
|
|
|
|
@ -175,61 +197,105 @@ 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";
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// // 获取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("消息推送成功!");
|
|
|
|
// }
|
|
|
|
/*public Result sendMessage() { |
|
|
|
// 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
|
|
|
String miniprogram_state = "trial"; |
|
|
|
// 模板ID
|
|
|
|
String template_id = "wagGg4eAeHu1N2N0o8Vhyan41Gr3DqFBD81o5Lv7XnQ"; |
|
|
|
// 用户openID oZCeb5TZc2oBwT4jHb0kDmQsN0Xw
|
|
|
|
String openId = "oZCeb5TZc2oBwT4jHb0kDmQsN0Xw";//os3585flOvphXsb7XNmYOZtooEro oZCeb5W6GEUCVHG3InnPv1Q6r0f0
|
|
|
|
// 小程序跳转页
|
|
|
|
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(resultMsg); |
|
|
|
}*/ |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param openId 用户openid |
|
|
|
* @param url 接口路径 |
|
|
|
* @param miniprogram_state 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版 |
|
|
|
* @param template_id 模板ID |
|
|
|
* @param m 模板内容: thing 20字以内(汉字数字字母符号) |
|
|
|
* number 32位以内,可带小数 |
|
|
|
* letter 32位以内,只能是字母 |
|
|
|
* time 24制时间格式(支持+年月日)eg:15:03 或 2020年5月28日 |
|
|
|
* 其他可查:https://blog.csdn.net/css_666/article/details/103513916
|
|
|
|
* @param lang 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN |
|
|
|
* @author wanggonfeng |
|
|
|
* @date 2020/05/26 14:18 |
|
|
|
*/ |
|
|
|
public String sendMessageByOpenId(String openId, String url, String miniprogram_state, String template_id, Map<String, TemplateData> m,String lang){ |
|
|
|
//拼接推送的模版
|
|
|
|
WxMssVo wxMssVo = new WxMssVo(); |
|
|
|
wxMssVo.setTouser(openId);//用户openid
|
|
|
|
wxMssVo.setTemplate_id(template_id);//模版id
|
|
|
|
wxMssVo.setMiniprogram_state(miniprogram_state); |
|
|
|
wxMssVo.setData(m); |
|
|
|
wxMssVo.setLang(lang); |
|
|
|
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class); |
|
|
|
log.error("小程序推送结果={}", responseEntity.getBody()); |
|
|
|
return responseEntity.getBody(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取access_token |
|
|
|
* @param appid |
|
|
|
* @param appsecret |
|
|
|
* @return |
|
|
|
* @author wanggonfeng |
|
|
|
* @date 2020/05/26 14:18 |
|
|
|
*/ |
|
|
|
public String getAccess_token(String appid, String appsecret) { |
|
|
|
// 获取access_token
|
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + appid + "&secret=" + appsecret; |
|
|
|
String json = restTemplate.getForObject(url, String.class); |
|
|
|
WxAccessTokenBean accessToken = new Gson().fromJson(json, WxAccessTokenBean.class); |
|
|
|
return accessToken.getAccess_token(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Result sendMessage(SendMessageFormDTO formDto) { |
|
|
|
@ -295,7 +361,15 @@ public class MessageServiceImpl implements MessageService { |
|
|
|
m, |
|
|
|
WxSendMessageUtils.lang); |
|
|
|
System.out.println("接收者【" + openId + "】,发送结果【" + resultMsg + "】"); |
|
|
|
return 111; |
|
|
|
//解析返回信息
|
|
|
|
Map map = JSONObject.parseObject(resultMsg, Map.class); |
|
|
|
// System.out.println("map:"+map);
|
|
|
|
// System.out.println("errcode:"+map.get("errcode"));
|
|
|
|
// System.out.println("errmsg:"+map.get("errmsg"));
|
|
|
|
if (Integer.parseInt(map.get("errcode").toString()) == 0) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
return 1; |
|
|
|
} |
|
|
|
}); |
|
|
|
list.add(future); |
|
|
|
@ -304,7 +378,7 @@ public class MessageServiceImpl implements MessageService { |
|
|
|
for (int i = 0; i < openIdList.size(); i++) { |
|
|
|
try { |
|
|
|
int resultStatus = list.get(i).get(); |
|
|
|
if (resultStatus == 111) {//发送成功
|
|
|
|
if (resultStatus == 0) {//发送成功
|
|
|
|
ai.incrementAndGet(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
|