微信小程序如何发送模板消息

  • 效果

这里写图片描述

  • 首先在页面中使用form画出页面
<view class='bottom'>
    <form  bindsubmit="formSubmit" report-submit>
      <!-- <view class='bootom-night' bindtap='auditClick'>审核通过</view> -->
      <button class='bootom-night' form-type='submit'>审核通过</button>
    </form>
    <view class='bootom-weekend' bindtap='notAuditClick'>不通过</view>

  </view>

注意:report-submit ,form-type=’submit’ 不要忘记,否则from的formSubmit事件无法触发

  • 发送模板消息

  formSubmit: function (e) {
    var page=this;
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=*****************&secret==*****************', 
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(e.detail);
        wx.request({
          url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + res.data.access_token,
          method: 'POST',
          data:{
            "touser": page.data.openid,
            "template_id": "WRA1ToxFnuEYcGIVVJEssAtYaZn7I_YaH-UANbqDSV0",
            "page": "/pages/launch/launch",
            "form_id": e.detail.formId,
            "data": {
              "keyword1": {
                "value": "339208499",
                "color": "#173177"
              },
              "keyword2": {
                "value": "2015年01月05日 12:30",
                "color": "#173177"
              },
              "keyword3": {
                "value": "粤海喜来登酒店",
                "color": "#173177"
              },
              "keyword4": {
                "value": "广州市天河区天河路208号",
                "color": "#173177"
              }
            },
            "emphasis_keyword": "keyword1.DATA"
          },

          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res)
          }
        })
      }
    })
  }

这里写图片描述

这里写图片描述

注意:e.detail.formId只有在真机上才会生成,编译器上是没有的!

猜你喜欢

转载自blog.csdn.net/huangxiaoguo1/article/details/79545486