zhanghua
2024-09-19 8eff378710b7074fe7241c73f3975345ffe8959b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<template>
    <div class="center" v-loading.fullscreen="successLoading">
        <el-upload
            class="upload-demo"
            :action="action"
            :headers="headers"
            :data="{ staffId: staffId, orgId: orgId }"
            :on-exceed="handleExceed"
            :on-success="fileSuccess"
            :on-error="fileSuccess"
            :file-list="fileList"
            :show-file-list="true"
            multiple
            :limit="limit"
            accept=".xls,.xlsx"
            :before-upload="beforeAvatarUpload"
        >
            <slot></slot>
            <slot name="tip"></slot>
        </el-upload>
    </div>
</template>
 
<script>
 
export default {
    data() {
        return {
            msg: "拖动上传",
            successLoading: false,
            fileList: [],
            headers: {
                token: localStorage.getItem("token"),
            },
        };
    },
    props: {
        limit: {
            type: Number,
        },
        action: {
            type: String,
        },
        type: {
            type: String,
        },
        orgId: {
            type: Number,
        },
        staffId: {
            type: Number,
        },
    },
    created() {
 
    },
    methods: {
        // 超出文件上传个数回调
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件`);
        },
        // 移除文件弹出层
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${file.name}?`);
        },
        // 上传成功回调
        fileSuccess(res, f, fl) {
            this.successLoading = false
            this.$emit("fileSuccess", res, this.type);
        },
 
        // 过滤文件
        beforeAvatarUpload(file) {
            this.successLoading = true
            let fileName = file.name.substring(file.name.lastIndexOf(".") + 1);
            const extension = fileName === "xls";
            const extension2 = fileName === "xlsx";
            if (!extension && !extension2) {
 
                this.successLoading = false
                this.$message({
                    message: "上传文件只能是 xls、xlsx格式!",
                    type: "warning",
                });
                return false;
            }
        },
    },
    mounted() { },
};
</script>
 
<style lang="scss" scoped>
.center {
    float: left;
}
</style>