小程序订阅消息

步骤:
1.在app.js页面登录时候获取code,传给后端去请求openid,返回给前端存到全局变量里;
2.做一个点击“接收消息通知”的按钮,一当用户点击按钮时就将其openid传给后端存起来;
3.服务器定时,比如每天18:00,去查找openid表里面的openid,遍历给这些openid发送消息提醒,接口地址:
https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=access_token

前端:

//1.获取openid

wx.login({
success: res => {
let _this = this
if (res.code) {
var mycode = res.code
wx.request({
url: `url?mycode=${mycode}`,
data: {
mycode: mycode
},
method: "post",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
_this.globalData.openid = res.data.openid
},
fail(){
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})

2.用户点击订阅按钮

dingyue: function () {
var _this = this
var openid = getApp().globalData.openid
wx.requestSubscribeMessage({
tmplIds: ["_v3q7cOdKfoUMZAhijmy_NpnAK75OyOxwRIx1G5fFDA"],
success: function (res) {
console.log("发送成功")
//用户同意了订阅,允许订阅消息
if(res.errMsg = "requestSubscribeMessage:ok"){
wx.showToast({ title: '订阅成功'})
//上传openid到后台
wx.request({
url: 'url',
method: 'POST',
data: {
Openid: openid,
},
header: {
'content-type': 'application/json'
},
success(res) {
console.log(res)
},
fail(e){
console.log(e)
}
})
}else{
wx.showToast({title: '订阅失败' })
}
},
fail(err) {
console.error(err);
console.log("发送失败")
}
})
},

 

后端
//3.接收code请求openid
app.post('/openid/getopenid/wqyjs', (req,res) => {
var mycode = req.query.mycode

var appid = "wx3fc0f0b476b37bee"
var secret= "c8e159ade760b4cc20464361f6ea2007"
var js_code = mycode
var url2 = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${js_code}`
request({
url:url2,
method: 'post',
json: true,
headers: {
"content-type": "application/json",
},
},
function (err,re,body) {
if (!err && re.statusCode == 200) {
res.send(body)
}else{
res.send("")
}
})
})

 

4.定时发送订阅消息
function sendwq(){
//这里可以改成直接查询数据库拿到openid
console.log("定时到,发送通知")
// 请求后台的openid过来
var urlOpenid = "url"
request({
url:urlOpenid,
method: 'get',
},
function (err,res,body) {
if (!err && res.statusCode == 200) {
var body = JSON.parse(body)
var openidArray = body.map( body => { return body.Openid})
// console.log(openidArray.length)
var nowtime = new Date()
var month = Number(nowtime.getMonth()) +1
var day = nowtime.getDate()
var newtime = nowtime.getFullYear() +"年"+month+"月"+ day + "日"

request({
url:'https://api.weixin.qq.com/cgi-bin/token?

grant_type=client_credential&appid=wx3fc0f0b476b37bee&secret=c8e159ade760b4cc20464361f6ea2007',
method: 'get',
},
function (err,res,body) {
var thing1 = "晚上好,有更多精彩图片更新啦!";var date3 = newtime;var name2 = "陈"
if (!err && res.statusCode == 200) {
var body = JSON.parse(body)
var token = body.access_token
// console.log(token)
for(i=0;i<=openidArray.length-1;i++){
// console.log(i)
let requestData ={
"touser": openidArray[i],
"template_id": "_v3q7cOdKfoUMZAhijmy_NpnAK75OyOxwRIx1G5fFDA",
"page": "/pages/index/index",
"miniprogram_state":"developer",
"lang":"zh_CN",
"data": {
"thing1": {
"value": thing1
},
"date3": {
"value": date3
},
"name2": {
"value": name2
},
}
}
var url2 = `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${token}`
request({
url:url2,
method: 'post',
json: true,
headers: {
"content-type": "application/json",
},
body: requestData
},
function (err,res,body) {
if (!err && res.statusCode == 200) {
console.log(body)
}
})
}
}
})
}
})
}

 

posted @ 2021-04-15 16:21  阿杰啊啊  阅读(191)  评论(0)    收藏  举报