uniapp 微信小程序 授权隐私流程 网上没有的踩坑记录!

首先什么时候我们需要授权操作,比如下图我们调用这些接口时候首先必须让用户授权,这个政策是2022年2月21日24时起对一下接口增加用户授权操作,详情可以看微信文档

授权的逻辑按照官网的意思是: 

这个时候就踩坑了,我把以上逻辑都写好后,发现一直接口走fail,百思不得其解,最后我灵光乍现,之前在处理微信的隐私政策时候调用保存相册功能,必须现在小程序后台管理系统里的用户隐私保护指引里添加上这个隐私类目,最后经过验证,果真是因为这个!!文档里丝毫没提需要有这一步操作。 希望看到这篇文章的兄弟们,避过此坑!

下面贴上我的授权逻辑代码(仅供参考)

getAuthority({ commit, state }, val) { //val:权限名称 和触发函数
			return new Promise(async (resolve, reject) => {
				let authorityMap = {
					'scope.werun': '微信运动',
					'scope.bluetooth': '蓝牙',
				}
				if (state.hasLogin) {
					uni.getSetting({
						async success(res) {
							// console.log(res);
							if (!res.authSetting[val.type]) {
									wx.requirePrivacyAuthorize({
										success: () => {
											// 用户同意授权
											// 继续小程序逻辑
											console.log("1");
											uni.authorize({
												scope: val.type,
												async success(res) {
													let a = await val.method()
													console.log("a", a);
													resolve(a)
												},
												fail() {
													console.log("2");
													uni.showModal({
														content: `为了您良好的用户体验,请允许${authorityMap[val.type]}权限`,
														showCancel: false,
														success() {
															uni.openSetting({
																async success(settingdata) {
																	if (settingdata.authSetting[val.type]) {
																		let a = await val.method()
																		console.log("a", a);
																		resolve(a)
																	} else {
																		console.log('获取权限失败')
																		uni.showToast({
																			title: '获取权限失败',
																			icon: 'error',
																			duration: 2000
																		})
																	}
																}
															})
														}
													})
												}
											})
										},
										fail: () => {}, // 用户拒绝授权
										complete: () => {}
									})

							} else {
								let a = await val.method()
								console.log("a", a);
								resolve(a)

							}
						}
					})
				}

			})
		},

使用:

	let authorityRes = await this.$store.dispatch("getAuthority", {
						type: 'scope.bluetooth',
						method: async () => {
							return new Promise(async (resolve) => {
								this.$jumpTo(`/pages-clockIn/weight/weightRecord/index`)
								resolve(1)
							})
						}
					})
					console.log("authorityRes", authorityRes);

当然method里也可以传入异步函数比如调用了接口都是可以的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柑橘乌云_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值