🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ const util = require('../../utils/util.js'); const api = require('../../config/api.js'); // components/ad-modal/ad.js Component({ /** * 组件的属性列表 */ properties: { }, /** * 组件的初始数据 */ data: { show:false, tpl_id:"" }, created(){ this.getSub() }, /** * 组件的方法列表 */ methods: { cancle(){ this.setData({ show:false }) }, clickSub(event){ this.getAdd('post') }, getSub(){ let that=this; let token = wx.getStorageSync('token'); wx.login({ success:({code})=>{ util.request(api.Sub, {token:token,code:code}, 'get').then(function (res) { let result = res.result that.setData({ tpl_id:result.tpl_id, show:!result.is_sub }) }); } }) }, tongzhi(){ wx.showLoading() let that=this; let tpl_id = this.data.tpl_id; wx.getSetting({// 这里设置为true,下面才会返回mainSwitch withSubscriptions: true, success: (result) => { // 调起授权界面弹窗 if(result.subscriptionsSetting.mainSwitch){// 用户打开了订阅消息总开关 wx.requestSubscribeMessage({ tmplIds: [tpl_id], success (res) { console.log(res) let token = wx.getStorageSync('token'); if(res[tpl_id]==="accept" || res[tpl_id]==="acceptWithAlert"){ wx.login({ success:({code})=>{ util.request(api.Sub, {token:token,code:code}, 'post').then(function (res) { wx.hideLoading() that.setData({ show:false }) wx.showToast({ icon:"none", title:res.errormsg }) }); } }) }else if(res[tpl_id]==="reject"){ //console.log("拒绝消息推送"); //接受通知开着,但是此模板设置为不接受的时候。但是这个情况下也保存一下 wx.login({ success:({code})=>{ util.request(api.Sub, {token:token,code:code}, 'post').then(function (res) { wx.hideLoading() that.setData({ show:false }) }); } }) // wx.showToast({ // title: 'ئۇچۇر قۇبۇللاشنى ئاچمىدىڭىز', // icon:"none" // }) //that.guideOpenSubscribeMessage(); }else if(res[tpl_id] === 'ban'){ console.log("已被后台封禁"); wx.hideLoading() that.setData({ show:false }) }else{ wx.hideLoading() that.setData({ show:false }) } } }) } else{ wx.hideLoading() that.guideOpenSubscribeMessage(); } }, fail: (res) => {}, complete: (res) => { }, }) }, guideOpenSubscribeMessage() { var that = this; wx.showModal({ title: 'ئەسكەرتىش', content: 'ئۇچۇر قۇبۇللاش ھالىتى ئوچۇق ئەمەسكەن، ئاچامسىز؟', cancelText:"ياق", confirmText:"ھەئە", success: function (sm) { if (sm.confirm) {// 用户点击了确定 可以调用删除方法了 wx.openSetting({ success (res) { if(res.subscriptionsSetting){ that.tongzhi() } } }) } else if (sm.cancel) { that.setData({ show:false }) } } }) }, } }) ~~~