el-upload 二次封装

使用场景:
导入或上传附件使用
代码如下:

<template>
    <div style="display: inline-block; margin-left: 10px">
        <el-upload
            class="upload-demo"
            :action="actionUrl"
            :file-list="files"
            :headers="header"
            :show-file-list="showFileList"
            :accept="accept"
            :limit="limit"
            :before-upload="beforeAvatarUpload"
            :on-remove="remove"
            :before-remove="beforeRemove"
            ref="fileUp"
            :auto-upload="autoUpload"
            :multiple="multiple"
            :disabled="disabled"
            :on-success="success"
            :on-error="error"
            :data="fileParams || {}"
            :on-change="changeFile"
        >
            <el-tooltip placement="bottom" effect="light">
                <div slot="content">
                    附件格式支持:{{ accept }}
                    <br />附件大小限制:单个文件不超过50MB
                </div>
                <el-button type="primary">{{btnName || '导入'}}</el-button>
            </el-tooltip>
        </el-upload>
        <Message NoCancelBtn ref="message" :msg="msg">  
            <div v-if="msg.title">{{ msg.title }}</div>
            <p v-for="item in msg.message" :key="item">{{ item }}</p>
        </Message>
    </div>
</template>

<script>
import {mapState} from 'vuex'
import req from '@/request.js'
import {Base64} from 'js-base64'
import Message from '@/components/investCommon/message.vue'
export default {
    components: {Message},
    // props: ['limit', 'verify', 'filesList', 'btnName', 'fileData'],
    props: {
        limit:{
            type:Number,
            default:9999999,
        },
        verify:{
            type:String,
            default:""
        },
        filesList:{
            type:Array,
            default:()=>[]
        },
        value:{
            type:Array,
            default:()=>[]
        },
        btnName:{
            type:String,
            default:"选择文件"
        },
        fileData:{
            type:Object,
            default:{}
        },
        autoUpload:{
            type:Boolean,
            default:true
        },
        url:{
            type:String,
            default:""
        },
        disabled:{
            type:Boolean,
            default:false
        },
        showFileList:{
            type:Boolean,
            default:true
        },
        multiple:{
            type:Boolean,
            default:false
        },
    },
    data() {
        return {
            src: '',
            fileId: '',
            fileTree: '',
            files: [],
            uploadingFiles: {},
            list: [],
            isChecking: true,
            accept: '.xls,.xlsx',
            num: 0,
            msg: {
                title: '',
                message: [],
            },
            formAdd: {},
            fileParams: {},
            loading: false
        }
    },
    computed: mapState({
        header: (state) => {
            if (state.login && state.login.currentUser) {
                return {
                    Authorization: `Bearer ${state.login.currentUser.token}`,
                }
            }
            return {}
        },
        actionUrl: function () {
            // return window.context.invest + '/fileView/importData'
            return window.context.invest + this.url
        },
    }),
    watch: {
        fileData: {
            handler: function(value) {
               this.fileParams.requestParam = JSON.stringify(value);
            }
        },
        value: {
            immediate: true,
            deep: true,
            handler: function (newValue) {
                if(!newValue){
                    this.files =[];
                }else{
                    this.files = newValue;
                }
            },
        },
        
    },
    mounted() {
        
    },
    methods: {
        remove(file, fileList) {
            this.files = fileList;
            this.$emit('input', this.files)
        },
        beforeRemove(file, fileList) {
            // if (this.isChecking) {
            //     return this.$confirm(`确定移除 ${file.name}?`).then(() => {
            //     this.num = 0
            //     })
            // } else {
            //     this.num = 0
            // }
        },
        error(response, file, fileList) {
            this.files=fileList;
            let errorData = `导入失败:${response.message}` ;
            this.$messageTips({ 
                width:"50%",
                message:errorData,
            })
            delete this.uploadingFiles[Base64.encode(file.name)]
        },
        beforeAvatarUpload(file) {
            // 文件类型限制
            const name = file.name ? file.name : ''
            const ext = name
                ? name.substr(name.lastIndexOf('.') + 1, name.length)
                : true
            const isExt = this.accept.indexOf(ext) < 0
            this.isChecking = !isExt
            if (isExt) {
                if (this.accept == 'false') {
                    this.$message.error('请配置可上传文件的格式!')
                } else {
                    this.$message.error(
                        '上传的附件只能是 ' + this.accept + '格式!'
                    )
                }
                return !isExt
            }
            this.uploadingFiles[Base64.encode(name)] = 'eror'
           
        },
        success(res,file, fileList) {
            if (res.state) {
                if(this.limit===1 && this.autoUpload){//上传成功并清空附件列表
                    this.files=[];
                    this.$emit('input', this.files)
                }
                this.$emit('successCallback',res)
                this.$message.success(res.message)
            } else {
                fileList.pop()
                this.files=fileList;
                this.$emit('input', this.files)
                this.$emit('errorCallback',res);
                let errorData = `导入失败:${res.message}`;
                this.$messageTips({ 
                    width:"50%",
                    message:errorData,
                })
            }
        },
        changeFile(file, fileList) {
            this.$nextTick(()=>{
                this.$emit('input', fileList)
            })
            
        },
        handleSubmitFile(form) {
            this.$nextTick(() => {
                this.$refs.fileUp.submit()
                    
            })
        }
    },
}
</script>
<style lang="scss" scoped>
.upload-demo {
    max-width: 570px;
    margin-top: 5px;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值