Skip to content
On this page

隐私指引

如果用户拒绝了隐私指引,不让其登录

2023-09-15之前需要在app.json配置

"__usePrivacyCheck__": true,

2023-09-15之后不需要

登录页

js
<template>
  ...
  <u-modal :show="showPrivacy" title="中健骑手">
    <view class="content">在你使用【中健骑手】小程序之前,请仔细阅读 <u-link text="《中健骑手隐私保护指引》" :under-line="true" @click="handleOpenPrivacyContract"></u-link>。如你同意《中健骑手隐私保护指引》,请点击 "同意" 开始使用【中健骑手】 </view>
    <template #confirmButton>
      <view style="display: flex;">
        <!-- 必须使用原生button 不然无法触发事件 -->
        <button style="flex: 1;" type="primary" open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button>
        <button style="flex: 1; margin-left: 10rpx;" @click="showPrivacy = false">拒绝</button>
      </view>
    </template>
  </u-modal>

</template>

<script>
export default {
  data() {
    return {
      showPrivacy: false,
    }
  },
  async onLoad() {
    const needSetting = await this.getPrivacySetting()
    if(needSetting) {
      this.showPrivacy = true
    }else {
      // 自动登录逻辑
    }
  },
  methods: {
    // 返回true代表需要授权
    // 返回false为不需要授权或者已经授权过了
    getPrivacySetting() {
      const version = wx.getAppBaseInfo().SDKVersion 
      return new Promise((resolve, reject) => {
        if(this.compareVersion(version, '2.32.3') >= 0) {
          wx.getPrivacySetting({
            success: res => {
              if (res.needAuthorization) {
                resolve(true)
              } else {
                resolve(false)
              }
            },
          })
        }else {
          resolve(false)
        }
      })
    },

    // 点击登录按钮
    async submit() {
      const needSetting = await this.getPrivacySetting()
      if(needSetting) {
        this.showPrivacy = true
      }else {
        // 登录逻辑
      }
    },

    handleOpenPrivacyContract() {
      // 打开隐私协议页面
      wx.openPrivacyContract({
        success: () => {}, // 打开成功
        fail: () => {}, // 打开失败
        complete: () => {}
      })
    },

    handleAgreePrivacyAuthorization() {
      this.showPrivacy = false
      // 自动登录逻辑
    },
    			// 比较版本号
    compareVersion(v1, v2) {
      v1 = v1.split('.')
      v2 = v2.split('.')
      const len = Math.max(v1.length, v2.length)
      while (v1.length < len) {
        v1.push('0')
      }
      while (v2.length < len) {
        v2.push('0')
      }
      for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i])
        const num2 = parseInt(v2[i])
    
        if (num1 > num2) {
          return 1
        } else if (num1 < num2) {
          return -1
        }
      }
      return 0
    },
  }
}


</script>

Released under the MIT License.