使用方式

1
2
3
4
<upload-img :maxLength="6"
limit-type=".png,.jpg,.jpeg"
:filesList="checkFileList"
@addFileList="checkFileUpdate" @deleteFileList="checkFileDelete"></upload-img>

可选属性

属性名称 属性说明
show-loading 是否展示上传时的loading
max-length 可上传的最大文件数量
files-list 回显的图片集合
type 上传按钮样式类型 0:图片 1:按钮
disabled 是否禁用上传
can-delete 是否允许删除文件
folder 文件在服务器上的文件夹,如minio中基础文件夹的下一级文件夹
max-size 允许上传的最大文件大小,单位M
limit-type 所允许上传的文件类型,比如设置limit-type = '.png,.jpg,.jpeg’则限值上传图片类型,为空时允许上传所有类型文件

回调事件

事件名称 事件说明
deleteFileList 删除文件时触发,将删除文件后获取到的文件信息返回
addFileList 新增文件时触发,将上传文件后获取到的文件信息返回

组件代码

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
<template>
<div class="imgList">
<div>
<div
class="img"
v-show="item.url"
v-for="(item, index) in upFileList"
:key="index"
draggable="true"
@dragstart="dragstartEventImg($event, index)"
@dragenter="dragenterEventImg($event, index)"
@dragend="dragendEventImg($event, index)"
@dragover="dragoverEventImg">
<el-tooltip class="item" effect="dark" :content="item.name" placement="top-start">
<img v-if="['png', 'jpg', 'jpeg'].find(function(ii,index,arr){return ii === getFileType(item.url)})"
:src="fileOnlineShow + item.url" alt="" @click="previewFile(fileOnlineShow + item.url)"/>
<svg-icon
v-else-if="['doc', 'docx', 'xls', 'xlsx', 'pdf'].find(function(ii,index,arr){return ii === getFileType(item.url)})"
:icon-class="getFileType(item.url)"></svg-icon>
<svg-icon v-else icon-class="other"></svg-icon>
</el-tooltip>
<i
v-if="canDelete"
class="el-icon-error"
@click="deleteFile(item)"
></i>
</div>
</div>
<div
class="uploadFile"
:class="{ disabledStyle: disabled }"
v-show="maxLength > filesList.length"
>
<el-upload
:disabled="disabled"
:multiple="true"
name="file"
ref="imageUpload"
:headers="headers"
:before-upload="beforeUploadFile"
:data="{ folder: folder }"
:show-file-list="false"
:action="fileUploadApi"
:on-success="uploadFile"
:limit="maxLength"
:file-list="filesList"
:on-progress="progress"
:on-exceed="onExceed"
:on-error="onError"
:accept="limitType"
lazy
>
<div :disabled="disabled" class="uploadBtn" v-if="type === 0">
<i class="el-icon-plus"></i>
</div>
<el-button
:disabled="disabled"
size="small"
class="normal"
v-else-if="type === 1"
>上传文件
</el-button
>
</el-upload>
</div>
<div class="uploading" v-if="showLoading" v-show="progressShow">
<div class="left">
<i class="el-icon-paperclip"></i>
</div>
<div class="right">
<div class="right-top">
<span>{{ fileName }}</span>
<i class="el-icon-close" @click="cancelUpload = true"></i>
</div>
<div class="right-bottom">
<el-progress
:percentage="percentage"
:format="format"
:stroke-width="2"
:status="status"
></el-progress>
</div>
</div>
</div>
</div>
</template>

<script>
import store from "@/store";
import {mapGetters} from "vuex";
import Base64 from "@/utils/base64";

export default {
props: {
// 是否展示上传时的loading
showLoading: {
type: Boolean,
default: true,
},
maxLength: {
// 可上传的最大数量
type: Number,
default: 1,
},
// 图片集合
filesList: {
type: Array,
default: () => [],
},
// 上传按钮样式类型
type: {
type: Number,
default: 0,
},
// 是否禁用
disabled: {
type: Boolean,
default: false,
},
// 是否可删除文件
canDelete: {
type: Boolean,
default: true,
},
// 基础子文件夹
folder: {
type: String,
default: 'imsp'
},
// 最大允许上传的文件大小,默认单位M
maxSize: {
type: Number,
default: 100
},
// 限制的文件类型
limitType: {
type: String,
default: ''
}
},
data() {
return {
headers: {
'Authorization': 'Bearer ' + store.getters.access_token
},
upFileList: [],
percentage: 0,
fileName: '',
status: null,
progressShow: false,
showBigImg: '',
dialogVisible: false,
tempArr: [],
startImgIndex: '', //拖动前图片index
endImgIndex: '', // 结束拖动的index
cancelUpload: false,
}
},
computed: {
// fileOnlineShow是根据文件路径获取文件流的后端controller接口地址,如果是直接用nginx代理,则为nginx配置的路径
// fileUploadApi是上传文件的接口地址
...mapGetters(['fileUploadApi', 'fileOnlineShow'])
},
components: {},
mounted() {
this.upFileList = this.filesList
},
watch: {
filesList(val) {
this.upFileList = val
},
},
methods: {
// 拖动开始
dragstartEventImg($event, index) {
this.startImgIndex = index
},
// 拖动时
dragenterEventImg($event, index) {
this.endImgIndex = index
},
// 拖动结束
dragendEventImg($event, index) {
const currRow = this.upFileList.splice(this.startImgIndex, 1)[0]
this.upFileList.splice(this.endImgIndex, 0, currRow)
// this.$emit('getImgList', this.upFileList)
},
// 放置
dragoverEventImg(e) {
e.preventDefault()
},
// 预览文件
previewFile(file) {
window.open("https://preview.allbs.cn/onlinePreview?url=" + encodeURIComponent(new Base64().encode(location.origin + file)));
},
format(percentage) {
return percentage === 100 ? '99%' : `${percentage}%` //防止到100时后端处理要时间,会卡在100一小段时间,优化体验
},
// 超出限制
onExceed() {
this.$message.error('选择的文件超出个数限制')
},
// 删除图片
deleteFile(item) {
this.upFileList.splice(this.upFileList.findIndex(item => item.fileId === item.fileId), 1)
console.log("删除文件" + item.fileId);
this.$emit('deleteFileList', item.fileId)
},
// 上传时的钩子
progress(event, file, fileList) {
this.progressShow = true
this.percentage = Math.ceil(event.percent)
if (this.cancelUpload) {
this.$refs.imageUpload.abort()
this.$message.error('文件已取消上传')
this.upFileList = this.upFileList.filter((item) => {
return item.url
})
this.progressShow = false
}
},
// 上传失败
onError(err, file, fileList) {
this.$message.error('文件上传失败')
this.progressShow = false
this.status = null
this.percentage = 0
this.fileName = ''
},
// 文件上传前
beforeUploadFile(file) {
this.cancelUpload = false
if (file.size / 1024 / 1024 >= this.maxSize) {
this.$message.error('只能上传小于' + this.maxSize + 'M的图片!')
return false
}
},
// 文件上传成功
uploadFile(response, file, fileList) {
if (response.code === 200) {
this.upFileList.push(response.data)
this.$emit('addFileList', response.data.fileId)
this.$message.success('文件上传成功')
} else {
this.$message.error('文件上传失败')
}
// 进度条消失
this.progressShow = false
this.status = null
this.percentage = 0
this.fileName = ''
},
getFileType(url) {
return url.substring(url.lastIndexOf(".") + 1, url.length);
},
},
}
</script>

<style lang="scss" scoped>
.imgList {
display: flex;
flex-wrap: wrap;

div {
.img {
position: relative;

img {
cursor: pointer;
width: 64px;
height: 64px;
display: inline-block;
}

width: 64px;
height: 64px;
display: inline-block;
margin-right: 8px;

.el-icon-error {
font-size: 16px;
position: absolute;
right: -8px;
top: -8px;
color: red;

&::before {
cursor: pointer;
}
}
}
}
}

.normal {
&:hover {
color: #5d8eff !important;
background-color: #fff !important;
border-color: #5d8eff !important;
}

&::before {
content: '';
cursor: pointer;
background-image: url('/assets/images/upload.png');
background-size: 14px 14px;
display: inline-block;
width: 14px;
height: 14px;
transform: translateY(2px);
margin-right: 6px;
}
}

.uploadBtn {
width: 64px;
height: 64px;
background: #f7f8fa;
border: 1px solid #ebebe8;
position: relative;

i {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #bebec5;
font-size: 24px;
}
}

.uploading {
display: flex;
width: 162px;
margin-left: 8px;
margin-top: 28px;

.left {
margin-right: 8px;

i {
display: inline-block;
width: 14px;
height: 14px;
color: #000;
}
}

.right {
width: 140px;

.right-top {
display: flex;
justify-content: space-between;

span {
height: 22px;
font-size: 14px;
font-weight: 400;
color: rgba(0, 0, 0, 0.45);
line-height: 22px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 130px;
}
}
}

.el-icon-close {
line-height: 22px;
}
}

.disabledStyle {
cursor: not-allowed;
}

/deep/ .svg-icon {
width: 64px;
height: 64px;
cursor: pointer;
}
</style>

涉及到的svg图片