kz-wx-privacy-check.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <tn-modal v-model="showPrivacy" :custom="true" title="隐私保护指引" :maskCloseable="false">
  3. <view class="content">
  4. <view class="title">隐私保护指引</view>
  5. <view class="des">
  6. 在使用当前小程序服务之前,请仔细阅读
  7. <text class="link text-primary" @tap="handleOpenPrivacyContract">{{privacyContractName}}</text>
  8. 。如你同意{{privacyContractName}},请点击“同意”开始使用。
  9. </view>
  10. <view class="btns">
  11. <button class="item reject" @tap="handleDisagree">拒绝</button>
  12. <button id="agree-btn" class="item agree bg-primary" open-type="agreePrivacyAuthorization"
  13. @agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button>
  14. </view>
  15. </view>
  16. </tn-modal>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'KzWxPrivacyCheck',
  21. data() {
  22. return {
  23. privacyContractName: '',
  24. showPrivacy: false,
  25. resolvePrivacyAuthorization: null,
  26. };
  27. },
  28. mounted() {
  29. // #ifdef MP-WEIXIN
  30. this.getPrivacySetting()
  31. if (wx.onNeedPrivacyAuthorization) {
  32. wx.onNeedPrivacyAuthorization(resolve => {
  33. console.log('触发 onNeedPrivacyAuthorization', resolve)
  34. this.showPrivacy = true
  35. this.resolvePrivacyAuthorization = resolve
  36. })
  37. }
  38. // #endif
  39. },
  40. methods: {
  41. getPrivacySetting(type) {
  42. return new Promise((resolve, reject) => {
  43. // #ifdef APP-PLUS
  44. resolve(true)
  45. // #endif
  46. // #ifdef MP-WEIXIN
  47. if (wx.getPrivacySetting) {
  48. wx.getPrivacySetting({
  49. success: res => {
  50. // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
  51. console.log(res)
  52. if (res.needAuthorization) {
  53. // 需要弹出隐私协议
  54. if (type == "showPrivacy") {
  55. this.showPrivacy = true
  56. }
  57. this.privacyContractName = res.privacyContractName
  58. resolve(false)
  59. } else {
  60. // 用户已经同意过隐私协议,所以不需要再弹出隐私协议,也能调用已声明过的隐私接口
  61. resolve(true)
  62. }
  63. }
  64. })
  65. }else{
  66. resolve(true)
  67. }
  68. // #endif
  69. })
  70. },
  71. async handleAgreePrivacyAuthorization() {
  72. // 用户点击了同意,之后所有已声明过的隐私接口和组件都可以调用了
  73. this.showPrivacy = false
  74. if (typeof this.resolvePrivacyAuthorization === 'function') {
  75. this.resolvePrivacyAuthorization({
  76. buttonId: 'agree-btn',
  77. event: 'agree'
  78. })
  79. }
  80. },
  81. handleDisagree() {
  82. this.showPrivacy = false
  83. if (typeof this.resolvePrivacyAuthorization === 'function') {
  84. this.resolvePrivacyAuthorization({
  85. event: 'disagree'
  86. })
  87. }
  88. },
  89. handleOpenPrivacyContract() {
  90. // 打开隐私协议页面
  91. wx.openPrivacyContract({
  92. success: () => {}, // 打开成功
  93. fail: () => {}, // 打开失败
  94. complete: () => {}
  95. })
  96. }
  97. },
  98. };
  99. </script>
  100. <style>
  101. .content .title {
  102. text-align: center;
  103. color: #333;
  104. font-weight: bold;
  105. font-size: 32rpx;
  106. }
  107. .content .des {
  108. font-size: 26rpx;
  109. color: #666;
  110. margin-top: 40rpx;
  111. text-align: justify;
  112. line-height: 1.6;
  113. }
  114. .content .des .link {
  115. color: #07c160;
  116. text-decoration: underline;
  117. }
  118. .btns {
  119. margin-top: 48rpx;
  120. display: flex;
  121. }
  122. .btns .item {
  123. width: 180rpx;
  124. height: 70rpx;
  125. overflow: visible;
  126. display: flex;
  127. align-items: center;
  128. margin: 0 12px;
  129. justify-content: center;
  130. box-sizing: border-box;
  131. border: none !important;
  132. font-size: 26rpx;
  133. }
  134. .btns .reject {
  135. background: #f4f4f5;
  136. color: #909399;
  137. }
  138. </style>