@ebiz/designer-components 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/package.json +3 -2
  2. package/src/apiService/mockDataService.js +116 -0
  3. package/src/apiService/simpleDataService.js +186 -80
  4. package/src/components/Button.vue +72 -22
  5. package/src/components/EbizAvatar.vue +116 -0
  6. package/src/components/EbizCheckbox.vue +94 -0
  7. package/src/components/EbizCheckboxGroup.vue +70 -0
  8. package/src/components/EbizDetailBlock.vue +82 -0
  9. package/src/components/EbizDialog.vue +244 -56
  10. package/src/components/EbizEmployeeInfo.vue +139 -0
  11. package/src/components/EbizFileUpload.vue +202 -0
  12. package/src/components/EbizPageHeader.vue +96 -0
  13. package/src/components/EbizPagination.vue +163 -0
  14. package/src/components/EbizRadio.vue +87 -0
  15. package/src/components/EbizRadioGroup.vue +84 -0
  16. package/src/components/EbizRemoteSelect.vue +118 -40
  17. package/src/components/EbizSpace.vue +101 -0
  18. package/src/components/EbizStatistic.vue +150 -0
  19. package/src/components/EbizSwiper.vue +114 -0
  20. package/src/components/EbizSwiperItem.vue +14 -0
  21. package/src/components/EbizSwitch.vue +86 -0
  22. package/src/components/EbizTabHeader.vue +145 -0
  23. package/src/components/EbizTabPanel.vue +23 -0
  24. package/src/components/EbizTable.vue +466 -0
  25. package/src/components/EbizTableColumn.vue +117 -0
  26. package/src/components/EbizTableSort.vue +181 -0
  27. package/src/components/EbizTabs.vue +133 -91
  28. package/src/components/EbizTimePicker.vue +144 -0
  29. package/src/components/EbizTitle.vue +3 -10
  30. package/src/components/EbizTree.vue +153 -0
  31. package/src/components/EbizTreeSelector.vue +423 -0
  32. package/src/components/Home.vue +8 -0
  33. package/src/components/TdesignAlert.vue +116 -0
  34. package/src/components/TdesignButton.vue +130 -0
  35. package/src/components/TdesignCalendar/index.vue +146 -0
  36. package/src/components/TdesignCard.vue +196 -0
  37. package/src/components/TdesignCol.vue +102 -0
  38. package/src/components/TdesignCollapse.vue +143 -0
  39. package/src/components/TdesignCollapsePanel.vue +80 -0
  40. package/src/components/TdesignDatePicker.vue +125 -0
  41. package/src/components/TdesignDialog.vue +226 -0
  42. package/src/components/TdesignForm.vue +134 -0
  43. package/src/components/TdesignFormItem.vue +106 -0
  44. package/src/components/TdesignGrid.vue +56 -0
  45. package/src/components/TdesignIcon.vue +68 -0
  46. package/src/components/TdesignImage.vue +163 -0
  47. package/src/components/TdesignImageViewer.vue +201 -0
  48. package/src/components/TdesignInput.vue +243 -0
  49. package/src/components/TdesignSelect.vue +445 -0
  50. package/src/components/TdesignTag.vue +118 -0
  51. package/src/components/TdesignTextarea.vue +143 -0
  52. package/src/components/TdesignTimeline.vue +58 -0
  53. package/src/components/TdesignTimelineItem.vue +72 -0
  54. package/src/components/TdesignUpload.vue +757 -0
  55. package/src/components/TdesignWatermark.vue +108 -0
  56. package/src/index.js +130 -0
  57. package/src/main.js +20 -4
  58. package/src/router/index.js +244 -5
  59. package/src/views/Button.vue +7 -3
  60. package/src/views/CheckboxDemo.vue +105 -0
  61. package/src/views/DialogDemo.vue +126 -0
  62. package/src/views/EbizAvatar.vue +224 -0
  63. package/src/views/EbizDetailBlockDemo.vue +31 -0
  64. package/src/views/EbizEmployeeInfo.vue +250 -0
  65. package/src/views/EbizRadioDemo.vue +152 -0
  66. package/src/views/EbizSpace.vue +186 -0
  67. package/src/views/EbizSwiper.vue +158 -0
  68. package/src/views/GridDemo.vue +239 -0
  69. package/src/views/Home.vue +63 -2
  70. package/src/views/PageHeaderDemo.vue +105 -0
  71. package/src/views/PaginationDemo.vue +97 -0
  72. package/src/views/RemoteSelect.vue +336 -5
  73. package/src/views/StatisticDemo.vue +191 -0
  74. package/src/views/SwitchDemo.vue +80 -0
  75. package/src/views/TableDemo.vue +335 -0
  76. package/src/views/TableSortDemo.vue +144 -0
  77. package/src/views/TableView.vue +69 -0
  78. package/src/views/TabsDemo.vue +283 -0
  79. package/src/views/TagDemo.vue +102 -0
  80. package/src/views/TdesignAlert.vue +99 -0
  81. package/src/views/TdesignButton.vue +191 -0
  82. package/src/views/TdesignCalendar.vue +95 -0
  83. package/src/views/TdesignCard.vue +297 -0
  84. package/src/views/TdesignCollapse.vue +294 -0
  85. package/src/views/TdesignDatePicker.vue +188 -0
  86. package/src/views/TdesignForm.vue +249 -0
  87. package/src/views/TdesignIcon.vue +204 -0
  88. package/src/views/TdesignImage.vue +216 -0
  89. package/src/views/TdesignImageViewer.vue +199 -0
  90. package/src/views/TdesignInput.vue +253 -0
  91. package/src/views/TdesignSelect.vue +474 -0
  92. package/src/views/TdesignSwiper.vue +158 -0
  93. package/src/views/TextareaDemo.vue +94 -0
  94. package/src/views/TimePickerDemo.vue +147 -0
  95. package/src/views/TimelineDemo.vue +161 -0
  96. package/src/views/TreeDemo.vue +255 -0
  97. package/src/views/TreeSelectorDemo.vue +246 -0
  98. package/src/views/UploadDemo.vue +122 -0
  99. package/src/views/WatermarkDemo.vue +86 -0
@@ -0,0 +1,757 @@
1
+ <template>
2
+ <t-upload :accept="accept" :action="internalAction" :allowUploadDuplicateFile="allowUploadDuplicateFile"
3
+ :autoUpload="autoUpload" :beforeUpload="handleBeforeUpload" :data="data" :disabled="disabled"
4
+ :draggable="draggable" :fileListDisplay="fileListDisplay" :files="internalFiles" :format="format"
5
+ :formatRequest="formatRequest" :headers="headers" :isBatchUpload="isBatchUpload" :max="max" :method="method"
6
+ :multiple="multiple" :name="name" :placeholder="placeholder"
7
+ :showUploadProgress="showUploadProgress" :sizeLimit="sizeLimit" :status="status" :theme="theme" :tips="tips"
8
+ :uploadAllFilesInOneRequest="uploadAllFilesInOneRequest" :uploadButton="uploadButton"
9
+ :useMockProgress="useMockProgress" :withCredentials="withCredentials" v-model="modelValue"
10
+ @change="handleChange" @click="handleClick" @drag="handleDrag" @drop="handleDrop" @fail="handleFail"
11
+ @preview="handlePreview" @progress="handleProgress" @remove="handleRemove" @select-change="handleSelectChange"
12
+ @success="handleSuccess" @validate="handleValidate">
13
+ <!-- 默认插槽 -->
14
+ <slot></slot>
15
+
16
+ <!-- 文件列表项插槽 -->
17
+ <template v-if="$slots.fileListDisplay" #fileListDisplay="slotProps">
18
+ <slot name="fileListDisplay" v-bind="slotProps"></slot>
19
+ </template>
20
+
21
+ <!-- 替换上传按钮插槽 -->
22
+ <template v-if="$slots.default" #default>
23
+ <slot name="default"></slot>
24
+ </template>
25
+
26
+ <!-- 单文件已上传内容插槽 -->
27
+ <template v-if="$slots.content" #content="slotProps">
28
+ <slot name="content" v-bind="slotProps"></slot>
29
+ </template>
30
+
31
+ <!-- 文件拖拽区域内容插槽 -->
32
+ <template v-if="$slots.dragContent" #dragContent>
33
+ <slot name="dragContent"></slot>
34
+ </template>
35
+
36
+ <!-- 上传按钮内容插槽 -->
37
+ <template v-if="$slots.trigger" #trigger>
38
+ <slot name="trigger"></slot>
39
+ </template>
40
+
41
+ <!-- 上传提示文本插槽 -->
42
+ <template v-if="$slots.tips" #tips>
43
+ <slot name="tips"></slot>
44
+ </template>
45
+ </t-upload>
46
+ </template>
47
+
48
+ <script>
49
+ export default {
50
+ name: "EbizUpload"
51
+ }
52
+ </script>
53
+
54
+ <script setup>
55
+ import { computed, defineProps, defineEmits, ref, onMounted, watch } from 'vue';
56
+ import { Upload as TUpload } from 'tdesign-vue-next';
57
+ import dataService from '../apiService/simpleDataService';
58
+
59
+ // 内部上传地址常量
60
+ const INTERNAL_UPLOAD_URL = '/api/file/app/td-upload';
61
+
62
+ // 内部维护的文件列表,代替直接使用props.files
63
+ const internalFiles = ref([]);
64
+
65
+ // 初始化内部文件列表
66
+ const initializeFiles = () => {
67
+ // 如果提供了files,使用它初始化
68
+ if (props.files && Array.isArray(props.files)) {
69
+ internalFiles.value = [...props.files];
70
+ }
71
+ };
72
+
73
+ // 默认的格式化响应函数
74
+ const defaultFormatResponse = (res) => {
75
+ console.log('Default formatResponse handling:', res);
76
+
77
+ // 处理不同的响应格式
78
+ if (res === null || res === undefined) {
79
+ return { url: '', error: '上传失败:未收到服务器响应' };
80
+ }
81
+
82
+ // 如果res已经是字符串(文件路径),直接使用
83
+ if (typeof res === 'string') {
84
+ return { url: res };
85
+ }
86
+
87
+ // 如果返回的就是服务器的原始响应 { code: 0, msg: '上传成功', data: '文件路径' }
88
+ // axios拦截器应该已经提取了data部分,但我们做个双重检查
89
+ if (res.code === 0 && res.data) {
90
+ return { url: typeof res.data === 'string' ? res.data : '' };
91
+ }
92
+
93
+ // 如果是自定义对象,可能已经处理过
94
+ if (res.url) {
95
+ return { url: res.url };
96
+ }
97
+
98
+ // 直接处理服务器响应的标准格式
99
+ if (typeof res === 'string') {
100
+ // 直接是文件路径字符串
101
+ return { url: res };
102
+ }
103
+
104
+ // 如果服务器直接响应了数据,没有包装
105
+ if (res && !res.code && !res.data && typeof res === 'object') {
106
+ // 尝试找到可能的URL字段
107
+ if (res.url || res.path || res.filePath || res.fileUrl) {
108
+ return { url: res.url || res.path || res.filePath || res.fileUrl };
109
+ }
110
+ }
111
+
112
+ // 处理其他可能的情况
113
+ console.warn('Unknown response format:', res);
114
+
115
+ // 无法识别的格式,返回空URL
116
+ return { url: '' };
117
+ };
118
+
119
+ const props = defineProps({
120
+ // v-model值,已上传和待上传的文件列表
121
+ modelValue: {
122
+ type: Array,
123
+ default: () => []
124
+ },
125
+ // 接受上传的文件类型,同input标签的accept属性
126
+ accept: {
127
+ type: String,
128
+ default: undefined
129
+ },
130
+ // 上传的接口地址,可选,如不提供则使用内部dataService上传
131
+ action: {
132
+ type: String,
133
+ default: ''
134
+ },
135
+ // 是否允许重复上传相同名称的文件
136
+ allowUploadDuplicateFile: {
137
+ type: Boolean,
138
+ default: false
139
+ },
140
+ // 是否在选择文件后自动上传
141
+ autoUpload: {
142
+ type: Boolean,
143
+ default: true
144
+ },
145
+ // 上传文件之前的钩子,返回值决定是否上传
146
+ beforeUpload: {
147
+ type: Function,
148
+ default: undefined
149
+ },
150
+ // 上传请求时附带的自定义数据
151
+ data: {
152
+ type: Object,
153
+ default: () => ({})
154
+ },
155
+ // 是否禁用
156
+ disabled: {
157
+ type: Boolean,
158
+ default: false
159
+ },
160
+ // 是否启用拖拽上传
161
+ draggable: {
162
+ type: Boolean,
163
+ default: false
164
+ },
165
+ // 文件列表的展示方式
166
+ fileListDisplay: {
167
+ type: Function,
168
+ default: undefined
169
+ },
170
+ // 已上传的文件列表,用于初始化内部文件列表
171
+ files: {
172
+ type: Array,
173
+ default: () => []
174
+ },
175
+ // 文件上传前转换文件的函数
176
+ format: {
177
+ type: Function,
178
+ default: undefined
179
+ },
180
+ // 用于格式化上传请求,可以处理请求参数等
181
+ formatRequest: {
182
+ type: Function,
183
+ default: undefined
184
+ },
185
+ // 上传请求的请求头
186
+ headers: {
187
+ type: Object,
188
+ default: () => ({})
189
+ },
190
+ // 是否批量上传
191
+ isBatchUpload: {
192
+ type: Boolean,
193
+ default: false
194
+ },
195
+ // 用于控制文件上传数量
196
+ max: {
197
+ type: Number,
198
+ default: 0
199
+ },
200
+ // 上传请求的HTTP方法
201
+ method: {
202
+ type: String,
203
+ default: 'POST',
204
+ validator: (val) => ['POST', 'GET', 'PUT', 'OPTIONS', 'PATCH', 'post', 'get', 'put', 'options', 'patch'].includes(val)
205
+ },
206
+ // 是否支持多文件上传
207
+ multiple: {
208
+ type: Boolean,
209
+ default: false
210
+ },
211
+ // 上传字段名
212
+ name: {
213
+ type: String,
214
+ default: 'file'
215
+ },
216
+ // 占位符
217
+ placeholder: {
218
+ type: String,
219
+ default: undefined
220
+ },
221
+ // 自定义上传方法
222
+ requestMethod: {
223
+ type: Function,
224
+ default: undefined
225
+ },
226
+ // 是否显示上传进度
227
+ showUploadProgress: {
228
+ type: Boolean,
229
+ default: true
230
+ },
231
+ // 文件大小限制,单位:KB
232
+ sizeLimit: {
233
+ type: [Number, Object],
234
+ default: undefined
235
+ },
236
+ // 上传状态
237
+ status: {
238
+ type: String,
239
+ default: undefined,
240
+ validator: (val) => ['default', 'success', 'warning', 'error'].includes(val)
241
+ },
242
+ // 上传组件风格
243
+ theme: {
244
+ type: String,
245
+ default: 'file',
246
+ validator: (val) => ['custom', 'file', 'file-input', 'file-flow', 'image', 'image-flow'].includes(val)
247
+ },
248
+ // 上传提示文本
249
+ tips: {
250
+ type: String,
251
+ default: ''
252
+ },
253
+ // 是否在同一个请求中上传多个文件
254
+ uploadAllFilesInOneRequest: {
255
+ type: Boolean,
256
+ default: false
257
+ },
258
+ // 上传按钮属性
259
+ uploadButton: {
260
+ type: Object,
261
+ default: () => ({})
262
+ },
263
+ // 是否使用模拟的上传进度
264
+ useMockProgress: {
265
+ type: Boolean,
266
+ default: true
267
+ },
268
+ // 上传请求时是否携带cookies
269
+ withCredentials: {
270
+ type: Boolean,
271
+ default: false
272
+ },
273
+ // 是否使用内部上传服务,如果为true则忽略action参数
274
+ useInternalUpload: {
275
+ type: Boolean,
276
+ default: false
277
+ }
278
+ });
279
+
280
+ const emit = defineEmits([
281
+ 'update:modelValue',
282
+ 'change',
283
+ 'click',
284
+ 'drag',
285
+ 'drop',
286
+ 'fail',
287
+ 'preview',
288
+ 'progress',
289
+ 'remove',
290
+ 'select-change',
291
+ 'success',
292
+ 'validate',
293
+ 'update:files'
294
+ ]);
295
+
296
+ // 当前上传进度
297
+ const uploadProgress = ref(0);
298
+
299
+ // 上传列表双向绑定
300
+ const modelValue = computed({
301
+ get() {
302
+ // 优先返回内部文件列表,确保上传状态正确
303
+ if (internalFiles.value.length > 0) {
304
+ return internalFiles.value;
305
+ }
306
+ return props.modelValue;
307
+ },
308
+ set(value) {
309
+ // 同时更新内部文件列表和v-model
310
+ internalFiles.value = value;
311
+ emit('update:modelValue', value);
312
+ }
313
+ });
314
+
315
+ // 监听props.modelValue的变化,同步到internalFiles
316
+ watch(
317
+ () => props.modelValue,
318
+ (newValue) => {
319
+ if (newValue && Array.isArray(newValue) && newValue.length > 0) {
320
+ console.log('External modelValue changed, updating internal files:', newValue);
321
+ // 过滤掉可能的空值
322
+ const validFiles = newValue.filter(file => file && file.name);
323
+ if (validFiles.length > 0) {
324
+ internalFiles.value = validFiles;
325
+ }
326
+ }
327
+ },
328
+ { deep: true }
329
+ );
330
+
331
+ // 内部上传地址,如果提供了action且未启用内部上传则使用action,否则使用内部默认地址
332
+ const internalAction = computed(() => {
333
+ return (props.action && !props.useInternalUpload) ? props.action : INTERNAL_UPLOAD_URL;
334
+ });
335
+
336
+ // 自定义上传方法,使用dataService进行上传
337
+ const customRequestMethod = (options) => {
338
+ console.log('Upload options:', options);
339
+
340
+ try {
341
+ // 解构选项,获取需要的属性
342
+ const { raw, onProgress, onSuccess, onError, data, method, name } = options || {};
343
+
344
+ // 创建FormData
345
+ const formData = new FormData();
346
+
347
+ // 创建一个执行上传的函数
348
+ const executeUpload = (fileToUpload) => {
349
+ console.log('Uploading file:', fileToUpload.name, fileToUpload.size);
350
+ formData.append('file', fileToUpload);
351
+
352
+ // 添加额外数据
353
+ if (data) {
354
+ Object.keys(data).forEach(key => {
355
+ formData.append(key, data[key]);
356
+ });
357
+ }
358
+
359
+ // 定义安全的进度回调
360
+ const safeProgressCallback = (progress) => {
361
+ uploadProgress.value = progress;
362
+ if (typeof onProgress === 'function') {
363
+ onProgress({ percent: progress });
364
+ }
365
+ };
366
+
367
+ // 使用dataService上传文件
368
+ return dataService
369
+ .upload(INTERNAL_UPLOAD_URL, formData, safeProgressCallback)
370
+ .then(response => {
371
+ console.log('Upload response:', response);
372
+
373
+ // 在组件内部处理响应数据
374
+ let formattedResponse;
375
+
376
+ try {
377
+ // 使用内部处理逻辑,不依赖props.formatResponse
378
+ formattedResponse = defaultFormatResponse(response);
379
+ } catch (error) {
380
+ console.error('Error formatting response:', error);
381
+ formattedResponse = { url: '', error: error.message };
382
+ }
383
+
384
+ // 确保格式化后的响应包含文件基本信息
385
+ const fileObject = {
386
+ ...formattedResponse,
387
+ name: fileToUpload.name,
388
+ size: fileToUpload.size,
389
+ type: fileToUpload.type,
390
+ status: 'success',
391
+ raw: fileToUpload, // 保存原始文件对象
392
+ lastModified: fileToUpload.lastModified
393
+ };
394
+
395
+ // 构建TDesign期望的成功响应对象
396
+ const successResponse = {
397
+ file: fileObject, // 文件对象
398
+ fileList: [fileObject], // 文件列表(可能包含多个文件)
399
+ response: {
400
+ url: fileObject.url, // 必需的URL字段
401
+ files: [fileObject], // 必需的files字段
402
+ data: response // 保留原始响应
403
+ },
404
+ e: { status: 'success' }, // 事件对象
405
+ status: 'success' // 必须的状态字段,只能是success或fail
406
+ };
407
+
408
+ console.log('Final formatted response:', successResponse);
409
+
410
+ // 安全地调用成功回调
411
+ if (typeof onSuccess === 'function') {
412
+ onSuccess(successResponse);
413
+ }
414
+ return successResponse;
415
+ })
416
+ .catch(error => {
417
+ console.error('Upload failed:', error);
418
+
419
+ // 构建错误响应对象
420
+ const failResponse = {
421
+ error: error,
422
+ status: 'fail', // 必须的状态字段,值为fail
423
+ e: { status: 'fail' }
424
+ };
425
+
426
+ // 安全地调用错误回调
427
+ if (typeof onError === 'function') {
428
+ onError(failResponse);
429
+ }
430
+ return Promise.reject(failResponse);
431
+ });
432
+ };
433
+
434
+ // 尝试获取真实的文件对象
435
+ // 1. 首先检查raw是否直接是File对象
436
+ if (raw instanceof File) {
437
+ console.log('Using raw as File directly');
438
+ return executeUpload(raw);
439
+ }
440
+
441
+ // 2. 直接从options本身获取信息创建文件
442
+ if (options && options.name && options.type && options.size) {
443
+ // 我们找到了文件信息,但没有实际内容
444
+ // 我们需要使用FileReader获取实际文件或创建一个伪文件
445
+ console.log('Creating file from options properties');
446
+
447
+ // 由于我们无法获取真实文件内容,创建一个伪文件
448
+ // 注意:这在实际上传时可能会失败,因为没有真实内容
449
+ const pseudoFileContent = new Blob([`Pseudo file content for ${options.name}`], { type: options.type });
450
+ const pseudoFile = new File([pseudoFileContent], options.name, {
451
+ type: options.type,
452
+ lastModified: options.lastModified || Date.now()
453
+ });
454
+
455
+ // 创建一个隐藏的文件输入,让用户重新选择文件
456
+ // 这是一个备选方案
457
+ const fileInput = document.createElement('input');
458
+ fileInput.type = 'file';
459
+ fileInput.style.display = 'none';
460
+ fileInput.accept = options.type;
461
+ document.body.appendChild(fileInput);
462
+
463
+ // 提示用户选择同一个文件
464
+ console.log('Please select the same file again');
465
+ alert(`上传出错:TDesign上传组件无法获取文件内容。请在出现的文件选择框中重新选择"${options.name}"文件。`);
466
+
467
+ return new Promise((resolve, reject) => {
468
+ fileInput.onchange = (e) => {
469
+ const selectedFile = e.target.files[0];
470
+ if (selectedFile) {
471
+ console.log('User selected file:', selectedFile.name);
472
+ document.body.removeChild(fileInput);
473
+ resolve(executeUpload(selectedFile));
474
+ } else {
475
+ document.body.removeChild(fileInput);
476
+ const failResponse = {
477
+ error: new Error('No file selected'),
478
+ status: 'fail',
479
+ e: { status: 'fail' }
480
+ };
481
+ reject(failResponse);
482
+ }
483
+ };
484
+
485
+ fileInput.onerror = (error) => {
486
+ document.body.removeChild(fileInput);
487
+ const failResponse = {
488
+ error: error,
489
+ status: 'fail',
490
+ e: { status: 'fail' }
491
+ };
492
+ reject(failResponse);
493
+ };
494
+
495
+ // 触发文件选择
496
+ fileInput.click();
497
+ });
498
+ }
499
+
500
+ console.error('Cannot find valid file information', options);
501
+ if (typeof onError === 'function') {
502
+ const failResponse = {
503
+ error: new Error('No valid file information found'),
504
+ status: 'fail',
505
+ e: { status: 'fail' }
506
+ };
507
+ onError(failResponse);
508
+ }
509
+ return Promise.reject({
510
+ error: new Error('No valid file information found'),
511
+ status: 'fail',
512
+ e: { status: 'fail' }
513
+ });
514
+ } catch (err) {
515
+ console.error('Upload error in customRequestMethod:', err);
516
+ return Promise.reject({
517
+ error: err,
518
+ status: 'fail',
519
+ e: { status: 'fail' }
520
+ });
521
+ }
522
+ };
523
+
524
+ // 上传前钩子,如果设置了beforeUpload则先执行
525
+ const handleBeforeUpload = (file, options) => {
526
+ if (props.beforeUpload) {
527
+ return props.beforeUpload(file, options);
528
+ }
529
+ return true;
530
+ };
531
+
532
+ // 监听internalFiles的变化,记录调试信息
533
+ watch(
534
+ internalFiles,
535
+ (newFiles) => {
536
+ console.log('Internal files changed:', newFiles);
537
+ },
538
+ { deep: true }
539
+ );
540
+
541
+ // 文件状态变化事件
542
+ const handleChange = (value, context) => {
543
+ console.log('Upload change event:', value, context);
544
+
545
+ // 如果是上传成功,确保文件被添加到内部列表
546
+ if (context && context.file && context.file.status === 'success') {
547
+ const successFile = context.file;
548
+
549
+ // 检查是否已存在于内部文件列表
550
+ const fileExists = internalFiles.value.some(
551
+ file => file.name === successFile.name && file.size === successFile.size
552
+ );
553
+
554
+ if (!fileExists) {
555
+ console.log('Adding successful file from change event:', successFile);
556
+ internalFiles.value = [...internalFiles.value, successFile];
557
+
558
+ // 更新v-model和files属性
559
+ emit('update:modelValue', internalFiles.value);
560
+ emit('update:files', internalFiles.value);
561
+ }
562
+ }
563
+
564
+ emit('change', value, context);
565
+ };
566
+
567
+ // 点击事件
568
+ const handleClick = (context) => {
569
+ emit('click', context);
570
+ };
571
+
572
+ // 拖拽事件
573
+ const handleDrag = (context) => {
574
+ emit('drag', context);
575
+ };
576
+
577
+ // 文件拖放事件
578
+ const handleDrop = (context) => {
579
+ emit('drop', context);
580
+ };
581
+
582
+ // 上传失败事件
583
+ const handleFail = (options) => {
584
+ emit('fail', options);
585
+ };
586
+
587
+ // 点击预览事件
588
+ const handlePreview = (options) => {
589
+ emit('preview', options);
590
+ };
591
+
592
+ // 上传进度事件
593
+ const handleProgress = (options) => {
594
+ emit('progress', options);
595
+ };
596
+
597
+ // 移除文件事件
598
+ const handleRemove = (context) => {
599
+ if (context && context.file) {
600
+ // 从内部文件列表中移除文件
601
+ const removedFile = context.file;
602
+
603
+ // 根据name和size过滤掉要删除的文件
604
+ internalFiles.value = internalFiles.value.filter(
605
+ file => !(file.name === removedFile.name && file.size === removedFile.size)
606
+ );
607
+
608
+ // 同时更新v-model绑定的值,保持一致性
609
+ emit('update:modelValue', internalFiles.value);
610
+
611
+ // 更新files属性,支持v-model:files双向绑定
612
+ emit('update:files', internalFiles.value);
613
+ }
614
+
615
+ // 发出原始移除事件
616
+ emit('remove', context);
617
+ };
618
+
619
+ // 选择文件变化事件
620
+ const handleSelectChange = (files, context) => {
621
+ emit('select-change', files, context);
622
+ };
623
+
624
+ // 上传成功事件
625
+ const handleSuccess = (context) => {
626
+ console.log('Upload success event triggered:', context);
627
+
628
+ // 将上传成功的文件添加到文件列表中
629
+ // 首先检查文件是否已经在列表中
630
+ if (context && context.file) {
631
+ // 获取有效的URL
632
+ let fileUrl = '';
633
+ if (context.file.url) {
634
+ fileUrl = context.file.url;
635
+ } else if (context.response && context.response.url) {
636
+ fileUrl = context.response.url;
637
+ } else if (context.response && context.response.data && typeof context.response.data === 'string') {
638
+ fileUrl = context.response.data;
639
+ }
640
+
641
+ const newFile = {
642
+ ...context.file,
643
+ url: fileUrl, // 确保有URL
644
+ status: 'success', // 设置状态为成功
645
+ response: context.response // 保留响应
646
+ };
647
+
648
+ console.log('Adding file to internal files:', newFile);
649
+
650
+ // 创建新的文件列表副本,以便进行修改
651
+ const currentFiles = [...internalFiles.value];
652
+
653
+ // 检查文件是否已存在(基于name和size)
654
+ const fileExists = currentFiles.some(
655
+ file => file.name === newFile.name && file.size === newFile.size
656
+ );
657
+
658
+ // 如果文件不存在于列表中,则添加它
659
+ if (!fileExists) {
660
+ currentFiles.push(newFile);
661
+ // 更新内部文件列表
662
+ internalFiles.value = currentFiles;
663
+ } else {
664
+ // 如果文件已存在,更新其状态和URL
665
+ internalFiles.value = currentFiles.map(file => {
666
+ if (file.name === newFile.name && file.size === newFile.size) {
667
+ return { ...file, ...newFile };
668
+ }
669
+ return file;
670
+ });
671
+ }
672
+
673
+ console.log('Updated internal files:', internalFiles.value);
674
+
675
+ // 同时更新v-model绑定的值,保持一致性
676
+ emit('update:modelValue', internalFiles.value);
677
+
678
+ // 更新files属性,支持v-model:files双向绑定
679
+ emit('update:files', internalFiles.value);
680
+ } else {
681
+ console.warn('Missing file in success context:', context);
682
+ }
683
+
684
+ // 发出原始成功事件
685
+ emit('success', context);
686
+ };
687
+
688
+ // 校验事件
689
+ const handleValidate = (context) => {
690
+ emit('validate', context);
691
+ };
692
+
693
+ // 手动添加已上传的文件到文件列表
694
+ const addUploadedFile = (file) => {
695
+ if (!file || !file.name || !file.url) {
696
+ console.error('Invalid file object. File must contain at least name and url properties.');
697
+ return false;
698
+ }
699
+
700
+ // 构建完整的文件对象
701
+ const fileObject = {
702
+ name: file.name,
703
+ url: file.url,
704
+ size: file.size || 0,
705
+ type: file.type || '',
706
+ status: 'success',
707
+ // 如果提供了原始文件对象,则使用它
708
+ raw: file.raw || null,
709
+ lastModified: file.lastModified || new Date().getTime(),
710
+ ...file
711
+ };
712
+
713
+ // 检查文件是否已存在
714
+ const fileExists = internalFiles.value.some(
715
+ f => f.name === fileObject.name && f.url === fileObject.url
716
+ );
717
+
718
+ if (!fileExists) {
719
+ // 更新内部文件列表
720
+ internalFiles.value = [...internalFiles.value, fileObject];
721
+
722
+ // 同时更新v-model绑定的值,保持一致性
723
+ emit('update:modelValue', internalFiles.value);
724
+
725
+ // 更新files属性,支持v-model:files双向绑定
726
+ emit('update:files', internalFiles.value);
727
+ return true;
728
+ }
729
+
730
+ return false;
731
+ };
732
+
733
+ // 导出组件方法供外部使用
734
+ defineExpose({
735
+ addUploadedFile,
736
+ // 导出内部文件列表,允许外部读取
737
+ getFiles: () => internalFiles.value
738
+ });
739
+
740
+ // 组件挂载时初始化文件列表
741
+ onMounted(() => {
742
+ initializeFiles();
743
+ });
744
+
745
+ // 监听files的变化,更新内部文件列表
746
+ watch(
747
+ () => props.files,
748
+ () => {
749
+ initializeFiles();
750
+ },
751
+ { deep: true }
752
+ );
753
+ </script>
754
+
755
+ <style lang="less" scoped>
756
+ /* 自定义样式 */
757
+ </style>