上传图片(单个)
<view class="list disbetween" hover-class="hover" @click="chooseImg" style="height: 120rpx;">
<view class="list-til">头像</view>
<view class="disflex">
<image class="list-img" :src="avatar||'/static/image/avatar.png'" mode=""></image>
<u-icon name="arrow-right" color="#666666" size="28"></u-icon>
</view>
</view>
chooseImg() {
const that = this;
uni.chooseImage({
count: 1,
success: function(res) {
let tempFilePaths = res.tempFilePaths;
uni.uploadFile({
url: that.$http.baseUrl + 'api/upload',
filePath: tempFilePaths[0],
name: 'file',
header: {
'Authorization': 'Bearer ' + that.$tool.getCache('token'),
'lang': 'zh-CN'
},
success: (res) => {
console.log(res,"上传图片");
if(res.statusCode == 200){
let avatar = JSON.parse(res.data).data;
that.avatar = that.$http.baseUrl +'storage'+ JSON.parse(res.data).data;
that.$http.post('api/user/update_info',{avatar:avatar}).then(res=>{
console.log('更换头像',res);
if(res.status_code == 200){
uni.showToast({
title:'头像更换成功'
})
}else{
that.$tool.toast(res.message);
}
})
}else{
that.$tool.toast(res.message);
}
}
});
}
});
},