wx.getUserInfo(OBJECT) 注意:此接口有調整,使用該接口將不再出現授權彈窗,請使用
<button open-type="getUserInfo"></button>
引導用戶主動進行授權操作
當用戶未授權過,調用該接口將直接報錯 當用戶授權過,可以使用該接口獲取用戶信息
所以我們要使用上述button來請求用戶授權
1.index.wxml
<button
wx:if="{{canIUse}}"
open-type="getUserInfo"
bindgetuserinfo="bindGetUserInfo"
>授權登錄</button>
<view wx:else>請升級微信版本</view>
2.index.js
Page({
data: {
//判斷小程序的API,回調,參數,組件等是否在當前版本可用。
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function () {
// 查看是否授權
wx.getSetting({
success: function (res) {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: function (res) {
console.log(res.userInfo)
//用戶已經授權過
}
})
}
}
})
},
bindGetUserInfo: function (e) {
console.log(e.detail.userInfo)
if (e.detail.userInfo) {
//用戶按了允許授權按鈕
} else {
//用戶按了拒絕按鈕
}
}
})
注:如果未出現微信授權的彈窗,則可能是因爲之前授權的緩存導致的,因爲只有未授權纔會出現彈窗,清除緩存即可
參考:https://blog.csdn.net/weixin_...
https://blog.csdn.net/weidong...
https://www.cnblogs.com/legen...