<template>
    <view class="upImg row justify-center items-center" @click="chooseImg">
        <u-icon name="plus" color="#5B5B5B" size="42"/>
    </view>
</template>
<script>
export default {
    props: [],
    components: { },
    data() {
        return {
            
        }
    },
    onLoad(options) {
        
    },
    onShow() {

    },
    mounted() {
       
    },
    methods: {
        chooseImg() {
            let that = this;
            uni.chooseImage({
                count: 1, //默认9
                sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ["album"], //从相册选择
                success: function (res) {
                    wx.getFileSystemManager().readFile({
                        filePath: res.tempFilePaths[0],
                        encoding: "base64", //编码格式
                        success: (ans) => {
                            uni.showLoading({
                                title: "上传中",
                            });
                            that.upFile(res.tempFilePaths[0], (uploadRes) => {
                                let BusinessLicense = JSON.parse(
                                    uploadRes.data
                                ).data;
                                let infor = {
                                    url: '',
                                    name: ''
                                }
                                const tempFilePath = BusinessLicense;
                                infor.name = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1);
                                infor.url = BusinessLicense
                                console.log('图片名称:');
                                console.log("infor", infor);
                                that.$emit('onSuccess', infor);
                            },err=>{
                                uni.hideLoading()
                            });
                                console.log("res.tempFilePaths[0]",res.tempFilePaths[0]);
                            
                        },
                    });
                },
            });
        },
        //图片上传
        upFile(filePath,resCall) {
            let that = this;
            let MallBaseId = uni.getStorageSync("mall_UserInfo").MallBaseId ? uni.getStorageSync("mall_UserInfo").MallBaseId : 1;
            let action = this.host2 + '/api/File/UploadTencent?MallBaseId=' + MallBaseId
            uni.uploadFile({
                url: action,
                filePath: filePath,
                name: 'file',
                formData: {
                    user: 'tesdt'
                },
                success: (uploadFileRes) => {
                    if (uploadFileRes.statusCode == 200) {
                        uni.hideLoading()
                        resCall(uploadFileRes);
                    }else{
                        wx.showToast({
                            title: uploadFileRes.message,
                            icon: 'none',
                            duration: 1000
                        })
                    }

                },
                fail: function(err) {
                    console.log(err)
                    wx.showToast({
                        title: '上传失败!'+JSON.stringify(err),
                        icon: 'none',
                        duration: 1000
                    })
                }
            });
        },
    }
}
</script>
<style lang="scss" scoped>
@import url("@/asset/css/flex.css");
.upImg{
    width: 208rpx;
    height: 208rpx;
    border-radius: 10rpx;
    border: 1rpx solid #ededed;
}
</style>
