小程序关联服务号推送模板消息

记录一下小程序关联服务号的消息推送功能。
准备工作:
1、获取小程序的appid与appsecret(小程序后台获取)
2、获取服务号的appid与appsecret(服务号后台获取)
3、小程序与服务号绑定在同一个微信开放平台
注意:小程序、服务号、微信开放平台都要认证!!!都要认证!!!都要认证!!!
接下来进入正题。
第一步:获取服务号的access_token
直接通过官方给的接口获取。这里有两个参数,都可以直接获取到

//接口地址换成自己服务号appid和secret
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret,

第二步:获取已关注服务号的所有用户的openid
还是通过官方提供的网址获取openid,这里需要一个参数access_token,已从上一步获取到了

getServiceMessage: (req, res, next) => {
  const access_token = req.query.access_token;
  let url = `https://api.weixin.qq.com/cgi-bin/user/get?access_token=${access_token}`;
  request.get(url, function (error, response, body) {
    let data = JSON.parse(body);
    if (data != null) {
      res.send(data);
    }
    else {
      res.send(error);
    }
  })
},

第三步:获取已关注用户的用户信息
这一步可获取用户的详细信息,包括昵称、头像、地理位置、unionid等。根据官方网址需要两个参数access_token与openid通过上面两部均已取得。这里后台可以生成一张表用于存储这些数据。

getServiceAll: (req, res, next) => {
  const access_token = req.query.access_token;
  const openid = req.query.openid;
  let url = `https://api.weixin.qq.com/cgi-bin/user/info?access_token=${access_token}&openid=${openid}&lang=zh_CN`;
  request.get(url, function (error, response, body) {
    let data = JSON.parse(body);
    if (data != null) {
      res.send(data);
    }
    else {
      res.send(error);
    }
  })
},

第四步:小程序登录时获取用户的unionid
(1)用户登录小程序时,通过wx.login()获取code
(2)通过微信官方网址获取unionid,需要的参数为code、appid、appsecret

 getUserMessage: (req, res, next) => {
    const code = req.query.code;
    const appid = '';//自己小程序的appid(可以写死)
    const secret = '';//自己小程序的secret(可以写死)
    let url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code +"&grant_type=authorization_code";
    request.get(url, function (error, response, body) {
      let data = JSON.parse(body);
      if (data != null) {
        res.send(data);
      }
      else {
        res.send(error);
      }
    })
  },

第五步:服务号的模板消息推送
(1)当服务号与小程序绑定在同一个开放平台时,他们的unionid是一样的,所以我们可以通过unionid这个中间量来进行数据的查找
(2)通过第四步获取的unionid,我们可以查找到当前登录小程序的用户他的服务号的openid。
(3)需要的两个参数都获取到了,我们就可以推送消息模板啦,接下来按照官方的教程操作就行了。
(4)如果想要一次推送给多人可以把接收人的openid存到一个数组中,然后加个循环就行了。

serviceMessage: (req,res,next) =>{
    let access_token = req.query.access_token;//获取access_token
    const openId = "";//服务号用户的openid
    let url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + access_token;
    let data = {
      "touser": openId,
      "template_id":"wPndNIsGGgonmz4MQRUVykhbuBUioX2RDH1AhLtz30k", //消息模板的id
      // "url":"http://weixin.qq.com/download",  
      "miniprogram":{
        "appid":"wx07d22155db47b80a", //小程序的appid
        // "pagepath":"index?foo=bar" //需跳转到小程序的页面
      },          
      "data":{
              "first": {
                  "value":"您收到新的通知",
                  "color":"#173177"
              },
              "keyword1":{
                  "value":"采购部",
                  "color":"#173177"
              },
              "keyword2": {
                  "value":"A型号玉米",
                  "color":"#173177"
              },
              "remark":{
                  "value":"期待您的参与!",
                  "color":"#173177"
              }
      }
    }
    httprequest(url,data);
    function httprequest(url,data){
      request({
        url: url,
        method: "POST",
        json: true,
        headers:{
          "content-type": "application/json",
        },
        body: data
        },function(error,response,body){
          if (!error && response.statusCode == 200) {
            res.send(resultJson(body));
            //console.log(body) // 请求成功的处理逻辑
          }
      })
    }
  },

总结:用户必须关注服务号,否则不发收到推送消息。

猜你喜欢

转载自blog.csdn.net/zhan_lijian/article/details/88170684