商城系統(tǒng) 注冊

微信小程序服務(wù)通知模板的實(shí)現(xiàn)

2020-09-27|HiShop
導(dǎo)讀:微信小程序服務(wù)通知對應(yīng)的技術(shù)實(shí)現(xiàn)是模板消息,是需要做技術(shù)開發(fā)的,對于工程師們來說,看一下官方文檔就能上手。...

微信小程序服務(wù)通知對應(yīng)的技術(shù)實(shí)現(xiàn)是模板消息,是需要做技術(shù)開發(fā)的,對于工程師們來說,看一下官方文檔就能上手。

 

下面直接上代碼:

wxml:

<form name='pushMsgFm' report-submit bindsubmit='form'>
<button form-type="submit">submit</button>
</form>
 

js:

// pages/index/index.js
Page({

data: {

},

/*

微信公眾平臺(tái)測試連接 https://mp.weixin.qq.com/debug/

全局返回碼說明:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433747234

 

*/
onLoad: function (options) {
var that = this
wx.login({
success: function (data) {
console.log(data.code, data)
// 獲取openid
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=你的自己的appid&secret=你自己的session_key&js_code=' + data.code ,
header:  {  "Content-Type":  "application/x-www-form-urlencoded"  },
method: "post",
success: function (res) {
console.log(res, "opind")
that.setData({
openid: res.data.openid,
session_key: res.data.session_key,
})
}
})

}
})

// 獲取access_token
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的自己的appid&secret=你自己的session_key',
method: "GET",
success: function (res) {
console.log(res, "res")
console.log(res.data.access_token, "access_token")
that.setData({
access_token: res.data.access_token,
})
}
})

},

// 點(diǎn)擊執(zhí)行方法
form: function (e) {
var that = this;
var fId = e.detail.formId;
// 網(wǎng)絡(luò)請求
var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token;
// 需要傳的參數(shù)
var d = {
touser: that.data.openid, //用戶的openid
template_id: 'XX1hceIwI1XiQaUc5Z4qIrZnYQkYEHElSq5m6yIa0M8',//這個(gè)是申請的模板消息id,位置在微信公眾平臺(tái)/模板消息中添加并獲取
page: '/pages/index/index', //點(diǎn)擊通知跳轉(zhuǎn)的頁面
form_id: fId, //表單提交場景下,為 submit 事件帶上的 formId
 
//此處必須為data,只有人說value也可以,可能官方已經(jīng)修復(fù)這個(gè)bug
data: {
"keyword1": {
"value": "酒店",
"color": "#4a4a4a"
},
"keyword2": {
"value": "2018-03-22",
"color": "#9b9b9b",
},
"keyword3": {
"value": "$300",
"color": "#9b9b9b"
},
"keyword4": {
"value": "中國",
"color": "#9b9b9b"
},
 
},
color: '#ccc',
emphasis_keyword: 'keyword1.DATA'
}
wx.request({
url: l,
data: d,
method: 'POST', //此處不能有請求頭
success: function (res) {
console.log(res, "push msg");
},
fail: function (err) {
console.log(err, "push err");
}
});
},

})

      最后編譯,點(diǎn)擊提交發(fā)起請求,這里只能手機(jī)調(diào)試,我用開發(fā)工具打印出來的formId: "the formId is a mock one"并不是數(shù)字串。此處需要真機(jī)測試才能成功

HiShop小程序工具提供多類型商城/門店小程序制作,可視化編輯 1秒生成5步上線。通過拖拽、拼接模塊布局小程序商城頁面,所看即所得,只需要美工就能做出精美商城。更多小程序商店請查看:小程序商店

微信小程序服務(wù)通知模板的實(shí)現(xiàn)

電話咨詢 預(yù)約演示 0元開店