在此之前,小程序获取微信的头像,昵称之类的用户信息,我用的都是wx.getUserInfo,例如:
onLoad: function (options) { var that = this; //获取用户信息 wx.getUserInfo({ success: function (res) { console.log(res); that.data.userInfo = res.userInfo; that.setData({ userInfo: that.data.userInfo }) } })},
wx.getUserInfo需要用户授权 scope.userInfo,也就是那个授权弹窗。
但是!!!重点来了,自从微信接口有了新的调整之后 这个wx.getUserInfo()便不再出现授权弹窗了,需要使用button做引导~
!--wxml--!-- 需要使用 button 来授权登录 --button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo"授权登录/buttonview wx:else请升级微信版本/view
//jsPage({ data: { canIUse: wx.canIUse('button.open-type.getUserInfo') }, onLoad: function() { // 查看是否授权 wx.getSetting({ success: function(res){ if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success: function(res) { console.log(res.userInfo) } }) } } }) }, bindGetUserInfo: function(e) { console.log(e.detail.userInfo) }})
这就是等于一步变两步了~现在用button的话 可以在产品上多加引导,不会显得那么突兀的出来一个弹窗了
然鹅,如果你仅仅只是想展示用户信息的话,那便使用open-data 吧,如下:
!-- 如果只是展示用户头像昵称,可以使用 open-data / 组件 --open-data type="userAvatarUrl"/open-dataopen-data type="userNickName"/open-data
只需要这两行wxml的代码,便可展示微信昵称和头像,是不是很惊喜!