微信小程序(uniapp)隐私授权弹窗_微信小程序授权弹窗
微信小程序(uniapp)隐私授权弹窗实现指南
在微信小程序开发中,隐私授权是保障用户权益的重要环节。随着用户对隐私保护的重视,微信官方提供了相关接口,如 uni.getPrivacySetting
和 uni.openPrivacyContract
,帮助开发者规范隐私授权流程。本文将详细介绍如何在 uniapp
中实现隐私授权弹窗,并提供完整的实现思路和代码示例。
一、隐私授权的背景与重要性
-
隐私授权的背景
根据相关法律法规和微信平台要求,小程序在收集、使用用户个人信息前,必须明确告知用户并获得其授权。 -
隐私授权的重要性
- 保障用户知情权,提升用户信任度。
- 避免因违规收集用户信息导致的法律风险。
- 提升小程序通过审核的概率,确保合规运营。
二、uniapp 隐私授权相关接口
-
uni.getPrivacySetting
该接口用于获取用户的隐私设置,包括是否同意隐私协议。 -
uni.openPrivacyContract
该接口用于打开隐私协议详情页面,用户可查看具体的隐私条款。
三、实现隐私授权弹窗的完整步骤
1. 检查用户隐私授权状态
在小程序启动时或需要收集用户信息前,调用 uni.getPrivacySetting
检查用户是否已同意隐私协议。
2. 弹出隐私授权提示
如果用户未同意隐私协议,弹出自定义弹窗,提示用户阅读并同意隐私协议。
3. 引导用户查看隐私协议
在弹窗中提供按钮,用户点击后可调用 uni.openPrivacyContract
打开隐私协议详情页面。
4. 用户同意后继续操作
用户同意隐私协议后,记录授权状态,并继续后续操作。
四、详细实现代码
1. 检查隐私授权状态
在App.vue globalData
中定义 privacyContractName
和 showPrivacy
两个变量。使用uni.getPrivacySetting
检查是否需要隐私协议授权。
注意:uni.getPrivacySetting 方法 小程序基础版本库必须高于或等于3.0.0,不然方法报错。
// App.vueglobalData: {privacyContractName: \'\', //隐私协议的名字showPrivacy: false //控制隐私弹窗显隐},onLaunch(option) {// 隐私授权弹窗uni.getPrivacySetting({success: res => {console.log(\'是否需要授权:\', res.needAuthorization, \'隐私协议的名称为:\', res.privacyContractName);if (res.needAuthorization) {this.globalData.privacyContractName = res.privacyContractName;this.globalData.showPrivacy = true;} else {this.globalData.showPrivacy = false;}}});}
2. 封装隐私授权弹窗组件
创建一个名为 PrivacyPopup.vue 的组件文件。添加以下代码:
<template><view class=\"privacy\" v-if=\"showPrivacy\"><view class=\"content\"><view class=\"title\">隐私保护指引</view><view class=\"des\">在使用当前小程序服务之前,请仔细阅读<text class=\"link\" @click=\"openPrivacyContract\">{{ privacyContractName }}</text>。如果你同意{{ privacyContractName }},请点击“同意”开始使用。</view><view class=\"btns\"><button class=\"item reject\" @click=\"exitMiniProgram\">拒绝</button><button id=\"agree-btn\" class=\"item agree\" open-type=\"agreePrivacyAuthorization\"@agreeprivacyauthorization=\"handleAgreePrivacyAuthorization\">同意</button></view></view></view></template>
export default {name: \'privacyPopup\',data() {return {privacyContractName: \'\',showPrivacy: false};},created() {setTimeout(() => {this.showPrivacy = getApp().globalData.showPrivacy;this.privacyContractName = getApp().globalData.privacyContractName;}, 500);},methods: {// 同意隐私协议handleAgreePrivacyAuthorization() {const that = this;uni.requirePrivacyAuthorize({success: res => {that.showPrivacy = false;getApp().globalData.showPrivacy = false;}});},// 拒绝隐私协议exitMiniProgram() {const that = this;uni.showModal({content: \'如果拒绝,我们将无法获取您的信息, 包括手机号、位置信息等该小程序十分重要的功能,您确定要拒绝吗?\',success: res => {if (res.confirm) {that.showPrivacy = false;uni.exitMiniProgram({success: () => {console.log(\'退出小程序成功\');}});}}});},// 跳转协议页面 // 在真机上点击高亮的协议名字会自动跳转页面 微信封装好的不用操作openPrivacyContract() {uni.openPrivacyContract({fail: () => {uni.showToast({title: \'网络错误\',icon: \'error\'});}});}}};
.privacy {position: fixed;top: 0;right: 0;bottom: 0;left: 0;background: rgba(0, 0, 0, 0.5);z-index: 9999999;display: flex;align-items: center;justify-content: center;.content {width: 85vw;padding: 50rpx;box-sizing: border-box;background: #fff;border-radius: 16rpx;.title {text-align: center;color: #333;font-weight: bold;font-size: 34rpx;}.des {font-size: 26rpx;color: #666;margin-top: 40rpx;text-align: justify;line-height: 1.6;.link {color: #07c160;text-decoration: underline;}}.btns {margin-top: 60rpx;display: flex;justify-content: space-between;.item {justify-content: space-between;width: 244rpx;height: 80rpx;display: flex;align-items: center;justify-content: center;box-sizing: border-box;border: none;}.reject {background: #f4f4f5;color: #909399;}.agree {background: #07c160;color: #fff;}}}}
完整代码:
<template><view class=\"privacy\" v-if=\"showPrivacy\"><view class=\"content\"><view class=\"title\">隐私保护指引</view><view class=\"des\">在使用当前小程序服务之前,请仔细阅读<text class=\"link\" @click=\"openPrivacyContract\">{{ privacyContractName }}</text>。如果你同意{{ privacyContractName }},请点击“同意”开始使用。</view><view class=\"btns\"><button class=\"item reject\" @click=\"exitMiniProgram\">拒绝</button><button id=\"agree-btn\" class=\"item agree\" open-type=\"agreePrivacyAuthorization\"@agreeprivacyauthorization=\"handleAgreePrivacyAuthorization\">同意</button></view></view></view></template><script>export default {name: \'privacyPopup\',data() {return {privacyContractName: \'\',showPrivacy: false};},created() {setTimeout(() => {this.showPrivacy = getApp().globalData.showPrivacy;this.privacyContractName = getApp().globalData.privacyContractName;}, 500);},methods: {// 同意隐私协议handleAgreePrivacyAuthorization() {const that = this;uni.requirePrivacyAuthorize({success: res => {that.showPrivacy = false;getApp().globalData.showPrivacy = false;}});},// 拒绝隐私协议exitMiniProgram() {const that = this;uni.showModal({content: \'如果拒绝,我们将无法获取您的信息, 包括手机号、位置信息等该小程序十分重要的功能,您确定要拒绝吗?\',success: res => {if (res.confirm) {that.showPrivacy = false;uni.exitMiniProgram({success: () => {console.log(\'退出小程序成功\');}});}}});},// 跳转协议页面 // 在真机上点击高亮的协议名字会自动跳转页面 微信封装好的不用操作openPrivacyContract() {uni.openPrivacyContract({fail: () => {uni.showToast({title: \'网络错误\',icon: \'error\'});}});}}};</script><style lang=\"scss\" scoped>.privacy {position: fixed;top: 0;right: 0;bottom: 0;left: 0;background: rgba(0, 0, 0, 0.5);z-index: 9999999;display: flex;align-items: center;justify-content: center;.content {width: 85vw;padding: 50rpx;box-sizing: border-box;background: #fff;border-radius: 16rpx;.title {text-align: center;color: #333;font-weight: bold;font-size: 34rpx;}.des {font-size: 26rpx;color: #666;margin-top: 40rpx;text-align: justify;line-height: 1.6;.link {color: #07c160;text-decoration: underline;}}.btns {margin-top: 60rpx;display: flex;justify-content: space-between;.item {justify-content: space-between;width: 244rpx;height: 80rpx;display: flex;align-items: center;justify-content: center;box-sizing: border-box;border: none;}.reject {background: #f4f4f5;color: #909399;}.agree {background: #07c160;color: #fff;}}}}</style>
五、代码解析
uni.getPrivacySetting
该接口用于获取用户的隐私设置。如果res.privacyContractName
为空,表示用户未同意隐私协议。- 自定义弹窗
使用v-if
控制弹窗的显示,提供“查看隐私协议”、“同意”和“不同意”按钮。 uni.openPrivacyContract
用户点击“查看隐私协议”按钮后,调用该接口打开隐私协议详情页面。- 用户同意或不同意
根据用户的选择,记录授权状态并提示相应信息。
六、注意事项
- 隐私协议的合规性
确保隐私协议内容符合相关法律法规要求,并在微信后台进行备案。 - 用户体验
隐私授权弹窗应简洁明了,避免过度打扰用户。 - 接口调用频率
避免频繁调用 uni.getPrivacySetting,建议在必要时(如首次启动、收集用户信息前)调用。 - 错误处理
在调用接口时,做好错误处理,确保用户体验。 - 小程序基础版本库
版本库必须高于或等于3.0.0,否则uni.getPrivacySetting
会报错。
七、总结
通过 uniapp
提供的 uni.getPrivacySetting
和 uni.openPrivacyContract
接口,开发者可以轻松实现隐私授权弹窗功能,确保小程序合规运营。