uni - 微信小程序 自定义隐私协议弹窗

  2023年9月15日之后 微信小程序 隐私协议更新,需要主动查询隐私授权同步状态以及展示隐私协议

获取用户隐私信息,或者访问相册都需要授权后才能通过

在父组件页面用此组件后会自动查询有没有权限,如果没权限会自动调出授权弹框

在第一次调用的时候getPrivacySetting 都会调用 exit 返回给父组件。true为同意,false 为解决

在父组件可以使用 this.$refs.privacy.openShow() 手动打开授权弹框 住:需要在组件上写 ref=privacy

<template>
  <u-popup v-model="showPrivacy" mode="center" border-radius="16" width="650rpx" :mask-close-able="false">
    <div class="box">
      <div class="title"> xxxx 隐私保护指引</div>
      <div class="des">
        在使用当前小程序服务之前,请仔细阅读<text class="link" @tap="openPrivacyContract">{{ privacyContractName }}</text>。如你同意{{ privacyContractName }},请点击“同意”开始使用。
      </div>
      <view class="btns">
        <button class="item reject" @tap="exitMiniProgram">拒绝</button>
        <button id="agree-btn" class="item agree" open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button>
      </view>
    </div>
  </u-popup>
</template>

<script>
// exit 事件返回 是否同意授权 true同意 false解决
// this.$refs.privacy.openShow() 父组件调用 ref=privacy
export default {
  props: {},
  data() {
    return {
      privacyContractName: '',
      showPrivacy: false
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      const _ = this
      wx.getPrivacySetting({ // 获取用户有没有授权过
        success(res) {
          console.log(res)
          if (res.errMsg === 'getPrivacySetting:ok') {
            _.privacyContractName = res.privacyContractName // 协议名称
            _.showPrivacy = res.needAuthorization
            if (res.needAuthorization) {
              _.$emit('exit', false)
            } else {
              _.$emit('exit', true)
            }
          }
        }
      })
    },
    openPrivacyContract() {
      const _ = this
      wx.openPrivacyContract({ // 跳转到隐私协议页面
        fail: () => {
          wx.showToast({
            title: '遇到错误',
            icon: 'error'
          })
        }
      })
    },
    // 拒绝隐私协议
    exitMiniProgram() {
      // 直接退出小程序
      // wx.exitMiniProgram()
      this.showPrivacy = false
      this.$emit('exit', false)
    },
    openShow() {
      this.showPrivacy = true
    },
    // 同意协议
    handleAgreePrivacyAuthorization() {
      this.showPrivacy = false
      this.$emit('exit', true)
    }
  }
}
</script>

<style scoped lang="scss">
.box{
  padding: 45rpx;
  .title{
    text-align: center;
    font-weight: 700;
    font-size: 30rpx;
  }
  .des {
    font-size: 26rpx;
    color: #666;
    margin-top: 40rpx;
    text-align: justify;
    line-height: 1.6;
    .link {
      color: #013369;
      text-decoration: underline;
    }
  }
  .btns {
    margin-top: 48rpx;
    display: flex;
    .item {
      justify-content: space-between;
      width: 244rpx;
      height: 80rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 16rpx;
      box-sizing: border-box;
      border: none;
    }
    .reject {
      background: #f4f4f5;
      color: #909399;
    }
    .agree {
      background: #013369;
      color: #fff;
    }
  }

  button{
    &::after{
      border: none;
    }
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值