fileUpload.vue
9.66 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
<template>
<div>
<el-upload
:headers="headers"
:ref="uploadRef"
:accept="accept"
:action="action"
:auto-upload="autoUpload"
:before-remove="beforeRemove"
:class="isUpload === false ? 'hidebtn' : ''"
:data="fileOtherData"
:file-list="fileList"
:limit="limit"
:multiple="multiple"
:on-change="handleChange"
:on-error="handleError"
:on-exceed="handleExceed"
:on-preview="handlePreview"
:on-remove="handleRemove"
class="upload-demo"
>
<el-button v-if="isUpload" size="small" type="primary">点击上传</el-button>
</el-upload>
</div>
</template>
<script>
// import commonApi from '@/api/Common.js'
import { uploadUrl } from '@/utils/request'
import { copy } from '@/api/shop'
import { getToken } from '@/utils/auth'
export default {
name: 'FileUpload',
props: {
uploadRef: {
type: String,
default: ''
},
headers: {
type: Object,
default: () => ({
'Authorization-business': getToken()
})
},
// 是否多选
multiple: {
type: Boolean,
default: true
},
// 是否自动上传
autoUpload: {
type: Boolean,
default: true
},
// 是否上传文件
isUpload: {
type: Boolean,
default: true
},
// 最大允许上传个数
limit: {
type: Number,
default: null
},
// 允许上传的文件类型
accept: {
type: String,
default: ''
},
action: {
type: String,
default: uploadUrl
},
// 允许上传的文件大小 单位:字节
acceptSize: {
type: Number,
default: null
},
// 默认额外上传数据
fileOtherData: {
type: Object,
default: function() {
return {
id: null,
bizId: '',
bizType: '',
isSingle: false
}
}
}
},
data() {
return {
// 默认附件列表
fileList: [],
// 删除附件列表
removeFileAry: [],
// 新增附件列表
addFileAry: [],
// 上传成功的文件数
successNum: 0,
// 上传失败的文件数
errorNum: 0,
// 已上传的文件数
uploadTotalNum: 0,
// 是否上传失败
isUploadError: false
// action: `${process.env.VUE_APP_BASE_API}/attachment/upload`
}
},
computed: {},
methods: {
// 附件初始化
init({ id, bizId, bizType, isSingle, isDetail }) {
console.log(this.uploadRef)
const vm = this
vm.fileOtherData.bizId = bizId
vm.fileOtherData.id = id || ''
vm.fileOtherData.bizType = bizType
vm.fileOtherData.isSingle = isSingle || false
vm.fileList.length = 0
vm.removeFileAry = []
vm.addFileAry = []
vm.$emit('fileLengthVaild', 0)
if (isDetail) {
vm.getAttachment()
}
vm.successNum = 0
vm.errorNum = 0
vm.uploadTotalNum = 0
vm.$refs[vm.uploadRef].clearFiles()
},
handleChange(file, fileList) {
console.log(file, fileList, 'tets')
const vm = this
if (file.response) {
console.log(file.response, 'response')
vm.uploadTotalNum += 1
if (file.response.data.url !== '') {
const arr = []
fileList.forEach(element => {
arr.push(element.response.data.url)
})
this.$emit('getImgList', arr)
// vm.fileOtherData.bizId = file.response.data.bizId;
vm.successNum += 1
} else {
setTimeout(() => {
vm.$message({
message: file.name + '上传失败,原因:<br/>' + file.response.msg,
type: 'error',
dangerouslyUseHTMLString: true,
showClose: true,
duration: 10000,
onClose: msgs => {
copy(msgs['message'])
vm.$message({
message: '复制错误消息成功',
type: 'success'
})
}
})
}, 0)
vm.isUploadError = false
vm.errorNum += 1
}
vm.$emit(
'setId',
vm.uploadTotalNum === fileList.length && vm.errorNum <= 0,
file.response
)
} else {
if (vm.acceptSize) {
const isLtAcceptSize = file.size > vm.acceptSize
if (isLtAcceptSize) {
setTimeout(() => {
vm.$message.error(
'只能上传' +
vm.renderSize(vm.acceptSize) +
'的文件!已为您过滤文件:' +
file.name
)
}, 10)
fileList.forEach((item, index) => {
if (item.uid === file.uid) {
fileList.splice(index, 1)
}
})
} else {
if (!vm.isUploadError) {
vm.addFileAry.push(file.name)
}
vm.isUploadError = false
}
} else {
if (!vm.isUploadError) {
vm.addFileAry.push(file.name)
}
vm.isUploadError = false
}
vm.$emit('fileLengthVaild', vm.fileList.length + vm.addFileAry.length)
}
vm.$store.state.hasLoading = false
},
// 附件上传失败
handleError() {
const vm = this
vm.$message.error('附件上传失败,请重试')
vm.isUploadError = true
vm.$store.state.hasLoading = false
},
renderSize(value) {
if (value == null || value === '') {
return '0 B'
}
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
let index = 0
const srcsize = parseFloat(value)
index = Math.floor(Math.log(srcsize) / Math.log(1024))
let size = srcsize / Math.pow(1024, index)
size = size.toFixed(2)
if (unitArr[index]) {
return size + unitArr[index]
}
return '文件太大'
},
handlePreview(file) {
if (file.bizId) {
this.downLoadFile(file)
}
},
beforeRemove(file) {
return this.$confirm('确定移除' + file.name, '删除确认')
},
// 文件超出个数限制时的钩子
handleExceed() {
const vm = this
vm.$message('当前最多允许上传' + vm.limit + '个文件')
},
// 删除附件列表
handleRemove(file) {
const vm = this
if (file.bizId) {
vm.removeFileAry.push(file.id)
vm.fileList.map((item, index) => {
if (item.name === file.name) {
vm.fileList.splice(index, 1)
return false
}
})
} else {
vm.addFileAry.map((item, index) => {
if (item === file.name) {
vm.addFileAry.splice(index, 1)
return false
}
})
}
vm.$emit('fileLengthVaild', vm.fileList.length + vm.addFileAry.length)
},
// // 服务器删除附件
// async deleteAttachment() {
// const vm = this
// const res = await commonApi.deleteAttachment({
// ids: vm.removeFileAry
// })
// if (res.status === 200) {
// if (res.data.code !== 0) {
// vm.$message(res.data.msg)
// } else {
// vm.removeFileAry = []
// }
// }
// },
// // 查询附件
// async getAttachment() {
// const vm = this
// const res = await commonApi.getAttachment({
// bizIds: vm.fileOtherData.bizId,
// bizTypes: vm.fileOtherData.bizType
// })
// if (res.status === 200) {
// if (res.data.code === 0) {
// if (res.data.data.length > 0) {
// const data = res.data.data[0].list
// data.map(item => {
// item.name = item.submittedFileName
// })
// vm.fileList = data
// vm.$emit('fileLengthVaild', vm.fileList.length)
// }
// }
// }
// },
// // 查询附件
// async getAttachmentByArr(bizIds, bizTypes) {
// const vm = this
// const res = await commonApi.getAttachment({
// bizIds: bizIds,
// bizTypes: bizTypes
// })
// if (res.status === 200) {
// if (res.data.code === 0) {
// if (res.data.data.length > 0) {
// const data = res.data.data[0].list
// data.map(item => {
// item.name = item.submittedFileName
// })
// vm.fileList = data
// }
// }
// }
// },
// 返回附件新增及删除数组长度
handleBack() {
const vm = this
return {
addLength: vm.addFileAry.length,
removeLength: vm.removeFileAry.length
}
},
// 附件上传服务器触发方法
submitFile(id, bizId, bizType, tagId) {
const vm = this
vm.fileOtherData.id = id
if (bizId) {
vm.fileOtherData.bizId = bizId
vm.isUpload = true
}
if (tagId) {
vm.fileOtherData.tagId = tagId
}
vm.fileOtherData.bizType = bizType
if (vm.limitType(vm.$refs[vm.uploadRef].uploadFiles).length) {
return vm.$message.error('只能上传jpg/png/mp4格式的文件')
}
vm.$refs[vm.uploadRef].submit()
vm.addFileAry = []
console.log(this.fileOtherData)
},
// 附件下载
async downLoadFile(data) {
const link = document.createElement('a')
link.href = data.url
link.download = data.name
link.click()
window.URL.revokeObjectURL(link.href)
},
// 限制上传类型
limitType(arr) {
return arr.filter(item => {
const t = item.raw.type
return t.indexOf('image') === -1 && t.indexOf('video') === -1
})
}
}
}
</script>
<style>
.hidebtn .el-upload {
display: none;
}
</style>