小程序发送订阅消息
官方文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
参数说明:
touser : 接收者openid
template_id : 模板ID
url : 模板跳转链接(海外帐号没有跳转能力)
page : 跳转小程序的路径地址
appid : 所需跳转到的小程序appid(该小程序 appid 必须与发模板消息的公众号是绑定关联关系,暂不支持小游戏)
pagepath: 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar),要求该小程序已发布,暂不支持小游戏
miniprogram_state : 跳转小程序类型:developer为开发版,trial为体验版,fomal正式版,默认为正式版
data : 模板数据
color : 模板内容字体颜色,不填默认为黑色
业务实现
public static final String SEND_INFO_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
public static void pushTemplateMsg(TemplateMsgDto templateMsg) {
RestTemplate restTemplate = new RestTemplate();
//获取token
String url = SEND_INFO_URL + getAccessToken();
//拼接推送的模版
WxMssVo wxMssVo = new WxMssVo();
wxMssVo.setTouser(templateMsg.getOpenId());
wxMssVo.setTemplate_id(templateMsg.getTemplateId());
try {
wxMssVo.setPage(templateConfigStatic.getCommonUrl() + URLEncoder.encode(templateMsg.getPage(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (DataUtil.isNotEmpty(templateMsg.getReason())) {
templateMsg.setReason(CommonUtils.truncateWithEllipsis(templateMsg.getReason()));
}
Map<String, TemplateData> m = new HashMap<>(5);
if (templateMsg.getTemplateType() == 1){
// 实名认证模板消息
m.put("thing1", new TemplateData(templateMsg.getEntName()));
m.put("phrase2", new TemplateData(templateMsg.getAuditResult()));
m.put("time3", new TemplateData(DateUtils.formatYyyyMMddHmsToStr(templateMsg.getAuditTime())));
m.put("thing4", new TemplateData(templateMsg.getReason()));
}else if (templateMsg.getTemplateType() == 2){
// 申诉审核结果模板消息
m.put("thing2", new TemplateData(CommonUtils.truncateWithEllipsis(templateMsg.getAppealContent())));
m.put("phrase3", new TemplateData(templateMsg.getAuditResult()));
m.put("name4", new TemplateData(templateMsg.getAuditPersonName()));
}else if (templateMsg.getTemplateType() == 3){
// 投诉(意见箱) 审核结果模板消息
m.put("thing6", new TemplateData(CommonUtils.truncateWithEllipsis(templateMsg.getComplainContent())));
m.put("time5", new TemplateData(DateUtils.formatYyyyMMddHmsToStr(templateMsg.getComplainTime())));
m.put("phrase2", new TemplateData(templateMsg.getAuditResult()));
m.put("thing3", new TemplateData(CommonUtils.truncateWithEllipsis(templateMsg.getReason())));
m.put("date1", new TemplateData(DateUtils.formatYyyyMMddHmsToStr(templateMsg.getAuditTime())));
}
wxMssVo.setData(m);
wxMssVo.setMiniprogram_state(templateConfigStatic.getMiniprogramState());
wxMssVo.setLang("zh_CN");
log.info("推送模版消息入参2222: {}", wxMssVo);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class);
log.info("推送模版返回结果: {}", responseEntity.getBody());
//Map map = JSONObject.parseObject(responseEntity.getBody(), Map.class);
}
这里多个类型消息区分开
获取AccessToken
/**
* 获取wx token
* @return
*/
public static String getAccessToken() {
ResponseEntity<String> responseEntity = restTemplateStatic.getForEntity(WeChatConstant.ACCESS_TOKEN_URL, String.class, weChatConfigStatic.getAppId(), weChatConfigStatic.getSecretKey());
log.info("获取wxToken结果集 {}", responseEntity);
Map map = JSONObject.parseObject(responseEntity.getBody(), Map.class);
if (!StringUtils.isEmpty(map.get("errcode"))) {
throw new DefaultException("获取token参数有误");
}
if (!StringUtils.isEmpty(map.get("access_token"))) {
return map.get("access_token").toString();
} else {
throw new DefaultException("获取token失败!");
}
}
WxMssVo
@Data public class WxMssVo { /** * 用户openid */ private String touser; /** * 订阅消息模版id */ private String template_id; /** * 默认跳到小程序首页 */ private String page; /** * 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版 */ private String miniprogram_state; /** * 语言类型 */ private String lang; /** * 推送内容 */ private Map<String, TemplateData> data; }