前言

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

步骤

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

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

<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>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐