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 +})