這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 一,問題起因 最新在開發小程式的時候,調用微信小程式來獲取用戶信息的時候經常報錯一個問題 fail api scope is not declared in the privacy agreement,api 更具公告,是微信更新對應的隱 ...
這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助
一,問題起因
最新在開發小程式的時候,調用微信小程式來獲取用戶信息的時候經常報錯一個問題
fail api scope is not declared in the privacy agreement,api
更具公告,是微信更新對應的隱私協議
二,解決方案
下麵是我總結的解決步驟
1.前往微信小程式公眾平臺配置設置,完善並提交信息(註意:更新好隱私協議,要通過審核的,介面才能正常訪問)
2.在components新增組件PrivacyPop
vue2版本
<template> <view class="privacy" v-if="showPrivacy"> <view class="content"> <view class="title">隱私保護指引</view> <view class="des"> 在使用當前小程式服務之前,請仔細閱讀<text class="link" @tap="openPrivacyContract">{{ privacyContractName }}</text>。如你同意{{ privacyContractName }},請點擊“同意”開始使用。 </view> <view class="btns"> <button class="item reject" @tap="exitMiniProgram">拒絕</button> <button id="agree-btn" class="item agree" open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button> </view> </view> </view> </template> <script> export default { data() { return { privacyContractName: '《XXX隱私保護引導》', showPrivacy: false } }, methods: { checkPrivacySetting(){ uni.getPrivacySetting({ success: res => { console.log("getPrivacySetting",res) this.showPrivacy = true // 返回結果為: res = { needAuthorization: true/false, privacyContractName: '《xxx隱私保護指引》' } // if (res.needAuthorization) { // 需要彈出隱私協議 // this.showPrivacy = false // } else { // this.showPrivacy = true // 用戶已經同意過隱私協議,所以不需要再彈出隱私協議,也能調用已聲明過的隱私介面 // wx.getUserProfile() // wx.chooseMedia() // wx.getClipboardData() // wx.startRecord() // } }, fail: () => {}, complete: () => {} }) }, // 打開隱私協議 openPrivacyContract() { uni.openPrivacyContract({ fail: () => { uni.showToast({ title: '遇到錯誤', icon: 'error' }) } }) }, // 拒絕隱私協議 exitMiniProgram() { console.log("拒絕隱私協議") const that = this; // 直接退出小程式 // wx.exitMiniProgram() uni.showModal({ // 如果拒絕,我們將無法獲取您的信息, 包括手機號、位置信息、相冊等該小程式十分重要的功能,您確定要拒絕嗎? content: '您確定要拒絕嗎?', success: res => { if (res.confirm) { that.showPrivacy = false; uni.exitMiniProgram({ success: () => { console.log('退出小程式成功'); } }); } } }); }, // 同意隱私協議 handleAgreePrivacyAuthorization() { wx.requirePrivacyAuthorize({ success: () => { // 用戶同意授權 // 繼續小程式邏輯 this.showPrivacy = false }, fail: () => {}, // 用戶拒絕授權 complete: () => {} }) } } } </script> <style scoped> .privacy { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0, 0, 0, .5); z-index: 9999999; display: flex; align-items: center; justify-content: center; } .content { width: 632rpx; padding: 48rpx; box-sizing: border-box; background: #fff; border-radius: 16rpx; } .content .title { text-align: center; color: #333; font-weight: bold; font-size: 32rpx; } .content .des { font-size: 26rpx; color: #666; margin-top: 40rpx; text-align: justify; line-height: 1.6; } .content .des .link { color: #07c160; text-decoration: underline; } .btns { margin-top: 48rpx; display: flex; } .btns .item { justify-content: space-between; width: 244rpx; height: 80rpx; display: flex; align-items: center; justify-content: center; border-radius: 16rpx; box-sizing: border-box; border: none; } .btns .reject { background: #f4f4f5; color: #909399; } .btns .agree { background: #07c160; color: #fff; } </style>
vue3版本
<template> <view class="privacy" v-if="showPrivacy"> <view class="content"> <view class="title">隱私保護指引</view> <view class="des"> 在使用當前小程式服務之前,請仔細閱讀<text class="link" @tap="openPrivacyContract">{{ privacyContractName }}</text>。如你同意{{ privacyContractName }},請點擊“同意”開始使用。 </view> <view class="btns"> <button class="item reject" @tap="exitMiniProgram">拒絕</button> <button id="agree-btn" class="item agree" open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button> </view> </view> </view> </template> <script lang="ts" setup> import { ref } from "vue"; const privacyContractName = ref('《用戶隱私保護引導》'); const showPrivacy = ref(false); const checkPrivacySetting = () => { uni.getPrivacySetting({ success: (res) => { showPrivacy.value = true } }) } // 打開隱私協議 const openPrivacyContract = () => { uni.openPrivacyContract({ fail: () => { uni.showToast({ title: '遇到錯誤', icon: 'error' }) } }) } // 拒絕隱私協議 const exitMiniProgram = () => { console.log("拒絕隱私協議") uni.showModal({ // 如果拒絕,我們將無法獲取您的信息, 包括手機號、位置信息、相冊等該小程式十分重要的功能,您確定要拒絕嗎? content: '您確定要拒絕嗎?', success: res => { if (res.confirm) { showPrivacy.value = false; uni.exitMiniProgram({ success: () => { console.log('退出小程式成功'); } }); } } }); } // 同意隱私協議 const handleAgreePrivacyAuthorization = () => { wx.requirePrivacyAuthorize({ success: () => { // 用戶同意授權 // 繼續小程式邏輯 showPrivacy.value = false }, fail: () => { }, // 用戶拒絕授權 complete: () => { } }) } </script> <style scoped> .privacy { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0, 0, 0, .5); z-index: 9999999; display: flex; align-items: center; justify-content: center; } .content { width: 632rpx; padding: 48rpx; box-sizing: border-box; background: #fff; border-radius: 16rpx; } .content .title { text-align: center; color: #333; font-weight: bold; font-size: 32rpx; } .content .des { font-size: 26rpx; color: #666; margin-top: 40rpx; text-align: justify; line-height: 1.6; } .content .des .link { color: #07c160; text-decoration: underline; } .btns { margin-top: 48rpx; display: flex; } .btns .item { justify-content: space-between; width: 244rpx; height: 80rpx; display: flex; align-items: center; justify-content: center; border-radius: 16rpx; box-sizing: border-box; border: none; } .btns .reject { background: #f4f4f5; color: #909399; } .btns .agree { background: #07c160; color: #fff; } </style>
3.在要使用的頁面中引入
vue2版本
import PrivacyPop from '../../components/PrivacyPop/PrivacyPop.vue'; components:{ PrivacyPop }, async onLoad() { this.$refs.PrivacyPopck.checkPrivacySetting(); },
vue3版本(建議點擊事件觸發)
import PrivacyPop from '@/components/PrivacyPop.vue'; import { ref } from "vue"; const PrivacyObj = ref({ }) const ClickFun = ()=>{ if(PrivacyObj.value){ PrivacyObj.value.checkPrivacySetting(); } }