对于小程序未受权的用户,官方取消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