uniapp 小程序订阅授权 个人中心检测是否勾选总是允许

1,902 阅读1分钟

以前好像授权订阅并不能知道用户是否勾选了总是允许,现在好像微信开放了这个字段

思路:为了小程序的顺利审核,我在登录页获取用户信息后弹出授权按钮,这个时候用户可以授权也可以不授权,但是只要用户没有勾选总是允许,我还是会在个人中心检测是否授权。

login 页面的方法

<button class="btnWxLogin" type="primary" @tap="getTmplID" hover-class="none">授权订阅通知</button>

getTmplID() {
	uni.requestSubscribeMessage({
		tmplIds: ['nBXhQn1unzK26l4ayd3yRRaFSXG6MF62yn_HivkRwgw','x6gWrZsjQPyC4zFlNSLLJOI0tYz2MH9U-Uc0RExAF2I'], //需要授权的消息模板类型
		success(res) {
			Show.successJump('登录成功', 'reLaunch', '../index/index');
		},
		fail(err) {
			Show.show('授权失败');
		}
	});
}

my 个人中心的检测方法

onShow的实现执行getSetting并判断是否登录,因为没登录的话我会触发全局的返回登录页

res.subscriptionsSetting.itemSettings 这个字段如果有值说明用户已经勾选了 总是允许

getSetting() {
	let token = uni.getStorageSync('Authorization');
	if (token) {
		uni.getSetting({
			withSubscriptions: true,
			success: res => {
				if (!res.subscriptionsSetting.itemSettings) {
					this.guideOpenSubscribeMessage();
				}
			}
		});
	}
},

//询问框 如果没勾选总是允许的话
guideOpenSubscribeMessage() {
	uni.showModal({
		title: '注意',
		content: '系统检测到您未开启授权订阅,是否去设置?',
		success: res => {
			if (res.confirm) {
				this.guidSubscribeMessageAuthAfter();
			} else if (res.cancel) {
				Show.show('您拒绝授权,对方将无法对您发送私信');
			}
		}
	});
},

//再次弹出订阅授权
guidSubscribeMessageAuthAfter() {
	uni.requestSubscribeMessage({
		tmplIds: ['nBXhQn1unzK26l4ayd3yRRaFSXG6MF62yn_HivkRwgw', 'x6gWrZsjQPyC4zFlNSLLJOI0tYz2MH9U-Uc0RExAF2I'], //需要授权的消息模板类型
		success(res) {
			uni.getSetting({
				withSubscriptions: true,
				success: res => {
					if (!res.subscriptionsSetting.itemSettings) {
						Show.show('请勾选底部总是保持以上选择,不在询问!');
					}
				}
			});
		},
		fail(err) {
			Show.show(err);
		}
	});
},

以上就是uni-app的授权订阅模式,用于记录方便复制