feat:【ele】【infra】优化文件管理
This commit is contained in:
@@ -60,7 +60,6 @@ function beforeUpload(file: FileType) {
|
|||||||
<Form class="mx-4">
|
<Form class="mx-4">
|
||||||
<template #file>
|
<template #file>
|
||||||
<div class="w-full">
|
<div class="w-full">
|
||||||
<!-- 上传区域 -->
|
|
||||||
<Upload.Dragger
|
<Upload.Dragger
|
||||||
name="file"
|
name="file"
|
||||||
:max-count="1"
|
:max-count="1"
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export function useFormSchema(): VbenFormSchema[] {
|
|||||||
{
|
{
|
||||||
fieldName: 'file',
|
fieldName: 'file',
|
||||||
label: '文件上传',
|
label: '文件上传',
|
||||||
component: 'FileUpload',
|
component: 'Upload',
|
||||||
componentProps: {
|
componentProps: {
|
||||||
placeholder: '请选择要上传的文件',
|
placeholder: '请选择要上传的文件',
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
|
import type { UploadFile, UploadRawFile } from 'element-plus';
|
||||||
|
|
||||||
import { useVbenModal } from '@vben/common-ui';
|
import { useVbenModal } from '@vben/common-ui';
|
||||||
import { isEmpty } from '@vben/utils';
|
|
||||||
|
import { ElMessage, ElUpload } from 'element-plus';
|
||||||
|
|
||||||
import { useVbenForm } from '#/adapter/form';
|
import { useVbenForm } from '#/adapter/form';
|
||||||
|
import { useUpload } from '#/components/upload/use-upload';
|
||||||
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
import { useFormSchema } from '../data';
|
import { useFormSchema } from '../data';
|
||||||
|
|
||||||
const emit = defineEmits(['success']);
|
const emit = defineEmits(['success']);
|
||||||
|
|
||||||
const [Modal, modalApi] = useVbenModal({
|
const [Form, formApi] = useVbenForm({
|
||||||
showConfirmButton: false,
|
|
||||||
showCancelButton: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const [Form] = useVbenForm({
|
|
||||||
commonConfig: {
|
commonConfig: {
|
||||||
componentProps: {
|
componentProps: {
|
||||||
class: 'w-full',
|
class: 'w-full',
|
||||||
@@ -25,20 +25,77 @@ const [Form] = useVbenForm({
|
|||||||
layout: 'horizontal',
|
layout: 'horizontal',
|
||||||
schema: useFormSchema().map((item) => ({ ...item, label: '' })), // 去除label
|
schema: useFormSchema().map((item) => ({ ...item, label: '' })), // 去除label
|
||||||
showDefaultActions: false,
|
showDefaultActions: false,
|
||||||
handleValuesChange: (values) => {
|
});
|
||||||
if (isEmpty(values)) {
|
|
||||||
|
const [Modal, modalApi] = useVbenModal({
|
||||||
|
async onConfirm() {
|
||||||
|
const { valid } = await formApi.validate();
|
||||||
|
if (!valid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 上传成功关闭 modal
|
modalApi.lock();
|
||||||
modalApi.close();
|
// 提交表单
|
||||||
emit('success');
|
const data = await formApi.getValues();
|
||||||
|
try {
|
||||||
|
await useUpload().httpRequest(data.file);
|
||||||
|
// 关闭并提示
|
||||||
|
await modalApi.close();
|
||||||
|
emit('success');
|
||||||
|
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/** 文件变化处理 */
|
||||||
|
function handleChange(uploadFile: UploadFile) {
|
||||||
|
if (uploadFile.raw) {
|
||||||
|
formApi.setFieldValue('file', uploadFile.raw);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 文件数量超出限制 */
|
||||||
|
function handleExceed() {
|
||||||
|
ElMessage.warning('最多只能上传一个文件!');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 上传前校验:不自动上传,仅保存文件 */
|
||||||
|
function beforeUpload(_rawFile: UploadRawFile) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<!-- TODO @puhui999:这个看看怎么和 antd 的 file/modules/form.vue 【UI】保持一致一点哈。 -->
|
<Modal title="上传图片">
|
||||||
<Modal title="上传文件">
|
<Form class="mx-4">
|
||||||
<Form class="mx-4" />
|
<template #file>
|
||||||
|
<div class="w-full">
|
||||||
|
<ElUpload
|
||||||
|
:auto-upload="false"
|
||||||
|
:limit="1"
|
||||||
|
:on-change="handleChange"
|
||||||
|
:on-exceed="handleExceed"
|
||||||
|
:before-upload="beforeUpload"
|
||||||
|
accept=".jpg,.png,.gif,.webp"
|
||||||
|
drag
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="flex min-h-[200px] flex-col items-center justify-center py-8"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
class="icon-[mdi--cloud-upload-outline] mb-4 text-6xl text-gray-400"
|
||||||
|
></span>
|
||||||
|
<div class="text-base text-gray-600">
|
||||||
|
点击或拖拽文件到此区域上传
|
||||||
|
</div>
|
||||||
|
<div class="mt-2 text-sm text-gray-400">
|
||||||
|
支持 .jpg、.png、.gif、.webp 格式图片文件
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</ElUpload>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</Form>
|
||||||
</Modal>
|
</Modal>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user