其实获取用户信息,一种是获取权限的,一种是不用获取权限,前者获取到的信息更多,包含一些敏感信息,包括给getaccessToken接口须要传的参数,后者就是简单获取一些头像、昵称等信息,具体可参考文档https://developers.weixin.qq.com/miniprogram/dev/component/open-data.htmljavascript
跟上一篇获取手机号权限方式同样的,都是经过button按钮触发,调用权限弹框:html
代码:java
<button open-type="getUserInfo" bindgetuserinfo="onGetUserInfo">登陆获取我的信息权限</button>
onGetUserInfo(e) { if (e.detail.userInfo !== undefined) { // 容许 app.loginfn() } else { // 拒绝 } }
若是用户容许的话,就能够调用获取用户信息的接口wx.getUserInfo:app
wx.login({ success: resCode => { // 发送 res.code 到后台换取 openId that.globalData.code = resCode.code wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) {// 已经受权 wx.getUserInfo({ success: rsl => { // 获取用户信息后的操做 } }) } } }) } })
<open-data type="userAvatarUrl"></open-data> <open-data type="userNickName"></open-data
详细参考文档:https://developers.weixin.qq.com/miniprogram/dev/component/open-data.htmlspa