privacyPopup.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. let privacyHandler
  2. let privacyResolves = new Set()
  3. let closeOtherPagePopUpHooks = new Set()
  4. if (wx.onNeedPrivacyAuthorization) {
  5. wx.onNeedPrivacyAuthorization((resolve,) => {
  6. console.log('触发 onNeedPrivacyAuthorization',resolve)
  7. if (typeof privacyHandler === 'function') {
  8. privacyHandler(resolve)
  9. }
  10. })
  11. }
  12. const closeOtherPagePopUp = (closePopUp) => {
  13. closeOtherPagePopUpHooks.forEach(hook => {
  14. if (closePopUp !== hook) {
  15. hook()
  16. }
  17. })
  18. }
  19. Component({
  20. options:{addGlobalClass: true},
  21. data: {
  22. title: "用户隐私保护提示",
  23. desc1: "感谢您使用本小程序,您使用本小程序前应当阅井同意",
  24. urlTitle: "《挑战家族小程序隐私保护指引》",
  25. desc2: "当您点击同意并开始时用产品服务时,即表示你已理解并同息该条款内容,该条款将对您产生法律约束力。如您拒绝,将无法进入小程序。",
  26. innerShow: false,
  27. height: 0,
  28. },
  29. lifetimes: {
  30. attached: function() {
  31. const closePopUp = () => {
  32. this.disPopUp()
  33. }
  34. privacyHandler = resolve => {
  35. privacyResolves.add(resolve)
  36. this.popUp()
  37. // 额外逻辑:当前页面的隐私弹窗弹起的时候,关掉其他页面的隐私弹窗
  38. closeOtherPagePopUp(closePopUp)
  39. }
  40. this.closePopUp = closePopUp
  41. closeOtherPagePopUpHooks.add(this.closePopUp)
  42. },
  43. detached: function() {
  44. closeOtherPagePopUpHooks.delete(this.closePopUp)
  45. }
  46. },
  47. pageLifetimes: {
  48. show() {
  49. if (this.closePopUp) {
  50. privacyHandler = resolve => {
  51. privacyResolves.add(resolve)
  52. this.popUp()
  53. // 额外逻辑:当前页面的隐私弹窗弹起的时候,关掉其他页面的隐私弹窗
  54. closeOtherPagePopUp(this.closePopUp)
  55. }
  56. }
  57. }
  58. },
  59. methods: {
  60. handleAgree(e) {
  61. this.disPopUp()
  62. // 这里演示了同时调用多个wx隐私接口时要如何处理:让隐私弹窗保持单例,点击一次同意按钮即可让所有pending中的wx隐私接口继续执行 (看page/index/index中的 wx.getClipboardData 和 wx.startCompass)
  63. privacyResolves.forEach(resolve => {
  64. resolve({
  65. event: 'agree',
  66. buttonId: 'agree-btn'
  67. })
  68. })
  69. privacyResolves.clear()
  70. },
  71. handleDisagree(e) {
  72. this.disPopUp()
  73. privacyResolves.forEach(resolve => {
  74. resolve({
  75. event: 'disagree',
  76. })
  77. })
  78. privacyResolves.clear()
  79. },
  80. popUp() {
  81. if (this.data.innerShow === false) {
  82. this.setData({
  83. innerShow: true
  84. })
  85. }
  86. },
  87. disPopUp() {
  88. if (this.data.innerShow === true) {
  89. this.setData({
  90. innerShow: false
  91. })
  92. }
  93. },
  94. openPrivacyContract() {
  95. wx.openPrivacyContract({
  96. success: res => {
  97. console.log('openPrivacyContract success')
  98. },
  99. fail: res => {
  100. console.error('openPrivacyContract fail', res)
  101. }
  102. })
  103. }
  104. }
  105. })