微信小程式新版授權
阿新 • • 發佈:2018-12-09
最初微信小程式獲取許可權只需呼叫getUserInfo,便會彈出授權視窗
wx.getUserInfo({
success: function (res) {
that.setData({
nickName: res.userInfo.nickName,
avatarUrl: res.userInfo.avatarUrl,
})
},
})
現在的小程式不會自動彈出授權框,如果需要授權可以通過按鈕的形式手動引導使用者進行授權。
<button class='returnButton' wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">開始使用</button>
可以先在onload函式中獲取看是否已經授權,直接獲取使用者資訊
onLoad: function (e) { var that = this wx.getSetting({ success: function (res) { if (res.authSetting['scope.userInfo']) { // 已經授權,可以直接呼叫 getUserInfo 獲取頭像暱稱 wx.getUserInfo({ success: function (res) { app.globalData.userInfo = res.userInfo } }) } } })
如果沒有授權,可以通過繫結函式來授權
bindGetUserInfo: function (e) { var that = this; wx.getUserInfo({ success: function (res) { that.setData({ userInfo: res.userInfo }) app.globalData.userInfo = res.userInfo wx.navigateTo({ url: '' //成功時跳轉 }) } , fail: function(){ wx.navigateTo({ url: '' //失敗了跳轉 }) } }) if (app.globalData.userInfo == null){ if (e.detail.userInfo != null){ app.globalData.userInfo = e.detail.userInfo } } }