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);
                    }
                }
            });
        }
    });
},
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值