2025-04-08 10:22:21 +08:00
|
|
|
|
<!--
|
|
|
|
|
TODO: 这个页面要优化逻辑
|
|
|
|
|
-->
|
2024-09-24 15:35:02 +08:00
|
|
|
|
<script setup lang="ts">
|
2025-01-20 11:43:19 +08:00
|
|
|
|
import type { MenuOption } from '#/api/system/menu/model';
|
|
|
|
|
|
|
|
|
|
import { computed, nextTick, ref } from 'vue';
|
2024-09-24 15:35:02 +08:00
|
|
|
|
|
|
|
|
|
import { useVbenDrawer } from '@vben/common-ui';
|
|
|
|
|
import { $t } from '@vben/locales';
|
2024-10-11 13:44:08 +08:00
|
|
|
|
import { cloneDeep, eachTree } from '@vben/utils';
|
2024-09-25 10:52:25 +08:00
|
|
|
|
|
2024-10-17 15:16:22 +08:00
|
|
|
|
import { useVbenForm } from '#/adapter/form';
|
2024-09-25 10:52:25 +08:00
|
|
|
|
import { menuTreeSelect, roleMenuTreeSelect } from '#/api/system/menu';
|
2024-09-24 15:35:02 +08:00
|
|
|
|
import { roleAdd, roleInfo, roleUpdate } from '#/api/system/role';
|
2025-01-20 11:43:19 +08:00
|
|
|
|
import { MenuSelectTable } from '#/components/tree';
|
2025-04-08 10:22:21 +08:00
|
|
|
|
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
|
2024-09-24 15:35:02 +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: {
|
|
|
|
|
componentProps: {
|
|
|
|
|
class: 'w-full',
|
|
|
|
|
},
|
2024-11-29 15:45:42 +08:00
|
|
|
|
formItemClass: 'col-span-1',
|
2024-09-24 15:35:02 +08:00
|
|
|
|
},
|
|
|
|
|
layout: 'vertical',
|
|
|
|
|
schema: drawerSchema(),
|
|
|
|
|
showDefaultActions: false,
|
2024-11-29 15:45:42 +08:00
|
|
|
|
wrapperClass: 'grid-cols-2 gap-x-4',
|
2024-09-24 15:35:02 +08:00
|
|
|
|
});
|
|
|
|
|
|
2025-01-20 11:43:19 +08:00
|
|
|
|
const menuTree = ref<MenuOption[]>([]);
|
2024-09-25 10:52:25 +08:00
|
|
|
|
async function setupMenuTree(id?: number | string) {
|
|
|
|
|
if (id) {
|
|
|
|
|
const resp = await roleMenuTreeSelect(id);
|
2024-10-11 13:44:08 +08:00
|
|
|
|
const menus = resp.menus;
|
|
|
|
|
// i18n处理
|
|
|
|
|
eachTree(menus, (node) => {
|
|
|
|
|
node.label = $t(node.label);
|
|
|
|
|
});
|
2024-09-25 10:52:25 +08:00
|
|
|
|
// 设置菜单信息
|
|
|
|
|
menuTree.value = resp.menus;
|
2025-01-20 11:43:19 +08:00
|
|
|
|
// keys依赖于menu 需要先加载menu
|
|
|
|
|
await nextTick();
|
|
|
|
|
await formApi.setFieldValue('menuIds', resp.checkedKeys);
|
2024-09-25 10:52:25 +08:00
|
|
|
|
} else {
|
|
|
|
|
const resp = await menuTreeSelect();
|
2024-10-11 13:44:08 +08:00
|
|
|
|
// i18n处理
|
|
|
|
|
eachTree(resp, (node) => {
|
|
|
|
|
node.label = $t(node.label);
|
|
|
|
|
});
|
2024-09-25 10:52:25 +08:00
|
|
|
|
// 设置菜单信息
|
|
|
|
|
menuTree.value = resp;
|
2025-01-20 11:43:19 +08:00
|
|
|
|
// keys依赖于menu 需要先加载menu
|
|
|
|
|
await nextTick();
|
|
|
|
|
await formApi.setFieldValue('menuIds', []);
|
2024-09-25 10:52:25 +08:00
|
|
|
|
}
|
2024-09-24 15:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 10:22:21 +08:00
|
|
|
|
async function customFormValueGetter() {
|
|
|
|
|
const v = await defaultFormValueGetter(formApi)();
|
|
|
|
|
// 获取勾选信息
|
|
|
|
|
const menuIds = menuSelectRef.value?.getCheckedKeys?.() ?? [];
|
|
|
|
|
const mixStr = v + menuIds.join(',');
|
|
|
|
|
return mixStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
|
|
|
|
|
{
|
|
|
|
|
initializedGetter: customFormValueGetter,
|
|
|
|
|
currentGetter: customFormValueGetter,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-24 15:35:02 +08:00
|
|
|
|
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
2025-04-08 10:22:21 +08:00
|
|
|
|
onBeforeClose,
|
|
|
|
|
onClosed: handleClosed,
|
2024-09-24 15:35:02 +08:00
|
|
|
|
onConfirm: handleConfirm,
|
2025-05-09 11:12:24 +08:00
|
|
|
|
destroyOnClose: true,
|
2024-09-24 15:35:02 +08:00
|
|
|
|
async onOpenChange(isOpen) {
|
|
|
|
|
if (!isOpen) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
drawerApi.drawerLoading(true);
|
2025-04-08 10:22:21 +08:00
|
|
|
|
|
2024-09-24 15:35:02 +08:00
|
|
|
|
const { id } = drawerApi.getData() as { id?: number | string };
|
|
|
|
|
isUpdate.value = !!id;
|
2024-09-25 10:52:25 +08:00
|
|
|
|
|
2024-09-24 15:35:02 +08:00
|
|
|
|
if (isUpdate.value && id) {
|
|
|
|
|
const record = await roleInfo(id);
|
2024-09-25 14:46:02 +08:00
|
|
|
|
await formApi.setValues(record);
|
2024-09-24 15:35:02 +08:00
|
|
|
|
}
|
2024-09-25 10:52:25 +08:00
|
|
|
|
// init菜单 注意顺序要放在赋值record之后 内部watch会依赖record
|
|
|
|
|
await setupMenuTree(id);
|
2025-04-08 10:22:21 +08:00
|
|
|
|
await markInitialized();
|
2024-09-25 10:52:25 +08:00
|
|
|
|
|
2024-09-24 15:35:02 +08:00
|
|
|
|
drawerApi.drawerLoading(false);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-20 11:43:19 +08:00
|
|
|
|
const menuSelectRef = ref<InstanceType<typeof MenuSelectTable>>();
|
2024-09-24 15:35:02 +08:00
|
|
|
|
async function handleConfirm() {
|
|
|
|
|
try {
|
2025-04-08 10:22:21 +08:00
|
|
|
|
drawerApi.lock(true);
|
|
|
|
|
|
2024-09-24 15:35:02 +08:00
|
|
|
|
const { valid } = await formApi.validate();
|
|
|
|
|
if (!valid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-09-25 10:52:25 +08:00
|
|
|
|
// 这个用于提交
|
2025-01-20 11:43:19 +08:00
|
|
|
|
const menuIds = menuSelectRef.value?.getCheckedKeys?.() ?? [];
|
2024-09-25 10:52:25 +08:00
|
|
|
|
// formApi.getValues拿到的是一个readonly对象,不能直接修改,需要cloneDeep
|
|
|
|
|
const data = cloneDeep(await formApi.getValues());
|
|
|
|
|
data.menuIds = menuIds;
|
2024-09-24 15:35:02 +08:00
|
|
|
|
await (isUpdate.value ? roleUpdate(data) : roleAdd(data));
|
|
|
|
|
emit('reload');
|
2025-04-08 10:22:21 +08:00
|
|
|
|
resetInitialized();
|
|
|
|
|
drawerApi.close();
|
2024-09-24 15:35:02 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
} finally {
|
2025-04-08 10:22:21 +08:00
|
|
|
|
drawerApi.lock(false);
|
2024-09-24 15:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 10:22:21 +08:00
|
|
|
|
async function handleClosed() {
|
2024-09-24 15:35:02 +08:00
|
|
|
|
await formApi.resetForm();
|
2025-04-08 10:22:21 +08:00
|
|
|
|
resetInitialized();
|
2024-09-24 15:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 10:52:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* 通过回调更新 无法通过v-model
|
|
|
|
|
* @param value 菜单选择是否严格模式
|
|
|
|
|
*/
|
|
|
|
|
function handleMenuCheckStrictlyChange(value: boolean) {
|
|
|
|
|
formApi.setFieldValue('menuCheckStrictly', value);
|
2024-09-24 15:35:02 +08:00
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-04-08 10:22:21 +08:00
|
|
|
|
<BasicDrawer :title="title" class="w-[800px]">
|
2024-09-24 15:35:02 +08:00
|
|
|
|
<BasicForm>
|
|
|
|
|
<template #menuIds="slotProps">
|
2025-01-20 11:43:19 +08:00
|
|
|
|
<div class="h-[600px] w-full">
|
|
|
|
|
<!-- association为readonly 不能通过v-model绑定 -->
|
|
|
|
|
<MenuSelectTable
|
|
|
|
|
ref="menuSelectRef"
|
|
|
|
|
:checked-keys="slotProps.value"
|
|
|
|
|
:association="formApi.form.values.menuCheckStrictly"
|
|
|
|
|
:menus="menuTree"
|
|
|
|
|
@update:association="handleMenuCheckStrictlyChange"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2024-09-24 15:35:02 +08:00
|
|
|
|
</template>
|
|
|
|
|
</BasicForm>
|
|
|
|
|
</BasicDrawer>
|
|
|
|
|
</template>
|