From 2991bb1670d44633d1f571374553d027e0dad8a2 Mon Sep 17 00:00:00 2001 From: zhang Date: Mon, 13 Nov 2023 13:14:21 +0800 Subject: [PATCH] =?UTF-8?q?perf(ImageUpload):=20=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=AE=98=E6=96=B9=E7=A4=BA=E4=BE=8B=E8=AE=BE=E7=BD=AE=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=9B=9E=E6=98=BE=E6=A0=BC=E5=BC=8F=20(#3252)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf(ImageUpload): 根据官方示例设置图片回显格式 * perf(ImageUpload): 优化图片上传组 --- .../Upload/src/components/ImageUpload.vue | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/components/Upload/src/components/ImageUpload.vue b/src/components/Upload/src/components/ImageUpload.vue index 3f64d753..fe5c340d 100644 --- a/src/components/Upload/src/components/ImageUpload.vue +++ b/src/components/Upload/src/components/ImageUpload.vue @@ -5,6 +5,8 @@ v-model:file-list="fileList" :list-type="listType" :accept="getStringAccept" + :multiple="multiple" + :maxCount="maxNumber" :before-upload="beforeUpload" :custom-request="customRequest" @preview="handlePreview" @@ -28,7 +30,7 @@ import type { UploadProps } from 'ant-design-vue'; import { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface'; import { useMessage } from '@/hooks/web/useMessage'; - import { isArray, isFunction } from '@/utils/is'; + import { isArray, isFunction, isObject, isString } from '@/utils/is'; import { warn } from '@/utils/log'; import { useI18n } from '@/hooks/web/useI18n'; import { useUploadType } from '../hooks/useUpload'; @@ -59,19 +61,23 @@ watch( () => props.value, (v) => { - if (isArray(v)) { - fileList.value = v.map((url, i) => ({ - uid: String(-i), - name: url ? url.substring(url.lastIndexOf('/') + 1) : 'image.png', - status: 'done', - url, - })); + if (v && isArray(v)) { + fileList.value = v.map((item, i) => { + if (item && isString(item)) { + return { + uid: -i + '', + name: item.substring(item.lastIndexOf('/') + 1), + status: 'done', + url: item, + }; + } else if (item && isObject(item)) { + return item; + } else { + return; + } + }) as UploadProps['fileList'][number]; } }, - { - immediate: true, - deep: true, - }, ); function getBase64(file: File) { @@ -109,21 +115,21 @@ const beforeUpload = (file: File) => { const { maxSize, accept } = props; const { name } = file; - isActMsg.value = isImgTypeByName(name); - if (!isActMsg.value) { + const isAct = isImgTypeByName(name); + if (!isAct) { createMessage.error(t('component.upload.acceptUpload', [accept])); isActMsg.value = false; // 防止弹出多个错误提示 setTimeout(() => (isActMsg.value = true), 1000); } - isLtMsg.value = file.size / 1024 / 1024 > maxSize; - if (isLtMsg.value) { + const isLt = file.size / 1024 / 1024 > maxSize; + if (isLt) { createMessage.error(t('component.upload.maxSizeMultiple', [maxSize])); isLtMsg.value = false; // 防止弹出多个错误提示 setTimeout(() => (isLtMsg.value = true), 1000); } - return (isActMsg.value && !isLtMsg.value) || Upload.LIST_IGNORE; + return (isAct && !isLt) || Upload.LIST_IGNORE; }; async function customRequest(info: UploadRequestOption) { @@ -143,6 +149,7 @@ info.onSuccess!(res.data); emit('change', fileList.value); } catch (e: any) { + console.log(e); info.onError!(e); } }