• uniapp微信小程序用户隐私保护


    使用wx.requirePrivacyAuthorize实现微信小程序用户隐私保护。

    一、前言

    微信小程序官方公告《关于小程序隐私保护指引设置的公告》。不处理的话,会导致很多授权无法使用,比如头像昵称、获取手机号、位置、访问相册、上传图片视频、访问剪切板内容等等,具体详见《小程序用户隐私保护指引内容介绍》 。

    二、隐私相关设置

    1、在 微信公众平台【设置】- 【服务内容与声明】 ,设置用户隐私保护指引,添加项目需要的接口权限。

    ps:【用户隐私保护指引】提交之后,官方会进行审核。审核通过之后,对应的接口权限才会生效。

    比如:上传头像报错如下。

    chooseAvatar:fail api scope is not declared in the privacy agreement。

    2、打开uniapp 项目的 manifest.json ,选择【源码视图】, 添加配置如下配置

    1. "mp-weixin": {
    2. "__usePrivacyCheck__": true, //隐私政策
    3. },

    3、设置微信开发者工具的调试基础库,最低2.33.0

     

    三、解决方案

    1)验证用户是否已经隐私授权

    使用wx.requirePrivacyAuthorize() 接口,验证用户之前已经同意过隐私授权

    1. onReady() {
    2. var _this = this;
    3. // 隐私政策
    4. wx.getPrivacySetting({
    5. success: res => {
    6. // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
    7. console.log(res)
    8. if (res.needAuthorization) {
    9. // 需要弹出隐私协议
    10. _this.$refs.privacy.privacyShow = true;
    11. return;
    12. } else {
    13. // 用户已经同意过隐私协议,所以不需要再弹出隐私协议,也能调用隐私接口
    14. }
    15. },
    16. fail: () => {},
    17. complete:() => {}
    18. })
    19. },

     如果needAuthorization返回值为true,则需要用户进行隐私授权。

    2)index引入组件

    1. <template>
    2. <view>
    3. <!-- 用户隐私保护指引弹窗租金 -->
    4. <UserPrivacy ref="privacy"></UserPrivacy>
    5. </view>
    6. </template>
    7. <script>
    8. import UserPrivacy from "@/components/user/userPrivacy.vue";
    9. export default {
    10. components: {
    11. UserPrivacy
    12. },
    13. data() {
    14. return {
    15. // 隐私设置弹窗开关
    16. privacyShow: false,
    17. }
    18. },
    19. onReady() {
    20. var _this = this;
    21. // #ifdef MP-WEIXIN
    22. // 隐私政策
    23. wx.getPrivacySetting({
    24. success: res => {
    25. // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
    26. console.log(res)
    27. if (res.needAuthorization) {
    28. // 显示用户隐私组件弹窗
    29. _this.$refs.privacy.privacyShow = true;
    30. return;
    31. } else {
    32. // 用户已经同意过隐私协议,所以不需要再弹出隐私协议,也能调用隐私接口
    33. // 调用授权位置接口
    34. _this.getLocation();
    35. }
    36. },
    37. fail: () => {},
    38. complete:() => {}
    39. })
    40. // #endif,
    41. methods: {
    42. // 获取当前位置
    43. getLocation() {
    44. let _this = this;
    45. var mapkey = uni.getStorageSync('webConfig').web_config_str.mapkey;
    46. uni.getFuzzyLocation({
    47. type: 'gcj02', //国测局坐标gcj02
    48. geocode: true, //是否解析地址信息,仅App平台支持
    49. isHighAccuracy: true, //开启高精度定位
    50. success(res) {
    51. console.log('==获取当前位置的经纬度-成功==');
    52. console.log(res);
    53. _this.longitude = res.longitude;
    54. _this.latitude = res.latitude;
    55. // 设置经纬度缓存
    56. uni.setStorageSync('longitude', res.longitude);
    57. uni.setStorageSync('latitude', res.latitude);
    58. // 引入腾讯地图SDK核心类
    59. var QQMapWX = require('@/util/qqmap-wx-jssdk.min.js');
    60. var qqmapsdk = new QQMapWX({
    61. key: mapkey,
    62. });
    63. // 根据经纬度获取所在位置
    64. qqmapsdk.reverseGeocoder({
    65. location: {
    66. longitude: res.longitude,
    67. latitude: res.latitude,
    68. },
    69. success: function(res) {
    70. console.log("==根据经纬度获取所在位置==");
    71. console.log(res);
    72. _this.city = res.result.ad_info.city;
    73. // 设置城市缓存
    74. uni.setStorageSync('province', res.result.ad_info.province);
    75. uni.setStorageSync('city', res.result.ad_info.city);
    76. uni.setStorageSync('district', res.result.ad_info.district);
    77. uni.setStorageSync('address', res.result.address);
    78. }
    79. });
    80. },
    81. fail(err) {
    82. console.log('获取当前位置的经纬度-失败');
    83. // 设置默认城市、经纬度
    84. }
    85. });
    86. },
    87. }
    88. }
    89. </script>

    3)  弹窗组件代码

    1. <template>
    2. <view>
    3. <u-popup v-model="privacyShow" mode="center" width="600rpx" border-radius="20" :mask-close-able="false">
    4. <view class="privacyBox">
    5. <view class="privacyTit">用户隐私保护提示view>
    6. <view class="privacyDesc">
    7. 感谢您的使用,在使用本小程序前,应当阅读并同意<text
    8. @click="openClick">《用户隐私保护指引》text>。当您点击同意并开始使用程序服务时,即表示您已理解并同意该条款内容,该条款将对您产生法律约束力。如您拒绝,将无法进入小程序。
    9. view>
    10. <view class="privacyPost">
    11. <view class="refuseBtn">
    12. <navigator target="miniProgram" open-type="exit">不同意并退出navigator>
    13. view>
    14. <button class="agreeBtn" open-type="agreePrivacyAuthorization"
    15. @agreeprivacyauthorization="agreeClick">同意并继续button>
    16. view>
    17. view>
    18. u-popup>
    19. view>
    20. template>
    21. <script>
    22. export default {
    23. data() {
    24. return {
    25. // 隐私设置弹窗开关
    26. privacyShow: false,
    27. }
    28. },
    29. onReady() {
    30. },
    31. methods: {
    32. // 打开隐私协议
    33. openClick() {
    34. wx.openPrivacyContract({
    35. success: () => {}, // 打开成功
    36. fail: () => {}, // 打开失败
    37. complete: () => {}
    38. })
    39. },
    40. // 同意
    41. agreeClick() {
    42. // 用户点击了同意,之后所有已声明过的隐私接口和组件都可以调用了
    43. this.privacyShow = false;
    44. // 重新授权定位,调取父组件方法
    45. this.$parent.getLocation();
    46. },
    47. }
    48. }
    49. script>
    50. <style scoped lang="scss">
    51. .privacyBox {
    52. width: 600rpx;
    53. padding: 60rpx;
    54. box-sizing: border-box;
    55. }
    56. .privacyTit {
    57. font-size: 32rpx;
    58. font-weight: bold;
    59. color: $uni-text-main;
    60. text-align: center;
    61. overflow: hidden;
    62. }
    63. .privacyDesc {
    64. font-size: 28rpx;
    65. color: $uni-text-sub;
    66. overflow: hidden;
    67. margin-top: 30rpx;
    68. }
    69. .privacyDesc text {
    70. color: $uni-primary;
    71. }
    72. .privacyPost {
    73. overflow: hidden;
    74. margin-top: 60rpx;
    75. display: flex;
    76. justify-content: center;
    77. align-items: center;
    78. }
    79. .privacyPost .refuseBtn {
    80. flex: 1;
    81. height: 80rpx;
    82. line-height: 80rpx;
    83. text-align: center;
    84. font-size: 28rpx;
    85. font-weight: bold;
    86. color: #fff;
    87. background: $uni-info-dark;
    88. border-radius: 40rpx;
    89. box-sizing: border-box;
    90. overflow: hidden;
    91. }
    92. .privacyPost .agreeBtn {
    93. flex: 1;
    94. height: 80rpx;
    95. line-height: 80rpx;
    96. text-align: center;
    97. font-size: 28rpx;
    98. font-weight: bold;
    99. color: #fff;
    100. background: $uni-primary;
    101. border-radius: 40rpx;
    102. box-sizing: border-box;
    103. overflow: hidden;
    104. margin-left: 20rpx;
    105. }
    106. style>

     ps:弹窗组件框架,demo用的uView1版本。底层遮罩样式,可自行用view代替。

    4)弹窗效果图

    参考示例:悦玩悦乐自助棋牌室、快洗坊自助洗车。

  • 相关阅读:
    ASAN入门参考
    JS力扣第九题-回文数
    PostCSS概述与指南:轻松应对浏览器兼容性与CSS优化!
    win10制作dos启动U盘
    [AutoSAR系列] 1.2 AutoSar 综述
    MySQL 主从复制、读写分离
    接口测试基本知识点
    通俗易懂的JAVA BIO NIO AIO 原理白话文解释,区别,优缺点及代码使用案例
    MySQL索引、使用场景、失效场景、回表、索引覆盖
    宁波效实中学国际课程与交流中心IB数学课程介绍
  • 原文地址:https://blog.csdn.net/qq_40047019/article/details/132607521