微信小程序--授權接口更換說明


2021年4月15日微信開放社區發布公告:回收wx.getUserInfo接口獲取用戶授權的個人信息能力,更新為getUserProfile

2021年4月28日24時后發布的小程序新版本,無法通過wx.getUserInfo<button open-type="getUserInfo"/> 獲取用戶個人信息,需要使用getUserProfile 開發者每次通過該接口獲取用戶個人信息均需用戶確認。具體接口文檔:《getUserProfile接口文檔》

主要看兩點:即wx.getUserInfo接口的返回參數不變可以繼續使用獲取openidunionid,但開發者獲取的userInfo為匿名信息可以換為getUserProfile獲取用戶信息。

若之前已經寫好的授權登錄,僅需要把button中的getUserInfo 換為getUserProfile即可。如:

<view style="border-top: 1px solid #ccc;" v-if="isHide">
    <view class="headView">
        <view class="titleText">申請獲取以下權限</view>
        <view class="contentText">獲得你的信息(昵稱,頭像,地區及性別等)</view>
        <!-- canIUseGetUserProfile變量 判斷是否兼容使用getUserProfile -->
        <button v-if="canIUseGetUserProfile" class="authBtn" type="primary" @tap="getUserProfile">getUserProfile授權登錄</button>
        <button v-else class="authBtn" type="primary" open-type="getUserInfo" @getuserinfo="getUserInfoFn">getUserInfoFn授權登錄</button>
    </view>
</view>

注意:getUserProfile只是一個button方法,該接口只返回用戶個人信息,不包含用戶身份標識符!!!

獲取openid和unionid等還是需要wx.login和wx.getUserInfo接口。

(官方公告原話: 插件申請獲取用戶頭像昵稱與用戶身份標識符仍保留功能頁的形式,不作調整。用戶在用戶信息功能頁中授權之后,插件就可以直接調用 wx.login 和 wx.getUserInfo 。)

onLoad: function () {
    //查看是否兼容有getUserProfile接口
    if(uni.getUserProfile()){
        this.canIUseGetUserProfile = true
    }
},
methods: {
  //原來寫的getUserInfoFn不用刪去,繼續寫getUserProfile方法
  getUserProfile: function(e) {
    let that = this;
    // 推薦使用wx.getUserProfile獲取用戶信息,開發者每次通過該接口獲取用戶個人信息均需用戶確認
    // 開發者妥善保管用戶快速填寫的頭像昵稱,避免重復彈窗
    uni.getUserProfile({
        desc: '用戶登錄', // 聲明獲取用戶個人信息后的用途,后續會展示在彈窗中,請謹慎填寫
        success: (res) => {
            //用戶按了允許授權按鈕
            //授權成功后,通過改變 isHide 的值,讓實現頁面顯示出來,把授權頁面隱藏起來
            that.setData({
                isHide: false
            });
            that.getOpenIdTap();//這個方法里就是寫wx.login和wx.getUserInfo接口獲取到參數以及加密生成openid和unionid
        }
    })
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM