张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<template>
    <div class="upload-file">
        <transition-group name="el-zoom-in-center" class="upload-transition">
            <div
                v-for="item in fileList"
                :key="item.id"
                class="file-list"
                @mouseenter="getClose(item)"
                @mouseleave="setClose">
                <div :class="{close_icon:close == item.id,close_icons: close != item.id}">
                    <el-tooltip class="item" effect="dark" content="删除文件" placement="right" v-if="!disabled">
                        <i class="el-icon-delete" @click="delectFile(item)"></i>
                    </el-tooltip>
                </div>
                <img src="../assets/p.jpg" class="file-img" v-if="item.type == 'pdf'">
                <img src="../assets/ppt.png" class="file-img" v-if="item.type == 'pptx'||item.type == 'ppt'" >
                <img src="../assets/w.jpg" class="file-img" v-if="item.type == 'doc'||item.type == 'docx'">
                <img src="../assets/e.jpg" class="file-img" v-if="item.type == 'xls'||item.type == 'xlsx'">
                <img src="../assets/t.png" class="file-img" v-if="item.type == 'txt'">
                <el-tooltip class="item" effect="light" :content="item.name" placement="top">
                    <div class="img-text">{{item.name}}</div>
                </el-tooltip>
            </div>
            <el-upload
                :key="5762"
                ref="upload" 
                class="upload" 
                :action="action" 
                :http-request="requestUpload"
                :disabled="disabled"
                :show-file-list="false"
                :auto-upload="true" 
                :before-upload="beforeUpload"
                :multiple="false">
                <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </transition-group>
        <div class="upload-info">
            <slot name="info"></slot>
        </div>
    </div>
</template>
 
<script>
    // import SparkMD5 from 'spark-md5';
    export default {
        name: "UploadFile",
        props: {
            fileList: {
                type: Array,
                default: () => {
                    let list = [];
                    return list;
                }
            },
            disabled:Boolean,
            limit:Boolean
        },
        data() {
            return {
                action: "upload/uploadSliceFile",
                close: "",
                formData: null,
                fileMD5Str: "",
                MD5Str: "",
                loading: null,
                fileNum: 0,
                fileSize: 1048570,
            }
        },
        methods: {
            //出现删除样式
            getClose(item) {
                this.close = item.id
            },
            //去除删除样式
            setClose() {
                this.close = ""
            },
            //检查文件类型
            checkFileType(name) {
                let obj = {
                    "xls": true,
                    "xlsx": true,
                    "ppt": true,
                    "pptx": true,
                    "doc": true,
                    "docx": true,
                    "pdf": true,
                }
                return obj[name]
            },
            beforeUpload() {
                if(this.limit) {
                    if(this.fileList.length === 1) {
                        this.$message.warning("仅能上传一个文件!")
                        return false
                    }
                }
            },
            requestUpload(response) {
                const list = response.file.name.split('.');
                if(!this.checkFileType(list[list.length - 1])) {
                    this.$message.warning('请上传正确格式的文件!');
                    return false;
                }
                this.createLoadling()
                const formData = new FormData();
                formData.set('file', response.file);
                this.$api.System.uploadFileRequest(formData).then((res) => {
                    if(res.success) {
                        this.$message.success('上传成功!');
                        this.$nextTick(() => {
                        this.fileList.push({
                            id: this.fileList.length + 1,
                            type: list[list.length - 1],
                            name: response.file.name,
                            url: res.data,
                        })
                        })
                    } else {
                        this.$message.warning(res.statusMsg);
                    }
                    this.loading.close();
                }).catch(() => {
                    this.loading.close();
                    this.$message.error('请检查网络连接后重新上传!');
                })
            },
            // //自定义上传
            // requestUpload(file) {
            //     let type = file.file.name.split(".")
            //     let bol = this.checkFileType(type[type.length - 1])
            //     if(!bol) {
            //         this.$message.warning("请上传正确格式的文件")
            //         return false
            //     }
            //     this.createLoadling()
            //     this.formData = new FormData()
            //     this.computedFileMD5(file.file)
            // },
            // //计算总的MD5值
            // computedFileMD5(file) {
            //     let fileReader = new FileReader()
            //     fileReader.onload = (e) => {
            //         if(file.size != e.target.result.length) {
            //             this.$message.warning("请检查网络连接或稍后再试...")
            //             return false
            //         } else {
            //             this.fileMD5Str = SparkMD5.hashBinary(e.target.result)
            //             this.formData.set('fileName', file.name)
            //             this.formData.set('fileMD5Str', this.fileMD5Str)
            //         }
            //         this.sliceFile(file)
            //     }
            //     fileReader.readAsBinaryString(file)
            // },
            // //计算切片数量
            // sliceFile(file) {
            //     this.fileNum = Math.ceil(file.size / this.fileSize)
            //     for(let i = 0;i < this.fileNum;i++) {
            //         setTimeout(() => {
            //             this.computedFileitem(i, file)
            //         },2000 * i)
            //     }
            // },
            // //计算每一片的MD5值
            // computedFileitem(num, file) {
            //     let fileReader = new FileReader()
            //     let spark = new SparkMD5()
            //     fileReader.onload = (e) => {
            //         spark.append(e.target.result)
            //         if((num+1) === this.fileNum) {
            //             this.formData.set('isLast', true)
            //         } else {
            //             this.formData.set('isLast', false)
            //         }
            //         this.MD5Str = spark.end()
            //         this.formData.set('order', num)
            //         this.formData.set('MD5Str', this.MD5Str)
            //         //检查每片是否存在
            //         this.inspectFile()
            //     }
            //     let start = num * this.fileSize
            //     let end = (num * this.fileSize) + this.fileSize
            //     let files = file.slice(start, end)
            //     fileReader.readAsBinaryString(file.slice(start, end))
            //     this.formData.set('file', files)
            // },
            // //检查切片文件是否存在
            // async inspectFile() {
            //     const {data } = await this.$api.CHIEF.CHECKFILL({
            //         fileMD5Str: this.fileMD5Str,
            //         sliceFileMD5Str: this.MD5Str
            //     })
            //     return data;
            //     // .then(res => {
            //     //     if(res.sym === 'ok') {
            //     //         if(!res.data.sliceFileExists) {
            //     //             setTimeout(() => {
            //     //                 this.requestUploadFile()
            //     //             },1000)
            //     //         }
            //     //     } else {
            //     //         this.$message.warning("请检查网络连接或稍后再试...")
            //     //         this.loading.close()
            //     //     }
            //     // })
            //     // .catch(() => {
            //     //     this.$message.warning("请检查网络连接或稍后再试...")
            //     //     this.loading.close()
            //     // })
            // },
            // //上传每片文件
            // requestUploadFile() {
            //     this.$api.CHIEF.SLICEUPLOADFILE(this.formData).then(res => {
            //         if(res) {
            //             if(res.data !== null) {
            //                 if(res.data.uploadFileId) {
            //                     const arr = res.data.fileOriginalName.split('.')
            //                     this.fileList.push({
            //                         id: res.data.uploadFileId,
            //                         name: res.data.fileOriginalName,
            //                         type: arr[arr.length - 1]
            //                     })
            //                     this.loading.close()
            //                     this.$message.success("上传成功!")
            //                 }
            //             }
            //         } else {
            //             setTimeout(() => {
            //                 this.$message.warning("上传失败,请检查网络后在上传一次吧!")
            //                 this.loading.close()
            //             },2000)
            //         }
            //     })
            //     .catch(() => {
            //         setTimeout(() => {
            //             this.loading.close()
            //         },2000)
            //     })
            // },
            //删除列表文件
            delectFile(row) {
                this.fileList.splice(this.fileList.findIndex(item => item.id == row.id),1);
                this.$message.success("删除成功!")
                this.$emit('update:fileList',this.fileList);
            },
            //加载动画
            createLoadling() {
                this.loading = this.$loading({
                    lock: true,
                    text: '正在上传中....',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                })
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .upload-file {
        padding: 0 0 30px 0;
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        position: relative;
        
        .upload-transition {
            display: flex;
            flex-wrap: wrap;
        }
        
        .file-list {
            width: 100px;
            height: 100px;
            display: flex;
            flex-direction: column;
            margin: 10px 4px;
            border-radius: 6px;
            position: relative;
            justify-content: space-around;
            // background-color: #005CC5;
            
            .file-img {
                width: 100%;
                height: 100%;
            }
            
            .close_icon {
                width: 100%;
                height: 100%;
                position: absolute;
                cursor: pointer;
                display: flex;
                font-size: 30px;
                color: #dfdfdf;
                border-radius: 6px;
                align-items: center;
                justify-content: center;
                background-color: rgba(0,0,0,.5);
            }
            
            .close_icons {
                display: none;
            }
            
            .look-line {
                width: 2px;
                height: 30px;
                margin: 0 10px;
                background-color: #cccccc;
            }
        }
        
        .upload {
            width: 96px;
            height: 96px;
            display: flex;
            margin: 10px 4px;
            align-items: center;
            justify-content: center;
            border-radius: 4px;
            position: relative;
            border: 2px dashed #39B5FE;
        }
        
        .avatar-uploader-icon {
            font-size: 34px;
            color: #D8D8D8;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translateX(-50%) translateY(-50%);
        }
        
        .upload-info {
            color: #828D9D;
            position: absolute;
            left: 6px;
            bottom: -10px;
            white-space:nowrap;
            overflow: hidden;
            text-align: center;
            text-overflow: ellipsis;
        }
    }
    
    .img-text {
        width: 100%;
        height: 20px;
        line-height: 20px;
        font-size: 14px;
        position: absolute;
        white-space:nowrap;
        overflow: hidden;
        text-align: center;
        text-overflow: ellipsis;
        bottom: -20px;
        color: #dfdfdf;
    }
</style>