zhanghua
2024-09-18 fd989d2af98e1ba50d82bbe9db6ce0a24a074bbf
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
<template>
  <div class="center">
    <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: "拖动上传",
      fileList: [],
      headers: {
        token: localStorage.getItem("token"),
      },
    };
  },
  props: {
    limit: {
      type: Number,
    },
    action: {
      type: String,
    },
    type: {
      type: String,
    },
    orgId: {
      type: Number,
    },
    staffId: {
      type: Number,
    },
  },
  methods: {
    // 超出文件上传个数回调
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    // 移除文件弹出层
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`);
    },
    // 上传成功回调
    fileSuccess(res, f, fl) {
      this.$emit("fileSuccess", res, this.type);
    },
    
    // 过滤文件
    beforeAvatarUpload(file) {
      let fileName = file.name.substring(file.name.lastIndexOf(".") + 1);
      const extension = fileName === "xls";
      const extension2 = fileName === "xlsx";
      if (!extension && !extension2) {
        this.$message({
          message: "上传文件只能是 xls、xlsx格式!",
          type: "warning",
        });
        return false;
      }
    },
  },
  mounted() {},
};
</script>
 
<style lang="scss" scoped>
.center {
  float: left;
}
 
</style>