|
|
@ -43,8 +43,11 @@ import java.util.Map; |
|
|
|
public class WechatRest { |
|
|
|
private static final String WX_URL_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; |
|
|
|
private static final String WX_URL_MESSAGE_SEND = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN"; |
|
|
|
private static final String WX_URL_MESSAGE_BIZ_SEND ="https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token=ACCESS_TOKEN"; |
|
|
|
private static final String APP_ID="wx1d44e0fcd110351a"; |
|
|
|
private static final String APP_ID_BIZ="wxc64c6cf0a06880e5"; |
|
|
|
private static final String APPSECRET="4764e32722a7d98656dea6afd9405701"; |
|
|
|
private static final String APPSECRETBIZ="eb3a0794da05f12a94c92134030246f1"; |
|
|
|
@Autowired |
|
|
|
WechatService wechatService; |
|
|
|
@Autowired |
|
|
@ -97,16 +100,17 @@ public class WechatRest { |
|
|
|
String accessToken=getAccessToken(APP_ID,APPSECRET); |
|
|
|
String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", accessToken); |
|
|
|
List<String> openIds=new ArrayList<>(); |
|
|
|
openIds.add("otnrM57kui-WCUmRtChhb1HguOgw"); |
|
|
|
// openIds.add("otnrM521f8KcCsRfL2Jj4Tk8np1g");
|
|
|
|
openIds.add("oGdho668Hew45t0xfBJC84TNGVzI"); |
|
|
|
// openIds.add("otnrM544hxIgkMOfaDU8VpJhj_6k");
|
|
|
|
// openIds.add("otnrM5ySvUo7ZZc4qafWzM9eyKBk");
|
|
|
|
// openIds.add("otnrM521f8KcCsRfL2Jj4Tk8np1g");
|
|
|
|
JSONObject jsonObject=new JSONObject(); |
|
|
|
for(String openId:openIds){ |
|
|
|
sendMag.put("thing3", new Template(csmCashReportVo.getCompanyName())); |
|
|
|
sendMag.put("time4", new Template(csmCashReportVo.getReportDate())); |
|
|
|
Map<String, String> miniprogram = new HashMap<>(); |
|
|
|
miniprogram.put("appid","wx11565021714ba796"); |
|
|
|
miniprogram.put("pagepath","pages/index/auditReport?companyName="+csmCashReportVo.getCompanyName()+"&dataDate="+csmCashReportVo.getDataDate()); |
|
|
|
//miniprogram.put("appid","wx11565021714ba796");
|
|
|
|
//miniprogram.put("pagepath","pages/index/auditReport?companyName="+csmCashReportVo.getCompanyName()+"&dataDate="+csmCashReportVo.getDataDate());
|
|
|
|
RestTemplate restTemplate = new RestTemplate(); |
|
|
|
//拼接base参数
|
|
|
|
Map<String, Object> sendBody = new HashMap<>(); |
|
|
@ -185,6 +189,71 @@ public class WechatRest { |
|
|
|
String at=jsonObject.getString("access_token"); |
|
|
|
return at; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信订阅消息 |
|
|
|
* @param orderDate |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
@PostMapping("/sendMessages") |
|
|
|
public void sendMessages(String orderDate) throws Exception { |
|
|
|
CsmCashReportVo csmCashReportVo=csmCashReportMapper.getCsmCashReport(orderDate); |
|
|
|
// openId代表一个唯一微信用户,即微信消息的接收人
|
|
|
|
// String openId="otnrM57kui-WCUmRtChhb1HguOgw";
|
|
|
|
//郭
|
|
|
|
// String openId="otnrM544hxIgkMOfaDU8VpJhj_6k";
|
|
|
|
//刘
|
|
|
|
//String openId ="otnrM5ySvUo7ZZc4qafWzM9eyKBk";
|
|
|
|
//聂总
|
|
|
|
// otnrM521f8KcCsRfL2Jj4Tk8np1g
|
|
|
|
// 模板参数
|
|
|
|
Map<String, Template> sendMag = new HashMap<String, Template>(); |
|
|
|
// 公众号的模板id(也有相应的接口可以查询到)
|
|
|
|
String templateId = "jvfjnNpuT_4rfhiV1aWW31qK31-CnICoIAp5Bgco8hw"; |
|
|
|
//微信的基础accessToken
|
|
|
|
String accessToken=getAccessTokenBiz(APP_ID_BIZ,APPSECRETBIZ); |
|
|
|
String wxUrl = WX_URL_MESSAGE_BIZ_SEND.replace("ACCESS_TOKEN", accessToken); |
|
|
|
List<String> openIds=new ArrayList<>(); |
|
|
|
openIds.add("oGdho668Hew45t0xfBJC84TNGVzI"); |
|
|
|
// openIds.add("otnrM521f8KcCsRfL2Jj4Tk8np1g");
|
|
|
|
// openIds.add("otnrM5ySvUo7ZZc4qafWzM9eyKBk");
|
|
|
|
JSONObject jsonObject=new JSONObject(); |
|
|
|
for(String openId:openIds){ |
|
|
|
sendMag.put("thing1", new Template("测试")); |
|
|
|
sendMag.put("date3", new Template("2023-07-14")); |
|
|
|
sendMag.put("thing2", new Template("测试")); |
|
|
|
Map<String, String> miniprogram = new HashMap<>(); |
|
|
|
miniprogram.put("appid",""); |
|
|
|
miniprogram.put("pagepath",""); |
|
|
|
RestTemplate restTemplate = new RestTemplate(); |
|
|
|
//拼接base参数
|
|
|
|
Map<String, Object> sendBody = new HashMap<>(); |
|
|
|
sendBody.put("miniprogram",miniprogram); |
|
|
|
sendBody.put("touser", openId); // openId
|
|
|
|
sendBody.put("page", ""); // 点击模板信息跳转地址
|
|
|
|
//sendBody.put("topcolor", "#FF0000"); // 顶色
|
|
|
|
sendBody.put("data", sendMag); // 模板参数
|
|
|
|
sendBody.put("template_id", templateId);// 模板Id
|
|
|
|
ResponseEntity<String> forEntity = restTemplate.postForEntity(wxUrl, sendBody, String.class); |
|
|
|
jsonObject = JSONObject.parseObject(forEntity.getBody()); |
|
|
|
System.out.println(sendBody); |
|
|
|
} |
|
|
|
System.out.println(sendMag); |
|
|
|
// 0
|
|
|
|
String messageCode = jsonObject.getString("errcode"); |
|
|
|
// 2431260672639467520
|
|
|
|
String msgId = jsonObject.getString("msgid"); |
|
|
|
System.out.println("messageCode : " + messageCode + ", msgId: " +msgId); |
|
|
|
//return forEntity.getBody();
|
|
|
|
} |
|
|
|
public String getAccessTokenBiz(String appId,String appsecret) throws Exception { |
|
|
|
String url = WX_URL_ACCESS_TOKEN.replace("APPID", appId).replace("APPSECRET", appsecret); |
|
|
|
RestTemplate restTemplate=new RestTemplate(); |
|
|
|
String re= restTemplate.getForObject(url,String.class); |
|
|
|
JSONObject jsonObject= JSONObject.parseObject(re); |
|
|
|
String at=jsonObject.getString("access_token"); |
|
|
|
return at; |
|
|
|
} |
|
|
|
/** |
|
|
|
* * 发送模板消息 |
|
|
|
* pagepath 用户点击时需要跳转的小程序页面 |
|
|
|