在wxml中:
<view>当前用户名:{{name}}</view>//这个{{name}}是data中的name
<view>当前用户头像:
<image src="{{head_path}}"></image>
</view>
<view bindtap="GetuserInfo">获取当前用户信息</view>
在js中:
data:{
name:"微信123",
head_path:"../../picture/head_portrait/default.png" //图片路径
},
GetuserInfo(){
var that = this;
wx.getUserProfile({
desc:'您的信息仅作为个人展示噢',
success:(res)=>{
console.log('获取用户信息成功',res)
//获取用户信息的操作,这里就可以把data中的name和头像改为微信用户名及头像
that.setData({name:res.userInfo.nickName,
head_path:res.userInfo.avatarUrl});
},
fail:(res)=>{
console.log('获取用户信息失败',res)
wx.showToast({
title:'信息授权失败~',
duration:1000,
icon:'error',
mask:true
})
}})},