zhaojs
2023-05-16 9707c4410a67d6a6d63b634f53565711d2d337da
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<template>
    <view class="u-upload" v-if="!disabled">
        <view
            v-if="showUploadList"
            class="u-list-item u-preview-wrap"
            v-for="(item, index) in lists"
            :key="index"
            :style="{
                width: width + 'rpx',
                height: width + 'rpx'
            }"
        >
            <view
                v-if="deletable"
                class="u-delete-icon"
                @tap.stop="deleteItem(index)"
                :style="{
                    background: delBgColor
                }"
            >
                <u-icon class="u-icon" :name="delIcon" size="20" :color="delColor"></u-icon>
            </view>
            <u-line-progress
                v-if="showProgress && item.progress > 0 && !item.error"
                :show-percent="false"
                height="16"
                class="u-progress"
                :percent="item.progress"
            ></u-line-progress>
            <view @tap.stop="retry(index)" v-if="item.error" class="u-error-btn">点击重试</view>
            <image @tap.stop="doPreviewImage(item.url || item.path, index)" class="u-preview-image" v-if="$jsutl.isImageOrVideo(item.url || item.path) == 0" :src="item.url || item.path" :mode="imageMode"></image>
            <video :src="item.url || item.path" class="u-preview-image" :controls="false"  v-if="$jsutl.isImageOrVideo(item.url || item.path) == 1">
                <cover-image class="controls-delete img" @click="deleteItem(index)" src="/static/images/close.png"></cover-image>
            </video>
        </view>
        <slot name="file" :file="lists"></slot>
        <view style="display: inline-block;" @tap="selectFile" v-if="maxCount > lists.length">    
            <slot name="addBtn"></slot>
            <view
                v-if="!customBtn"
                class="u-list-item u-add-wrap"
                hover-class="u-add-wrap__hover"
                hover-stay-time="150"
                :style="{
                    width: width + 'rpx',
                    height: width + 'rpx'
                }"
            >
                <u-icon name="plus" class="u-add-btn" size="40"></u-icon>
                <view class="u-add-tips">{{ uploadText }}</view>
            </view>
        </view>
    </view>
</template>
 
<script>
/**
 * upload 图片上传
 * @description 该组件用于上传图片场景
 * @tutorial https://www.uviewui.com/components/upload.html
 * @property {String} action 服务器上传地址
 * @property {String Number} max-count 最大选择图片的数量(默认99)
 * @property {Boolean} custom-btn 如果需要自定义选择图片的按钮,设置为true(默认false)
 * @property {Boolean} show-progress 是否显示进度条(默认true)
 * @property {Boolean} disabled 是否启用(显示/移仓)组件(默认false)
 * @property {String} image-mode 预览图片等显示模式,可选值为uni的image的mode属性值(默认aspectFill)
 * @property {String} del-icon 右上角删除图标名称,只能为uView内置图标
 * @property {String} del-bg-color 右上角关闭按钮的背景颜色
 * @property {String} del-color 右上角关闭按钮图标的颜色
 * @property {Object} header 上传携带的头信息,对象形式
 * @property {Object} form-data 上传额外携带的参数
 * @property {String} name 上传文件的字段名,供后端获取使用(默认file)
 * @property {Array<String>} size-type original 原图,compressed 压缩图,默认二者都有(默认['original', 'compressed'])
 * @property {Array<String>} source-type 选择图片的来源,album-从相册选图,camera-使用相机,默认二者都有(默认['album', 'camera'])
 * @property {Boolean} preview-full-image    是否可以通过uni.previewImage预览已选择的图片(默认true)
 * @property {Boolean} multiple    是否开启图片多选,部分安卓机型不支持(默认true)
 * @property {Boolean} deletable 是否显示删除图片的按钮(默认true)
 * @property {String Number} max-size 选择单个文件的最大大小,单位B(byte),默认不限制(默认Number.MAX_VALUE)
 * @property {Array<Object>} file-list 默认显示的图片列表,数组元素为对象,必须提供url属性
 * @property {Boolean} upload-text 选择图片按钮的提示文字(默认“选择图片”)
 * @property {Boolean} auto-upload 选择完图片是否自动上传,见上方说明(默认true)
 * @property {Boolean} show-tips 特殊情况下是否自动提示toast,见上方说明(默认true)
 * @property {Boolean} show-upload-list 是否显示组件内部的图片预览(默认true)
 * @event {Function} on-oversize 图片大小超出最大允许大小
 * @event {Function} on-preview 全屏预览图片时触发
 * @event {Function} on-remove 移除图片时触发
 * @event {Function} on-success 图片上传成功时触发
 * @event {Function} on-change 图片上传后,无论成功或者失败都会触发
 * @event {Function} on-error 图片上传失败时触发
 * @event {Function} on-progress 图片上传过程中的进度变化过程触发
 * @event {Function} on-uploaded 所有图片上传完毕触发
 * @event {Function} on-choose-complete 每次选择图片后触发,只是让外部可以得知每次选择后,内部的文件列表
 * @example <u-upload :action="action" :file-list="fileList" ></u-upload>
 */
export default {
    name: 'u-upload',
    props: {
        //是否显示组件自带的图片预览功能
        showUploadList: {
            type: Boolean,
            default: true
        },
        // 后端地址
        action: {
            type: String,
            default: ''
        },
        // 最大上传数量
        maxCount: {
            type: [String, Number],
            default: 52
        },
        //  是否显示进度条
        showProgress: {
            type: Boolean,
            default: true
        },
        // 是否启用
        disabled: {
            type: Boolean,
            default: false
        },
        // 预览上传的图片时的裁剪模式,和image组件mode属性一致
        imageMode: {
            type: String,
            default: 'aspectFill'
        },
        // 头部信息
        header: {
            type: Object,
            default() {
                return {};
            }
        },
        // 额外携带的参数
        formData: {
            type: Object,
            default() {
                return {};
            }
        },
        // 上传的文件字段名
        name: {
            type: String,
            default: 'file'
        },
        // 所选的图片的尺寸, 可选值为original compressed
        sizeType: {
            type: Array,
            default() {
                return ['original', 'compressed'];
            }
        },
        sourceType: {
            type: Array,
            default() {
                return ['album', 'camera'];
            }
        },
        // 是否在点击预览图后展示全屏图片预览
        previewFullImage: {
            type: Boolean,
            default: true
        },
        // 是否开启图片多选,部分安卓机型不支持
        multiple: {
            type: Boolean,
            default: true
        },
        // 是否展示删除按钮
        deletable: {
            type: Boolean,
            default: true
        },
        // 文件大小限制,单位为byte
        maxSize: {
            type: [String, Number],
            default: Number.MAX_VALUE
        },
        // 显示已上传的文件列表
        fileList: {
            type: Array,
            default() {
                return [];
            }
        },
        // 上传区域的提示文字
        uploadText: {
            type: String,
            default: '选择图片'
        },
        // 是否自动上传
        autoUpload: {
            type: Boolean,
            default: true
        },
        // 是否显示toast消息提示
        showTips: {
            type: Boolean,
            default: true
        },
        // 是否通过slot自定义传入选择图标的按钮
        customBtn: {
            type: Boolean,
            default: false
        },
        // 内部预览图片区域和选择图片按钮的区域宽度,高等于宽
        width: {
            type: [String, Number],
            default: 200
        },
        // 右上角关闭按钮的背景颜色
        delBgColor: {
            type: String,
            default: '#fa3534'
        },
        // 右上角关闭按钮的叉号图标的颜色
        delColor: {
            type: String,
            default: '#ffffff'
        },
        // 右上角删除图标名称,只能为uView内置图标
        delIcon: {
            type: String,
            default: 'close'
        },
        // 如果上传后的返回值为json字符串,是否自动转json
        toJson: {
            type: Boolean,
            default: true
        },
        // 上传前的钩子,每个文件上传前都会执行
        beforeUpload: {
            type: Function,
            default: null
        },
    },
    mounted() {},
    data() {
        return {
            lists: [],
            isInCount: true,
            uploading: false,
            tapIndex:0
        };
    },
    watch: {
        fileList: {
            immediate: true,
            handler(val) {
                val.map(value => {
                    // 首先检查内部是否已经添加过这张图片,因为外部绑定了一个对象给fileList的话(对象引用),进行修改外部fileList
                    // 时,会触发watch,导致重新把原来的图片再次添加到this.lists
                    // 数组的some方法意思是,只要数组元素有任意一个元素条件符合,就返回true,而另一个数组的every方法的意思是数组所有元素都符合条件才返回true
                    let tmp = this.lists.some(val => {
                        return val.url == value.url;
                    })
                    // 如果内部没有这个图片(tmp为false),则添加到内部
                    !tmp && this.lists.push({ url: value.url, error: false, progress: 100 });    
                });
            }
        },
        // 监听lists的变化,发出事件
        lists(n) {
            this.$emit('on-list-change', n);
        }
    },
    methods: {
        // 清除列表
        clear() {
            this.lists = [];
        },
        // 重新上传队列中上传失败的所有文件
        reUpload() {
            this.uploadFile();
        },
        // 选择图片
        selectFile() {
            if (this.disabled) return;
            const { name = '', maxCount, multiple, maxSize, sizeType, lists, camera, compressed, maxDuration, sourceType } = this;
            let chooseFile = null;
            const newMaxCount = maxCount - lists.length;
            // 设置为只选择图片的时候使用 chooseImage 来实现
            chooseFile = new Promise((resolve, reject) => {
                uni.showActionSheet({
                    title:"选择上传类型",
                    itemList: ['图片','视频'],
                    success: (res) => {
                        if(res.tapIndex == 0){
                            this.tapIndex = res.tapIndex
                            uni.chooseImage({
                                count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
                                sourceType: sourceType,
                                sizeType,
                                success: resolve,
                                fail: reject
                            });
                        } else {
                            this.tapIndex = res.tapIndex
                            uni.chooseVideo({
                                    // count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
                                    count:1,
                                    sourceType: sourceType,
                                    success: resolve,
                                    fail: reject
                            });
                        }
                    }
                })
            });
            chooseFile
                .then(res => {
                    let file = null;
                    let listOldLength = this.lists.length;
                    if(this.tapIndex == 0){
                        res.tempFiles.map((val, index) => {
                            // 如果是非多选,index大于等于1或者超出最大限制数量时,不处理
                            if (!multiple && index >= 1) return;
                            if (val.size > maxSize) {
                                this.$emit('on-oversize', val, this.lists);
                                this.showToast('超出允许的文件大小');
                            } else {
                                if (maxCount <= lists.length) {
                                    this.$emit('on-exceed', val, this.lists);
                                    this.showToast('超出最大允许的文件个数');
                                    return;
                                }
                                lists.push({
                                    url: val.path,
                                    progress: 0,
                                    error: false
                                });
                            }
                        });
                    }else{
                            // 如果是非多选,index大于等于1或者超出最大限制数量时,不处理
                            if (!multiple && index >= 1) return;
                                if (maxCount <= lists.length) {
                                    this.showToast('超出最大允许的文件个数');
                                    return;
                                }
                                lists.push({
                                    url: res.tempFilePath,
                                    progress: 0,
                                    error: false
                                });
                    }
                    // 每次图片选择完,抛出一个事件,并将当前内部选择的图片数组抛出去
                    this.$emit('on-choose-complete', this.lists);
                    if (this.autoUpload) this.uploadFile(listOldLength);
                })
                .catch(error => {
                    // this.$emit('on-error', error);
                });
        },
        // 提示用户消息
        showToast(message, force = false) {
            if (this.showTips || force) {
                uni.showToast({
                    title: message,
                    icon: 'none'
                });
            }
        },
        // 该方法供用户通过ref调用,手动上传
        upload() {
            this.uploadFile();
        },
        // 对失败的图片重新上传
        retry(index) {
            this.lists[index].progress = 0;
            this.lists[index].error = false;
            this.lists[index].response = null;
            uni.showLoading({
                title: '重新上传'
            });
            this.uploadFile(index);
        },
        // 上传图片
        async uploadFile(index = 0) {
            if (this.disabled) return;
            if (this.uploading) return;
            // 全部上传完成
            if (index >= this.lists.length) {
                this.$emit('on-uploaded', this.lists);
                return;
            }
            // 检查上传地址
            if (!this.action) {
                this.showToast('请配置上传地址', true);
                return;
            }
            // 检查是否是已上传或者正在上传中
            if (this.lists[index].progress == 100) {
                if (this.autoUpload == false) this.uploadFile(index + 1);
                return;
            }
            // 执行before-upload钩子
            if(this.beforeUpload && typeof(this.beforeUpload) === 'function') {
                // 执行回调,同时传入索引和文件列表当作参数
                let beforeResponse = this.beforeUpload(index, this.lists);
                // 判断是否返回了promise
                if (!!beforeResponse && typeof beforeResponse.then === 'function') {
                    await beforeResponse.then(res => {
                        // promise返回成功,不进行动作,继续上传
                    }).catch(err => {
                        // 进入catch回调的话,继续下一张
                        return this.uploadFile(index + 1);
                    })
                } else if(beforeResponse === false) {
                     // 如果返回false,继续下一张图片的上传
                    return this.uploadFile(index + 1);
                }
            }
            this.lists[index].error = false;
            this.uploading = true;
            // 创建上传对象
            const task = uni.uploadFile({
                url: this.action,
                filePath: this.lists[index].url,
                name: this.name,
                formData: this.formData,
                header: this.header,
                success: res => {
                    console.log(res);
                    // 判断是否json字符串,将其转为json格式
                    let data = this.toJson && this.$u.test.jsonString(res.data) ? JSON.parse(res.data) : res.data;
                    if (![200, 201].includes(res.statusCode)) {
                        this.uploadError(index, data);
                    } else {
                        // 上传成功
                        this.lists[index].response = data;
                        this.lists[index].progress = 100;
                        this.lists[index].error = false;
                        this.$emit('on-success', data, index, this.lists);
                    }
                },
                fail: e => {
                    this.uploadError(index, e);
                },
                complete: res => {
                    uni.hideLoading();
                    this.uploading = false;
                    this.uploadFile(index + 1);
                    this.$emit('on-change', res, index, this.lists);
                }
            });
            task.onProgressUpdate(res => {
                if (res.progress > 0) {
                    this.lists[index].progress = res.progress;
                    this.$emit('on-progress', res, index, this.lists);
                }
            });
        },
        // 上传失败
        uploadError(index, err) {
            this.lists[index].progress = 0;
            this.lists[index].error = true;
            this.lists[index].response = null;
            this.$emit('on-error', err, index, this.lists);
            this.showToast('上传失败,请重试');
        },
        // 删除一个图片
        deleteItem(index) {
            uni.showModal({
                title: '提示',
                content: '您确定要删除此项吗?',
                success: res => {
                    if (res.confirm) {
                        if (this.lists[index].process < 100 && this.lists[index].process > 0) {
                            typeof this.lists[index].uploadTask != 'undefined' && this.lists[index].uploadTask.abort();
                        }
                        this.lists.splice(index, 1);
                        this.$forceUpdate();
                        this.$emit('on-remove', index, this.lists);
                        this.showToast('移除成功');
                    }
                }
            });
        },
        // 用户通过ref手动的形式,移除一张图片
        remove(index) {
            // 判断索引的合法范围
            if (index >= 0 && index < this.lists.length) {
                this.lists.splice(index, 1);
                this.$emit('on-list-change', this.lists);
            }
        },
        // 预览图片
        doPreviewImage(url, index) {
            if (!this.previewFullImage) return;
            const images = this.lists.map(item => item.url || item.path);
            uni.previewImage({
                urls: images,
                current: url,
                success: () => {
                    this.$emit('on-preview', url, this.lists);
                },
                fail: () => {
                    uni.showToast({
                        title: '预览图片失败',
                        icon: 'none'
                    });
                }
            });
        }
    }
};
</script>
 
<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';
.controls-delete{
    right: 10rpx;
    background-color: #FFFFFF;
    border-radius: 50%;
}
.u-upload {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
}
.img {
        position: absolute;
        width: 45rpx;
        height: 45rpx;
        top: 10rpx
    }
.u-list-item {
    width: 200rpx;
    height: 200rpx;
    overflow: hidden;
    margin: 10rpx;
    background: rgb(244, 245, 246);
    position: relative;
    border-radius: 10rpx;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
 
.u-preview-wrap {
    border: 1px solid rgb(235, 236, 238);
}
 
.u-add-wrap {
    flex-direction: column;
    color: $u-content-color;
    font-size: 28rpx;
}
 
.u-add-tips {
    margin-top: 20rpx;
    line-height: 40rpx;
}
 
.u-add-wrap__hover {
    background-color: rgb(235, 236, 238);
}
 
.u-preview-image {
    display: block;
    width: 100%;
    height: 100%;
    border-radius: 10rpx;
}
 
.u-delete-icon {
    position: absolute;
    top: 10rpx;
    right: 10rpx;
    z-index: 99999;
    background-color: $u-type-error;
    border-radius: 100rpx;
    width: 44rpx;
    height: 44rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}
 
.u-icon {
    display: flex;
    align-items: center;
    justify-content: center;
}
 
.u-progress {
    position: absolute;
    bottom: 10rpx;
    left: 8rpx;
    right: 8rpx;
    z-index: 9;
    width: auto;
}
 
.u-error-btn {
    color: #ffffff;
    background-color: $u-type-error;
    font-size: 20rpx;
    padding: 4px 0;
    text-align: center;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 9;
    line-height: 1;
}
</style>