前言
这只是模拟一下,消息模板功能的实现,前台只需要搜集fromid传给后台就可以,
一个fromid可以给用户发送一个模板消息,有效期为7天,
一.添加消息模板
二.模板配置详情
三.以上都配置好就可以开始写代码了
appid:小程序后台获取
secret:小程序后台获取
_token:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
tmpID:小程序后台模板消息获取,
openID:有appid,有secret,openid的获取我就不多说了
<form bind:submit="subMsg" report-submit="true">
<button formType="submit">发送模板消息</button>
</form>
const app = getApp()
Page({
data: {
appid:"xxxxxx", //小程序appid
secret:'xxxxxx',//小程序秘钥
_token:"", // 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + that.data.appid + '&secret=' + that.data.secret,
tmpID:'xxxxxx', //复制模板id
openID:'', //openid
},
subMsg(e){
var that = this
var params = { //发送给weixin接口的数据
access_token: that.data._token, //必填
touser: that.data.openID, //必填
template_id: that.data.tmpID, //必填
form_id: e.detail.formId, //开发者工具会报错没有formid 真机联调就不报错了
page: "pages/index/index", //点击消息模板进去的页面路径
data: {
"keyword1": { "value": "0123456", "color": "#000" },
"keyword2": { "value": "99", "color": "#000" },
"keyword3": { "value": "2018-11-15", "color": "#000" },
"keyword4": { "value": "西瓜", "color": "#000" },
"keyword5": { "value": "微信支付", "color": "#000" },
}
}
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data._token,
header: {
'content-type': 'application/json'
},
data: params,
method: "POST",
success: function (res) {
console.log(res,'推送成功')
},
fail: function (err) {
console.log(err,'失败');
}
})
}
})
开发者工具发送模板消息会报错,不要担心,真机联调就可以拿到from id
喜欢上方小程序,需要源码的,私信小编留下邮箱。