popup-privacy.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. overlay: { type: Boolean, value: true },
  45. },
  46. data: {
  47. visible: false,
  48. title: '用户隐私保护提示',
  49. appName: '本小程序',
  50. privacyContractName: '《隐私政策》'
  51. },
  52. observers: {
  53. 'show'(visible) {
  54. this.setData({ visible })
  55. }
  56. },
  57. methods: {
  58. onShow() {
  59. if (!this.data.visible) this.setData({ visible: true });
  60. },
  61. onHide() {
  62. if (this.data.visible) this.setData({ visible: false });
  63. },
  64. onOpenPrivacyContract() {
  65. openPrivacyContract().then();
  66. },
  67. handleAgree() {
  68. console.log('[log: popup-privacy] 触发 handleAgree');
  69. privacyResolves.forEach(resolve => {
  70. resolve({
  71. event: 'agree',
  72. buttonId: 'agree-btn'
  73. })
  74. })
  75. privacyResolves.clear();
  76. this.onHide();
  77. this.triggerEvent('agree')
  78. },
  79. handleDisagree() {
  80. console.log('[log: popup-privacy] 触发 handleDisagree');
  81. privacyResolves.forEach(resolve => {
  82. resolve({
  83. event: 'disagree',
  84. })
  85. })
  86. privacyResolves.clear();
  87. this.onHide();
  88. this.triggerEvent('disagree')
  89. }
  90. }
  91. })