HTML
<view>
<view class='headView'>
<view class='headImageView'>
</view>
<button bindtap="getUserProfile" wx:if="{{ showInfo}}"> 获取头像昵称 </button>
<image bindtap="bindViewTap" class="userinfo-avatar" src="/image/8482661.jpg" mode="cover" wx:if="{{ pic }}" style="width: 200rpx;height: 200rpx;padding-left:40% ;"></image>
<!-- <button class='authBtn' type='primary' open-type='getUserInfo' bindgetuserinfo='auth' wx:if="{{shoeAuth}}">授权登录</button> -->
<view class='titleText' style="text-align: center;">{{ userInfo.nick_name }}</view>
</view>
</view>
JS
getUserProfile(e) {
wx.getUserProfile({
success: (res) => {
// console.log(res);
let nick_name = res.userInfo.nickName;
let avatar_url = res.userInfo.avatarUrl;
let that =this;
wx.login({
//获取code 来与服务器交换 获取openid
success(ret){
// console.log(ret)
let code = ret.code;
wx.request({
url: 'http://www.week3.com/login',
data:{
code:code,
nick_name:nick_name,
},
success(e){
// console.log(e.data.data)
let uid=e.data.data;
//将用户信息保存到缓存里面
wx.setStorage({
key:"uid",
data:uid
})
wx.showToast({
title: '登录成功!',
})
that.setData({
userInfo:e.data.data,
showInfo:false,
pic:true
})
},
fail(res){
wx.showToast({
title: '请求失败',
icon:'error'
})
}
})
}
})
},
fail(res){
wx.showToast({
title: '授权失败',
icon:"error"
})
}
})
}