微信小程序 服务通知研究成果
生活随笔
收集整理的這篇文章主要介紹了
微信小程序 服务通知研究成果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
概念:模板消息。
發送位置:服務通知
發送條件:支付或者觸發表單提交
發送鏈接:
POST https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN參數(七個):
?? access_token,
?? page,
?? template_id,
?? formId,如果是支付就寫prepay_id,如果是 表單就寫formid
?? touser(接受者的openid),
?? data(模板中的數據)數組
?? emphasis_keyword,需要放大的值
實現的效果:如果是表單觸發或者是支付觸發就按照正常的流程來走發送服務通知消息模板。
???????????? 如果我們要主動給用戶發送給用戶,這個要怎么實現?
?? 解決方案:我們把formId和用戶綁定在一起存入數據庫,當用的時候取出來使用即可,那么存儲formId成為了重中之重,如何存儲formId呢?formId只能由前臺觸發表單操作生成,因此要將前臺的formId和用戶id發送到后臺,PHP接受formid和用戶信息綁定存到數據庫。但是這樣的話formId容易存儲無限個,因此要在后臺判斷是否一致(formId有七天的有效期),一旦過去,formId就會失效。
1.觸發表單提交index.wxml:
<form bindsubmit="form" report-submit='true' > <button form-type="submit" class='xx'> <text>123</text> </button> </form>2.index.js
Page({data: {openid: "",session_key: "",access_token: "",},onLoad: function (options) {var that = this//第一步獲取openidwx.login({success: function (data) {console.log(data.code, data)console.log("12345");wx.request({url: 'https://api.weixin.qq.com/sns/jscode2session',data: {appid: "你的appid",secret: "小程序秘鑰",js_code: data.code//wx.login獲取到的code},method: "post",header: {"Content-Type": "application/x-www-form-urlencoded"},success: function (res) {console.log(res);that.setData({openid: res.data.openid,session_key: res.data.session_key,})}})}})//第二步 獲取access_tokenwx.request({url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的小程序秘鑰',method: "GET",success: function (res) {console.log("xxx");console.log(res);that.setData({access_token: res.data.access_token,//獲取到的access_token})}})},form: function (e) {console.log(e);var that = this;var fId = e.detail.formId; // 網絡請求 var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token;var d = {touser: that.data.openid, //用戶的openid template_id: '模板消息ID',page: '/pages/index/index',form_id: fId,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: JSON.stringify(d),method: 'POST',success: function (res) {console.log(res);},fail: function (err) {console.log(err);}});}, })擴展:如何一次獲取50個formId呢?
?
總結
以上是生活随笔為你收集整理的微信小程序 服务通知研究成果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一年学习编程的感想
- 下一篇: 坚果pro android版本,好用得不