Files
ruoyi-plus-vben5/apps/web-antd/src/views/system/tenant/tenant-drawer.vue

112 lines
2.7 KiB
Vue
Raw Normal View History

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-25 14:46:02 +08:00
import { cloneDeep } from '@vben/utils';
2024-09-18 11:45:52 +08:00
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: [] }>();
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-25 14:46:02 +08:00
const { id } = drawerApi.getData() as { id?: number | string };
isUpdate.value = !!id;
2024-09-18 11:45:52 +08:00
// 初始化
2024-09-23 09:12:35 +08:00
await setupPackageSelect();
2024-09-25 14:46:02 +08:00
if (isUpdate.value && id) {
2024-09-23 09:47:47 +08:00
const record = await tenantInfo(id);
2024-09-25 14:46:02 +08:00
await formApi.setValues(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;
}
2024-09-25 14:46:02 +08:00
const data = cloneDeep(await formApi.getValues());
2024-09-18 11:45:52 +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>
<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>