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_915921433 小时前
iOS 是开源的吗?苹果系统的封闭与开放边界全解析(含开发与开心上架(Appuploader)实战)
android·ios·小程序·uni-app·开源·iphone·webview
2501_915909067 小时前
原生 iOS 开发全流程实战,Swift 技术栈、工程结构、自动化上传与上架发布指南
android·ios·小程序·uni-app·自动化·iphone·swift
2501_915106327 小时前
Comodo HTTPS 在工程中的部署与排查实战(证书链、兼容性与真机抓包策略)
网络协议·http·ios·小程序·https·uni-app·iphone
2501_915909067 小时前
苹果软件混淆与 iOS 代码加固趋势,IPA 加密、应用防反编译与无源码保护的工程化演进
android·ios·小程序·https·uni-app·iphone·webview
2501_916007477 小时前
苹果软件混淆与 iOS 应用加固实录,从被逆向到 IPA 文件防反编译与无源码混淆解决方案
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088913 小时前
iOS 26 性能分析深度指南 包含帧率、渲染、资源瓶颈与 KeyMob 协助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
iOS阿玮15 小时前
喜欢做马甲包的有福了~现在多了一招续费方式!
uni-app·app·apple
_AaronWong18 小时前
一键搞定UniApp WiFi连接!这个Vue 3 Hook让你少走弯路
前端·微信小程序·uni-app
2501_915909061 天前
tcpdump 抓包数据分析实战,命令、过滤、常见故障定位与真机补充流程
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump