// components/popup-privacy/popup-privacy.ts import { getPrivacySetting, openPrivacyContract } from "../../lib/wx/open-api"; let privacyHandler: (resolve: WechatMiniprogram.GeneralCallbackResult) => void; let privacyResolves = new Set(); let closeOtherPagePopUpHooks = new Set<() => void>(); if (wx.onNeedPrivacyAuthorization) { wx.onNeedPrivacyAuthorization(resolve => { console.log('[log: popup-privacy] 触发 onNeedPrivacyAuthorization'); if (typeof privacyHandler === 'function') privacyHandler(resolve); }) } Component({ lifetimes: { attached() { const hide = () => this.onHide(); privacyHandler = resolve => { privacyResolves.add(resolve); this.onShow(); closeOtherPagePopUpHooks.forEach(hook => { if (hide !== hook) hook(); }) } closeOtherPagePopUpHooks.add(hide); (this).hide = hide; }, detached() { closeOtherPagePopUpHooks.delete((this).hide); } }, pageLifetimes: { show() { getPrivacySetting().then(({ needAuthorization, privacyContractName }) => { const [app, contract] = privacyContractName.split('小程序'); this.triggerEvent('setting', { needAuthorization, privacyContractName }) this.setData({ appName: app.slice(1), privacyContractName: `《${contract}` }); if (this.data.pre && needAuthorization) this.onShow(); }) } }, properties: { pre: { type: Boolean, value: false }, show: { type: Boolean, value: false }, overlay: { type: Boolean, value: true }, }, data: { visible: false, title: '用户隐私保护提示', appName: '本小程序', privacyContractName: '《隐私政策》' }, observers: { 'show'(visible) { this.setData({ visible }) } }, methods: { onShow() { if (!this.data.visible) this.setData({ visible: true }); }, onHide() { if (this.data.visible) this.setData({ visible: false }); }, onOpenPrivacyContract() { openPrivacyContract().then(); }, handleAgree() { console.log('[log: popup-privacy] 触发 handleAgree'); privacyResolves.forEach(resolve => { resolve({ event: 'agree', buttonId: 'agree-btn' }) }) privacyResolves.clear(); this.onHide(); this.triggerEvent('agree') }, handleDisagree() { console.log('[log: popup-privacy] 触发 handleDisagree'); privacyResolves.forEach(resolve => { resolve({ event: 'disagree', }) }) privacyResolves.clear(); this.onHide(); this.triggerEvent('disagree') } } })