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>