對於小程序未受權的用戶,官方取消wx.getUserInfo方法的直接調用,首次受權必須主動觸發自定義按鈕,纔可調起官方受權組件能夠獲取到的信息有:暱稱、頭像、性別、國家、省份、城市、性別、語言json
未受權顯示帶有button的自定義頁面,bindGetUserInfo會返回用戶信息,該按鈕會調用微信官方受權小程序
<button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">容許用戶受權</button>
app.js----我放在登錄方法以後微信
// 查看是否受權,保存受權狀態 wx.getSetting({ success: function(res) { if (res.authSetting['scope.userInfo']) { wx.setStorageSync('isAuthorize', 'true'); wx.getUserInfo({ success: function(res) { wx.setStorageSync('userInfo', res.rawData); } }) } else { wx.setStorageSync('isAuthorize', 'false'); } } })
main.wxml------項目主頁面app
<!-- 小程序受權組件 --> <authorize id="authorize"></authorize>
main.js------onload中進行判斷是否要顯示自定義的按鈕ide
// 已受權隱藏彈框,未受權顯示彈框 this.authorize = this.selectComponent("#authorize"); if (wx.getStorageSync('isAuthorize')=='true'){ this.authorize.hideDialog() }
main.json-----主頁面配置參數this
"usingComponents": { "authorize": "自定義受權組件的路徑" }
authorize.js------自定義帶有button的頁面/彈窗組件autiorize,這裏只貼出js部分code
/*authorize.js*/ Component({ options: { multipleSlots: true }, data: { isHide: false, canIUse: wx.canIUse('button.open-type.getUserInfo') }, methods: { //隱藏彈框 hideDialog() { this.setData({ isHide: true }) }, // 受權信息保存 bindGetUserInfo(e){ wx.setStorageSync('isAuthorize', 'true'); wx.setStorageSync('userInfo', JSON.stringify(e.detail.userInfo)); this.hideDialog() } } })
這樣整個受權就完成了!xml