2024-09-18 11:45:52 +08:00
|
|
|
<script setup lang="ts">
|
2024-09-23 09:47:47 +08:00
|
|
|
import { computed, h, ref } from 'vue';
|
2024-09-18 11:45:52 +08:00
|
|
|
|
|
|
|
import { useVbenDrawer } from '@vben/common-ui';
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
|
2024-09-23 09:47:47 +08:00
|
|
|
import { Tag } from 'ant-design-vue';
|
|
|
|
|
2024-09-18 11:45:52 +08:00
|
|
|
import { useVbenForm } from '#/adapter';
|
|
|
|
import { clientAdd, clientUpdate } from '#/api/system/client';
|
2024-09-23 09:47:47 +08:00
|
|
|
import { tenantInfo } from '#/api/system/tenant';
|
2024-09-23 09:12:35 +08:00
|
|
|
import { packageSelectList } from '#/api/system/tenant-package';
|
2024-09-18 11:45:52 +08:00
|
|
|
|
|
|
|
import { drawerSchema } from './data';
|
|
|
|
|
|
|
|
const emit = defineEmits<{ reload: [] }>();
|
|
|
|
|
|
|
|
interface DrawerProps {
|
|
|
|
update: boolean;
|
2024-09-23 09:47:47 +08:00
|
|
|
id?: number | string;
|
2024-09-18 11:45:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const isUpdate = ref(false);
|
|
|
|
const title = computed(() => {
|
|
|
|
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
|
|
|
});
|
|
|
|
|
|
|
|
const [BasicForm, formApi] = useVbenForm({
|
|
|
|
commonConfig: {
|
|
|
|
formItemClass: 'col-span-2',
|
|
|
|
labelWidth: 100,
|
|
|
|
},
|
|
|
|
schema: drawerSchema(),
|
|
|
|
showDefaultActions: false,
|
|
|
|
wrapperClass: 'grid-cols-2',
|
|
|
|
});
|
|
|
|
|
2024-09-23 09:12:35 +08:00
|
|
|
async function setupPackageSelect() {
|
|
|
|
const tenantPackageList = await packageSelectList();
|
2024-09-23 09:47:47 +08:00
|
|
|
const options = tenantPackageList.map((item) => ({
|
|
|
|
label: h('div', { class: 'flex items-center gap-[6px]' }, [
|
|
|
|
h('span', null, item.packageName),
|
|
|
|
h(Tag, { color: 'processing' }, () => `${item.menuIds.length}个菜单项`),
|
|
|
|
]),
|
|
|
|
title: item.packageName,
|
|
|
|
value: item.packageId,
|
|
|
|
}));
|
2024-09-23 09:12:35 +08:00
|
|
|
formApi.updateSchema([
|
|
|
|
{
|
|
|
|
componentProps: {
|
2024-09-23 09:47:47 +08:00
|
|
|
optionFilterProp: 'title',
|
|
|
|
optionLabelProp: 'title',
|
|
|
|
options,
|
|
|
|
showSearch: true,
|
2024-09-23 09:12:35 +08:00
|
|
|
},
|
|
|
|
fieldName: 'packageId',
|
|
|
|
},
|
|
|
|
]);
|
2024-09-18 11:45:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
|
|
|
onCancel: handleCancel,
|
|
|
|
onConfirm: handleConfirm,
|
|
|
|
async onOpenChange(isOpen) {
|
|
|
|
if (!isOpen) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
drawerApi.drawerLoading(true);
|
2024-09-23 09:47:47 +08:00
|
|
|
const { id, update } = drawerApi.getData() as DrawerProps;
|
2024-09-18 11:45:52 +08:00
|
|
|
isUpdate.value = update;
|
|
|
|
// 初始化
|
2024-09-23 09:12:35 +08:00
|
|
|
await setupPackageSelect();
|
2024-09-23 09:47:47 +08:00
|
|
|
if (update && id) {
|
|
|
|
const record = await tenantInfo(id);
|
2024-09-18 11:45:52 +08:00
|
|
|
for (const key in record) {
|
2024-09-23 09:47:47 +08:00
|
|
|
await formApi.setFieldValue(key, record[key as keyof typeof record]);
|
2024-09-18 11:45:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
drawerApi.drawerLoading(false);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
async function handleConfirm() {
|
|
|
|
try {
|
|
|
|
drawerApi.drawerLoading(true);
|
|
|
|
const { valid } = await formApi.validate();
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = await formApi.getValues();
|
|
|
|
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-18 11:45:52 +08:00
|
|
|
<BasicForm />
|
|
|
|
</BasicDrawer>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
:deep(.ant-divider) {
|
|
|
|
margin: 8px 0;
|
|
|
|
}
|
|
|
|
</style>
|