WXL
5 天以前 631c8f37b449b09d19345b76400a39abdb7800f6
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<template>
  <div class="upload-attachment">
    <el-upload
      ref="upload"
      :action="uploadAction"
      :headers="headers"
      :file-list="fileList"
      :auto-upload="false"
      :multiple="true"
      :limit="limit"
      :accept="accept"
      :on-exceed="handleExceed"
      :on-change="handleFileChange"
      :on-remove="handleFileRemove"
      :on-success="handleUploadSuccess"
      :on-error="handleUploadError"
      :before-upload="beforeUpload"
    >
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button
        style="margin-left: 10px;"
        size="small"
        type="success"
        @click="submitUpload"
        :disabled="fileList.length === 0"
      >
        上传到服务器
      </el-button>
      <div slot="tip" class="el-upload__tip">
        请上传 {{ accept }} 格式的文件,单个文件不超过10MB,最多可上传{{ limit }}个文件
      </div>
    </el-upload>
  </div>
</template>
 
<script>
import { getToken } from "@/utils/auth";
 
export default {
  name: "UploadAttachment",
  props: {
    // 文件列表
    fileList: {
      type: Array,
      default: () => []
    },
    // 上传数量限制
    limit: {
      type: Number,
      default: 10
    },
    // 接受的文件类型
    accept: {
      type: String,
      default: ".pdf,.jpg,.jpeg,.png,.doc,.docx,.xls,.xlsx"
    },
    // 上传地址
    uploadUrl: {
      type: String,
      default: process.env.VUE_APP_BASE_API + "/common/upload"
    }
  },
  data() {
    return {
      uploadAction: process.env.VUE_APP_BASE_API + "/common/upload",
      headers: {
        Authorization: "Bearer " + getToken()
      }
    };
  },
  methods: {
    /** 文件选择变化 */
    handleFileChange(file, fileList) {
      this.$emit('change', fileList);
    },
 
    /** 文件移除 */
    handleFileRemove(file, fileList) {
      this.$emit('change', fileList);
      this.$emit('remove', file);
    },
 
    /** 上传成功处理 */
    handleUploadSuccess(response, file, fileList) {
      if (response.code === 200) {
        file.url = response.data || response.url;
        this.$message.success('文件上传成功');
        this.$emit('upload-success', {
          file,
          fileList,
          response
        });
      } else {
        this.$message.error(response.msg || '文件上传失败');
      }
    },
 
    /** 上传错误处理 */
    handleUploadError(err, file, fileList) {
      console.error('上传失败:', err);
      this.$message.error('文件上传失败,请重试');
      this.$emit('upload-error', {
        file,
        fileList,
        error: err
      });
    },
 
    /** 上传前校验 */
    beforeUpload(file) {
      // 检查文件类型
      const allowedTypes = this.accept.split(',').map(type => type.trim().toLowerCase());
      const fileExtension = '.' + file.name.split('.').pop().toLowerCase();
 
      if (!allowedTypes.includes(fileExtension) && !allowedTypes.includes(fileExtension.substring(1))) {
        this.$message.error(`不支持的文件格式: ${fileExtension}`);
        return false;
      }
 
      // 检查文件大小 (10MB限制)
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isLt10M) {
        this.$message.error('文件大小不能超过 10MB!');
        return false;
      }
 
      return true;
    },
 
    /** 文件数量超出限制 */
    handleExceed(files, fileList) {
      this.$message.warning(`最多只能上传 ${this.limit} 个文件,当前选择了 ${files.length} 个文件`);
    },
 
    /** 手动提交上传 */
    submitUpload() {
      if (this.fileList.length === 0) {
        this.$message.warning('请先选择要上传的文件');
        return;
      }
 
      this.$refs.upload.submit();
    },
 
    /** 清空文件列表 */
    clearFiles() {
      this.$refs.upload.clearFiles();
    },
 
    /** 获取当前文件列表 */
    getFileList() {
      return this.fileList;
    }
  }
};
</script>
 
<style scoped>
.upload-attachment {
  width: 100%;
}
 
::v-deep .el-upload {
  display: block;
}
 
::v-deep .el-upload__tip {
  margin-top: 8px;
  font-size: 12px;
  color: #909399;
}
</style>