把pages第一个页面设置成一个loading来处理隐私弹窗的问题
<view>
<loading></loading>
<privacyPopup></privacyPopup>
</view>
<script>
export default {
async onLoad(options) {
try {
if (wx.onNeedPrivacyAuthorization) {
//getPrivacySetting不支持Promise转了一下Promise
let {needAuthorization} = await toPromise(wx.getPrivacySetting)({})
if (needAuthorization) {
//此处要有剪切板权限 也可以是任意隐私权限的接口
await wx.getClipboardData({})
}
}
} finally {
//此处同意后的跳转逻辑
}
}
}
</script>
privacyPopup组件
let privacyHandler
let privacyResolves = new Set()
let closeOtherPagePopUpHooks = new Set()
wx.onNeedPrivacyAuthorization((resolve) => {
if (typeof privacyHandler === 'function') {
privacyHandler(resolve)
}
})
const closeOtherPagePopUp = (closePopUp) => {
closeOtherPagePopUpHooks.forEach((hook) => {
if (closePopUp !== hook) {
hook()
}
})
}
export default {
name: 'privacyPopup',
data: () => ({
title: '用户隐私政策',
desc1: '感谢您使用本应用,请仔细阅读一下内容,并做出适当的选择',
urlTitle: '点击阅读《隐私政策(小程序)》完整版>',
desc2: '主要说明:我们处理的信息种类、方式和目的;你所享有的权益;第三方插件信息等。如您拒绝/暂不使用,将无法进入此应用。',
innerShow: false,
height: 0,
}),
created() {
const closePopUp = () => {
this.disPopUp()
}
privacyHandler = (resolve) => {
privacyResolves.add(resolve)
this.popUp()
// 额外逻辑:当前页面的隐私弹窗弹起的时候,关掉其他页面的隐私弹窗
closeOtherPagePopUp(closePopUp)
}
closeOtherPagePopUpHooks.add(closePopUp)
this.closePopUp = closePopUp
},
beforeDestroy() {
closeOtherPagePopUpHooks.delete(this.closePopUp)
},
methods: {
handleAgree(e) {
this.disPopUp()
// 这里演示了同时调用多个wx隐私接口时要如何处理:让隐私弹窗保持单例,点击一次同意按钮即可让所有pending中的wx隐私接口继续执行 (看page/index/index中的 wx.getClipboardData 和 wx.startCompass)
privacyResolves.forEach((resolve) => {
resolve({
event: 'agree',
buttonId: 'agree-btn',
})
})
privacyResolves.clear()
},
handleDisagree(e) {
this.disPopUp()
privacyResolves.forEach((resolve) => {
resolve({
event: 'disagree',
})
})
privacyResolves.clear()
//此处你可以退出也可以处理其他逻辑
wx.exitMiniProgram()
},
popUp() {
if (this.innerShow === false) {
this.innerShow = true
//uni.hideTabBar()
}
},
disPopUp() {
if (this.innerShow === true) {
this.innerShow = false
// uni.showTabBar()
}
},
openPrivacyContract() {
wx.openPrivacyContract({
success: (res) => {
console.log('openPrivacyContract success')
},
fail: (res) => {
console.error('openPrivacyContract fail', res)
},
})
},
},
}