uni微信小程序隐私协议

x33g5p2x  于9个月前 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(362)

最近小程序又新增了个 隐私协议弹窗。需要用户去授权,官网的一些API才能使用。官网地址

功能展示

项目地址:https://ext.dcloud.net.cn/plugin?id=14358

1.manifest.json中添加

  1. "mp-weixin": {
  2. //小程序隐私协议
  3. "__usePrivacyCheck__": true},

注:小程序运行会报 无效的 app.json ["__usePrivacyCheck__"] ,有说切换基础库调试,但是这个提示一直在,不影响代码的运行

2.创建协议弹窗

创建components文件夹下,创建privacy-popup文件夹与privacy-popup.vue。

主要通过 wx.onNeedPrivacyAuthorization 接口来监听何时需要提示用户阅读隐私政策。

调用 wx.openPrivacyContract 接口打开隐私协议页面为空白,具体原因还不清楚。可能是个人原因。

注:微信公众平台—设置—找到—用户隐私保护指引。添加协议内容,等待审核通过。

不配置协议内容,点击头像获取提示,[渲染层错误] [Component] <button>: chooseAvatar:fail api scope is not declared in the privacy agreement(env: Windows,mp,1.06.2307260; lib: 3.0.1)

  1. <!--
  2. *@author: Jay
  3. *@description: 小程序隐私协议 弹窗
  4. * @createTime: 2023-08-31 10:07:54
  5. -->
  6. <template>
  7. <view v-if="showPop">
  8. <view class="privacy-mask">
  9. <view class="privacy-wrap">
  10. <view class="privacy-title">用户隐私保护提示
  11. </view>
  12. <view class="privacy-desc">感谢您使用本小程序,在使用前您应当阅读井同意
  13. <text class="privacy-link" @tap="openPrivacyContract">《用户隐私保护指引》</text>
  14. 当点击同意并继续时,即表示您已理解并同意该条款内容,该条款将对您产生法律约束力;如您不同意,将无法继续使用小程序相关功能。
  15. </view>
  16. <view class="privacy-button-flex">
  17. <button class="privacy-button-btn bg-disagree" @tap="handleDisagree">不同意
  18. </button>
  19. <button id="agree-btn" class="privacy-button-btn bg-agree" open-type="agreePrivacyAuthorization"@agreeprivacyauthorization="handleAgree">同意并继续
  20. </button>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>export default{
  27. data() {
  28. return{
  29. showPop: false,
  30. privacyAuthorization: null,
  31. privacyResolves: newSet(),
  32. closeOtherPagePopUpHooks: newSet(),
  33. }
  34. },
  35. mounted() {
  36. this.init()
  37. this.curPageShow()
  38. },
  39. created() {
  40. //查询微信侧记录的用户是否有待同意的隐私政策信息
  41. wx.getPrivacySetting({
  42. success(res) {
  43. console.log('隐私政策信息', res)
  44. //if (res.needAuthorization) {
  45. ////打开弹窗
  46. //that.popUp()
  47. //}
  48. }
  49. });
  50. },
  51. methods: {
  52. //监听何时需要提示用户阅读隐私政策
  53. init() {
  54. let that = this;
  55. if(wx.onNeedPrivacyAuthorization) {
  56. wx.onNeedPrivacyAuthorization((resolve) =>{
  57. if (typeof that.privacyAuthorization === 'function') {
  58. that.privacyAuthorization(resolve)
  59. }
  60. })
  61. }
  62. },
  63. //初始化监听程序
  64. curPageShow() {
  65. this.privacyAuthorization = resolve =>{
  66. this.privacyResolves.add(resolve)
  67. //打开弹窗
  68. this.popUp()
  69. //额外逻辑:当前页面的隐私弹窗弹起的时候,关掉其他页面的隐私弹窗
  70. this.closeOtherPagePopUp(this.disPopUp)
  71. }
  72. this.closeOtherPagePopUpHooks.add(this.disPopUp)
  73. },
  74. //额外逻辑:当前页面的隐私弹窗弹起的时候,关掉其他页面的隐私弹窗
  75. closeOtherPagePopUp(closePopUp) {
  76. this.closeOtherPagePopUpHooks.forEach(hook =>{
  77. if (closePopUp !==hook) {
  78. hook()
  79. }
  80. })
  81. },
  82. //打开隐私协议
  83. openPrivacyContract() {
  84. wx.openPrivacyContract({
  85. success(res) {
  86. console.log('打开隐私协议', res);
  87. },
  88. fail(err) {
  89. console.error('打开隐私协议失败', err)
  90. }
  91. });
  92. },
  93. //不同意
  94. handleDisagree() {
  95. this.privacyResolves.forEach(resolve =>{
  96. resolve({
  97. event: 'disagree',
  98. })
  99. })
  100. this.privacyResolves.clear()
  101. //关闭弹窗
  102. this.disPopUp()
  103. //退出小程序
  104. //wx.exitMiniProgram();
  105. },
  106. //同意并继续
  107. handleAgree() {
  108. this.privacyResolves.forEach(resolve =>{
  109. resolve({
  110. event: 'agree',
  111. buttonId: 'agree-btn'})
  112. })
  113. this.privacyResolves.clear()
  114. //关闭弹窗
  115. this.disPopUp()
  116. },
  117. //打开弹窗
  118. popUp() {
  119. if (this.showPop === false) {
  120. this.showPop = true}
  121. },
  122. //关闭弹窗
  123. disPopUp() {
  124. if (this.showPop === true) {
  125. this.showPop = false}
  126. },
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>.privacy-mask {
  131. position: fixed;
  132. z-index: 5000;
  133. top: 0;
  134. right: 0;
  135. left: 0;
  136. bottom: 0;
  137. background: rgba(0, 0, 0, 0.2);
  138. display: flex;
  139. align-items: center;
  140. justify-content: center;
  141. }
  142. .privacy-wrap {
  143. width: 632rpx;
  144. padding: 48rpx 30rpx;
  145. box-sizing: border-box;
  146. background: #fff;
  147. border-radius: 16rpx;
  148. }
  149. .privacy-title {
  150. padding: 0rpx 30rpx 40rpx 30rpx;
  151. font-weight: 700;
  152. font-size: 36rpx;
  153. text-align: center;
  154. }
  155. .privacy-desc {
  156. font-size: 30rpx;
  157. color: #555;
  158. line-height: 2;
  159. text-align: left;
  160. padding: 040rpx;
  161. }
  162. .privacy-link {
  163. color: #2f80ed;
  164. }
  165. .privacy-button-flex {
  166. display: flex;
  167. padding: 20rpx 40rpx;
  168. }
  169. .privacy-button-btn {
  170. color: #FFF;
  171. font-size: 30rpx;
  172. font-weight: 500;
  173. line-height: 100rpx;
  174. text-align: center;
  175. height: 100rpx;
  176. border-radius: 20rpx;
  177. border: none;
  178. background: #07c160;
  179. flex: 1;
  180. margin-right: 30rpx;
  181. justify-content: center;
  182. }
  183. .privacy-button-btn::after {
  184. border: none;
  185. }
  186. .bg-disagree {
  187. color: #07c160;
  188. background: #f2f2f2;
  189. }
  190. .bg-agree {
  191. margin-right: 0rpx;
  192. }
  193. </style>

privacy-popup.vue

3.引入插件

在登录或其他需要调用微信API的页面到处插件

注:插件导入即可,触发微信API方可自动调用,打开协议弹窗

  1. <template>
  2. <view>
  3. <!-- #ifdef MP-WEIXIN -->
  4. <!-- 小程序隐私协议 -->
  5. <privacy-popup></privacy-popup>
  6. <!-- #endif -->
  7. </view>
  8. </template>
  9. <script>
  10. //小程序隐私协议
  11. import PrivacyPopup from "@/components/privacy-popup/privacy-popup.vue"export default{
  12. data() {},
  13. components: {
  14. PrivacyPopup
  15. },
  16. }
  17. </script>
  18. <style lang="scss" scoped>
  19. </style>

4.注意事项

页面导入插件后,以获取用户名与头像为例

去微信公众平台(https://mp.weixin.qq.com/ )配置协议内容,点击头像,用户名获取提示该错误

注:其他api接口调用失败的回调函数,验证报错

微信公众平台—设置

找到下面

配置当前隐私保护协议

根据需求添加所需协议

完成协议配置等待审核通过,在页面使用该插件--弹窗即可出现。

微信原生开发可使用该组件:https://juejin.cn/post/7272276908381175819 ,参照改成更适合uni体质的组件。

本文来自博客园,作者:虚乄,转载请注明原文链接:https://www.cnblogs.com/lovejielive/p/17669140.html

相关文章