vue项目实战项目总结

vue实战(16)——公众号发送订阅消息、模版消息

2020-05-25  本文已影响0人  wayne1125

1、应用场景

基于公众号开发的web页面通常会涉及到微信支付、卡券、授权登录、模版消息、订阅消息等功能,这些功能虽然都是非常成熟的内容,但由于调试起来不方便,问题排查麻烦原因很容易入坑,本节主要围绕订阅消息和模版消息应用场景以及使用方法

2、一次性订阅消息

2.1、一次性订阅消息授权
2.2、一次性订阅消息发送
{
    "touser":"ola5v0pJ_d2OzIEzADFjNdOeko",
    "template_id":"L6t3Q5jnTr1YG7xLY54P2jD_n8OEjr_VUMoyk",
    "miniprogram":{
        "appid":"wx39867ed535e89",
        "pagepath":"index"    
    },
    "scene":"1000",
    "title":"商品发货通知",
    "data":{
        "content":{
            "value":"消息正文内容消息正文内容消息正文内容消息正文内容消息正文内容消息正文内容\n说明文字:描述内容",
            "color":"#f60"
        }
    }
}
// 成功返回值
{
  "errcode": 0,
  "errmsg": "ok"
}
{
  "errcode": 42001,
  "errmsg": "access_token expired hints: [FImcf8VhE-WRDEza!]"
}
一次性订阅消息

3、模版消息

请求接口:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=33_wYNe1eOoT3QyqI_oh4Q7MKP0ff_3dz9qU2NsC_36iHpITgJwuYMnnxRUNt9Edo_P-zNfd8QbgyOarE_ZY9LLSOc_CiHdYnzUa1H_E_sTZoZc6806WU4HoRDFRJRUJYAfAFATIK
body请求体、成功返回值

{
           "touser":"ola5v0pJ_dzADDFSNdOeko",
           "template_id":"rdeBdYf9F1h9sj_3s78JJFdseUmaQIev1XcpV94",
           "url":"http://weixin.qq.com/download",          
           "data":{
                   "first": {
                       "value":"恭喜你购买成功!",
                       "color":"#173177"
                   },
                   "keyword1":{
                       "value":"巧克力",
                       "color":"#173177"
                   },
                   "keyword2": {
                       "value":"39.8元",
                       "color":"#173177"
                   },
                   "keyword3": {
                       "value":"2014年9月22日",
                       "color":"#173177"
                   },
                   "remark":{
                       "value":"欢迎再次购买!",
                       "color":"#173177"
                   }
           }
       }
模版消息

4、实现代码

subscribeAuth() {
  let encodeHost = encodeURIComponent('https://www.tinl.com/active/#/materialResult'); 
  let appId = "wx1d0efh8e6718b";
  alert(encodeHost)
  let templeteId = 'L6t3Q89reTr1Y87MpeP2jDK3_n8OEjr_VUMoyk'
  window.location.href = `https://mp.weixin.qq.com/mp/subscribemsg?action=get_confirm&appid=${appId}&scene=1000&template_id=${templeteId}&redirect_url=${encodeHost}&reserved=test#wechat_redirect`
}
reportInfo () {
  const appKey = 'd7879fsdJFDF06bf87d817ec61aca6'
  const screat = 'FNKJFKFJC9D9F467313C321FE9B60'
  const openId = this.materialOpenid
  const appid = 'wx1d0ef78e6718b'
  let times = getMinutes()
  let data = {
    formId: "-1",
    openId: openId,
    projectCode: appid,
    userId: global.materialUserId && Number(global.materialUserId),
  }
  let signStr = `${screat}appKey${appKey}body${JSON.stringify(data)}timestamp${times}${screat}`
  let sign = md5(signStr)
  let response = await axios.post( `api/consumer-sms-feign/miniProgramManager/addUserFormId?appKey=${appKey}&timestamp=${times}&sign=${sign}`,data,{headers:{'x-access-token':global.materialToken}})
  console.log(response)
  if(response.data.code === '1'){
    if(response.data.data === 0){
      alert(`data=>>${response.data.data}订阅上报成功`)
    }
  }
},
上一篇下一篇

猜你喜欢

热点阅读