From da0a9144b04681fa9c9a328af7c421180883c50c Mon Sep 17 00:00:00 2001 From: zhaojinfeng <121016171@qq.com> Date: Fri, 21 Jul 2023 16:49:21 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=A4=B4=E5=83=8F=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/upload-avatar/index.vue | 34 ++++++++++++++++++++++++++------ stories/UploadAvatar.stories.ts | 9 +++++---- types/components.d.ts | 13 +++++++----- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/packages/upload-avatar/index.vue b/packages/upload-avatar/index.vue index 2cc5415..1ed3da4 100644 --- a/packages/upload-avatar/index.vue +++ b/packages/upload-avatar/index.vue @@ -14,7 +14,7 @@ - + Promise + /** 文件信息请求函数 */ + fileFunction: (fileId?: number | string) => Promise disabled?: boolean }>(), { accpet: '.jpeg,.jpg,.png,.bmp', + fileId: '', + fileUrl: '', fileSize: 2, autoCropHeight: 200, autoCropWidth: 200, - uploadFunction: () => Promise.resolve({}), }) const emit = defineEmits<{ - (event: 'update:modelValue', modelValue?: string): void + (event: 'update:fileUrl', fileId?: number): void + (event: 'update:filelId', modelValue?: string): void (event: 'onSuccess', file: FileVO): void (event: 'onError', error: Error): void }>() -const avatarUrl = useVModel(props, 'modelValue', emit) +let avatarUrl = $ref(props.fileUrl) +const filelId = useVModel(props, 'fileId', emit) const imageStyle = computed(() => ({ height: `${props.autoCropHeight}px`, @@ -182,8 +189,14 @@ function selectImage() { } async function uploadImg(blob: Blob) { + if (!props.uploadFunction) { + ElMessage.error('请定义接口请求函数') + emit('onError', new Error('请定义接口请求函数')) + return + } if (blob.size > props.fileSize * 1024 * 1024) { ElMessage.error('您的图片经过剪裁依然很大,请重新选择图片') + emit('onError', new Error('您的图片经过剪裁依然很大,请重新选择图片')) return } @@ -193,7 +206,8 @@ async function uploadImg(blob: Blob) { try { const res = await props.uploadFunction(formData) - avatarUrl.value = res.url + avatarUrl = res.url + filelId.value = res.id showDialog = false emit('onSuccess', res) } @@ -212,6 +226,14 @@ function closeDialog() { reset() loading = false } + +watch(() => props.fileId, async () => { + if (!props.fileId || avatarUrl) + return + + const res = await props.fileFunction(props.fileId) + avatarUrl = res.url +})