官方文件: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 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失敗!");
}
}