欢迎光临,了解微信小程序开发,就上易用通!

小程序开发心得:千里传音(模板消息)

发布:2018-01-25 10:50浏览: 来源:网络 作者:tianshu

模板信息是一个非常重要的事项,关系到小程序跟微信自身的连通问题,合理的利用模板信息,可以让你的小程序更进一步,所以十分值得研究和学习;这篇文章中,兜兜乐客把自己的实践分享了一下,感谢作者的分享;

今天分享一篇关于消息模板的简易教程。
老规矩先把官方的定义再讲一下,消息模板是基于微信的通知渠道,为我们开发者提供了触发模板消息的能力,以便实现服务的闭环和更好的用户体验。
发起消息模板是有限制条件的,必须用户本人在微信体系与小程序页面有交互行为后才能触发,只有两种情况允许。
第一种是用户在小程序完成支付的行为,可允许开发者向用户在7天内推送有限的模板消息,一次支付可发一条。
第二种是通过提交表单行为且表单需要声明为要发模板消息的,可以允许开发者向用户在7天内推送有限条数的模板消息,一次提交表单可发一条。
好了,废话就不多说了,还是看代码吧。更多官方的定义打开这个地址即可。https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html

表单触发模板消息

小程序开发心得:千里传音(模板消息)(图1)

基于微信的消息提示

小程序开发心得:千里传音(模板消息)(图2)

由于没有正式上线,目前只能收到这样的信息,就表示模板消息发送成功了。

小程序开发心得:千里传音(模板消息)(图3)

我用的是表单触发的模板消息,index.wxml部分很简单:代码如下

  1. <form bindsubmit="formSubmit" report-submit="true">
  2.     <button formType="submit">提交表单发起通知</button>
  3. </form>
复制代码

需要注意的是要在form加上 report-submit="true" 的属性和值。官方要求的,要遵循。
然后再看index.js部分
  1. var app = getApp()
  2. Page({
  3.   data: {
  4. token: "",
  5. openid: "",
  6.     appid: "你的appid",
  7.     secret: "你的secret",
  8.     template_id: "消息模板id,在小程序管理员账号中模板消息中设置获取"     
  9.   },
  10. onLoad: function () {
  11.     console.log('onLoad')
  12.   },
  13.   onLoad: function () {             //登录获取openid并保存
  14.     var that = this
  15.     wx.login({
  16.       success: function (res) {
  17.         if (res.code) {
  18.           wx.request({
  19.             url: 'https://www.zgdnbxg.com/login.php',           //后端处理页面返回openid
  20.             data: {
  21.               appid: that.data.appid,
  22.               secret: that.data.secret,
  23.               js_code: res.code,
  24.               grant_type: "authorization_code"
  25.  
  26.             },
  27.             method: 'GET',
  28.             success: function (res) {
  29.               that.setData({
  30.                 openid: res.data.openid
  31.  
  32.               })
  33.  
  34.             },
  35.             fail: function (res) {
  36.               console.log(res)
  37.             },
  38.             complete: function (res) {
  39.               console.log(res)
  40.             }
  41.           })
  42.         }
  43.       },
  44.       fail: function () {
  45.         // fail
  46.       },
  47.       complete: function () {
  48.         // complete
  49.       }
  50.     })
  51.   },
  52.   onReady: function () {
  53.     //判断toke有没有值,没有就settokentoken 有的话就获取缓存并设置token
  54.     var that = this
  55.     if (that.data.token == "") {
  56.       console.log("token 为空")
  57.       that.setToken()
  58.     } else {
  59.       console.log("token 不为空")
  60.       that.setData({
  61.         token: wx.getStorageSync('token')
  62.       })
  63.     }
  64.   },
  65.   //表单事件 触发模板消息
  66.   formSubmit: function (e) {
  67.     var that = this
  68.  
  69.  
  70.     wx.request({
  71.       url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + wx.getStorageSync('token'),
  72.       data: {
  73.         touser: that.data.openid,
  74.         form_id: e.detail.formId,
  75.         template_id: that.data.template_id,
  76.         value: "",
  77.       },
  78.       method: 'POST',
  79.       success: function (res) {
  80.         console.log(res)
  81.  
  82.       },
  83.       fail: function (res) {
  84.         console.log(res)
  85.  
  86.       },
  87.       complete: function () {
  88.  
  89.       }
  90.     })
  91.  
  92.  
  93.   },
  94.  
  95.   //获取token并保存在本地
  96.   setToken: function () {
  97.     var that = this
  98.     wx.request({
  99.       url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + that.data.appid + '&secret=' + that.data.secret,
  100.       data: {},
  101.       method: 'GET',
  102.       success: function (res) {
  103.         var token = res.data.access_token
  104.         that.setData({
  105.           token: token
  106.         })
  107.         wx.setStorageSync('token', token)
  108.       },
  109.       fail: function (res) {
  110.         console.log("获取token失败")
  111.         console.log(res)
  112.       }
  113.     })
  114.   }
  115. })
复制代码

[size=1em]
 
 



你可以把这段代码全部复制,然后把appid,secret,template_id换成你自己的,并通过小程序管理员配置下request,加上www.zgdnbxg.com,即可真机体验。
 




免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。