可正常启用

This commit is contained in:
戚辰先生 2024-12-01 16:16:38 +08:00
parent 07f974491e
commit 75b1393c9b
2 changed files with 45 additions and 13 deletions

View File

@ -16,6 +16,7 @@ import type {MenuProps} from 'antd';
import {getCurrentUserMenus} from '@/pages/System/Menu/service';
import {getWeather} from '../services/weather';
import type {MenuResponse} from '@/pages/System/Menu/types';
import {MenuTypeEnum} from '@/pages/System/Menu/types';
import type {RootState} from '../store';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
@ -26,6 +27,29 @@ const {confirm} = Modal;
// 设置中文语言
dayjs.locale('zh-cn');
// 添加清理空children的函数同时过滤掉按钮类型菜单
const cleanEmptyChildren = (menus: MenuResponse[]): MenuResponse[] => {
// 先过滤掉按钮类型的菜单
return menus
.filter(menu => menu.type !== MenuTypeEnum.BUTTON)
.map(menu => {
const cleanedMenu = { ...menu };
if (cleanedMenu.children && cleanedMenu.children.length > 0) {
// 递归处理子菜单,同样需要过滤按钮
cleanedMenu.children = cleanEmptyChildren(cleanedMenu.children);
// 如果过滤后子菜单为空则删除children属性
if (cleanedMenu.children.length === 0) {
delete cleanedMenu.children;
}
} else {
delete cleanedMenu.children;
}
return cleanedMenu;
});
};
const BasicLayout: React.FC = () => {
const navigate = useNavigate();
const location = useLocation();
@ -49,12 +73,16 @@ const BasicLayout: React.FC = () => {
// 初始化用户数据
useEffect(() => {
const initializeUserData = async () => {
// try {
setLoading(true);
const menuData = await getCurrentUserMenus();
console.log(menuData)
dispatch(setMenus(menuData));
setLoading(false);
try {
const menuData = await getCurrentUserMenus();
dispatch(setMenus(cleanEmptyChildren(menuData)));
} catch (error) {
message.error('获取菜单数据失败');
console.error('获取菜单数据失败:', error);
} finally {
setLoading(false);
}
};
if (!userInfo) {
@ -62,7 +90,7 @@ const BasicLayout: React.FC = () => {
} else {
setLoading(false);
}
}, []);
}, [dispatch, userInfo]);
// 处理时间和天气更新
useEffect(() => {
@ -129,12 +157,16 @@ const BasicLayout: React.FC = () => {
// 将菜单数据转换为antd Menu需要的格式
const getMenuItems = (menuList: MenuResponse[]): MenuProps['items'] => {
return menuList?.map(menu => ({
key: menu.path || menu.id.toString(),
icon: getIcon(menu.icon),
label: menu.name,
children: menu.children && menu.children.length > 0 ? getMenuItems(menu.children) : undefined
}));
return menuList
?.filter(menu => menu.type !== MenuTypeEnum.BUTTON) // 过滤掉按钮类型的菜单
?.map(menu => ({
key: menu.path || menu.id.toString(),
icon: getIcon(menu.icon),
label: menu.name,
children: menu.children && menu.children.length > 0
? getMenuItems(menu.children) // 递归处理子菜单
: undefined
}));
};
if (loading) {

View File

@ -203,7 +203,7 @@ const UserPage: React.FC = () => {
{
title: '操作',
key: 'action',
width: 240,
width: 320,
fixed: 'right' as FixedType,
render: (_: any, record: UserResponse) => (
<Space size={0}>