• wx.getPrivacySetting 小程序隐私保护指引的使用(复制粘贴即用)


    创建privacyPopup 组件

    privacyPopup.js

    Component({
        properties: {},
        data: {
            wxPrivacyName: '',
            showAgreement: false
        },
        lifetimes: {
        	attached() {
        		this.init();
        	}
        },
        methods: {
            async init() {
                if (isLogin()) {
                    const userPrivacy = await this.getPrivacy();
                    this.setData({
                        wxPrivacyName: userPrivacy.needAuthorization,
                        showAgreement: userPrivacy.needAuthorization
                    })
                }
    
            },
            async getPrivacy() {
                return new Promise(resolve => {
                    try {
                        wx.getPrivacySetting({
                            success: res => {
                                resolve(res)
                            },
                            fail: err => {
                            // 获取是否授权错误时,直接默认为不需要授权
                                resolve({needAuthorization: false, privacyContractName: ""})
                            }
                        })
                    } catch(err) {
                    	// 兼容2.32.2版本以下不支持wx.getPrivacySetting方法
                    	// 不支持的版本,默认为不需要授权
                        resolve({needAuthorization: false, privacyContractName: ""})
                    }
                })
            },
            handleOpenPrivacyContract() {
                // 打开隐私协议页面
                wx.openPrivacyContract()
            },
            reject() {
                let that = this
                wx.showModal({
                    title: "提示",
                    content: "拒绝后,可能会导致部分功能无法正常使用,你确定要拒绝吗?",
                    confirmText: "确定",
                    cancelText: "取消",
                    success: res => {
                        if (res.confirm) {
                            that.setData({showAgreement: false})
                        }
                    }
                })
            },
            handleAgreePrivacyAuthorization() {
                this.setData({showAgreement: false})
            },
        }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    privacyPopup.json

    {
      "component": true,
      "usingComponents": {
        "van-popup": "@vant/weapp/popup/index"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    privacyPopup.wxml

    	
        
            XXX小程序隐私保护授权
            
                
                    {{wxPrivacyName}}
                
            
            
                拒绝
                
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    privacyPopup.wxss

    
    .wrap_agreement {
      width: 600rpx;
      min-height: 300rpx;
      padding: 32rpx;
      background: #fff;
      display: flex;
      flex-direction: column;
    }
    .wrap_agreement_title {
      color: #000;
      text-align: center;
      font-size: 28rpx;
    }
    .wrap_agreement_btn {
      display: flex;
      align-items: center;
      justify-content: space-evenly;
    }
    .btn_agreement {
      height: 80rpx;
      font-size: 28rpx;
      color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 220rpx;
      border-radius: 50rpx;
      border: 1rpx solid #dedede;
    }
    .wrap_agreement_item {
      margin-top: 30rpx;
      color: #B48C6E;
    }
    .wrap_agreement_item:last-child {
      margin-bottom: 30rpx;
    }
    .agree {
      border-color: #B48C6E;
      background: #B48C6E;
      color: #fff;
    }
    .wrap_agreement_items {
      flex: 1;
      width: 100%;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    效果如下:
    在这里插入图片描述

    注意:
    如果wx.getPrivacySetting中 needAuthorization始终返回的是false, 则需要在app.json中添加"usePrivacyCheck": true,,如图所示:
    在这里插入图片描述

    引入方式:

    在需要使用的页面的wxml文件中,直接 ****即可,注意组件需要在对应的.json文件中引入

  • 相关阅读:
    Bridge 桥接模式简介与 C# 示例【结构型2】【设计模式来了_7】
    拜托!佛系点,你只是给社区打工而已
    京东Java面试题、笔试题(含答案)
    0085 查找算法
    Python3编程基础-变量与计算器
    前缀,后缀转中缀以及中缀转前缀,后缀
    FORCESPRO的使用教程(暂未完结)
    1.在vsCode上创建Hello,World
    Postman使用总结
    UniApp Vue 3 中的网络请求封装详解及用法
  • 原文地址:https://blog.csdn.net/qq_31420313/article/details/132905489