click(){ //点击事件
this.subscribeMessage({
url:'跳转页面路径地址'
})
}
subscribeMessage(item){
var that = this
var templateid = 'id1' //由微信公众平台创建
let templateid2 = 'id2' //由微信公众平台创建
uni.requestSubscribeMessage({
tmplIds: [templateid,templateid2],
success (res) {
console.log(res)
if(res[templateid]=='accept' || res[templateid2]=='accept'){
that.isClick = false
uni.navigateTo({url:item.url})
}
},
fail:(res) => {
console.log(res,'-----requestSubscribeMessage');
uni.showToast({
title: '需要订阅才能接收通知',
icon: 'none'
});
if(res.errMsg == 'requestSubscribeMessage:fail cancel') {
that.isClick = false
uni.navigateTo({url:item.url})
return
}
that.settingDialog(item)
},
complete(res){
if(res.errCode == 20004) return
console.log(res,'------complete');
if(res[templateid]=='reject' && res[templateid2]=='reject'){
uni.showToast({
title: '需要订阅才能接收通知',
icon: 'none'
});
that.settingDialog(item)
}
}
})
// uni.getSetting({
// withSubscriptions:true, //是否获取用户订阅消息的订阅状态,默认false不返回
// success(res) {
// if(res.authSetting['scope.subscribeMessage']) { //用户点击了“总是保持以上,不再询问”
// uni.openSetting({ // 打开设置页
// success(res) {
// console.log(res.authSetting)
// }
// });
// }else {}
// }
// })
},
settingDialog(item){
let that = this
uni.showModal({
title:'提示',
content: '需要在设置页面打开订阅并勾选通知,去打开?',
cancelText: "取消", // 取消按钮的文字
confirmText: "确定", // 确认按钮文字
showCancel: true, // 是否显示取消按钮,默认为 true
confirmColor: '#343bad',
cancelColor: '#999',
success(res){
if(res.confirm){
uni.openSetting({ // 打开设置页
success(res) {
console.log(res.authSetting)
},fail(err){
console.log(err,'----openSetting');
},complete(res){
that.isClick = false
}
});
}else{
setTimeout(()=>{
that.isClick = false
uni.navigateTo({url:item.url})
},1000)
}
}
})
},