privacy-popup.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <view>
  3. <wd-popup
  4. v-model="showPopup"
  5. :close-on-click-modal="false"
  6. custom-class="wd-privacy-popup"
  7. @close="handleClose"
  8. >
  9. <view class="wd-privacy-popup__header">
  10. <!--标题-->
  11. <view class="wd-picker__title">{{ title }}</view>
  12. </view>
  13. <view class="wd-privacy-popup__container">
  14. <text>{{ desc }}</text>
  15. <text class="wd-privacy-popup__container-protocol" @click="openPrivacyContract">
  16. {{ protocol }}
  17. </text>
  18. <text>{{ subDesc }}</text>
  19. </view>
  20. <view class="wd-privacy-popup__footer">
  21. <button
  22. class="wd-privacy-popup__footer-disagree wd-button is-block is-round is-medium is-plain"
  23. id="disagree-btn"
  24. @click="handleDisagree"
  25. >
  26. 拒绝
  27. </button>
  28. <button
  29. class="wd-privacy-popup__footer-agree wd-button is-primary is-block is-round is-medium"
  30. id="agree-btn"
  31. open-type="agreePrivacyAuthorization"
  32. @agreeprivacyauthorization="handleAgree"
  33. >
  34. 同意
  35. </button>
  36. </view>
  37. </wd-popup>
  38. </view>
  39. </template>
  40. <script lang="ts">
  41. export default {
  42. name: 'privacy-popup',
  43. options: {
  44. virtualHost: true,
  45. addGlobalClass: true,
  46. styleIsolation: 'shared',
  47. },
  48. }
  49. </script>
  50. <script lang="ts" setup>
  51. import { onBeforeMount, ref } from 'vue'
  52. type Props = {
  53. title?: string // 标题
  54. desc?: string // 描述
  55. subDesc?: string // 字描述
  56. protocol?: string // 协议名称
  57. }
  58. const props = withDefaults(defineProps<Props>(), {
  59. title: '用户隐私保护提示',
  60. desc: '感谢您使用本应用,您使用本应用的服务之前请仔细阅读并同意',
  61. subDesc:
  62. '。当您点击同意并开始时用产品服务时,即表示你已理解并同息该条款内容,该条款将对您产生法律约束力。如您拒绝,将无法使用相应服务。',
  63. protocol: '《用户隐私保护指引》',
  64. })
  65. const showPopup = ref<boolean>(false) // 是否展示popup
  66. const privacyResolves = ref(new Set()) // onNeedPrivacyAuthorization的reslove
  67. const privacyHandler = (resolve: any) => {
  68. showPopup.value = true
  69. privacyResolves.value.add(resolve)
  70. }
  71. const emit = defineEmits(['agree', 'disagree'])
  72. onBeforeMount(() => {
  73. // 注册监听
  74. if ((wx as any).onNeedPrivacyAuthorization) {
  75. ;(wx as any).onNeedPrivacyAuthorization((resolve: any) => {
  76. if (typeof privacyHandler === 'function') {
  77. privacyHandler(resolve)
  78. }
  79. })
  80. }
  81. })
  82. /**
  83. * 同意隐私协议
  84. */
  85. function handleAgree() {
  86. showPopup.value = false
  87. privacyResolves.value.forEach((resolve: any) => {
  88. resolve({
  89. event: 'agree',
  90. buttonId: 'agree-btn',
  91. })
  92. })
  93. privacyResolves.value.clear()
  94. emit('agree')
  95. }
  96. /**
  97. * 拒绝隐私协议
  98. */
  99. function handleDisagree() {
  100. showPopup.value = false
  101. privacyResolves.value.forEach((resolve: any) => {
  102. resolve({
  103. event: 'disagree',
  104. })
  105. })
  106. privacyResolves.value.clear()
  107. }
  108. /**
  109. * 打开隐私协议
  110. */
  111. function openPrivacyContract() {
  112. ;(wx as any).openPrivacyContract({
  113. success: (res) => {
  114. console.log('openPrivacyContract success')
  115. },
  116. fail: (res) => {
  117. console.error('openPrivacyContract fail', res)
  118. },
  119. })
  120. }
  121. /**
  122. * 弹出框关闭时清空
  123. */
  124. function handleClose() {
  125. privacyResolves.value.clear()
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. @import './index.scss';
  130. </style>