Upload.vue
24 KB
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
<script>
import { getCosConfig, getVideoPic, upload } from '@/api/common'
import { dateFormat, uuid } from '@/utils/index'
// import Video from 'video.js'
import Cos from 'cos-js-sdk-v5'
import BenzAMRRecorder from 'benz-amr-recorder'
import MP4Box from 'mp4box'
import OSS from 'ali-oss'
export default {
components: {},
props: {
// 单文件上传时使用
fileUrl: {
type: String,
default: '',
},
fileName: {
type: String,
default: '',
},
// 多文件上传时使用,例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}]
fileList: {
type: Array,
default: () => [],
},
// 0 图片(image)、1 语音(voice)、2 视频(video),3 普通文件(file)
type: {
type: String,
default: '0',
validator: function (value) {
// 这个值必须匹配下列字符串中的一个
return ['0', '1', '2', '3'].includes(value)
},
},
// 上传文件大小不能超过 maxSize MB, 各类型有默认限制 参见: maxSizeDefault
maxSize: {
type: Number,
default: undefined,
},
// 图片的宽高像素限制 [width(number), height(number)],默认null不限制
maxImgPx: {
type: Array,
default: [1024, 1024], // () => [100, 100]
},
// 允许上传的文件格式后缀名 eg:["jpg", "png"],['*']为不限制,各类型有默认限制 参见: formatDefault
format: {
type: Array,
default: undefined,
},
// 选择上传文件类型, 用于过滤系统选择文件类型, 默认根据类型自动匹配:见 acceptAuto,
accept: {
type: String,
default: '',
},
// 是否能上传多个
multiple: {
type: Boolean,
default: false,
},
// multiple为true时有效,最多上传几个,默认不限制,最小为1
limit: {
type: Number,
default: undefined,
},
// onProgress: {
// type: Function,
// default: null,
// },
// beforeUpload: {
// type: Function,
// default: function() {
// return function() {}
// },
// },
// 图片操作 目前支持有 view:查看,remove:删除,download:下载
// action: {
// type: Array,
// default: () => ["view", "remove"],
// },
},
data() {
return {
loading: false,
fileUrlWatch: this.fileUrl,
fileNameWatch: this.fileName,
fileListWatch: this.fileList,
picUrl: '', // 视频第一帧
file: undefined,
speed: 0, // 上传网速
percentage: 0, //上传进度
// cos配置信息
cosConfig: { bucketName: '', cosImgUrlPrefix: '', region: '' },
cosInstance: undefined, // cos实例
ossObj: undefined,
}
},
watch: {
fileUrl: {
handler(value) {
this.fileUrlWatch = value
},
},
fileName: {
handler(value) {
this.fileNameWatch = value
},
},
fileList: {
handler(value) {
this.fileListWatch = value
},
},
loading(val) {
this.$emit('loadingChange', val)
},
},
computed: {
// 识别选择文件类型
acceptAuto() {
return ['image/*', 'amr/*', 'video/*'][this.type]
},
},
created() {
getCosConfig().then((res) => {
this.cosConfig = res
if (this.cosConfig.fileObject == 'tencentOss') {
this.cosInstance = new Cos({
SecretId: res.secretId,
SecretKey: res.secretKey,
})
} else if (this.cosConfig.fileObject == 'local') {
} else {
let region =
this.cosConfig.region.indexOf('//') != -1 ? this.cosConfig.region.split('//')[1] : this.cosConfig.region
this.ossObj = new OSS({
// yourRegion填写Bucket所在地域。Region填写为oss-cn-hangzhou。
region: region.split('.')[0],
// 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
accessKeyId: this.cosConfig.secretId,
accessKeySecret: this.cosConfig.secretKey,
// 填写Bucket名称。
bucket: this.cosConfig.bucketName,
})
}
})
},
mounted() {
// const cosInstance = new Cos({
// // getAuthorization 必选参数
// getAuthorization: function (options, callback) {
// // 服务端例子:https://github.com/tencentyun/qcloud-cos-sts-sdk/blob/master/scope.md
// // 异步获取临时密钥
// var url = window.lwConfig.SYSTEM_API + '/file/get/config' // url替换成您自己的后端服务
// var xhr = new XMLHttpRequest()
// xhr.open('get', url, true)
// xhr.setRequestHeader('Content-Type', 'application/json')
// xhr.onload = function (e) {
// try {
// var data = JSON.parse(e.target.responseText)
// var credentials = data.credentials
// } catch (e) {}
// if (!data || !credentials) {
// return console.error('credentials invalid:\n' + JSON.stringify(data, null, 2))
// }
// callback({
// TmpSecretId: decrypt(credentials.secretId),
// TmpSecretKey: credentials.secretKey,
// SecurityToken: credentials.sessionToken,
// // 建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
// StartTime: data.startTime, // 时间戳,单位秒,如:1580000000
// ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000000
// // ScopeLimit: true, // 细粒度控制权限需要设为 true,会限制密钥只在相同请求时重复使用
// })
// }
// xhr.send()
// // xhr.send(JSON.stringify(options.Scope))
// },
// })
},
methods: {
upload() {
if (this.cosConfig.fileObject == 'tencentOss') {
this.tencentFn()
} else if (this.cosConfig.fileObject == 'local') {
this.localFn()
} else if (this.cosConfig.fileObject == 'aliOss') {
this.aliOss()
}
},
localFn() {
this.loading = true
let file = undefined
if (!this.multiple || this.limit == 1) {
file = this.file
} else {
// 多选上传是多次调用单传的
file = this.file.shift()
}
let formData = new FormData()
formData.append('file', file)
upload(formData).then((dd) => {
if (dd.code == 200) {
let location = dd.data.url
this.$emit('upSuccess', location)
this.type == 2
? //获取视频第一帧画面
getVideoPic({ url: location }).then((res) => {
this.loading = false
this.$emit('getPicUrl', res.data.url)
})
: (this.loading = false)
// 使用本地链接提供预览,避免上传后下载的问题
let url = window.URL.createObjectURL(file)
let name = file.name
if (!this.multiple) {
this.fileUrlWatch = url
this.$emit('update:fileUrl', location)
this.$emit('update:fileName', (this.fileNameWatch = name))
} else {
this.fileListWatch = this.fileListWatch.concat({ name, url })
this.$emit('update:fileList', this.fileList.concat({ name, url: location }))
}
} else {
this.loading = false
this.$message.error('上传失败,请稍后再试')
}
})
},
async aliOss() {
this.loading = true
let file = undefined
if (!this.multiple || this.limit == 1) {
file = this.file
} else {
// 多选上传是多次调用单传的
file = this.file.shift()
}
let date = new Date()
let format = file.name.match(/\.(\w+)$/g)
format = format && format[0]
// 实例可能未初始化完成
if (!this.ossObj) {
this.$message.error('存储空间正忙,请稍后再试')
return
}
let name = `${dateFormat(date, 'yyyy-MM-dd')}/t${date.getTime()}-${uuid()}${format}`
try {
const data = await this.ossObj.multipartUpload(name, file, {
progress: (progressData) => {
this.percentage = progressData * 100
// this.speed = (progressData.speed / 1024 / 1024).toFixed(2)
},
})
if (data) {
this.percentage = this.speed = 0
let location = this.cosConfig.cosImgUrlPrefix + data.name
this.$emit('upSuccess', location)
this.type == 2
? //获取视频第一帧画面
getVideoPic({ url: location }).then((res) => {
this.loading = false
this.$emit('getPicUrl', res.data.url)
})
: (this.loading = false)
// 使用本地链接提供预览,避免上传后下载的问题
let url = window.URL.createObjectURL(file)
let name = file.name
if (!this.multiple) {
this.fileUrlWatch = url
this.$emit('update:fileUrl', location)
this.$emit('update:fileName', (this.fileNameWatch = name))
} else {
this.fileListWatch = this.fileListWatch.concat({ name, url })
this.$emit('update:fileList', this.fileList.concat({ name, url: location }))
}
}
} catch (e) {
this.loading = false
this.$message.error('上传失败,请稍后再试')
}
},
tencentFn() {
this.loading = true
let file = undefined
if (!this.multiple || this.limit == 1) {
file = this.file
} else {
// 多选上传是多次调用单传的
file = this.file.shift()
}
let date = new Date()
let format = file.name.match(/\.(\w+)$/g)
format = format && format[0]
const params = {
Bucket: this.cosConfig.bucketName /* 填入您自己的存储桶,必须字段 */,
Region: this.cosConfig.region /* 存储桶所在地域,例如ap-beijing,必须字段 */,
Key: `/${dateFormat(date, 'yyyy-MM-dd')}/t${date.getTime()}-${uuid()}${format}`,
/* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段。*/
// 此处使用的格式: /日期/t时间戳-uid-文件后缀名
}
// 实例可能未初始化完成
if (!this.cosInstance) {
this.$message.error('存储空间正忙,请稍后再试')
return
}
this.cosInstance.uploadFile(
{
...params,
Body: file /* 必须,上传文件对象,可以是input[type="file"]的file对象 */,
onProgress: (progressData) => {
this.percentage = progressData.percent * 100
this.speed = (progressData.speed / 1024 / 1024).toFixed(2)
// this.onProgress && this.onProgress(this.percentage, this.speed)
},
},
(err1, data) => {
this.percentage = this.speed = 0
if (err1) {
this.loading = false
this.$message.error('上传失败,请稍后再试')
} else {
let location = 'https://' + data.Location
this.$emit('upSuccess', location)
this.type == 2
? //获取视频第一帧画面
getVideoPic({ url: location }).then((res) => {
this.loading = false
this.$emit('getPicUrl', res.data.url)
})
: (this.loading = false)
// 使用本地链接提供预览,避免上传后下载的问题
let url = window.URL.createObjectURL(file)
let name = file.name
if (!this.multiple) {
this.fileUrlWatch = url
this.$emit('update:fileUrl', location)
this.$emit('update:fileName', (this.fileNameWatch = name))
} else {
this.fileListWatch = this.fileListWatch.concat({ name, url })
this.$emit('update:fileList', this.fileList.concat({ name, url: location }))
}
}
},
)
},
remove(i) {
this.$confirm('确定要删除吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}).then(() => {
let clone = JSON.parse(JSON.stringify(this.fileList))
this.fileListWatch.splice(i, 1)
clone.splice(i, 1)
this.$emit('update:fileList', clone)
})
},
handleExceed(file, fileList) {
this.$message.error('最多上传' + this.limit + '张')
this.loading = false
},
async handleBeforeUpload(file, filelist) {
this.loading = true
let isFormat = true
let isSize = true
// type: 0 图片(image)、1 语音(voice)、2 视频(video),3 普通文件(file)
// 统一校验文件后缀名格式
// 如果没有显示配置 format 格式限制,则使用默认下面校验
let format = this.format
let tip = ''
if (!format || !format.length) {
let formatDefault = {
0: { tip: 'png/jpg', value: ['png', 'jpg', 'jpeg'] },
1: { value: ['amr'] },
2: { value: ['mp4'] },
3: { tip: 'word/pdf/ppt', value: ['doc', 'docx', 'pdf', 'ppt', 'pptx', 'pps', 'pptsx'] },
}
format = formatDefault[this.type].value
tip = formatDefault[this.type].tip
}
let match = file.name.match(/\.(\w+)$/g)
let fileFormat = match && match[0].replace('.', '').toLowerCase()
isFormat = format[0] === '*' || format.includes(fileFormat)
if (!isFormat) {
this.$message.error('文件格式错误,仅支持 ' + (tip || format.join(',')) + ' 格式!')
this.loading = false
return Promise.reject()
}
// 统一校验文件体积
// 如果没有显式配置 maxSize 限制大小,则使用下面默认值,单位 MB,
let maxSize = this.maxSize
if (!maxSize) {
let maxSizeDefault = { 0: 2, 1: 2, 2: 100, 3: 50 }
maxSize || (maxSize = maxSizeDefault[this.type])
}
isSize = file.size / 1024 / 1024 < maxSize
if (!isSize) {
this.$message.error('上传文件大小不能超过 ' + maxSize + 'MB!')
this.loading = false
return Promise.reject()
}
// 各类型独有的校验
let validate = true
if (this.type === '0') {
// 图片
let maxImgPx = this.maxImgPx
if (maxImgPx) {
try {
await new Promise((resolve) => {
let width, height
let image = new Image()
//加载图片获取图片真实宽度和高度
image.onload = () => {
width = image.width
height = image.height
if (width > maxImgPx[0]) {
validate = false
this.$message.error(`图片“宽”度超过${maxImgPx[0]}像素,请重新选择`)
} else if (height > maxImgPx[1]) {
this.$message.error(`图片“高”度超过${maxImgPx[1]}像素,请重新选择`)
validate = false
}
window.URL && window.URL.revokeObjectURL(image.src)
resolve()
}
if (window.URL) {
let url = window.URL.createObjectURL(file)
image.src = url
} else if (window.FileReader) {
let reader = new FileReader()
reader.onload = function (e) {
let data = e.target.result
image.src = data
}
reader.readAsDataURL(file)
}
})
} catch (e) {
console.error(e)
}
}
} else if (this.type === '1') {
// 语音
let amr = new BenzAMRRecorder()
try {
await amr.initWithBlob(file)
validate = amr.getDuration() <= 60
if (!validate) {
this.$message.error('上传文件时长不能超过 60秒!')
}
} catch (error) {
console.log(error)
this.$message.error('文件已损坏')
}
} else if (this.type === '2') {
// 视频
let result = await this.checkVideoCode(file)
if (result.mime.indexOf('video/mp4') === -1) {
this.$message.error('mp4 格式不正确, 请使用标准编码视频!')
this.loading = false
return Promise.reject()
}
} else if (this.type === '3') {
// 普通文件
}
if (!validate) {
this.loading = false
}
// if (beforeUpload) {
// return beforeUpload(file)
// }
if (!this.multiple || this.limit == 1) {
this.file = file
} else {
Array.isArray(this.file) || (this.file = []) // 多选
this.file.push(file)
}
return validate || Promise.reject()
},
onError(err, file, fileList) {
this.loading = false
this.$message.error('上传文件失败')
},
checkVideoCode(file) {
return new Promise((resolve, reject) => {
const mp4boxFile = MP4Box.createFile()
const reader = new FileReader()
reader.readAsArrayBuffer(file)
reader.onload = function (e) {
const arrayBuffer = e.target.result
arrayBuffer.fileStart = 0
mp4boxFile.appendBuffer(arrayBuffer)
}
mp4boxFile.onReady = function (info) {
resolve(info)
}
mp4boxFile.onError = function (info) {
reject(info)
}
})
},
showView(index) {
let imager = index !== undefined ? this.$refs.image[index] : this.$refs.image
imager.clickHandler()
this.$nextTick(() => {
// 为遮罩层添加关闭事件
let maskEl = imager.$children[0].$refs['el-image-viewer__wrapper'].firstChild
maskEl.addEventListener('click', () => {
event.stopPropagation()
imager.closeViewer()
})
})
// this.$refs.image.$refs.closeViewer();
},
},
}
</script>
<template>
<div>
<!-- 多个上传文件列表展示 -->
<template v-if="multiple">
<!-- 图片 -->
<template v-if="type == 0">
<!-- <transition-group> -->
<div v-for="(item, index) in fileListWatch" :key="index" class="img-item upload-item">
<el-image
ref="image"
class="upload-img uploader-size"
:src="item.url"
fit="contain"
:preview-src-list="fileListWatch.map((e) => e.url)"
alt="" />
<div class="action-mask">
<i class="el-icon-search mr5" @click="showView(index)"></i>
<!-- <span v-if="action.includes('download')" @click="download(item)">
<i class="el-icon-download"></i>
</span> -->
<i class="el-icon-delete mr5" @click="remove(index)"></i>
</div>
</div>
<!-- </transition-group> -->
</template>
<!-- 后续再这里扩展其他文件列表 -->
</template>
<el-upload
v-if="!multiple || !limit || fileListWatch.length < limit"
class="uploader"
action="/api"
:accept="accept || acceptAuto"
:http-request="upload"
:data="{ mediaType: type }"
:show-file-list="false"
:file-list="fileListWatch"
:disabled="loading"
:multiple="multiple && limit != 1"
:limit="limit"
:on-error="onError"
:on-exceed="handleExceed"
:before-upload="handleBeforeUpload">
<!--
element-loading-text="正在上传..."
:on-success="onSuccess"
-->
<slot>
<i v-if="!loading && !fileUrlWatch" class="el-icon-plus uploader-icon upload-action uploader-size"></i>
<transition>
<!-- 上传进度条 -->
<div class="upload-action uploader-size" v-if="loading">
<el-progress class="progress cc" type="circle" :percentage="percentage"></el-progress>
<div class="el-loading-spinner">
<svg viewBox="25 25 50 50" class="circular">
<circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
</svg>
</div>
<div class="cc" style="margin-top: 35px" v-if="speed">
{{ speed + 'M/s' }}
</div>
</div>
<!-- 单文件上传的文件展示 -->
<div v-if="!loading && fileUrlWatch && !multiple" class="upload-item">
<template v-if="type === '0'">
<el-image
v-if="fileUrlWatch"
ref="image"
:src="fileUrlWatch"
class="upload-img upload-img-single uploader-size"
:preview-src-list="[fileUrlWatch]"
fit="contain" />
<div class="action-mask" @click.self.stop>
<i class="el-icon-search" @click.prevent.stop="showView()"></i>
<i class="el-icon-edit"></i>
<!-- <span v-if="action.includes('download')" @click.prevent.stop="download(item)">
<i class="el-icon-download"></i>
</span> -->
<!-- <span @click.prevent.stop="remove()">
<i class="el-icon-delete"></i>
</span> -->
</div>
</template>
<template v-else-if="type === '2'">
<video
ref="video"
id="myVideo"
class="upload-video"
width="100%"
controls
webkit-playsinline="true"
playsinline="true"
:autoplay="false"
:key="fileUrlWatch"
preload="auto">
<source :src="fileUrlWatch" type="video/mp4" />
</video>
<div class="action-mask" style="height: 30%" @click.self.stop>
<i class="el-icon-edit"></i>
</div>
</template>
<template v-else class="al">
{{ fileNameWatch || fileUrlWatch }}
<i class="el-icon-edit ml10"></i>
<!-- a链接用本地视频打不开,视频地址使用远程地址 -->
<a @click.stop :href="/\.mp4$/.test(fileNameWatch) ? fileUrl : fileUrlWatch" target="_blank">
<i class="el-icon-view ml10" style="vertical-align: middle"></i>
</a>
</template>
</div>
</transition>
</slot>
</el-upload>
<!-- 上传格式,大小等提示语 -->
<div class="tip">
<slot name="tip"></slot>
</div>
</div>
</template>
<style lang="scss" scoped>
::v-deep.uploader {
display: inline-block;
vertical-align: middle;
.el-upload {
display: block;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
}
// 默认组件大小,如需修改,请通过外部重写该类样式
.uploader-size {
width: 178px;
height: 178px;
}
.upload-action {
position: relative;
text-align: center;
}
.uploader-icon {
display: flex;
font-size: 28px;
align-items: center;
justify-content: center;
color: #8c939d;
border-radius: 6px;
border: 1px dashed #d9d9d9;
transition: all 0.3s;
&:hover {
border-color: var(--color);
color: var(--color);
}
}
.progress {
overflow: hidden;
}
.upload-img-single {
border: 1px dashed #eee;
}
.upload-img {
display: block;
}
.upload-video {
display: block;
width: 300px;
height: 150px;
box-sizing: border-box;
color: #fff;
background-color: #000;
position: relative;
padding: 0;
font-size: 10px;
line-height: 1;
font-weight: normal;
font-style: normal;
word-break: initial;
}
.tip {
margin-top: 10px;
color: #aaa;
font-size: 12px;
}
.img-item {
position: relative;
display: inline-block;
vertical-align: middle;
margin: 0 10px 10px 0;
transition: all 0.3s;
}
.action-mask {
position: absolute;
display: flex;
justify-content: space-around;
align-items: center;
padding: 0 30px;
width: 100%;
height: 100%;
left: 0;
top: 0;
cursor: pointer;
text-align: center;
color: #fff;
opacity: 0;
font-size: 20px;
transition: opacity 0.3s;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;
}
.upload-item {
&:hover {
.action-mask {
opacity: 1;
}
}
}
</style>