index.vue
9.35 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
<!--
* @FileDescription: index
* @Author: kahu
* @Date: 2022/12/14
* @LastEditors: kahu
* @LastEditTime: 2022/12/14
-->
<template>
<div class="content">
<el-upload
v-loading="isUploading"
:disabled="componentError"
class="upload-demo"
drag
:headers="headers"
:file-list="viewFileList"
:name="name"
:show-file-list="showFileList"
:list-type="showFileListType"
:multiple="multiple"
:action="uploadUrl"
:limit="limit"
:on-success="handleUploadSuccess"
:before-upload="handleBeforeUpload"
:on-error="handleUploadError"
:on-exceed="handleExceed"
:before-remove="handleBeforeRemove"
:on-remove="handleRemove"
:on-preview="handlePreviewOpen"
>
<i class="el-icon-upload"></i>
<div
class="el-upload__text"
v-if="!componentError"
>将文件拖到此处,或<em>点击上传</em></div>
<div
class="error-text"
v-else
> 组件配置错误,请查看控制台
</div>
<div
class="el-upload__tip"
slot="tip"
>
文件大小不超过{{ limitSize }}m,文件类型为{{ types.toString() }}
</div>
</el-upload>
<!-- 预览 -->
<el-dialog
title="预览"
:visible.sync="previewObj.show"
width="60%"
:before-close="handlePreviewClose"
>
<div class="preview-content">
<template v-if="previewObj.file && previewObj.file.type.includes('image')">
<el-image class="preview-item" :src="previewObj.file.url" />
</template>
<template v-if="previewObj.file && previewObj.file.type.includes('video')">
<video class="preview-item" controls :src="previewObj.file.url" />
</template>
</div>
</el-dialog>
</div>
</template>
<script>
import mime from 'mime'
let fullLoading = null
const baseURL = process.env.VUE_APP_DOMAIN_PREFIX
export default {
name: "Upload",
props: {
headers:{
type:Object,
default:()=>({})
},
/** 上传时候表单的KEY */
name: {
type: String,
default: () => "file"
},
/** 限制上传数量 */
limit: {
type: Number,
default: () => 5
},
/** 限制的那张大小 单位M */
limitSize: {
type: Number,
default: () => 5
},
/** 是否多选 */
multiple: {
type: Boolean,
default: () => true
},
/** 是否展示文件列表 */
showFileList: {
type: Boolean,
default: () => true
},
/** 文件展示方式 text/picture/picture-card */
showFileListType:{
type:String,
default:()=>'text'
},
/** 允许上传的文件尾缀 string[] */
types: {
type: Array,
default: () => (['jpg', 'png', 'gif'])
},
/** 默认的文件列表 string[] */
defaultFileList: {
type: Array,
default: () => ([])
},
/** 上传成功后端返回的字段名称 */
responseFileName: {
type: String,
default: () => 'url'
},
/** 是否需要全屏loading */
needFullScreenLoading:{
type:Boolean,
default:()=>true
}
},
data() {
return {
uploadUrl: `${ baseURL }/file/upload`,
// 真实文件列表
fileList: [],
// 默认展示的list,解决多上传只回调success一次的问题
viewFileList:[],
// 组件是否部署错误
componentError: false,
// 是否正在上传
isUploading:false,
// 预览对象
previewObj:{
show:false,
file:null
}
}
},
watch: {
defaultFileList: {
handler() {
// 判断类型
const flag = Object.prototype.toString.call(this.defaultFileList) === '[object Array]'
&& this.defaultFileList.length > 0 &&
Object.prototype.toString.call(this.defaultFileList[0]) !== '[object String]'
if (flag) {
this.componentError = true
throw new Error('defaultFileList格式错误,应为string[]格式')
}else{
this.componentError = false
}
this.viewFileList = this.defaultFileList.map(defaultFilePath => ({name: defaultFilePath, url: defaultFilePath}))
this.viewFileList.forEach(item=>{
this.fileList.push(item)
})
},
deep: true,
immediate: true
},
fileList:{
handler(){
this.handleNotifyFather()
},
deep:true,
immediate:false
}
},
methods: {
/**
* 检查type是否符合types的mime
* @param type 文件后缀
* @param types 可用文件后缀集合
*/
handleCheckFileMime(type, types) {
const typeMimes = types.map(item => mime.getType(item))
return typeMimes.includes(type)
},
handleCheckFileSize(fileSize, limitSize) {
const limitByteSize = limitSize * 1024 * 1024
return limitByteSize > fileSize
},
/**
* 上传之前的钩子
* @param file
* @return {undefined}
*/
handleBeforeUpload(file) {
// 检查mime
const fileType = file.type || mime.getType(file.name.slice(file.name.lastIndexOf('.') + 1))
const checkFileMime = this.handleCheckFileMime(fileType, this.types)
const checkFileSize = this.handleCheckFileSize(file.size, this.limitSize);
!checkFileSize ? file.isJumpRemove = true : undefined
!checkFileSize ? this.$notify.warning(`文件大小不得超出${ this.limitSize }m`) : undefined
!checkFileMime ? file.isJumpRemove = true : undefined
!checkFileMime ? this.$notify.warning(`文件类型不在合法列表 ${ this.types }`) : undefined
if(checkFileSize && checkFileMime){
// 开启loading
this.isUploading = true
if(this.needFullScreenLoading){
fullLoading = this.$loading({
background:`rgba(255,255,255,0.5)`,
text:'上传中',
fullscreen:true
})
}
}
return checkFileSize && checkFileMime
},
/**
* 上传成功钩子
* @param response
* @param file
* @param fileList
*/
handleUploadSuccess(response, file, fileList) {
this.isUploading = false
if(this.needFullScreenLoading){
fullLoading?.close()
}
const successObj = {
url: response.data[this.responseFileName],
name: file.name
}
file.url = response.data[this.responseFileName]
this.fileList.push(successObj)
},
/**
* 上传失败的钩子
* @param err
* @param file
* @param fileList
*/
handleUploadError(err, file, fileList) {
},
/**
* 超出数量的钩子
* @param files
* @param fileList
*/
handleExceed(files, fileList) {
this.$notify.warning(`文件总数大于可上传数量 ${ this.limit }`)
},
/**
* 文件即将移除的钩子
* @param file
* @param fileList
*/
async handleBeforeRemove(file, fileList) {
// 如果是超出文件大小调用,放行
if (file?.raw?.isJumpRemove) {
return true
}
return await this.$confirm('此操作将会删除已上传的文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
},
/**
* 移除文件的钩子
*/
handleRemove(file, fileList) {
if (file?.raw?.isJumpRemove) {
return
}
this.fileList.splice(this.fileList.findIndex(fileItem => file?.response?.data[this.responseFileName] === fileItem.url || file.url === fileItem.url), 1)
},
/**
* 通知父组件
*/
handleNotifyFather(){
this.$emit('change',this.fileList)
},
/**
* 预览
* 图片视频直接预览,其他下载
* @param file
*/
handlePreviewOpen(file){
if(!file.type){
file.type = mime.getType(file?.url?.slice(file?.url?.lastIndexOf('.')+1)) || mime.getType(file?.name?.slice(file?.name.lastIndexOf('.')+1)) || undefined
}
if(file.type.includes('image') || file.type.includes('video')){
this.previewObj.file = file
this.previewObj.show = true
}else{
this.$confirm('需要下载才能预览此文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
let htmlAnchorElement = document.createElement('a');
htmlAnchorElement.download = file?.url.slice(file?.url.lastIndexOf('/')+1)
htmlAnchorElement.target='_bank'
htmlAnchorElement.href = file?.url
htmlAnchorElement.click()
htmlAnchorElement = null
}).catch(() => {
});
}
},
handlePreviewClose(){
this.previewObj.file = null
this.previewObj.show = false
}
}
}
</script>
<style
lang="scss"
scoped
>
::v-deep .el-upload {
width: 100% !important;
.el-upload-dragger {
width: 100% !important;
}
}
.error-text {
font-size: 18px;
font-weight: bolder;
color: red;
animation: error-animation 2.5s ease-in-out infinite;
}
@keyframes error-animation {
0%, 100% {
font-size: 18px;
color: red;
}
25%, 75% {
font-size: 16px;
color: #b9b1b1;
}
50% {
font-size: 18px;
color: #500000;
}
}
.preview-content{
display: flex;
align-items: center;
justify-content: center;
.preview-item{
min-width: 800px;
}
}
</style>