popup-privacy.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // components/popup-privacy/popup-privacy.ts
  2. import { getPrivacySetting, openPrivacyContract } from "../../lib/wx/open-api";
  3. let privacyHandler: (resolve: WechatMiniprogram.GeneralCallbackResult) => void;
  4. let privacyResolves = new Set<WechatMiniprogram.GeneralCallbackResult>();
  5. let closeOtherPagePopUpHooks = new Set<() => void>();
  6. if (wx.onNeedPrivacyAuthorization) {
  7. wx.onNeedPrivacyAuthorization(resolve => {
  8. console.log('[log: popup-privacy] 触发 onNeedPrivacyAuthorization');
  9. if (typeof privacyHandler === 'function') privacyHandler(resolve);
  10. })
  11. }
  12. Component({
  13. lifetimes: {
  14. attached() {
  15. const hide = () => this.onHide();
  16. privacyHandler = resolve => {
  17. privacyResolves.add(resolve);
  18. this.onShow();
  19. closeOtherPagePopUpHooks.forEach(hook => { if (hide !== hook) hook(); })
  20. }
  21. closeOtherPagePopUpHooks.add(hide);
  22. (<any>this).hide = hide;
  23. },
  24. detached() {
  25. closeOtherPagePopUpHooks.delete((<any>this).hide);
  26. }
  27. },
  28. pageLifetimes: {
  29. show() {
  30. getPrivacySetting().then(({ needAuthorization, privacyContractName }) => {
  31. const [app, contract] = privacyContractName.split('小程序');
  32. this.triggerEvent('setting', { needAuthorization, privacyContractName })
  33. this.setData({
  34. appName: app.slice(1),
  35. privacyContractName: `《${contract}`
  36. });
  37. if (this.data.pre && needAuthorization) this.onShow();
  38. })
  39. }
  40. },
  41. properties: {
  42. pre: { type: Boolean, value: false },
  43. show: { type: Boolean, value: false },
  44. },
  45. data: {
  46. visible: false,
  47. title: '用户隐私保护提示',
  48. appName: '本小程序',
  49. privacyContractName: '《隐私政策》'
  50. },
  51. observers: {
  52. 'show'(visible) {
  53. this.setData({ visible })
  54. }
  55. },
  56. methods: {
  57. onShow() {
  58. if (!this.data.visible) this.setData({ visible: true });
  59. },
  60. onHide() {
  61. if (this.data.visible) this.setData({ visible: false });
  62. },
  63. onOpenPrivacyContract() {
  64. openPrivacyContract().then();
  65. },
  66. handleAgree() {
  67. console.log('[log: popup-privacy] 触发 handleAgree');
  68. privacyResolves.forEach(resolve => {
  69. resolve({
  70. event: 'agree',
  71. buttonId: 'agree-btn'
  72. })
  73. })
  74. privacyResolves.clear();
  75. this.onHide();
  76. this.triggerEvent('agree')
  77. },
  78. handleDisagree() {
  79. console.log('[log: popup-privacy] 触发 handleDisagree');
  80. privacyResolves.forEach(resolve => {
  81. resolve({
  82. event: 'disagree',
  83. })
  84. })
  85. privacyResolves.clear();
  86. this.onHide();
  87. this.triggerEvent('disagree')
  88. }
  89. }
  90. })