
10 changed files with 414 additions and 1 deletions
@ -0,0 +1,46 @@ |
|||
package com.yxt.supervise.report.wx; |
|||
|
|||
import com.yxt.supervise.report.wx.obj.RespMessReturn; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
|
|||
import java.util.HashMap; |
|||
|
|||
public class SuperviseWxMessSender { |
|||
|
|||
private static String jgsjzl_template_id = "BT6BHEojCmgWNTcpz1raHNIpJEEISP1E134btP51p_8"; |
|||
private static String jgsjzl_pagepath = "pages/index/RegulatoryReporting?orderDate=ORDER_DATE&projectSid=PROJECT_SID"; |
|||
|
|||
/** |
|||
* 监管数据总览 |
|||
* |
|||
* @param sender |
|||
* @param touser |
|||
* @param projectSid |
|||
* @param projectName |
|||
* @param reportTime |
|||
* @return |
|||
*/ |
|||
public static RespMessReturn jgsjzlSend(String sender, String touser, String projectSid, String projectName, String reportTime) { |
|||
String orderDate = reportTime.substring(0, 10); |
|||
String pagepath = jgsjzl_pagepath.replace("ORDER_DATE", orderDate).replace("PROJECT_SID", projectSid); |
|||
HashMap<String, String> data = new HashMap<>(); |
|||
data.put("time1", reportTime); |
|||
String thing2 = "监管数据总览(" + projectName + ")"; |
|||
data.put("thing2", maxLength20(thing2)); |
|||
data.put("thing3", sender); |
|||
RespMessReturn respMessReturn = WxMessage.sendMessage(jgsjzl_template_id, touser, pagepath, data); |
|||
return respMessReturn; |
|||
} |
|||
|
|||
private static String maxLength(String src, int length) { |
|||
if (StringUtils.isBlank(src) || src.length() <= length) |
|||
return src; |
|||
String newStr = src.substring(0, length - 1) + "…"; |
|||
return newStr; |
|||
} |
|||
|
|||
private static String maxLength20(String src) { |
|||
return maxLength(src, 20); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,73 @@ |
|||
package com.yxt.supervise.report.wx; |
|||
|
|||
import cn.hutool.http.HttpUtil; |
|||
import cn.hutool.json.JSON; |
|||
import cn.hutool.json.JSONUtil; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
public class WxConfig { |
|||
|
|||
private static final Logger L = LoggerFactory.getLogger(WxConfig.class); |
|||
|
|||
private static final long token_expires_in = 7200 * 1000 - 10000; |
|||
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 String wx_access_token; |
|||
private static long wx_token_create_timestamp; |
|||
private static String mp_access_token; |
|||
private static long mp_token_create_timestamp; |
|||
|
|||
public static final String WX_APPID = "wxc64c6cf0a06880e5"; |
|||
private static final String WX_APPSECRET = "eb3a0794da05f12a94c92134030246f1"; |
|||
public static final String MP_APPID = "wx05604ce2a8bede05"; |
|||
private static final String MP_APPSECRET = "3d36e8a61212cf773a2fa4e6c9a83334"; |
|||
|
|||
|
|||
/** |
|||
* 使用AppId和AppSecret获取AccessToken(公众号和小程序通用) |
|||
* |
|||
* @param appid |
|||
* @param secret |
|||
* @return |
|||
*/ |
|||
public static String getAccessToken(String appid, String secret) { |
|||
String url = WX_URL_ACCESS_TOKEN.replace("APPID", appid).replace("APPSECRET", secret); |
|||
String res = new RestTemplate().getForObject(url, String.class); |
|||
JSONObject jsonObject = JSONObject.parseObject(res); |
|||
String token = jsonObject.getString("access_token"); |
|||
return token; |
|||
} |
|||
|
|||
/** |
|||
* 获取公众号的AccessToken |
|||
* |
|||
* @return |
|||
*/ |
|||
public static String wxAccessToken() { |
|||
if (StringUtils.isBlank(wx_access_token) || System.currentTimeMillis() > (wx_token_create_timestamp + token_expires_in)) { |
|||
wx_access_token = getAccessToken(WX_APPID, WX_APPSECRET); |
|||
wx_token_create_timestamp = System.currentTimeMillis(); |
|||
} |
|||
return wx_access_token; |
|||
} |
|||
|
|||
/** |
|||
* 获取小程序的AccessToken |
|||
* |
|||
* @return |
|||
*/ |
|||
public static String mpAccessToken() { |
|||
if (StringUtils.isBlank(mp_access_token) || System.currentTimeMillis() > (mp_token_create_timestamp + token_expires_in)) { |
|||
mp_access_token = getAccessToken(MP_APPID, MP_APPSECRET); |
|||
mp_token_create_timestamp = System.currentTimeMillis(); |
|||
} |
|||
return mp_access_token; |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,78 @@ |
|||
package com.yxt.supervise.report.wx; |
|||
|
|||
import com.yxt.supervise.report.wx.obj.RespMessReturn; |
|||
import com.yxt.supervise.report.wx.obj.TemplateDataValue; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
public class WxMessage { |
|||
|
|||
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 MP_URL_MESSAGE_SEND = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN"; |
|||
|
|||
public static RespMessReturn sendMessage(String template_id, String touser, String pagepath, Map<String, String> data) { |
|||
String wxUrl = MP_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.mpAccessToken()); |
|||
|
|||
Map<String, Object> sendBody = new HashMap<>(); |
|||
sendBody.put("touser", touser); |
|||
|
|||
//拼接base参数
|
|||
Map<String, Object> mp_template_msg = new HashMap<>(); |
|||
mp_template_msg.put("appid", WxConfig.WX_APPID); |
|||
mp_template_msg.put("template_id", template_id);// 模板Id
|
|||
mp_template_msg.put("url", ""); // 点击模板信息跳转地址
|
|||
|
|||
Map<String, String> miniprogram = new HashMap<>(); |
|||
miniprogram.put("appid", WxConfig.MP_APPID); |
|||
miniprogram.put("pagepath", pagepath); |
|||
mp_template_msg.put("miniprogram", miniprogram); |
|||
|
|||
Map<String, TemplateDataValue> sendData = new HashMap<>(); |
|||
data.forEach((key, val) -> { |
|||
sendData.put(key, new TemplateDataValue(val)); |
|||
}); |
|||
mp_template_msg.put("data", sendData); |
|||
|
|||
sendBody.put("mp_template_msg", mp_template_msg); |
|||
|
|||
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|||
return forEntity.getBody(); |
|||
} |
|||
|
|||
/** |
|||
* 微信公众号发送消息 |
|||
* |
|||
* @param template_id |
|||
* @param touser |
|||
* @param pagepath |
|||
* @param data |
|||
* @return |
|||
*/ |
|||
public static RespMessReturn messageTemplateSend(String template_id, String touser, String pagepath, Map<String, String> data) { |
|||
String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.wxAccessToken()); |
|||
//拼接base参数
|
|||
Map<String, Object> sendBody = new HashMap<>(); |
|||
sendBody.put("template_id", template_id);// 模板Id
|
|||
sendBody.put("touser", touser); // openId
|
|||
|
|||
Map<String, String> miniprogram = new HashMap<>(); |
|||
miniprogram.put("appid", WxConfig.MP_APPID); |
|||
miniprogram.put("pagepath", pagepath); |
|||
sendBody.put("miniprogram", miniprogram); |
|||
|
|||
Map<String, TemplateDataValue> sendData = new HashMap<>(); |
|||
data.forEach((key, val) -> { |
|||
sendData.put(key, new TemplateDataValue(val)); |
|||
}); |
|||
sendBody.put("data", sendData); |
|||
|
|||
sendBody.put("url", ""); // 点击模板信息跳转地址
|
|||
sendBody.put("topcolor", "#FF0000"); // 顶色
|
|||
|
|||
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|||
return forEntity.getBody(); |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.yxt.supervise.report.wx.obj; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class RespMessReturn { |
|||
// {"errcode":40037,"errmsg":"invalid template_id rid: 64d21740-3e296cf3-501aa457"}
|
|||
private int errcode; |
|||
private String errmsg; |
|||
} |
@ -0,0 +1,32 @@ |
|||
package com.yxt.supervise.report.wx.obj; |
|||
|
|||
public class TemplateDataValue { |
|||
private String value; |
|||
private String color; |
|||
|
|||
public TemplateDataValue(String value) { |
|||
this.value = value; |
|||
this.color = "#173177"; |
|||
} |
|||
|
|||
public TemplateDataValue(String value, String color) { |
|||
this.value = value; |
|||
this.color = color; |
|||
} |
|||
|
|||
public String getValue() { |
|||
return value; |
|||
} |
|||
|
|||
public void setValue(String value) { |
|||
this.value = value; |
|||
} |
|||
|
|||
public String getColor() { |
|||
return color; |
|||
} |
|||
|
|||
public void setColor(String color) { |
|||
this.color = color; |
|||
} |
|||
} |
@ -0,0 +1,101 @@ |
|||
package com.yxt.supervise.report; |
|||
|
|||
import cn.hutool.core.thread.ThreadUtil; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.yxt.supervise.report.api.messageopenid.MessageOpenidDto; |
|||
import com.yxt.supervise.report.api.messagepushlog.MessagePushLogDto; |
|||
import com.yxt.supervise.report.api.wechat.Template; |
|||
import com.yxt.supervise.report.biz.projectdaily.ProjectDaily; |
|||
import org.junit.Test; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
public class WechatTest { |
|||
|
|||
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 WX_URL_MESSAGE_BIZ_SEND_xcx = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?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"; |
|||
|
|||
// @Test
|
|||
public void testWechatSendMessage() throws Exception { |
|||
|
|||
// 模板参数
|
|||
Map<String, Template> sendMag = new HashMap<String, Template>(); |
|||
// 公众号的模板id(也有相应的接口可以查询到)
|
|||
String templateId = "BT6BHEojCmgWNTcpz1raHNIpJEEISP1E134btP51p_8"; |
|||
//微信的基础accessToken
|
|||
// String accessToken = getAccessToken(APP_ID_BIZ, APPSECRETBIZ);
|
|||
String accessToken = getAccessToken("wx05604ce2a8bede05", "3d36e8a61212cf773a2fa4e6c9a83334"); |
|||
// String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", accessToken);
|
|||
String wxUrl = WX_URL_MESSAGE_BIZ_SEND_xcx.replace("ACCESS_TOKEN", accessToken); |
|||
String pa = ""; |
|||
JSONObject jsonObject = new JSONObject(); |
|||
sendMag.put("time1", new Template("2003-03-15")); |
|||
sendMag.put("thing2", new Template("监管数据总览")); |
|||
sendMag.put("thing3", new Template("aaa")); |
|||
Map<String, String> miniprogram = new HashMap<>(); |
|||
miniprogram.put("appid", "wx05604ce2a8bede05"); |
|||
miniprogram.put("pagepath", "pages/index/RegulatoryReporting?orderDate=2023-08-06&projectSid=111111"); |
|||
RestTemplate restTemplate = new RestTemplate(); |
|||
|
|||
|
|||
//拼接base参数
|
|||
Map<String, Object> sendBody = new HashMap<>(); |
|||
sendBody.put("appid", "wxc64c6cf0a06880e5"); |
|||
sendBody.put("miniprogram", miniprogram); |
|||
// sendBody.put("touser", "oGdho68CArVhC_-7ELkG-jX21ruk"); // openId
|
|||
// sendBody.put("touser", "oA-GE6zKB_5wQsVX6USlRQYV39vE"); // openId
|
|||
sendBody.put("url", ""); // 点击模板信息跳转地址
|
|||
sendBody.put("topcolor", "#FF0000"); // 顶色
|
|||
sendBody.put("data", sendMag); // 模板参数
|
|||
sendBody.put("template_id", "templateId");// 模板Id
|
|||
// ResponseEntity<String> forEntity = restTemplate.postForEntity(wxUrl, sendBody, String.class);
|
|||
|
|||
Map<String, Object> sendBodyMM = new HashMap<>(); |
|||
// sendBodyMM.put("touser","oGdho68CArVhC_-7ELkG-jX21ruk");
|
|||
sendBodyMM.put("touser", "oA-GE6zKB_5wQsVX6USlRQYV39vE"); |
|||
sendBodyMM.put("mp_template_msg", sendBody); |
|||
ResponseEntity<String> forEntity = restTemplate.postForEntity(wxUrl, sendBodyMM, String.class); |
|||
String body = forEntity.getBody(); |
|||
|
|||
System.out.println("BBBBBBBB : " + body); |
|||
// {"errcode":0,"errmsg":"ok"}
|
|||
jsonObject = JSONObject.parseObject(body); |
|||
|
|||
// 0
|
|||
String messageCode = jsonObject.getString("errcode"); |
|||
// 2431260672639467520
|
|||
String msgId = jsonObject.getString("msgid"); |
|||
System.out.println("messageCode : " + messageCode + ", msgId: " + msgId); |
|||
} |
|||
|
|||
public String getAccessToken(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); |
|||
System.out.println(jsonObject); |
|||
String at = jsonObject.getString("access_token"); |
|||
return at; |
|||
} |
|||
|
|||
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; |
|||
} |
|||
} |
@ -0,0 +1,21 @@ |
|||
package com.yxt.supervise.report.wx; |
|||
|
|||
import com.yxt.supervise.report.wx.obj.RespMessReturn; |
|||
import org.junit.Test; |
|||
|
|||
import java.util.HashMap; |
|||
|
|||
public class SuperviseWxMessSenderTest { |
|||
|
|||
@Test |
|||
public void testJgsjzlSend() { |
|||
|
|||
String touser = "oGdho68CArVhC_-7ELkG-jX21ruk"; |
|||
|
|||
RespMessReturn respMessReturn = SuperviseWxMessSender.jgsjzlSend("liuliu1", touser, "1111", "测试项目567890123", "2023-03-12 14:22:05"); |
|||
System.out.println(respMessReturn.getErrcode()); |
|||
System.out.println(respMessReturn.getErrmsg()); |
|||
|
|||
|
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
package com.yxt.supervise.report.wx; |
|||
|
|||
import com.yxt.supervise.report.api.wechat.Template; |
|||
import com.yxt.supervise.report.wx.obj.RespMessReturn; |
|||
import org.junit.Test; |
|||
|
|||
import java.util.HashMap; |
|||
|
|||
public class WxMessageTest { |
|||
// @Test
|
|||
public void testMessageTemplateSend() { |
|||
String template_id="BT6BHEojCmgWNTcpz1raHNIpJEEISP1E134btP51p_8"; |
|||
String touser="oGdho68CArVhC_-7ELkG-jX21ruk"; |
|||
String pagepath="pages/index/RegulatoryReporting?orderDate=2023-08-06&projectSid=111111"; |
|||
HashMap<String, String> data = new HashMap<>(); |
|||
data.put("time1", "2003-03-15"); |
|||
data.put("thing2", "监管数据总览"); |
|||
data.put("thing3", "aaa"); |
|||
RespMessReturn respMessReturn = WxMessage.messageTemplateSend(template_id, touser, pagepath, data); |
|||
System.out.println(respMessReturn.getErrcode()); |
|||
System.out.println(respMessReturn.getErrmsg()); |
|||
} |
|||
@Test |
|||
public void testSendMessage() { |
|||
String template_id="BT6BHEojCmgWNTcpz1raHNIpJEEISP1E134btP51p_8"; |
|||
String touser="oGdho68CArVhC_-7ELkG-jX21ruk"; |
|||
String pagepath="pages/index/RegulatoryReporting?orderDate=2023-08-06&projectSid=111111"; |
|||
HashMap<String, String> data = new HashMap<>(); |
|||
data.put("time1", "2003-03-22"); |
|||
data.put("thing2", "监管数据总览XX"); |
|||
data.put("thing3", "aaaBB"); |
|||
RespMessReturn respMessReturn = WxMessage.sendMessage(template_id, touser, pagepath, data); |
|||
System.out.println(respMessReturn.getErrcode()); |
|||
System.out.println(respMessReturn.getErrmsg()); |
|||
} |
|||
} |
@ -0,0 +1,16 @@ |
|||
package com.supervise.rms.biz.wechat; |
|||
|
|||
import cn.hutool.core.util.StrUtil; |
|||
import org.junit.Test; |
|||
|
|||
public class HutoolStrUtilTest { |
|||
|
|||
@Test |
|||
public void testStrUtil(){ |
|||
String a = "abc"; |
|||
String b = "abcdefghijklmnopqrstuvwxyz"; |
|||
String c = "12345678901234567890123"; |
|||
String d = "一二三四五六七八九拾一二三四五六七八九拾一二三四五六七八九拾"; |
|||
System.out.println(StrUtil.maxLength(d,5)); |
|||
} |
|||
} |
Loading…
Reference in new issue