微信小程序> 微信小程序获取用户信息的两种方法wx.getUserInfo&opendata

微信小程序获取用户信息的两种方法wx.getUserInfo&opendata

浏览量:6135 时间: 来源:_zengZeng

在此之前,小程序获取微信的头像,昵称之类的用户信息,我用的都是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的代码,便可展示微信昵称和头像,是不是很惊喜!

 

 

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎