微信小程序---模版消息

微信小程序發(fā)送模版消息:
通過微信提供的以下接口:

https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN

請求方式:

POST

請求參數(shù):

{
  "touser": "openid",
  "template_id": "申請的模版消息id",
  "page": "點擊模版消息時跳轉的路徑",
  "form_id": "提交時獲取的formid(只能使用一次)",
  "data": {
    "keyword1": {
      "value": "已接單",
      "color": "#173177"
    },
    "keyword2": {
      "value": "2017年10月19日 12:31",
      "color": "#173177"
    },
    "keyword3": {
      "value": "張一山",
      "color": "#173177"
    },
    "keyword4": {
      "value": "136xxxx1720",
      "color": "#173177"
    },
    "keyword5": {
      "value": "請手機保持通暢",
      "color": "#173177"
    }
  },
  "emphasis_keyword": "keyword1.DATA"
}

1.第一步首先獲取用戶的openID
通過wx.login獲取用戶登錄code

 wx.login({
      success: function (res) {
        var appid = appid //小程序appid
        var secret = secret //小程序secret
        var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=' + res.code + '&grant_type=authorization_code';
//code用戶登錄憑證(有效期五分鐘)。開發(fā)者需要在開發(fā)者服務器后臺調用 api,使用 code 換取 openid 和 session_key 等信息
        console.log(res.code)
      }
    })  

再通過拼接的url獲取用戶openid(小程序無法訪問該鏈接需要配合服務器完成)

https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=res.code&grant_type=authorization_code

請求方式:

GET

返回如下結果:

{
"session_key":"xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"expires_in":xxxx,
"openid":"xxxxxxxxxxxxxxxxxx"
}

2.發(fā)送模版消息需要微信access_token(7200秒后作廢)
通過下面鏈接獲取

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

請求方式:

GET
{
  "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "expires_in": 7200
}

3.獲取form_id
提交form是觸發(fā)formSubmit

 <form bindsubmit="formSubmit" report-submit="true">
    <button type="default" form-type='submit' bindtap='submit'>確定</button>
 </form>

獲取formid

formSubmit:function(e){
    console.log(e.detail.formId)
}

特別說明:

支付
當用戶在小程序內完成過支付行為,可允許開發(fā)者向用戶在7天內推送有限條數(shù)的模板消息(1次支付可下發(fā)3條,多次支付下發(fā)條數(shù)獨立,互相不影響)

提交表單
當用戶在小程序內發(fā)生過提交表單行為且該表單聲明為要發(fā)模板消息的,開發(fā)者需要向用戶提供服務時,可允許開發(fā)者向用戶在7天內推送有限條數(shù)的模板消息(1次提交表單可下發(fā)1條,多次提交下發(fā)條數(shù)獨立,相互不影響)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容