微信公众号开发-模板推送封装参数
不得不说微信的开发文档的恶心之处
{{first.DATA}}
审核状态:{{keyword1.DATA}}
操作时间:{{keyword2.DATA}}
{{remark.DATA}}
{
"touser":"${touser}",
"data":${data},
"template_id":"${template_id}",
"miniprogram":{"pagepath":"${pagepath}","appid":"${appid}"}
}
public List<String> wxTemplateConversion(WxTemplateVo wxTemplateVo, WxPushMsg wxPushMsg){
List<String> wxPushPostMsg = new ArrayList<>();
if(wxPushMsg != null && ValidateHelper.isNotEmptyCollection(wxPushMsg.getOpenidList())){
for(String openid:wxPushMsg.getOpenidList()){
//替换模板参数
String postStr = wxTemplateVo.getContent();
//模板ID
postStr = postStr.replace("${template_id}", wxTemplateVo.getTemplateId());
//微信小程序appid
postStr = postStr.replace("${appid}", wxPushMsg.getAppid());
//微信公众号用户openid
postStr = postStr.replace("${touser}", openid);
//微信小程序内部地址
postStr = postStr.replace("${pagepath}", wxPushMsg.getPagepath());
//推送参数
postStr = postStr.replace("${data}", JSONObject.toJSONString(wxPushMsg.getData()));
wxPushPostMsg.add(postStr);
}
}
return wxPushPostMsg;
}
- 主要的是data的数据封装,使用可变参进行封装,尽量避免繁琐的first,remark的写法
public static TreeMap<String,WxTemplateData> buildWxTemplateMsgData(WxTemplateData first, WxTemplateData remark, WxTemplateData...templateList){
TreeMap<String, WxTemplateData> dataTreeMap = new TreeMap<>();
dataTreeMap.put("first", first);
for(int index = 0;index < templateList.length ;index++){
String key = "keyword" + (index+ 1);
dataTreeMap.put(key, templateList[index]);
}
dataTreeMap.put("remark", remark);
return dataTreeMap;
}
TreeMap<String, WxTemplateData> dataTreeMap = WxPushUtil.buildWxTemplateMsgData(new WxTemplateData("感谢您进行实名认证!", ""),
new WxTemplateData("**********************!", "#FF0000"),
new WxTemplateData("认证成功", ""),
new WxTemplateData(TimeUtil.getNowTime(), "")
);