uniapp如何应用onNeedPrivacyAuthorization实现微信小程序隐私政策

前言

微信小程序要求9.15日前实现隐私政策弹窗,但是uniapp文档一直没有更新,尝试直接使用wx.onNeedPrivacyAuthorization ,是可以生效的

步骤

  1. 微信小程序后台设置--服务内容与声明 ,设置好小程序所需要的隐私政策
  2. uniappmanifest.json 中选择 源码视图 添加设置:
json 复制代码
"mp-weixin": {
    // ...
    "__usePrivacyCheck__": true
  },
  1. 微信开发者工具的调试基础库,最好>=3.0
  2. 在你调用隐私接口的页面中,写入一下代码:
js 复制代码
wx.getPrivacySetting({
  success: res => {
    console.log("是否需要授权:", res.needAuthorization, "隐私协议的名称为:", res.privacyContractName)
  },
  fail: () => {},
  complete: () => {},
})

如果看到 是否需要授权:true ,则说明配置生效,可以直接新建你的通用组件,并实现,以下为简单原理示例代码,非完整业务代码,仅供参考:

js 复制代码
<template>
	<view class="privacy-menu">
		<view class="privacy-menu-button" @click="handleDisagree">取消</view>
		<button id="agree-btn" class="privacy-menu-button agree-button" open-type="agreePrivacyAuthorization" 
		            @agreeprivacyauthorization="handleAgree">同意并继续</button>
	</view>
</template>

<script>
let privacyHandler

wx.onNeedPrivacyAuthorization(resolve => {
	console.log("触发:onNeedPrivacyAuthorization")
	if (typeof privacyHandler === 'function') {
		privacyHandler(resolve)
	}
})

export default {
	data() {
		return {
			urlTitle: ''
		}
	},

	mounted() {
		privacyHandler = resolve => {
			if (wx.getPrivacySetting) {
				wx.getPrivacySetting({
					success: res => {
						console.log("是否需要授权:", res.needAuthorization, "隐私协议的名称为:", res.privacyContractName)
						if (res.needAuthorization) {
							this.urlTitle = res.privacyContractName
							// 其他操作,如显示隐私政策弹窗
						} else {
						this.$emit("agree")
						}
					},
					fail: () => {},
					complete: () => {},
				})
			} else {
				// 低版本基础库不支持 wx.getPrivacySetting 接口,隐私接口可以直接调用
				this.$emit("agree")
			}
		}
	}
}
</script>
相关推荐
码云之上1 小时前
小程序 iOS 多输入框焦点乱序脱坑记
ios·微信小程序·taro
2501_915909069 小时前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者82 天前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
Greg_Zhong2 天前
微信小程序 + 腾讯云人体分析:从 0 到 1 实现 AI 抠图打卡合照(细节待更新~)
人工智能·微信小程序·腾讯云·ai抠图-ai打卡拍照
游戏开发爱好者82 天前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
AI_AGENT_DEV_AI2 天前
原生 APP 开发的开发优点
uni-app
JackieDYH2 天前
uniapp-通知滚动组件+轮播图组件封装-案例
uni-app·轮播图·通知
2501_915921433 天前
appuploader-cli 命令行上传 IPA 到 App Store Connect upload CI 集成
android·ci/cd·小程序·https·uni-app·iphone·webview
结网的兔子3 天前
【前端开发】UniApp 项目地图选型与Web-APP跨端迁移方案
前端·uni-app
java1234_小锋4 天前
【免费】基于Python的微信小程序网约车(打车,在线叫车,移动出行)系统(FastAPI+Vue3) 锋哥原创出品,必属精品
微信小程序·小程序·fastapi·网约车系统·在线叫车系统