refactor: 重构client-drawer
This commit is contained in:
parent
b13fafdaf5
commit
1f78061ec9
@ -5,7 +5,7 @@ import { useVbenDrawer } from '@vben/common-ui';
|
|||||||
import { $t } from '@vben/locales';
|
import { $t } from '@vben/locales';
|
||||||
|
|
||||||
import { useVbenForm } from '#/adapter';
|
import { useVbenForm } from '#/adapter';
|
||||||
import { clientAdd, clientUpdate } from '#/api/system/client';
|
import { clientAdd, clientInfo, clientUpdate } from '#/api/system/client';
|
||||||
|
|
||||||
import { drawerSchema } from './data';
|
import { drawerSchema } from './data';
|
||||||
import SecretInput from './secret-input.vue';
|
import SecretInput from './secret-input.vue';
|
||||||
@ -14,7 +14,7 @@ const emit = defineEmits<{ reload: [] }>();
|
|||||||
|
|
||||||
interface DrawerProps {
|
interface DrawerProps {
|
||||||
update: boolean;
|
update: boolean;
|
||||||
record?: any;
|
id?: number | string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isUpdate = ref(false);
|
const isUpdate = ref(false);
|
||||||
@ -32,45 +32,28 @@ const [BasicForm, formApi] = useVbenForm({
|
|||||||
wrapperClass: 'grid-cols-2',
|
wrapperClass: 'grid-cols-2',
|
||||||
});
|
});
|
||||||
|
|
||||||
function setupForm(update: boolean, record?: any) {
|
function setupForm(update: boolean) {
|
||||||
formApi.setState((prev) => {
|
formApi.updateSchema([
|
||||||
return {
|
{
|
||||||
...prev,
|
|
||||||
schema: prev.schema?.map((item) => {
|
|
||||||
if (item.fieldName === 'clientId') {
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
dependencies: {
|
dependencies: {
|
||||||
show: () => update,
|
show: () => update,
|
||||||
triggerFields: [''],
|
triggerFields: [''],
|
||||||
},
|
},
|
||||||
};
|
fieldName: 'clientId',
|
||||||
}
|
},
|
||||||
if (
|
{
|
||||||
item.fieldName === 'clientKey' ||
|
|
||||||
item.fieldName === 'clientSecret'
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
componentProps: {
|
componentProps: {
|
||||||
...item.componentProps,
|
|
||||||
disabled: update,
|
disabled: update,
|
||||||
},
|
},
|
||||||
};
|
fieldName: 'clientKey',
|
||||||
}
|
|
||||||
if (item.fieldName === 'status') {
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
componentProps: {
|
|
||||||
...item.componentProps,
|
|
||||||
disabled: record?.id === 1,
|
|
||||||
},
|
},
|
||||||
};
|
{
|
||||||
}
|
componentProps: {
|
||||||
return item;
|
disabled: update,
|
||||||
}),
|
},
|
||||||
};
|
fieldName: 'clientSecret',
|
||||||
});
|
},
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
||||||
@ -81,13 +64,24 @@ const [BasicDrawer, drawerApi] = useVbenDrawer({
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
drawerApi.drawerLoading(true);
|
drawerApi.drawerLoading(true);
|
||||||
const { record, update } = drawerApi.getData() as DrawerProps;
|
const { id, update } = drawerApi.getData() as DrawerProps;
|
||||||
isUpdate.value = update;
|
isUpdate.value = update;
|
||||||
// 初始化
|
// 初始化
|
||||||
setupForm(update, record);
|
setupForm(update);
|
||||||
if (update && record) {
|
if (update && id) {
|
||||||
|
const record = await clientInfo(id);
|
||||||
|
// 不能禁用id为1的记录
|
||||||
|
formApi.updateSchema([
|
||||||
|
{
|
||||||
|
componentProps: {
|
||||||
|
disabled: record.id === 1,
|
||||||
|
},
|
||||||
|
fieldName: 'status',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
for (const key in record) {
|
for (const key in record) {
|
||||||
await formApi.setFieldValue(key, record[key]);
|
await formApi.setFieldValue(key, record[key as keyof typeof record]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
drawerApi.drawerLoading(false);
|
drawerApi.drawerLoading(false);
|
||||||
@ -102,7 +96,6 @@ async function handleConfirm() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const data = await formApi.getValues();
|
const data = await formApi.getValues();
|
||||||
console.log(data);
|
|
||||||
await (isUpdate.value ? clientUpdate(data) : clientAdd(data));
|
await (isUpdate.value ? clientUpdate(data) : clientAdd(data));
|
||||||
emit('reload');
|
emit('reload');
|
||||||
await handleCancel();
|
await handleCancel();
|
||||||
@ -130,7 +123,11 @@ async function handleCancel() {
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
/**
|
||||||
|
自定义组件校验失败样式
|
||||||
|
*/
|
||||||
:deep(.form-valid-error .ant-input[name='clientSecret']) {
|
:deep(.form-valid-error .ant-input[name='clientSecret']) {
|
||||||
border-color: hsl(var(--destructive));
|
border-color: hsl(var(--destructive));
|
||||||
|
box-shadow: 0 0 0 2px rgb(255 38 5 / 6%);
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Page, useVbenDrawer } from '@vben/common-ui';
|
import { Page, useVbenDrawer } from '@vben/common-ui';
|
||||||
|
import { $t } from '@vben/locales';
|
||||||
|
|
||||||
import clientDrawer from './client-drawer.vue';
|
import clientDrawer from './client-drawer.vue';
|
||||||
|
|
||||||
@ -15,7 +16,9 @@ function handleAdd() {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Page>
|
<Page>
|
||||||
<a-button type="primary" @click="handleAdd">add</a-button>
|
<a-button type="primary" @click="handleAdd">
|
||||||
|
{{ $t('pages.common.add') }}
|
||||||
|
</a-button>
|
||||||
<ClientDrawer />
|
<ClientDrawer />
|
||||||
</Page>
|
</Page>
|
||||||
</template>
|
</template>
|
||||||
|
Loading…
Reference in New Issue
Block a user