微信更新api後,wx.getUserInfo在開發和體驗版本都不能彈出授權窗口。微信文檔說明:
注意:此接口有調整,使用該接口將不再出現授權彈窗,請使用 <button open-type="getUserInfo"></button> 引導用戶主動進行授權操作。
調用接口函數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所帶來的問題。
原文鏈接:https://blog.csdn.net/xishuchen0520/article/details/80410861