refactor: 客户端管理useBeforeCloseDiff
This commit is contained in:
parent
f7128b099e
commit
c4962aaf85
@ -7,6 +7,7 @@ import { cloneDeep } from '@vben/utils';
|
|||||||
|
|
||||||
import { useVbenForm } from '#/adapter/form';
|
import { useVbenForm } from '#/adapter/form';
|
||||||
import { clientAdd, clientInfo, clientUpdate } from '#/api/system/client';
|
import { clientAdd, clientInfo, clientUpdate } from '#/api/system/client';
|
||||||
|
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
|
||||||
|
|
||||||
import { drawerSchema } from './data';
|
import { drawerSchema } from './data';
|
||||||
import SecretInput from './secret-input.vue';
|
import SecretInput from './secret-input.vue';
|
||||||
@ -55,6 +56,13 @@ function setupForm(update: boolean) {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
|
||||||
|
{
|
||||||
|
initializedGetter: defaultFormValueGetter(formApi),
|
||||||
|
currentGetter: defaultFormValueGetter(formApi),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// 提取生成状态字段Schema的函数
|
// 提取生成状态字段Schema的函数
|
||||||
const getStatusSchema = (disabled: boolean) => [
|
const getStatusSchema = (disabled: boolean) => [
|
||||||
{
|
{
|
||||||
@ -64,13 +72,15 @@ const getStatusSchema = (disabled: boolean) => [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
||||||
onCancel: handleCancel,
|
onBeforeClose,
|
||||||
|
onClosed: handleClosed,
|
||||||
onConfirm: handleConfirm,
|
onConfirm: handleConfirm,
|
||||||
async onOpenChange(isOpen) {
|
async onOpenChange(isOpen) {
|
||||||
if (!isOpen) {
|
if (!isOpen) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
drawerApi.drawerLoading(true);
|
drawerApi.drawerLoading(true);
|
||||||
|
|
||||||
const { id } = drawerApi.getData() as { id?: number | string };
|
const { id } = drawerApi.getData() as { id?: number | string };
|
||||||
isUpdate.value = !!id;
|
isUpdate.value = !!id;
|
||||||
// 初始化
|
// 初始化
|
||||||
@ -84,36 +94,39 @@ const [BasicDrawer, drawerApi] = useVbenDrawer({
|
|||||||
// 新增模式: 确保状态字段可用
|
// 新增模式: 确保状态字段可用
|
||||||
formApi.updateSchema(getStatusSchema(false));
|
formApi.updateSchema(getStatusSchema(false));
|
||||||
}
|
}
|
||||||
|
await markInitialized();
|
||||||
|
|
||||||
drawerApi.drawerLoading(false);
|
drawerApi.drawerLoading(false);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handleConfirm() {
|
async function handleConfirm() {
|
||||||
try {
|
try {
|
||||||
drawerApi.drawerLoading(true);
|
drawerApi.lock(true);
|
||||||
const { valid } = await formApi.validate();
|
const { valid } = await formApi.validate();
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const data = cloneDeep(await formApi.getValues());
|
const data = cloneDeep(await formApi.getValues());
|
||||||
await (isUpdate.value ? clientUpdate(data) : clientAdd(data));
|
await (isUpdate.value ? clientUpdate(data) : clientAdd(data));
|
||||||
|
resetInitialized();
|
||||||
emit('reload');
|
emit('reload');
|
||||||
await handleCancel();
|
drawerApi.close();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
} finally {
|
} finally {
|
||||||
drawerApi.drawerLoading(false);
|
drawerApi.lock(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleCancel() {
|
async function handleClosed() {
|
||||||
drawerApi.close();
|
|
||||||
await formApi.resetForm();
|
await formApi.resetForm();
|
||||||
|
resetInitialized();
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<BasicDrawer :close-on-click-modal="false" :title="title" class="w-[600px]">
|
<BasicDrawer :title="title" class="w-[600px]">
|
||||||
<BasicForm>
|
<BasicForm>
|
||||||
<template #clientSecret="slotProps">
|
<template #clientSecret="slotProps">
|
||||||
<SecretInput v-bind="slotProps" :disabled="isUpdate" />
|
<SecretInput v-bind="slotProps" :disabled="isUpdate" />
|
||||||
|
Loading…
Reference in New Issue
Block a user