官方文档

https://developers.weixin.qq.com/miniprogram/dev/api/sendTemplateMessage.html

API:sendTemplateMessage

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

参数

小程序后台发送模板消息

let a = {};
a.touser = obj.touser;
a.template_id = 'xxx';  //在微信后台配置的模板id
a.form_id = obj.form_id;  //用户触发submit提交的formId 7天有效
a.page = 'pages/home/url=/pages/detail!id:'+obj.huodongId;
request({
    url:'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='+access_token,
    method: "POST",//请求方式
    json: true,
    headers: {
        "content-type": "application/json",
    },
    body:a
},function(err,res,body){
	//返回当中body里面有errcode和errmsg
    if(err){
        console.log("serviceSendMsgErr");
        return
    }
	//如果报错40001  是access_token过期,进行重新获取
    if(body.errcode == 40001){
        service.getAccessToken();   //获取access_token 的函数
        setTimeout(()=>{
            service.sendTemplateMsg(obj);
        },1000)
    }

    console.log('res===','body===',body);
})

注意 参数中的page配置应为 'pages/xxx’

获取参数access_token

官方文档

https://developers.weixin.qq.com/miniprogram/dev/api/getAccessToken.html

API getAccessToken

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

实例

request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret',function(err,res){
    if(err){
         console.log(err);
     }else{
         console.log(res.body);
         let accessToken = JSON.parse(res.body).access_token;
     }
 })

注意 access_token获取的结果是字符串需要转换成对象再获取

相关文章:

  • 2021-11-20
  • 2022-12-23
  • 2021-04-04
  • 2021-11-22
  • 2021-12-05
  • 2021-11-20
  • 2021-12-30
猜你喜欢
  • 2022-02-06
  • 2022-12-23
  • 2021-12-15
  • 2021-12-18
  • 2021-05-16
  • 2021-10-07
相关资源
相似解决方案