下面我们介绍一种官方推荐的获取用户信息的方法
- wxml
<view>当前用户名:{{name}}</view> <view> 当前用户头像: <image src="{{path}}" style="200rpx;height:200rpx"></image> </view> <button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
- wxjs
Page({ data: { name: "", path: "/static/default.png" }, bindGetUserInfo: function () { var that = this; wx.getUserInfo({ success: function (res) { that.setData({ name: res.userInfo.nickName, path: res.userInfo.avatarUrl, }) }, fail: function (res) { console.log('fail', res) } }) }, })