|
|
@ -15,7 +15,7 @@ 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) { |
|
|
|
public static RespMessReturn sendMessageMiniProgram(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> mp_template_msg = buildMsg(template_id, pagepath, data); |
|
|
@ -55,7 +55,7 @@ public class WxMessage { |
|
|
|
return mp_template_msg; |
|
|
|
} |
|
|
|
|
|
|
|
public static RespMessReturn sendMessage(String template_id, String touser, Map<String, String> data) { |
|
|
|
public static RespMessReturn sendMessageMiniProgram(String template_id, String touser, Map<String, String> data) { |
|
|
|
String wxUrl = MP_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.mpAccessToken()); |
|
|
|
|
|
|
|
Map<String, Object> mp_template_msg = buildMsg(template_id, data); |
|
|
@ -76,7 +76,7 @@ public class WxMessage { |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static Map<String, RespMessReturn> sendMessages(String template_id, List<String> tousers, String pagepath, Map<String, String> data) { |
|
|
|
public static Map<String, RespMessReturn> sendMessagesMiniProgram(String template_id, List<String> tousers, String pagepath, Map<String, String> data) { |
|
|
|
String wxUrl = MP_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.mpAccessToken()); |
|
|
|
|
|
|
|
Map<String, Object> mp_template_msg = buildMsg(template_id, pagepath, data); |
|
|
@ -102,7 +102,7 @@ public class WxMessage { |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static Map<String, RespMessReturn> sendMessages(String template_id, List<String> tousers, Map<String, String> data) { |
|
|
|
public static Map<String, RespMessReturn> sendMessagesMiniProgram(String template_id, List<String> tousers, Map<String, String> data) { |
|
|
|
String wxUrl = MP_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.mpAccessToken()); |
|
|
|
|
|
|
|
Map<String, Object> mp_template_msg = buildMsg(template_id, data); |
|
|
@ -129,28 +129,113 @@ public class WxMessage { |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static RespMessReturn messageTemplateSend(String template_id, String touser, String pagepath, Map<String, String> data) { |
|
|
|
public static RespMessReturn sendMessage(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
|
|
|
|
Map<String, Object> sendBody = buildMessageBody(template_id, pagepath, data); |
|
|
|
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); |
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
RespMessReturn body = forEntity.getBody(); |
|
|
|
body.setSendData(sendBody); |
|
|
|
return body; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信公众号发送消息,不打开小程序 |
|
|
|
* |
|
|
|
* @param template_id |
|
|
|
* @param touser |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static RespMessReturn sendMessage(String template_id, String touser, Map<String, String> data) { |
|
|
|
String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.wxAccessToken()); |
|
|
|
|
|
|
|
Map<String, Object> sendBody = buildMessageBody(template_id, data); |
|
|
|
sendBody.put("touser", touser); // openId
|
|
|
|
|
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
RespMessReturn body = forEntity.getBody(); |
|
|
|
body.setSendData(sendBody); |
|
|
|
return body; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信公众号发送消息,向多个用户发送 |
|
|
|
* |
|
|
|
* @param template_id |
|
|
|
* @param tousers |
|
|
|
* @param pagepath |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static Map<String, RespMessReturn> sendMessages(String template_id, List<String> tousers, String pagepath, Map<String, String> data) { |
|
|
|
String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.wxAccessToken()); |
|
|
|
//拼接base参数
|
|
|
|
Map<String, Object> sendBody = buildMessageBody(template_id, pagepath, data); |
|
|
|
|
|
|
|
Map<String, RespMessReturn> ret = new HashMap<>(); |
|
|
|
for (String touser : tousers) { |
|
|
|
sendBody.put("touser", touser); |
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
RespMessReturn body = forEntity.getBody(); |
|
|
|
body.setSendData(sendBody); |
|
|
|
ret.put(touser, body); |
|
|
|
} |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信公众号发送消息,不打开小程序,向多个用户发送 |
|
|
|
* |
|
|
|
* @param template_id |
|
|
|
* @param tousers |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static Map<String, RespMessReturn> sendMessages(String template_id, List<String> tousers, Map<String, String> data) { |
|
|
|
String wxUrl = WX_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.wxAccessToken()); |
|
|
|
|
|
|
|
Map<String, Object> sendBody = buildMessageBody(template_id, data); |
|
|
|
|
|
|
|
Map<String, RespMessReturn> ret = new HashMap<>(); |
|
|
|
for (String touser : tousers) { |
|
|
|
sendBody.put("touser", touser); |
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
RespMessReturn body = forEntity.getBody(); |
|
|
|
body.setSendData(sendBody); |
|
|
|
ret.put(touser, body); |
|
|
|
} |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Map<String, Object> buildMessageBody(String template_id, Map<String, String> data) { |
|
|
|
//拼接base参数
|
|
|
|
Map<String, Object> mp_template_msg = new HashMap<>(); |
|
|
|
mp_template_msg.put("template_id", template_id);// 模板Id
|
|
|
|
Map<String, TemplateDataValue> sendData = new HashMap<>(); |
|
|
|
data.forEach((key, val) -> { |
|
|
|
sendData.put(key, new TemplateDataValue(val)); |
|
|
|
}); |
|
|
|
sendBody.put("data", sendData); |
|
|
|
mp_template_msg.put("data", sendData); |
|
|
|
mp_template_msg.put("url", ""); // 点击模板信息跳转地址
|
|
|
|
mp_template_msg.put("topcolor", "#FF0000"); // 顶色
|
|
|
|
return mp_template_msg; |
|
|
|
} |
|
|
|
|
|
|
|
sendBody.put("url", ""); // 点击模板信息跳转地址
|
|
|
|
sendBody.put("topcolor", "#FF0000"); // 顶色
|
|
|
|
private static Map<String, Object> buildMessageBody(String template_id, String pagepath, Map<String, String> data) { |
|
|
|
//拼接base参数
|
|
|
|
Map<String, Object> mp_template_msg = buildMessageBody(template_id, data); |
|
|
|
|
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
return forEntity.getBody(); |
|
|
|
Map<String, String> miniprogram = new HashMap<>(); |
|
|
|
miniprogram.put("appid", WxConfig.MP_APPID); |
|
|
|
miniprogram.put("pagepath", pagepath); |
|
|
|
mp_template_msg.put("miniprogram", miniprogram); |
|
|
|
|
|
|
|
return mp_template_msg; |
|
|
|
} |
|
|
|
} |
|
|
|