2025-04-07 10:22:27 +08:00
|
|
|
<script lang="ts" setup>
|
2025-04-07 19:27:50 +08:00
|
|
|
import type { FileType } from 'ant-design-vue/es/upload/interface';
|
2025-04-07 10:22:27 +08:00
|
|
|
|
|
|
|
|
import { useVbenModal } from '@vben/common-ui';
|
|
|
|
|
import { message } from 'ant-design-vue';
|
2025-04-07 19:27:50 +08:00
|
|
|
import { Upload, Button } from 'ant-design-vue';
|
2025-04-07 10:22:27 +08:00
|
|
|
|
2025-04-07 19:27:50 +08:00
|
|
|
import { $t } from '#/locales';
|
2025-04-07 10:22:27 +08:00
|
|
|
import { useVbenForm } from '#/adapter/form';
|
|
|
|
|
import { uploadFile } from '#/api/infra/file';
|
|
|
|
|
|
2025-04-07 19:27:50 +08:00
|
|
|
import { useFormSchema } from '../data';
|
2025-04-07 10:22:27 +08:00
|
|
|
|
2025-04-07 19:27:50 +08:00
|
|
|
const emit = defineEmits(['success']);
|
2025-04-07 10:22:27 +08:00
|
|
|
|
|
|
|
|
const [Form, formApi] = useVbenForm({
|
|
|
|
|
layout: 'horizontal',
|
2025-04-07 19:27:50 +08:00
|
|
|
schema: useFormSchema(),
|
2025-04-07 10:22:27 +08:00
|
|
|
showDefaultActions: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [Modal, modalApi] = useVbenModal({
|
|
|
|
|
async onConfirm() {
|
|
|
|
|
const { valid } = await formApi.validate();
|
|
|
|
|
if (!valid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
modalApi.lock();
|
|
|
|
|
// 提交表单
|
2025-04-07 19:27:50 +08:00
|
|
|
const data = await formApi.getValues();
|
2025-04-07 10:22:27 +08:00
|
|
|
try {
|
2025-04-07 19:27:50 +08:00
|
|
|
await uploadFile(data);
|
2025-04-07 10:22:27 +08:00
|
|
|
// 关闭并提示
|
|
|
|
|
await modalApi.close();
|
|
|
|
|
emit('success');
|
|
|
|
|
message.success({
|
2025-04-07 19:27:50 +08:00
|
|
|
content: $t('ui.actionMessage.operationSuccess'),
|
2025-04-07 10:22:27 +08:00
|
|
|
key: 'action_process_msg',
|
|
|
|
|
});
|
|
|
|
|
} finally {
|
|
|
|
|
modalApi.lock(false);
|
|
|
|
|
}
|
2025-04-07 19:27:50 +08:00
|
|
|
}
|
2025-04-07 10:22:27 +08:00
|
|
|
});
|
|
|
|
|
|
2025-04-07 19:27:50 +08:00
|
|
|
/** 上传前 */
|
|
|
|
|
function beforeUpload(file: FileType) {
|
|
|
|
|
formApi.setFieldValue('file', file);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-04-07 10:22:27 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-04-07 19:27:50 +08:00
|
|
|
<Modal title="上传图片">
|
|
|
|
|
<Form class="mx-4">
|
|
|
|
|
<template #file>
|
|
|
|
|
<div class="w-full">
|
|
|
|
|
<Upload :max-count="1" accept=".jpg,.png,.gif,.webp" :beforeUpload="beforeUpload">
|
|
|
|
|
<Button type="primary"> 选择图片 </Button>
|
|
|
|
|
</Upload>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
</Form>
|
2025-04-07 10:22:27 +08:00
|
|
|
</Modal>
|
|
|
|
|
</template>
|