Node.js给用户推送消息(服务通知)

2020-08-14  本文已影响0人  有一种感动叫做丶只有你懂

1.获取acess_token

   request({
        url:'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET',
        method:'GET'
    },(err,response,body) => {
        if(err) throw err;
        console.log(body)
    })
//body里面就有acess_token和过期时间

2.推送流程

1.必要参数(剩余参数参考微信官网)
{
  "touser":"用户的openId",
  "form_id":"提交表单的返回的form_id",
  "template_id":"微信管理后台配置的模板,每个模板会有id"
}
//openId后端的数据库会有,form_id和template_id需要发送给后端
2.服务端代码
request({
    url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCEESS_TOKEN',
    method: 'POST',
    headers: {
        "content-type": "application/json",
    },
    body: JSON.stringify({
      "touser":"用户的openId",
      "form_id":"提交表单的返回的form_id",
      "template_id":"微信管理后台配置的模板,每个模板会有id"
  })
}, (err, head, body) => {
    console.log(body)
})
//form_id使用1次就会失效,prepay_id使用3次就会失效,并且需要注意的是此处请求的参数类型,必须是String

3.form_id的获取

<form report-submit="{{true}}"bindsubmit="submit" >
  <button form-type="submit">点击提交</button>
</form>
//回调函数submit会返回form_id,只限于真机,编译器返回的是一个模拟的字符串并不是真正的form_id
上一篇 下一篇

猜你喜欢

热点阅读