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>

uni-app开发微信小程序中,当弹窗(Modal)出现并且包含输入框(Input)时,你可以通过监听`input`元素的`focus`事件来获取焦点。首先,在弹窗组件的数据里,你需要定义一个表示输入框是否聚焦的状态,比如: ```javascript data() { return { modalVisible: false, focusOnInput: false // 新增一个变量用于追踪输入框焦点状态 } } ``` 然后,在你想显示弹窗的地方,添加打开弹窗并监听输入框焦点的代码: ```javascript openModal() { this.modalVisible = true; const inputEle = this.$refs.yourInputRef; // 假设输入框的ref名为yourInputRef if (inputEle) { inputEle.focus(); // 当弹窗打开时立即聚焦输入框 inputEle.addEventListener('focus', () => { // 监听输入框聚焦事件 this.focusOnInput = true; }); inputEle.addEventListener('blur', () => { // 监听输入框失去焦点事件 this.focusOnInput = false; }); } }, closeModal() { this.modalVisible = false; // 如果需要,移除之前的事件监听 if (this.focusOnInput) { // 这里可以移除inputEle的事件监听,防止内存泄漏 // 具体代码取决于uni-app API,可能类似于 this.$refs.yourInputRef.removeEventListener('focus', ...) } } ``` 在模板部分,确保将`ref`属性应用到输入框上: ```html <view v-if="modalVisible"> <input type="text" ref="yourInputRef" placeholder="请输入内容" @focus="handleFocus" /> </view> ``` 并在对应的JS文件中处理`handleFocus`函数: ```javascript methods: { handleFocus(e) { this.focusOnInput = true; // 更新状态 } } ``` 当你需要检查输入框是否有焦点时,可以直接访问`focusOnInput`这个数据字段。例如: ```javascript if (this.focusOnInput) { console.log('当前有输入框聚焦'); } else { console.log('输入框未聚焦'); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值