微信小程序一次性订阅消息
一、微信公众平台配置
在微信公众平台订阅消息模块公共模版库选用需要使用的模版。
详情页面需要记录下详细内容的key值。
data数据格式如下
"data": {
"name01": {
"value": "某某"
},
"amount01": {
"value": "¥100"
},
"thing01": {
"value": "广州至北京"
} ,
"date01": {
"value": "2018-01-01"
}
}
二、前后端交互
1.前端
代码如下(示例):
getMessage(){
wx.requestSubscribeMessage({
tmplIds: ['你的模版id'],
success (res) {
console.log(res);
}
})
},
2.后端
代码如下(示例):
public ResponseEntity<Object> createProject(@Validated @RequestBody BaseProjectsDto resources){
Map<String,Object> map = new LinkedHashMap<>(2);
projectsService.create(resources);
map.put("data","创建成功!");
map.put("status",HttpStatus.OK);
Map<String,Object> wxmap = new HashMap<>();
Map<String,String> dataMap = new HashMap<>();
dataMap.put("value",resources.getName());
Map<String, Object> thing1 = new HashMap<>();
thing1.put("value", dataMap.get("value"));
dataMap.put("value", resources.getType());
Map<String, Object> thing2 = new HashMap<>();
thing2.put("value", dataMap.get("value"));
dataMap.put("value", resources.getPrincipal());
Map<String, Object> thing3 = new HashMap<>();
thing3.put("value", dataMap.get("value"));
wxmap.put("data", new HashMap<String, Map<String, Object>>(){
{
put("thing1", thing1);
put("thing2", thing2);
put("thing4", thing3);
}
});
wxmap.put("template_id","n你的模版id");
wxmap.put("touser","接收人的openid");
wxmap.put("page","跳转的页面");
wxmap.put("miniprogram_state","环境");
String wxdata = JSON.toJSONString(wxmap);
System.out.println(wxdata+"wxdata数据的请求体");
String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret";
String accetoken = httpClientUtil.sendHttpGet(tokenUrl);
String token = JSON.parseObject(accetoken.toString()).getString("access_token");
String result = httpClientUtil.sendHttpPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+token,wxdata);
System.out.print(result+"返回的模板数据");
return new ResponseEntity<>(map,HttpStatus.CREATED);
总结
消息的推送会在微信服务通知接收,点击之后会进入小程序后端接口配置page页面。