uniapp 上传图片(单个)

上传图片(单个)

<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, //默认9
        // sizeType: ['original'], //, original 原图,compressed 压缩图,默认二者都有
        //sourceType: [], //从相册选择 'album' camera 
        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){
    
    
                        // if (JSON.parse(res.data).code == 500) {
    
    
                        // 	that.$tool.toast('请上传小于2M的图片')
                        // 	return
                        // }
                        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);
                    }
                }
            });
        }
    });
},

猜你喜欢

转载自blog.csdn.net/m0_52459016/article/details/122099134