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

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

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

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

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

javascript 复制代码
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)

							}
						}
					})
				}

			})
		},

使用:

javascript 复制代码
	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里也可以传入异步函数比如调用了接口都是可以的。

相关推荐
&白帝&2 小时前
uniapp中使用picker-view选择时间
前端·uni-app
fakaifa3 小时前
八戒农场小程序V2最新源码
小程序·uni-app·php·生活·开源软件
平凡シンプル7 小时前
安卓 uniapp跨端开发
android·uni-app
艾小逗9 小时前
uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
小程序·uni-app·app·es6
鸭子嘎鹅子呱1 天前
uniapp使用高德地图设置marker标记点,后续根据接口数据改变某个marker标记点,动态更新
uni-app·map·高德地图
计算机源码社1 天前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
Angus-zoe1 天前
uniapp+vue+微信小程序实现侧边导航
vue.js·微信小程序·uni-app
Pluto & Ethereal1 天前
uni-app尺寸单位、flex布局于背景图片
uni-app
天空下sky2 天前
uniapp+若依 开发租房小程序源码分享
uni-app
帅过二硕ฅ2 天前
uniapp点击跳转到对应位置
前端·javascript·uni-app