1.用户点击某按钮时才能调用,方法:
var _this = this;
wx.requestSubscribeMessage({
tmplIds: ['IfU3eOcH_ubviSy5EqYJkGpN_i1-w_JsUHCyZTTybS0'],
success (res) {
console.log(res);
_this.message();
}
})
需要推送消息的时候 调服务端接口
message() {
var that = this;
//订阅消息模板id
var template_id ="IfU3eOcH_ubviSy5EqYJkGpN_i1-w_JsUHCyZTTybS0";
//发送access_token请求
console.log(app.globalData.userInfo.openId);
var test = {
access_token: '50_iVSlpAnvMouQeEs14oAo-rL2vrBTFWblzuhXGrAvP8NMvyiC2BHzql3NU-LwB1KnbiDtH616M3ymenO4bMd9bH2IFhc2pPdRgvxS1OVzrx9fd4-QRlN9llhbGGpRjXAozB1ho_oQMaVJm0HLQKNfAHAJDG',
data:{
"touser": app.globalData.userInfo.openId,
"template_id": template_id,
"page": "pages/index/index",
"miniprogram_state": "trial",
"lang": "zh_CN",
"data": {
thing2: {value:"11111"},
thing5: {value: "订单已送达"},
character_string6: {value: "SF4420210302"},
}
}
}
wx.request({
url: 'http://192.168.210.10:3000/list',
method: 'post',
data: test,
success: function(res) {
console.log("订阅成功");
console.log(res);
},
fail: function(res) {
console.log("订阅失败");
},
})
},
服务端接口中 调用微信提供的api
我用nodejs写的
app.post('/list', (req, res) => {
console.log('---',req);
console.log("%s", "guoyansi", req.body.access_token);
request({
url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='+req.body.access_token,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: req.body.data
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
res.json({
code: 200,
message: '成功',
data: {
list: body
}
});
}
});
})
网友评论