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

相关推荐
2501_916008891 天前
手机抓包app大全:无需root的安卓抓包软件列表
android·ios·智能手机·小程序·uni-app·iphone·webview
优倍网络2 天前
宝塔部署websocket服务,后台fastadmin,用户端uniapp
uni-app
墨着染霜华2 天前
UniApp 微信小程序分享
微信小程序·uni-app
2501_915921432 天前
Windows 系统下的 IPA 加密工具实战指南,如何在非 macOS 环境完成 IPA 混淆、加固与工程化处理
android·windows·macos·ios·小程序·uni-app·iphone
iOS阿玮2 天前
最近苹果审核效率提高了,周末竟然都在审核。
uni-app·app·apple
Lsx-codeShare3 天前
一文读懂 Uniapp 小程序登录流程
前端·javascript·小程序·uni-app
00后程序员张3 天前
接口调试从入门到精通,Fiddler抓包工具、代理配置与HTTPS抓包实战技巧
前端·ios·小程序·https·fiddler·uni-app·webview
酸奶弄死你3 天前
uniapp调用后台接口
uni-app·前后端
游戏开发爱好者83 天前
iOS IPA 上传工具全面解析,从 Transporter 到开心上架(Appuploader)命令行的高效上架实践
android·ios·小程序·https·uni-app·iphone·webview
半开半落4 天前
uniapp通过npm使用第三方库兼容微信小程序
微信小程序·npm·uni-app