2021年4月13日后發布的小程序新版本,最新微信小程序授權獲取用戶信息(getUserInfo(淘汰) 替換 getUserProfile)后總結


今天花了一天時間,踩坑無數,特此總結

官方宣布2021年4月13日后發布的小程序新版本,無法通過wx.getUserInfo與< button open-type=“getUserInfo” />獲取用戶個人信息,以前的代碼,獲得的用戶信息,均為匿名信息。用 “微信用戶"代替了用戶的呢稱,看着用着都難受呀。

   4月13日后發布的新版本小程序,開發者調用wx.getUserInfo或< button open-type=“getUserInfo”/>將不再彈出彈窗,直接返回匿名的用戶個人信息,獲取加密后的openID、unionID數據的能力不做調整。

    如仍使用原來方式將會獲得紅色區域的匿名信息,即一切流程會非常順暢,使用wx.getUserInfo獲得也很順暢,但是獲得都是匿名信息
    若開發者調用wx.authorize接口請求scope.userInfo授權,用戶側不會觸發授權彈框,直接返回授權成功。即wx.authorize 只會獲得假性的授權成功。
    若開發者調用wx.getSetting接口請求用戶的授權狀態,會直接讀取到scope.userInfo為true

即wx.getSetting去判斷是否已授權獲取用戶信息也是假性的。
wx.login接口獲取的登錄憑證可在后台直接換取unionID,openid。開發者需要在獲取用戶的頭像昵稱信息,可調用wx.getUserProfile接口,開發者每次通過該接口均需用戶確認。

        wx.getUserProfile接口調用,花的時間很長,一開始,

wx.getUserProfile({
    desc:'獲取你的昵稱、頭像、地區及性別',
    success: (res) => {
        console.log('獲取用戶信息成功', res)
        //獲取用戶信息的各類操作
    }
})

沒任何反應,最后加上fail:部分,問題出現了

wx.getUserProfile({
    desc:'獲取你的昵稱、頭像、地區及性別',
    success: (res) => {
        console.log('獲取用戶信息成功', res)
        //獲取用戶信息的各類操作
    },
    fail: (res) =>{
        console.log('獲取用戶信息失敗',res)
        wx.showToast({
            title: '信息授權失敗~',
            duration: 1000,
            icon: 'error',
            mask: true
        })
    }
})

PC端小程序報錯wx.getUserProfile is not a function,值得注意的是新的接口wx.getUserProfile,只能使用catchtap或者bindtap進行調用(可以在wx.showmodel中使用),並不能再onload、onshow等位置直接調用,並且返回參數有所改變
調整 按鈕類型后此問題解決

但又出現了新的問題:

問題1. fail can only be invoked by user TAP gesture.
需要用戶手動確認才能通過驗證:

uni.showModal({
	title: '溫馨提示',
	content: '親,授權微信登錄后才能正常使用小程序功能',
	success(res) {
  }
}

此問題改來改去,就是一直出這個問題,最后換了個號,試一下,同樣的代碼,可以用,很時奇怪,查了很多原因,網上有的說版本,有的設置,等試了很多,沒用,最后發現,試過其它號,一行行代碼對過后,發現,都一樣,結果不一樣,這才想起重啟電腦,可以了

getUserProfile(e) {
    console.log(app.ggdata.userbzid)

    if (app.ggdata.userbzid > 0) {
      // api.myshowm('提示', '您已授權,無需重復操作,如沒顯示,可退出再重新進入即可。', '確定' ,showCancel: false);

      wx.showModal({
        title: '提示',

        content: '您已授權,無需重復操作,如沒顯示,可退出再重新進入即可',

        showCancel: false

      })

      return

    }

    else

    {
      wx.getUserProfile({
        lang:'zh_CN',

        desc: '獲取你的昵稱、頭像、地區及性別',

        success: res => {
          console.log(res);

          console.log(1);

          wx.login({
            success: function (loginres) {
              console.log(loginres)              

              // 下面開始調用注冊接口

              wx.request({
                url: api.apiPath.apireg,

                data: { 

                  code: loginres.code, 

                  encrypted_data: res.encryptedData, 

                  iv: res.iv 

                }, // 設置請求的 參數

                header: {
                  "Content-Type": "json"

                },

                success: (res) => {
                  wx.hideLoading();

                  console.log('注冊成功')

                }

              })

            }

          })

        },

        fail: res => {
          console.log(2);

          console.log(res)

          //拒絕授權

          wx.showToast({
            title: '您拒絕了請求,不能正常使用小程序',

            icon: 'error',

            duration: 2000

          });

          return;

        }

      });

    }

  },

問題2. fail desc length does not meet the requirements
接口字段有調整:desc

uni.getUserProfile({
	desc: '獲取你的昵稱、頭像、地區及性別',
	success: res => {
		console.log(res);
		console.log(1);
	}
}

特記:wx.login >>> code >>> 請求接口換取openid >>> openid >>> 自定義請求態 >>> uid
只是獲取用戶信息的地方發生改變了,獲取用戶信息必須通過wx.getUserProfile獲取

還有一種方式就是將調試基礎庫調到2.14版本

 "libVersion": "2.14",


免責聲明!

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



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