From 1246f8f77ba6c234b5af7f04d8cabef12f939eec Mon Sep 17 00:00:00 2001 From: dap <15891557205@163.com> Date: Tue, 20 Jan 2026 11:19:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor(upload):=20=E4=BF=AE=E6=94=B9=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E7=BB=84=E4=BB=B6=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=80=BC=E4=B8=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将文件上传组件的双向绑定值从字符串或字符串数组统一修改为字符串类型,使用逗号分隔多个文件ID 简化值处理逻辑,避免数组类型判断和操作 --- .../src/components/upload/src/file-upload.vue | 4 +-- .../src/components/upload/src/hook.ts | 34 +++++++++---------- .../components/upload/src/image-upload.vue | 4 +-- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/apps/web-antd/src/components/upload/src/file-upload.vue b/apps/web-antd/src/components/upload/src/file-upload.vue index 60111f1a..8f969551 100644 --- a/apps/web-antd/src/components/upload/src/file-upload.vue +++ b/apps/web-antd/src/components/upload/src/file-upload.vue @@ -57,8 +57,8 @@ const CurrentUploadComponent = computed(() => { }); // 双向绑定 ossId -const ossIdList = defineModel('value', { - default: () => [], +const ossIdList = defineModel('value', { + default: '', }); const { diff --git a/apps/web-antd/src/components/upload/src/hook.ts b/apps/web-antd/src/components/upload/src/hook.ts index bc1acb46..ae9ab8ca 100644 --- a/apps/web-antd/src/components/upload/src/hook.ts +++ b/apps/web-antd/src/components/upload/src/hook.ts @@ -88,7 +88,7 @@ export function useImagePreview() { export function useUpload( props: Readonly, emit: UploadEmits, - bindValue: ModelRef, + bindValue: ModelRef, uploadType: UploadType, ) { // 组件内部维护fileList @@ -202,11 +202,9 @@ export function useUpload( bindValue.value = ossId; } else { // 给默认值 - if (!Array.isArray(bindValue.value)) { - bindValue.value = []; - } - // 直接使用.value无法触发useForm的更新(原生是正常的) 需要修改地址 - bindValue.value = [...bindValue.value, ossId]; + const validIds = bindValue.value ? bindValue.value.split(',') : []; + validIds.push(ossId); + bindValue.value = validIds.join(','); } break; } @@ -224,10 +222,12 @@ export function useUpload( if (props.maxCount === 1) { bindValue.value = ''; } else { - (bindValue.value as string[]).splice( - bindValue.value.indexOf(currentFile.uid), - 1, - ); + const validIds = bindValue.value ? bindValue.value.split(',') : []; + const index = validIds.indexOf(currentFile.uid); + if (index !== -1) { + validIds.splice(index, 1); + bindValue.value = validIds.join(','); + } } // 触发remove事件 emit('remove', currentFile); @@ -316,7 +316,7 @@ export function useUpload( watch( () => bindValue.value, async (value) => { - if (value.length === 0) { + if (!value) { // 清空绑定值时,同时清空innerFileList,避免外部使用时还能读取到 innerFileList.value = []; return; @@ -329,7 +329,8 @@ export function useUpload( return; } - const resp = await ossInfo(value); + const ids = value.split(','); + const resp = await ossInfo(ids); function transformFile(info: OssFile) { const cb = { type: 'info', response: info } as const; @@ -353,17 +354,14 @@ export function useUpload( // 多文件 // 单文件查到了也会走这里的逻辑 filter会报错 需要maxCount判断处理 if ( - resp.length !== value.length && + resp.length !== ids.length && !props.keepMissingId && props.maxCount !== 1 ) { - // 给默认值 - if (!Array.isArray(bindValue.value)) { - bindValue.value = []; - } - bindValue.value = bindValue.value.filter((ossId) => + const validIds = ids.filter((ossId) => resp.map((res) => res.ossId).includes(ossId), ); + bindValue.value = validIds.join(','); } }, { immediate: true }, diff --git a/apps/web-antd/src/components/upload/src/image-upload.vue b/apps/web-antd/src/components/upload/src/image-upload.vue index b3afbc3d..8e60de66 100644 --- a/apps/web-antd/src/components/upload/src/image-upload.vue +++ b/apps/web-antd/src/components/upload/src/image-upload.vue @@ -52,8 +52,8 @@ const props = withDefaults(defineProps(), { const emit = defineEmits(); // 双向绑定 ossId -const ossIdList = defineModel('value', { - default: () => [], +const ossIdList = defineModel('value', { + default: '', }); const {