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_915909061 天前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview
行走的陀螺仪1 天前
uni-app + Vue3 实现折叠文本(超出省略 + 展开收起)
前端·javascript·css·uni-app·vue3
小禾青青1 天前
uniapp安卓打包遇到报错:Uncaught SyntaxError: Invalid regular expression: /[\p{L}\p{N}]/
android·uni-app
环信即时通讯云1 天前
实现小程序 uniApp 输入框展示自定义表情包
小程序·uni-app
2501_915921431 天前
iOS 抓不到包怎么办?工程化排查与替代抓包方案(抓包/HTTPS/Charles代理/tcpdump)
android·ios·小程序·https·uni-app·iphone·tcpdump
黄交大彭于晏1 天前
UniApp 全局通知功能实现
前端·vue.js·uni-app
abigale031 天前
开发实战 - ego商城 - 7 地址管理模块
前端·uni-app·node.js
笨笨狗吞噬者1 天前
【uniapp】解决小程序分包下的json文件编译后生成到主包的问题
前端·uni-app
2501_915918411 天前
Fiddler抓包工具详解,HTTP/HTTPS抓包、代理设置与调试技巧一站式教程(含实战案例)
http·ios·小程序·https·fiddler·uni-app·webview