uniapp微信小程序消息订阅详解

时间:2024-03-31 21:53:31
//dom <button type='default' @click="onClick" fontColor="#ffffff">订 阅</button> //methods onClick() { uni.getSetting({ withSubscriptions: true, success(res) { console.log( res, '订阅信息', res.subscriptionsSetting); if (!res.subscriptionsSetting.mainSwitch) { uni.openSetting({ success(res) { console.log('打开设置页', res.authSetting); } }) } else { uni.requestSubscribeMessage({ tmplIds: ['模板id', ], success(res) { console.log('requestSubscribeMessage 订阅信息', res); if (res['模板id'] == "accept") { // 用户点击确定后 console.log('用户订阅点击确定按钮'); // 后端接口 } else { console.log('拒绝,不会再弹出弹框 只能去设置页膝盖'); uni.showModal({ title: '您未开启消息订阅', content: '为了给您提供更好的服务,请您授权消息订阅', success: res2 => { if (res2.confirm) { uni.openSetting({ success(res) { console.log('打开设置页', res.authSetting); } }) } else { console.log('决绝') } } }) } }, fail(errMessage) { console.log("订阅消息 失败 ", errMessage); }, complete() { console.log("成功 失败都执行 ", errMessage); } }) } }, }) }