代码如下,再去点订阅提示已经订阅,然而信息依然推送不过去
// pages/wxmes/ordmes.js
import requestb from '../../utils/requs';
var api=require('../../config/api');
var util=require('../../utils/util');
const SUBSCRIBE_ID = 'mIgZEdR6apP3g3ndPsNjB2RKi78vsGxw38oH7hdkC9o' // 模板ID
Page({
/**
* 页面的初始数据
*/
data: {
ifdinyue:'0',
isdinye:true
},
goCollectSet() {
let that = this;
var tmplIds='mIgZEdR6apP3g3ndPsNjB2RKi78vsGxw38oH7hdkC9o' ;
var wxid=wx.getStorageSync("wxid");
if (wx.requestSubscribeMessage) {
wx.requestSubscribeMessage({
tmplIds: [SUBSCRIBE_ID],
success(res) {
// console.log(res)
if (res[SUBSCRIBE_ID] === 'accept') {
// 用户主动点击同意...do something
// console.log(res[SUBSCRIBE_ID]);
// console.log(wxid+"/"+res)
that.upd_userts(wxid,"Y");
that.setData({
isdinye: false
})
wx.showToast({
title: '您订阅成功了,以后每天会提醒您填报',
icon: 'none'
})
} else if (res[SUBSCRIBE_ID] === 'reject') {
// 用户主动点击拒绝...do something
// console.log(res[SUBSCRIBE_ID]);
that.dingyueComfirm(tmplIds);
wx.showToast({
title: '您拒绝订阅了,以后就不推送',
icon: 'none'
})
} else {
wx.showToast({
title: '授权订阅消息有误',
icon: 'none'
})
}
},
fail(res) {
// 20004:用户关闭了主开关,无法进行订阅,引导开启
if (res.errCode == 20004) {
// 显示引导设置弹窗
that.setData({
isShowSetModel: true
})
}else{
// 其他错误信息码,对应文档找出原因
wx.showModal({
title: '提示',
content: res.errMsg,
showCancel: false
})
}
}
});
} else {
wx.showModal({
title: '提示',
content: '请更新您微信版本,来获取订阅消息功能',
showCancel: false
})
}
}
,
dingyue: function (SUBSCRIBE_ID){
let that = this
let tmplId=SUBSCRIBE_ID;
tmplId='mIgZEdR6apP3g3ndPsNjB2RKi78vsGxw38oH7hdkC9o';
var wxid=wx.getStorageSync("wxid");
wx.getSetting({
withSubscriptions: true,
success(res) {
// console.log(res)
if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
// console.log("abdd"+res)
that.goCollectSet();
} else {
that.dingyueComfirm(tmplId)
}
}
})
},
dingyue_old: function (SUBSCRIBE_ID){
let that = this
let tmplId=SUBSCRIBE_ID;
tmplId='mIgZEdR6apP3g3ndPsNjB2RKi78vsGxw38oH7hdkC9o';
var wxid=wx.getStorageSync("wxid");
wx.getSetting({
withSubscriptions: true,
success(res) {
console.log(res)
if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tmplId]) {
let item = res.subscriptionsSetting.itemSettings[tmplId]
if (item == "reject") {
that.dingyueComfirm(tmplId)
} else if (item == "accept") {
that.setData({
ifdinyue:1,
isdinye:false
})
that.upd_userts(wxid,"Y");
wx.showModal({
title: '订阅消息',
content: '您已经开启订阅消息,以后我们定时提醒您填报信息'
})
console.log('提示:您已经开启订阅消息')
} else if (item == "ban") {
console.log('提示:您已经被后台封禁')
wx.showModal({
title: '订阅消息',
content: '提示:您已经被后台封禁'
})
}
}else{
that.dingyueComfirm(tmplId)
}
} else {
that.dingyueComfirm(tmplId)
}
}
})
},
dingyueComfirm: function (tmplId){
var wxid=wx.getStorageSync("wxid");
let that=this;
wx.showModal({
title: '订阅消息',
content: '订阅后,有消息会通过微信通知您',
success: (res) => {
if (res.confirm) {
wx.requestSubscribeMessage({
tmplIds: [tmplId],
success: (res) => {
if (res[tmplId] === 'accept') {
that.upd_userts(wxid,"Y");
wx.showToast({
title: '订阅成功,后续每日定时提醒!',
duration: 1000
// success(data) {
// //成功
// console.error(data);
// }
})
} else if (res[tmplId] == "reject") {
//引导用户,手动引导用户点击按钮,去设置页开启,## Modals是自定义组件
wx.showModal({
title: '订阅消息',
content: '您当前拒绝接受消息通知,是否去开启',
confirmText: '开启授权',
confirmColor: '#345391',
cancelText: '仍然拒绝',
cancelColor: '#999999',
success: res => {
this.globalData.isUpload = true
wx.openSetting({
success(res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
},
fail(err) {
//失败
console.error(err);
}
})
}
})
}
},
fail(err) {
//失败
console.error(err);
}
})
}
}
})
},
upd_userts:function(userid,res){
// console.log("cs:"+userid+"/"+res);
let usercode=userid;
let yorn=res;
requestb({
url: api.dinyue,
data:{userid:usercode,
zt:yorn},
method: 'get',
dataType: 'json',
header: 'application/json'
})
.then(res =>{
console.log( res);
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})