zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<template>
    <div class="upload-file-wrapper">
        <!-- 上传文件组件 -->
        <uploader class="uploader-app" ref="uploader" :options="options" :autoStart="false" :fileStatusText="fileStatusText"
            @files-added="handleFilesAdded" @file-success="handleFileSuccess" @file-error="handleFileError">
            <uploader-unsupport></uploader-unsupport>
            <!-- 选择按钮 在这里隐藏 -->
            <uploader-btn class="select-file-btn" :attrs="attrs" ref="uploadBtn">选择文件</uploader-btn>
            <uploader-btn class="select-file-btn" :attrs="attrs" :directory="true" ref="uploadDirBtn">选择目录</uploader-btn>
 
        </uploader>
        <!-- <el-dialog title="提示" style="z-index: 30;" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <span>这是一段信息</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span>
        </el-dialog> -->
    </div>
</template>
 
<script>
import store from '@/store/index.js'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import SparkMD5 from 'spark-md5'
import Vue from 'vue'
export default {
    data() {
        return {
            dialogVisible: false,
            // 上传组件配置项
            options: {
                target: `${window._CONFIG['domianURL']}/document/compareFile`, // 上传文件-目标 URL
                chunkSize: 1024 * 1024, //  每个分片的大小
                fileParameterName: 'file', //  上传文件时文件的参数名,默认 file
                maxChunkRetries: 3, //  并发上传数,默认 3
                testChunks: false, //  是否开启分片已存在于服务器的校验
                // 服务器分片校验函数,秒传及断点续传基础
                checkChunkUploadedByResponse: function (chunk, message) {
                    //    console.log("chunk:::", chunk);
                    //    console.log("message:::", message);
                    let objMessage = JSON.parse(message)
                    if (objMessage.success) {
                        let data = objMessage.result
                        if (data.skipUpload) {
                            // 分片已存在于服务器中
                            return true
                        }
                        return (data.uploaded || []).indexOf(chunk.offset + 1) >= 0
                    } else {
                        console.log(objMessage.message)
                        return true
                    }
                },
                headers: {
                    'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)
                },
                query() { }
            },
            // 文件状态文案映射
            fileStatusText: {
                success: '上传成功',
                error: 'error',
                uploading: '上传中',
                paused: '暂停中',
                waiting: '等待中'
            },
            attrs: {
                accept: '*'
            },
            panelShow: false, //  上传文件面板是否显示
            collapse: false, //    上传文件面板是否折叠
            dropBoxShow: false, //  拖拽上传是否显示
            // 粘贴图片的信息
            pasteImg: {
                src: '',
                name: ''
            },
            pasteImgObj: null, //  粘贴图片 File 对象
            filesLength: 0 //  上传的文件个数
        }
    },
    computed: {
        // Uploader    上传组件实例
        uploaderInstance() {
            return this.$refs.uploader.uploader
        },
        // 剩余存储空间
        remainderStorageValue() {
            return store.getters.remainderStorageValue
        }
    },
    methods: {
        handleClose(done) {
            this.$confirm('确认关闭?')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        /**
         * 上传组件预处理
         */
        handlePrepareUpload() {
            console.log("上传组件预处理");
            this.options.headers['X-Access-Token'] = Vue.ls.get(ACCESS_TOKEN)
            console.log("this.uploadWay", this.uploadWay);
            switch (this.uploadWay) {
                case "1": {
                    console.log("this.$refs.uploadBtn", this.$refs.uploadBtn);
                    this.$refs.uploadBtn.$el.click()
                    break
                }
                case "2": {
                    this.$refs.uploadDirBtn.$el.click()
                    break
                }
                case "3": {
                    this.pasteImg.src = ''
                    this.pasteImg.name = ''
                    this.pasteImgObj = null
                    this.dropBoxShow = true
                    break
                }
            }
        },
 
        /**
         * 添加批量文件的回调函数
         * @description 对单个或批量文件都按此逻辑处理
         * @param {object} files 批量文件信息
         */
        handleFilesAdded(files) {
            console.log("批量添加文件::::", files);
            // 批量选择的文件的总体大小
            const filesTotalSize = files
                .map((item) => item.size)
                .reduce((pre, next) => {
                    return pre + next
                }, 0)
            if (this.remainderStorageValue < filesTotalSize) {
                // 批量选择的文件超出剩余存储空间
                this.$message.warning(
                    `剩余存储空间不足,请重新选择${files.length > 1 ? '批量' : ''}文件`
                )
                // https://github.com/simple-uploader/vue-uploader/blob/master/README_zh-CN.md#%E4%BA%8B%E4%BB%B6
                files.ignored = true //  本次选择的文件过滤掉
            } else {
 
                // 批量或单个选择的文件未超出剩余存储空间,正常上传
                this.filesLength += files.length
                files.forEach((file) => {
                    this.dropBoxShow = false
                    this.panelShow = true
                    this.collapse = false
                    this.computeMD5(file)
                })
            }
        },
        /**
         * 文件上传成功 回调函数
         * @param {object} rootFile 成功上传的文件所属的根 Uploader.File 对象,它应该包含或者等于成功上传文件
         * @param {object} file 当前成功的 Uploader.File 对象本身
         * @param {string} response 服务端响应内容,永远都是字符串
         */
        handleFileSuccess(rootFile, file, response) {
            if (response == '') {
                file.statusStr = '上传失败'
                this.callback(false)
                return
            }
 
            let result = JSON.parse(response)
            console.log(`output->result`, result)
            if (result.success) {
                // this.dialogVisible = true;
                store.commit('changeCompareModal', true)
                store.commit('addCompareList', result.result)
                file.statusStr = ''
                if (this.filesLength === 1) {
 
                    // 本次所有的文件均已上传完毕
                    this.$message.success(`对比完毕`)
                    // callType 调用此服务的方式:1 - 顶部栏,2 - 右键菜单
                    // if (this.callType === 1) {
                    //     this.serviceEl.$emit('reloadPathTree')
                    //     //this.serviceEl.$emit('searchFileList')
                    // } else {
                    //     this.serviceEl.$emit('reloadPathTree')
                    //     //this.serviceEl.getTableDataByType()
                    // }
                    //this.serviceEl.$store.dispatch('showStorage')
                    this.callback(true)
                }
            } else {
                this.$message.error(result.message)
                file.statusStr = '上传失败'
            }
            this.filesLength--
        },
        /**
         * 文件上传失败 回调函数
         * @param {object} rootFile 成功上传的文件所属的根 Uploader.File 对象,它应该包含或者等于成功上传文件
         * @param {object} file 当前成功的 Uploader.File 对象本身
         * @param {string} response 服务端响应内容,永远都是字符串
         */
        handleFileError(rootFile, file, response) {
            this.$message({
                message: response,
                type: 'error'
            })
        },
        /**
         * 计算md5,实现断点续传及秒传
         * @param {object} file 文件信息
         */
        computeMD5(file) {
            let fileReader = new FileReader()
            let blobSlice =
                File.prototype.slice ||
                File.prototype.mozSlice ||
                File.prototype.webkitSlice
            let currentChunk = 0
            const chunkSize = 1 * 1024 * 1024
            let chunks = Math.ceil(file.size / chunkSize)
            let spark = new SparkMD5.ArrayBuffer()
            // 文件状态设为"计算MD5"
            file.statusStr = '计算MD5'
            file.pause()
            loadNext()
            fileReader.onload = (e) => {
                spark.append(e.target.result)
                if (currentChunk < chunks) {
                    currentChunk++
                    loadNext()
                    // 实时展示MD5的计算进度
                    file.statusStr = `校验MD5 ${((currentChunk / chunks) * 100).toFixed(
                        0
                    )}%`
                } else {
                    let md5 = spark.end()
                    this.calculateFileMD5End(md5, file)
                }
            }
            fileReader.onerror = function () {
                this.$notify({
                    title: '错误',
                    message: `文件${file.name}读取出错,请检查该文件`,
                    type: 'error',
                    duration: 2000
                })
                file.cancel()
            }
            function loadNext() {
                let start = currentChunk * chunkSize
                let end = start + chunkSize >= file.size ? file.size : start + chunkSize
                fileReader.readAsArrayBuffer(blobSlice.call(file.file, start, end))
            }
        },
        /**
         * 文件MD5计算结束
         * @param {string} md5 文件 MD5 值
         * @param {object} file 文件对象
         */
        calculateFileMD5End(md5, file) {
            // 将自定义参数直接加载uploader实例的opts上
            Object.assign(this.uploaderInstance.opts, {
                query: {
                    ...this.params
                }
            })
            file.uniqueIdentifier = md5
            file.resume()
            // 移除自定义状态
            file.statusStr = ''
        },
        /**
         * 关闭上传面板,并停止上传
         */
        handleClosePanel() {
            this.uploaderInstance.cancel()
            this.panelShow = false
            this.callback('cancel')
        }
    }
}
</script>
 
<style lang="less" scoped>
@import '~@assets/file/less/varibles.less';
@import '~@assets/file/less/mixins.less';
 
.upload-file-wrapper {
    position: fixed;
    z-index: 20;
    right: 16px;
    bottom: 16px;
 
    .drop-box {
        position: fixed;
        z-index: 19;
        top: 0;
        left: 0;
        border: 5px dashed #8091a5 !important;
        background: #ffffffd9;
        color: #8091a5 !important;
        text-align: center;
        box-sizing: border-box;
        height: 100%;
        line-height: 100%;
        width: 100%;
 
        .text {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 100%;
            transform: translate(-50%, -50%);
            font-size: 30px;
        }
 
        .upload-icon {
            position: absolute;
            right: 176px;
            top: 16px;
            cursor: pointer;
 
            &:hover {
                color: @Primary;
            }
        }
 
        .delete-icon {
            position: absolute;
            right: 80px;
            top: 16px;
            cursor: pointer;
 
            &:hover {
                color: @Danger;
            }
        }
 
        .close-icon {
            position: absolute;
            right: 16px;
            top: 16px;
            cursor: pointer;
 
            &:hover {
                color: @Success;
            }
        }
 
        .paste-img-wrapper {
            width: 100%;
            height: 100%;
        }
 
        .paste-img {
            margin-top: 16px;
            max-width: 90%;
            max-height: 80%;
        }
 
        .paste-name {
            height: 24px;
            line-height: 24px;
            font-size: 18px;
            color: @PrimaryText;
        }
    }
 
    .uploader-app {
        width: 560px;
    }
 
    .file-panel {
        background-color: #fff;
        border: 1px solid #e2e2e2;
        border-radius: 7px 7px 0 0;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
 
        .file-title {
            display: flex;
            height: 40px;
            line-height: 40px;
            padding: 0 15px;
            border-bottom: 1px solid #ddd;
 
            .title-span {
                padding-left: 0;
                margin-bottom: 0;
                font-size: 16px;
 
                .count {
                    color: @SecondaryText;
                }
            }
 
            .operate {
                flex: 1;
                text-align: right;
 
                /deep/ .el-button--text {
                    color: @PrimaryText;
 
                    i[class^=el-icon-] {
                        font-weight: 600;
                    }
 
                    &:hover {
 
                        .el-icon-full-screen,
                        .el-icon-minus {
                            color: @Success;
                        }
 
                        .el-icon-close {
                            color: @Danger;
                        }
                    }
                }
            }
        }
 
        .file-list {
            position: relative;
            height: 240px;
            overflow-x: hidden;
            overflow-y: auto;
            background-color: #fff;
            font-size: 12px;
            list-style: none;
            padding: 0px;
            .setScrollbar(6px, #EBEEF5, #C0C4CC);
 
            .file-item {
                position: relative;
                background-color: #fff;
                width: 558px;
 
 
                /deep/ .uploader-file {
                    height: 40px;
                    line-height: 40px;
 
                    .uploader-file-progress {
                        border: 1px solid @Success;
                        border-right: none;
                        border-left: none;
                        background: #e1f3d8;
                    }
 
                    .uploader-file-name {
                        width: 44%;
                    }
 
                    .uploader-file-size {
                        width: 16%;
                    }
 
                    .uploader-file-meta {
                        display: none;
                    }
 
                    .uploader-file-status {
                        width: 30%;
                        text-indent: 0;
                    }
 
                    .uploader-file-actions>span {
                        margin-top: 12px;
                    }
                }
 
                /deep/ .uploader-file[status='success'] {
                    .uploader-file-progress {
                        border: none;
                    }
                }
            }
 
            .file-item.custom-status-item {
                /deep/ .uploader-file-status {
                    visibility: hidden;
                }
 
                .custom-status {
                    position: absolute;
                    top: 0;
                    right: 10%;
                    width: 24%;
                    height: 40px;
                    line-height: 40px;
                }
            }
        }
 
        &.collapse {
            .file-title {
                background-color: #E7ECF2;
            }
        }
    }
 
    .no-file {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-size: 16px;
    }
 
    /deep/.uploader-file-icon {
        display: none;
    }
 
    /deep/.uploader-file-actions>span {
        margin-right: 6px;
    }
}
 
/* 隐藏上传按钮 */
.select-file-btn {
    display: none;
}
</style>