2024-09-12 14:41:28 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { computed, ref } from 'vue';
|
|
|
|
|
|
|
|
import { useVbenDrawer } from '@vben/common-ui';
|
|
|
|
import { $t } from '@vben/locales';
|
2024-09-25 14:46:02 +08:00
|
|
|
import { cloneDeep } from '@vben/utils';
|
2024-09-12 14:41:28 +08:00
|
|
|
|
2024-10-17 15:16:22 +08:00
|
|
|
import { useVbenForm } from '#/adapter/form';
|
2024-09-23 08:38:55 +08:00
|
|
|
import { clientAdd, clientInfo, clientUpdate } from '#/api/system/client';
|
2024-09-12 14:41:28 +08:00
|
|
|
|
|
|
|
import { drawerSchema } from './data';
|
|
|
|
import SecretInput from './secret-input.vue';
|
|
|
|
|
|
|
|
const emit = defineEmits<{ reload: [] }>();
|
|
|
|
|
|
|
|
const isUpdate = ref(false);
|
|
|
|
const title = computed(() => {
|
|
|
|
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
|
|
|
});
|
|
|
|
|
|
|
|
const [BasicForm, formApi] = useVbenForm({
|
2024-09-12 21:06:10 +08:00
|
|
|
commonConfig: {
|
|
|
|
formItemClass: 'col-span-2',
|
2024-11-29 15:55:16 +08:00
|
|
|
componentProps: {
|
|
|
|
class: 'w-full',
|
|
|
|
},
|
2024-09-12 21:06:10 +08:00
|
|
|
},
|
2024-09-12 14:41:28 +08:00
|
|
|
layout: 'vertical',
|
|
|
|
schema: drawerSchema(),
|
|
|
|
showDefaultActions: false,
|
2024-12-09 08:09:04 +08:00
|
|
|
wrapperClass: 'grid-cols-2 gap-x-4',
|
2024-09-12 14:41:28 +08:00
|
|
|
});
|
|
|
|
|
2024-09-23 08:38:55 +08:00
|
|
|
function setupForm(update: boolean) {
|
|
|
|
formApi.updateSchema([
|
|
|
|
{
|
|
|
|
dependencies: {
|
|
|
|
show: () => update,
|
|
|
|
triggerFields: [''],
|
|
|
|
},
|
|
|
|
fieldName: 'clientId',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
componentProps: {
|
|
|
|
disabled: update,
|
|
|
|
},
|
|
|
|
fieldName: 'clientKey',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
componentProps: {
|
|
|
|
disabled: update,
|
|
|
|
},
|
|
|
|
fieldName: 'clientSecret',
|
|
|
|
},
|
|
|
|
]);
|
2024-09-12 14:41:28 +08:00
|
|
|
}
|
|
|
|
|
2025-02-05 13:20:30 +08:00
|
|
|
// 提取生成状态字段Schema的函数
|
|
|
|
const getStatusSchema = (disabled: boolean) => [
|
|
|
|
{
|
|
|
|
componentProps: { disabled },
|
|
|
|
fieldName: 'status',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-09-12 14:41:28 +08:00
|
|
|
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
|
|
|
onCancel: handleCancel,
|
|
|
|
onConfirm: handleConfirm,
|
|
|
|
async onOpenChange(isOpen) {
|
|
|
|
if (!isOpen) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
drawerApi.drawerLoading(true);
|
2024-09-25 14:46:02 +08:00
|
|
|
const { id } = drawerApi.getData() as { id?: number | string };
|
|
|
|
isUpdate.value = !!id;
|
2024-09-12 14:41:28 +08:00
|
|
|
// 初始化
|
2024-09-25 14:46:02 +08:00
|
|
|
setupForm(isUpdate.value);
|
|
|
|
if (isUpdate.value && id) {
|
2024-09-23 08:38:55 +08:00
|
|
|
const record = await clientInfo(id);
|
|
|
|
// 不能禁用id为1的记录
|
2025-02-05 13:20:30 +08:00
|
|
|
formApi.updateSchema(getStatusSchema(record.id === 1));
|
2024-09-25 14:46:02 +08:00
|
|
|
await formApi.setValues(record);
|
2025-02-05 13:20:30 +08:00
|
|
|
} else {
|
|
|
|
// 新增模式: 确保状态字段可用
|
|
|
|
formApi.updateSchema(getStatusSchema(false));
|
2024-09-12 14:41:28 +08:00
|
|
|
}
|
|
|
|
drawerApi.drawerLoading(false);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
async function handleConfirm() {
|
|
|
|
try {
|
|
|
|
drawerApi.drawerLoading(true);
|
|
|
|
const { valid } = await formApi.validate();
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-25 14:46:02 +08:00
|
|
|
const data = cloneDeep(await formApi.getValues());
|
2024-09-12 14:41:28 +08:00
|
|
|
await (isUpdate.value ? clientUpdate(data) : clientAdd(data));
|
|
|
|
emit('reload');
|
|
|
|
await handleCancel();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
} finally {
|
|
|
|
drawerApi.drawerLoading(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleCancel() {
|
|
|
|
drawerApi.close();
|
|
|
|
await formApi.resetForm();
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2024-09-20 14:48:17 +08:00
|
|
|
<BasicDrawer :close-on-click-modal="false" :title="title" class="w-[600px]">
|
2024-09-12 14:41:28 +08:00
|
|
|
<BasicForm>
|
|
|
|
<template #clientSecret="slotProps">
|
|
|
|
<SecretInput v-bind="slotProps" :disabled="isUpdate" />
|
|
|
|
</template>
|
|
|
|
</BasicForm>
|
|
|
|
</BasicDrawer>
|
|
|
|
</template>
|
2024-09-21 19:26:44 +08:00
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2024-09-23 08:38:55 +08:00
|
|
|
/**
|
|
|
|
自定义组件校验失败样式
|
|
|
|
*/
|
2024-09-21 19:26:44 +08:00
|
|
|
:deep(.form-valid-error .ant-input[name='clientSecret']) {
|
|
|
|
border-color: hsl(var(--destructive));
|
2024-09-23 08:38:55 +08:00
|
|
|
box-shadow: 0 0 0 2px rgb(255 38 5 / 6%);
|
2024-09-21 19:26:44 +08:00
|
|
|
}
|
|
|
|
</style>
|