| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- // components/popup-privacy/popup-privacy.ts
- import { getPrivacySetting, openPrivacyContract } from "../../lib/wx/open-api";
- let privacyHandler: (resolve: WechatMiniprogram.GeneralCallbackResult) => void;
- let privacyResolves = new Set<WechatMiniprogram.GeneralCallbackResult>();
- 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);
- (<any>this).hide = hide;
- },
- detached() {
- closeOtherPagePopUpHooks.delete((<any>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 },
- },
- 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')
- }
- }
- })
|