|
|
@ -18,57 +18,55 @@ public class WxMessage { |
|
|
|
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> mp_template_msg = buildMsg(template_id, pagepath, data); |
|
|
|
|
|
|
|
Map<String, Object> sendBody = new HashMap<>(); |
|
|
|
sendBody.put("mp_template_msg", mp_template_msg); |
|
|
|
sendBody.put("touser", touser); |
|
|
|
|
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
return forEntity.getBody(); |
|
|
|
} |
|
|
|
|
|
|
|
private static Map<String, Object> buildMsg(String template_id, Map<String, String> data) { |
|
|
|
//拼接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); |
|
|
|
// mp_template_msg.put("url", ""); // 点击模板信息跳转地址
|
|
|
|
|
|
|
|
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(); |
|
|
|
return mp_template_msg; |
|
|
|
} |
|
|
|
public static RespMessReturn sendMessages(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); |
|
|
|
|
|
|
|
private static Map<String, Object> buildMsg(String template_id, String pagepath, Map<String, String> data) { |
|
|
|
//拼接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, Object> mp_template_msg = buildMsg(template_id, data); |
|
|
|
|
|
|
|
// Map<String, String> miniprogram = new HashMap<>();
|
|
|
|
// miniprogram.put("appid", WxConfig.MP_APPID);
|
|
|
|
// miniprogram.put("pagepath", pagepath);
|
|
|
|
// mp_template_msg.put("miniprogram", miniprogram);
|
|
|
|
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); |
|
|
|
return mp_template_msg; |
|
|
|
} |
|
|
|
|
|
|
|
public static RespMessReturn sendMessage(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); |
|
|
|
Map<String, Object> sendBody = new HashMap<>(); |
|
|
|
sendBody.put("mp_template_msg", mp_template_msg); |
|
|
|
sendBody.put("touser", touser); |
|
|
|
|
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
return forEntity.getBody(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 向多个用户推送同一模板消息 |
|
|
|
* |
|
|
@ -80,22 +78,34 @@ public class WxMessage { |
|
|
|
*/ |
|
|
|
public static Map<String, RespMessReturn> sendMessages(String template_id, List<String> tousers, String pagepath, Map<String, String> data) { |
|
|
|
String wxUrl = MP_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.mpAccessToken()); |
|
|
|
//拼接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, Object> mp_template_msg = buildMsg(template_id, pagepath, data); |
|
|
|
|
|
|
|
Map<String, TemplateDataValue> sendData = new HashMap<>(); |
|
|
|
data.forEach((key, val) -> { |
|
|
|
sendData.put(key, new TemplateDataValue(val)); |
|
|
|
}); |
|
|
|
mp_template_msg.put("data", sendData); |
|
|
|
Map<String, RespMessReturn> ret = new HashMap<>(); |
|
|
|
for (String touser : tousers) { |
|
|
|
Map<String, Object> sendBody = new HashMap<>(); |
|
|
|
sendBody.put("touser", touser); |
|
|
|
sendBody.put("mp_template_msg", mp_template_msg); |
|
|
|
ResponseEntity<RespMessReturn> forEntity = new RestTemplate().postForEntity(wxUrl, sendBody, RespMessReturn.class); |
|
|
|
RespMessReturn body = forEntity.getBody(); |
|
|
|
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 = MP_URL_MESSAGE_SEND.replace("ACCESS_TOKEN", WxConfig.mpAccessToken()); |
|
|
|
|
|
|
|
Map<String, Object> mp_template_msg = buildMsg(template_id, data); |
|
|
|
|
|
|
|
Map<String, RespMessReturn> ret = new HashMap<>(); |
|
|
|
for (String touser : tousers) { |
|
|
|