perf(ImageUpload): 根据官方示例设置图片回显格式 (#3252)

* perf(ImageUpload): 根据官方示例设置图片回显格式

* perf(ImageUpload): 优化图片上传组
This commit is contained in:
zhang 2023-11-13 13:14:21 +08:00 committed by GitHub
parent bfb7cd32f2
commit 2991bb1670
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,8 @@
v-model:file-list="fileList" v-model:file-list="fileList"
:list-type="listType" :list-type="listType"
:accept="getStringAccept" :accept="getStringAccept"
:multiple="multiple"
:maxCount="maxNumber"
:before-upload="beforeUpload" :before-upload="beforeUpload"
:custom-request="customRequest" :custom-request="customRequest"
@preview="handlePreview" @preview="handlePreview"
@ -28,7 +30,7 @@
import type { UploadProps } from 'ant-design-vue'; import type { UploadProps } from 'ant-design-vue';
import { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface'; import { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface';
import { useMessage } from '@/hooks/web/useMessage'; 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 { warn } from '@/utils/log';
import { useI18n } from '@/hooks/web/useI18n'; import { useI18n } from '@/hooks/web/useI18n';
import { useUploadType } from '../hooks/useUpload'; import { useUploadType } from '../hooks/useUpload';
@ -59,18 +61,22 @@
watch( watch(
() => props.value, () => props.value,
(v) => { (v) => {
if (isArray(v)) { if (v && isArray(v)) {
fileList.value = v.map((url, i) => ({ fileList.value = v.map((item, i) => {
uid: String(-i), if (item && isString(item)) {
name: url ? url.substring(url.lastIndexOf('/') + 1) : 'image.png', return {
uid: -i + '',
name: item.substring(item.lastIndexOf('/') + 1),
status: 'done', status: 'done',
url, url: item,
})); };
} else if (item && isObject(item)) {
return item;
} else {
return;
}
}) as UploadProps['fileList'][number];
} }
},
{
immediate: true,
deep: true,
}, },
); );
@ -109,21 +115,21 @@
const beforeUpload = (file: File) => { const beforeUpload = (file: File) => {
const { maxSize, accept } = props; const { maxSize, accept } = props;
const { name } = file; const { name } = file;
isActMsg.value = isImgTypeByName(name); const isAct = isImgTypeByName(name);
if (!isActMsg.value) { if (!isAct) {
createMessage.error(t('component.upload.acceptUpload', [accept])); createMessage.error(t('component.upload.acceptUpload', [accept]));
isActMsg.value = false; isActMsg.value = false;
// //
setTimeout(() => (isActMsg.value = true), 1000); setTimeout(() => (isActMsg.value = true), 1000);
} }
isLtMsg.value = file.size / 1024 / 1024 > maxSize; const isLt = file.size / 1024 / 1024 > maxSize;
if (isLtMsg.value) { if (isLt) {
createMessage.error(t('component.upload.maxSizeMultiple', [maxSize])); createMessage.error(t('component.upload.maxSizeMultiple', [maxSize]));
isLtMsg.value = false; isLtMsg.value = false;
// //
setTimeout(() => (isLtMsg.value = true), 1000); setTimeout(() => (isLtMsg.value = true), 1000);
} }
return (isActMsg.value && !isLtMsg.value) || Upload.LIST_IGNORE; return (isAct && !isLt) || Upload.LIST_IGNORE;
}; };
async function customRequest(info: UploadRequestOption<any>) { async function customRequest(info: UploadRequestOption<any>) {
@ -143,6 +149,7 @@
info.onSuccess!(res.data); info.onSuccess!(res.data);
emit('change', fileList.value); emit('change', fileList.value);
} catch (e: any) { } catch (e: any) {
console.log(e);
info.onError!(e); info.onError!(e);
} }
} }