獲取用戶信息與其他授權不太一樣,其他授權會自動彈出對話框,請求獲取用戶授權,但獲取用戶信息不回,需要引導用戶點擊授權按鈕同意授權獲取用戶基本信息
1、前端頁面要有授權登錄按鈕
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" > 授權登錄 </button> <view wx:else>請升級微信版本</view>
2、數據canIUse
data: { canIUse: wx.canIUse('button.open-type.getUserInfo') },
3、如果用戶已經授權則直接顯示用戶信息,不在顯示授權按鈕
onLoad: function (options) { // 查看是否授權 wx.getSetting({ success(res) { if (res.authSetting['scope.userInfo']) { // 已經授權,可以直接調用 getUserInfo 獲取頭像昵稱 wx.getUserInfo({ success(res) { console.log(res.userInfo) } }) } } }) },
4、沒有顯示授權按鈕引導用戶點擊獲取
bindGetUserInfo:function(e){ console.log(e.detail.userInfo) },