privacyPopup.js 2.9 KB

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