From 75b1393c9b9ee65c9bf400c62bf0053be7e81d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=9A=E8=BE=B0=E5=85=88=E7=94=9F?= Date: Sun, 1 Dec 2024 16:16:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E6=AD=A3=E5=B8=B8=E5=90=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/layouts/BasicLayout.tsx | 56 +++++++++++++++++++----- frontend/src/pages/System/User/index.tsx | 2 +- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/frontend/src/layouts/BasicLayout.tsx b/frontend/src/layouts/BasicLayout.tsx index d16f6bd7..3aafe2da 100644 --- a/frontend/src/layouts/BasicLayout.tsx +++ b/frontend/src/layouts/BasicLayout.tsx @@ -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) { diff --git a/frontend/src/pages/System/User/index.tsx b/frontend/src/pages/System/User/index.tsx index b268e856..9484832c 100644 --- a/frontend/src/pages/System/User/index.tsx +++ b/frontend/src/pages/System/User/index.tsx @@ -203,7 +203,7 @@ const UserPage: React.FC = () => { { title: '操作', key: 'action', - width: 240, + width: 320, fixed: 'right' as FixedType, render: (_: any, record: UserResponse) => (