1. 程式人生 > >小程式wx.getUserInfo不能彈出授權視窗後的解決方案

小程式wx.getUserInfo不能彈出授權視窗後的解決方案

微信更新api後,wx.getUserInfo在開發和體驗版本都不能彈出授權視窗。微信文件說明:

  1. 當用戶未授權過,呼叫該介面將直接報錯
  2. 當用戶授權過,可以使用該介面獲取使用者資訊

對此,給出以下解決方案。

wx.getUserInfo({ withCredentials: true, success: function (res) { //此處為獲取微信資訊後的業務方法 }, fail: function () { //獲取使用者資訊失敗後。請跳轉授權頁面 wx.showModal({ title:
'警告', content: '尚未進行授權,請點選確定跳轉到授權頁面進行授權。', success: function (res) { if (res.confirm) { console.log('使用者點選確定') wx.navigateTo({ url: '../tologin/tologin', }) } } }) } })

調取該方法失敗後跳轉到授權頁面。

授權頁面加入

<button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授權登入</button>

並在js中,加入這個方法

bindGetUserInfo: function(e){ var that = this; //此處授權得到userInfo console.log(e.detail.userInfo); //接下來寫業務程式碼
//最後,記得返回剛才的頁面 wx.navigateBack({ delta: 1 }) }

至此,即可完成引導使用者手動授權的過程。解決此次更新api所帶來的問題。