微信小程序前端后台配置
详细内容就是后端要发送的data,前端应该把对应的数据传过去,thing2.DATA,thing2就是后端发送对应的类是属性名
前端订阅
wx.requestSubscribeMessage({
tmplIds: [你的订阅模板id],
success (res:any) {
}
})
前端调用接口
wx.request({
url:url+"xcxAdmin/call_repairman",
method:"POST",
data:JSON.stringify({repairman:tmp.repairman,openid:tmp.openid,thing2:"2",phone_number4:"2",thing7:"2"}),
header:{
'content-type': 'application/json',
'Authorization':wx.getStorageSync('token'),
},
success(res:any)
{
console.log(res.data)
},
fail(err:any)
{
console.log(err);
}
});
nodejs后端
app.post('/xcxAdmin/call_repairman',(req:any,res:any)=>{
console.log(req.body)
call_repairman(req.body,res)
})
async call_repairman(data:any,res:any)
{
console.log("callall"+data.openid)
let url='https://api.weixin.qq.com/cgi-bin/token?'+'grant_type=client_credential&appid='+appid+'&secret='+appsecret
let response=await axios.get(url)
let access_token=response.data.access_token
let urls = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+access_token
let message={
touser:data.openid,
template_id:你的模板id,
data:{
//填你自己的模板内容,value:然后填前端发过来的消息,thing2是属性名就是你在微信小程序后台设置的
thing2:{value:data.thing2},
phone_number4:{value:data.phone_number4},
thing7:{value:data.thing7}
}
}
response=axios.post(urls,message)
res.send({success:true,reason:"",condition:"call_repairman:"+data.openid})
}
评论已关闭