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>
相关推荐
2501_9159090620 小时前
深度解析 iOS 内存占用,构建多工具协同的内存诊断、监控与优化体系
android·ios·小程序·https·uni-app·iphone·webview
菜鸟学Python20 小时前
零基础用AI编程开发微信小程序-开始篇
微信小程序·小程序·ai编程
q_191328469521 小时前
基于Springboot2+Vue2+uniapp的单商家在线点餐外卖小程序
vue.js·spring boot·mysql·小程序·uni-app·计算机毕业设计
2501_9159184121 小时前
iOS CPU 使用率深度分析,多工具协同定位高占用瓶颈的工程化方法
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063221 小时前
如何防止资源文件被替换?一套针对 iOS App 的多层资源安全方案
android·安全·ios·小程序·uni-app·iphone·webview
烟囱土著1 天前
捣鼓30天,我写了一个数学加减练习小程序
学习·算法·微信小程序·小程序
你真的可爱呀1 天前
uniapp+vue3项目中的常见报错情况以及解决方法
前端·vue.js·uni-app
郑州光合科技余经理1 天前
同城系统海外版:一站式多语种O2O系统源码
java·开发语言·git·mysql·uni-app·go·phpstorm
博客zhu虎康1 天前
uniApp 开发
arcgis·uni-app
qq_12498707531 天前
基于微信小程序的线下点餐系统的设计与实现(源码+论文+部署+安装)
spring boot·微信小程序·小程序·毕业设计