-
Notifications
You must be signed in to change notification settings - Fork 78
/
fs-file-uploader.vue
541 lines (516 loc) · 13.2 KB
/
fs-file-uploader.vue
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
<template>
<div class="fs-file-uploader" :class="{ 'fs-file-uploader-limit': computedOnLimit() }">
<component :is="$fsui.upload.name" ref="fileUploaderRef" v-model:fileList="fileList" v-bind="computedBinding">
<component :is="computedFileSelectBtn.is" v-bind="computedFileSelectBtn" />
</component>
<fs-uploader ref="uploaderImplRef" :type="uploader?.type" />
<component
:is="$fsui.dialog.name"
v-if="isPicture()"
v-model:[$fsui.dialog.visible]="previewVisible"
v-bind="computedPreview"
>
<img style="width: 100%" :src="previewImage" />
</component>
</div>
</template>
<script>
import { computed, ref, watch } from "vue";
import { uiContext, useI18n } from "@fast-crud/fast-crud";
import _ from "lodash-es";
import FsUploader from "./fs-uploader.vue";
/**
* 文件上传组件
* 支持对应ui库的[x]-file-uploader组件的配置
*/
export default {
name: "FsFileUploader",
components: { FsUploader },
inheritAttrs: false,
props: {
/**
* value
*/
modelValue: {},
/**
* 限制文件数量
*/
limit: {
type: Number
},
/**
* 文件大小限制,单位:字节
* 可以直接传一个数字,也可以传 {limit:number,tip:'自定义提示文本'}
*/
sizeLimit: {
type: [Number, Object]
},
/**
* 构建url的方法
* 后台返回key之后,将其build成一个可访问的url,用于反显
*/
buildUrl: {
default() {
return (value) => {
return value;
};
}
},
buildUrls: {
default() {
return async (urls) => {
return urls;
};
}
},
/**
* 上传按钮配置,参考FsButton参数
*/
button: {
type: Object
},
/**
* 展示模式,对应[x]-uploader组件的 listType参数
*/
listType: {
type: String
},
/**
* 上传前的判断操作
*/
beforeUpload: {
type: Function
},
/**
* fs-uploader的配置
*/
uploader: {
type: Object
},
/**
* 预览配置
*/
preview: {
type: Object
},
/**
* 返回值类型
* 支持:`[url,key,object]`
*/
valueType: {
type: String, // url ,key, object
default: "url"
} //''
},
emits: ["change", "update:modelValue", "success", "exceed"],
setup(props, ctx) {
const ui = uiContext.get();
const { t } = useI18n();
// uploader 的 modelValue
const fileList = ref([]);
const currentValue = ref();
const fileListLocal = ref([]);
const fileUploaderRef = ref();
function pickFileName(url) {
return url.substring(url.lastIndexOf("/") + 1);
}
function buildOneToFile(value) {
if (typeof value === "string") {
return {
value,
name: pickFileName(value),
url: props.buildUrl(value),
uid: Math.random()
};
}
return value;
}
function buildOneToValue(file) {
const res = file.response || file.fsRes;
const value = {
size: file.size,
name: file.name,
uid: file.uid,
...(res != null ? res : file)
};
if (props.valueType === "object") {
return value;
}
return value[props.valueType];
}
function initValue(value) {
const array = [];
if (value == null || value.length === 0) {
fileList.value = array;
return;
}
if (value instanceof Array) {
_.forEach(value, (item) => {
array.push(buildOneToFile(item));
});
} else {
array.push(buildOneToFile(value));
}
fileList.value = array;
}
initValue(props.modelValue);
updateLocalFileList(fileList.value);
function onChange(value) {
ctx.emit("change", value);
}
function onInput(value) {
currentValue.value = value;
ctx.emit("update:modelValue", value);
}
watch(
() => {
return props.modelValue;
},
(value) => {
if (value === currentValue.value) {
return;
}
initValue(value);
}
);
function buildEmitValue(fList) {
if (fList == null || fList.length === 0) {
return [];
}
if (props.limit === 1) {
//单个文件
return buildOneToValue(fList[0]);
}
const array = [];
_.forEach(fList, (item) => {
array.push(buildOneToValue(item));
});
return array;
}
function hasUploading() {
const uploading = fileListLocal.value.filter((item) => {
return item.status === ui.upload.status.uploading;
});
return uploading.length > 0;
}
function emitValue(list) {
let value = buildEmitValue(list);
onInput(value);
onChange(value);
}
function handleChange(file, list) {
updateLocalFileList(list);
emitValue(list);
}
function handleSuccess(res, file, list) {
ctx.emit("success", { res, file, fileList: list });
handleChange(file, list);
}
const uploaderImplRef = ref();
function computeFileSize(fileSize) {
let sizeTip = fileSize;
if (fileSize > 1024 * 1024 * 1024) {
sizeTip = (fileSize / (1024 * 1024 * 1024)).toFixed(2) + "G";
} else if (fileSize > 1024 * 1024) {
sizeTip = (fileSize / (1024 * 1024)).toFixed(2) + "M";
} else {
sizeTip = Math.round(fileSize / 1024) + "K";
}
return sizeTip;
}
const computedOnLimit = (isCheck = false) => {
const add = isCheck ? ui.upload.limitAdd : 0;
return props.limit > 0 && fileListLocal.value.length >= props.limit + add;
};
function showLimitTip() {
ui.message.warn(t("fs.extends.fileUploader.limitTip", [props.limit]));
}
function checkLimit() {
if (computedOnLimit(true)) {
showLimitTip();
throw new Error("文件数量超限");
}
}
function checkSizeLimit(file) {
if (props.sizeLimit != null) {
let limit = props.sizeLimit;
let showMessage = null;
if (typeof props.sizeLimit === "number") {
showMessage = (fileSize, limit) => {
const limitTip = computeFileSize(limit);
const fileSizeTip = computeFileSize(file.size);
ui.message.warn(t("fs.extends.fileUploader.sizeLimitTip", [limitTip, fileSizeTip]));
};
} else {
limit = props.sizeLimit.limit;
showMessage = props.sizeLimit.tip;
}
if (file.size > limit) {
console.log("文件大小超过限制:", file.size);
showMessage(file.size, limit);
throw new Error("文件大小超过限制:" + file.size);
}
}
}
const beforeUpload = async (file) => {
if (props.beforeUpload) {
const ret = await props.beforeUpload({ file, fileList: fileListLocal.value });
if (ret === false) {
return;
}
}
checkLimit();
checkSizeLimit(file);
};
function updateLocalFileList(list) {
fileListLocal.value = list;
}
async function doUpload(option) {
option.options = props.uploader;
let uploaderRef = uploaderImplRef.value.getUploaderRef();
if (uploaderRef == null) {
throw new Error("Sorry,The component is not ready yet");
}
return await uploaderRef?.upload(option);
}
async function customRequest(context) {
const { file, onProgress, onSuccess, onError } = context;
const option = {
file,
fileName: file.name,
onProgress
};
try {
const ret = await doUpload(option);
onSuccess(ret);
} catch (e) {
console.error("上传失败", e);
onError(e);
}
}
const computedFileSelectBtn = computed(() => {
if (isPictureCard()) {
return {
is: "FsIcon",
icon: ui.icons.plus
};
}
return {
is: "FsButton",
icon: ui.icons.upload,
text: t("fs.extends.fileUploader.text"),
...props.button
};
});
const previewVisible = ref(false);
const previewImage = ref();
const computedPreview = computed(() => {
return {
...ui.dialog.footer(),
...props.preview
};
});
function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
});
}
function isPicture() {
return props.listType === ui.upload.typeImageCard || props.listType === ui.upload.typeImage;
}
function isPictureCard() {
return props.listType === ui.upload.typeImageCard;
}
const handlePreview = async (file) => {
if (!isPicture()) {
let url;
if (file.url) {
url = file.url;
} else {
if (ui.type === "antdv") {
url = file.response.url;
} else if (ui.type === "element") {
url = file.fsRes.url;
} else {
url = file.url;
}
}
window.open(url, "_blank");
}
if (!file.url && !file.preview && file.originFileObj) {
file.preview = await getBase64(file.originFileObj);
}
previewImage.value = file.url || file.preview;
previewVisible.value = true;
};
function buildAntdvBinding() {
return {
customRequest,
beforeUpload,
listType: props.listType,
onChange: (change) => {
const { file, fileList } = change;
handleChange(file, fileList);
},
onPreview: handlePreview
};
}
function buildElementBinding() {
return {
action: "",
listType: props.listType,
beforeUpload: beforeUpload,
httpRequest: customRequest,
onExceed: () => {
checkLimit();
ctx.emit("exceed", { fileList: fileListLocal.value });
},
onRemove: (file, fileList) => {
handleChange(file, fileList);
},
onChange: (file, fileList) => {
handleChange(file, fileList);
},
onSuccess: (res, file, fileList) => {
if (res == null) {
return;
}
file.response = res;
file.fsRes = res;
handleSuccess(res, file, fileList);
},
// "on-error": "handleUploadFileError",
// "on-progress": "handleUploadProgress"
onPreview: handlePreview
};
}
function buildNaiveBinding() {
return {
action: "",
listType: props.listType,
onBeforeUpload: async ({ file, fileList }) => {
return beforeUpload(file, fileList);
},
customRequest: (context) => {
const fileInfo = context.file;
customRequest({
...context,
file: fileInfo.file,
onSuccess: (res) => {
_.merge(fileInfo, res);
context.onFinish();
},
onProgress: (opts) => {
context.onProgress(opts);
}
});
},
onExceed: () => {
checkLimit();
ctx.emit("exceed", { fileList: fileList.value });
},
onRemove: (file) => {
handleChange(file, fileList.value);
},
onChange: ({ event, file, fileList }) => {
handleChange(file, fileList);
},
onFinish: (file) => {
handleSuccess({}, file, fileList.value);
},
onPreview: handlePreview
};
}
const computedBinding = computed(() => {
let binding = null;
if (ui.type === "antdv") {
binding = buildAntdvBinding();
} else if (ui.type === "element") {
binding = buildElementBinding();
} else {
binding = buildNaiveBinding();
}
return {
...binding,
...ctx.attrs
};
});
return {
fileList,
fileListLocal,
fileUploaderRef,
initValue,
onChange,
onInput,
hasUploading,
isPicture,
uploaderImplRef,
computedFileSelectBtn,
previewVisible,
previewImage,
computedPreview,
computedOnLimit,
computedBinding
};
}
};
</script>
<style lang="less">
.fs-file-uploader {
// antdv
&.fs-file-uploader-limit {
.ant-upload-select-picture-card {
display: none;
}
}
.ant-upload-list-item-actions {
display: flex;
align-items: center;
> a {
display: flex;
align-items: center;
}
}
.el-upload {
justify-content: left;
}
// element
.el-upload-list--picture-card .el-upload-list__item {
width: 100px;
height: 100px;
//line-height: 100px;
}
.el-upload--picture-card {
width: 100px;
height: 100px;
justify-content: center;
}
&.fs-file-uploader-limit {
.el-upload--picture-card {
display: none;
}
}
.el-upload--text {
width: 100%;
text-align: left;
.el-upload-dragger {
width: 100%;
.el-button {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
.el-icon-upload {
margin: 0;
margin-bottom: 10px;
}
}
}
}
}
</style>