import { useNavigate, useLocation } from 'react-router-dom' import { Dropdown, Avatar } from 'antd' import { UserOutlined, LogoutOutlined, // SettingOutlined, // MVP阶段暂时隐藏设置按钮 ControlOutlined, BankOutlined, } from '@ant-design/icons' import type { MenuProps } from 'antd' import { MODULES } from '../modules/moduleRegistry' import { useAuth } from '../auth' /** * 顶部导航栏组件 * * @description * - 显示Logo和平台名称 * - 显示模块导航(根据用户权限过滤)⭐ Week 2 Day 7 新增 * - 显示用户菜单 * * @version Week 2 Day 7 - 任务17:集成权限系统 */ const TopNavigation = () => { const navigate = useNavigate() const location = useLocation() const { user, logout: authLogout, hasModule } = useAuth() // 根据用户模块权限过滤可显示的模块 const availableModules = MODULES.filter(module => { // 没有 moduleCode 的模块跳过(占位模块) if (!module.moduleCode) return false; // 检查用户是否有权限访问 return hasModule(module.moduleCode); }); // 获取当前激活的模块 const activeModule = availableModules.find(module => location.pathname.startsWith(module.path) ) // 检查用户权限,决定显示哪些切换入口 const userRole = user?.role || '' const canAccessAdmin = ['SUPER_ADMIN', 'PROMPT_ENGINEER'].includes(userRole) const canAccessOrg = ['HOSPITAL_ADMIN', 'PHARMA_ADMIN', 'DEPARTMENT_ADMIN', 'SUPER_ADMIN'].includes(userRole) // 用户菜单 - 动态构建 const userMenuItems: MenuProps['items'] = [ { key: 'profile', icon: , label: '个人中心', }, // MVP阶段暂时隐藏设置按钮 // { // key: 'settings', // icon: , // label: '设置', // }, // 切换入口 - 根据权限显示 ...(canAccessOrg || canAccessAdmin ? [{ type: 'divider' as const }] : []), ...(canAccessOrg ? [{ key: 'switch-org', icon: , label: '切换到机构管理', }] : []), ...(canAccessAdmin ? [{ key: 'switch-admin', icon: , label: '切换到运营管理', }] : []), { type: 'divider', }, { key: 'logout', icon: , label: '退出登录', danger: true, }, ] // 处理用户菜单点击 const handleUserMenuClick = ({ key }: { key: string }) => { if (key === 'logout') { authLogout() navigate('/login') } else if (key === 'switch-admin') { navigate('/admin/dashboard') } else if (key === 'switch-org') { navigate('/org/dashboard') } else { navigate(`/user/${key}`) } } return (
{/* Logo */}
navigate('/')} > AI临床研究平台 AI临床研究平台
{/* 导航菜单 - 只显示有权限的模块 ⭐ 2026-01-16 更新为模块权限系统 */}
{availableModules.map(module => { const isActive = activeModule?.id === module.id return (
navigate(module.path)} className={` px-4 py-2 rounded-md transition-all cursor-pointer ${isActive ? 'bg-blue-50 text-blue-600 font-semibold' : 'text-gray-600 hover:bg-gray-50 hover:text-blue-600' } `} > {module.name}
) })}
{/* 用户菜单 - 显示真实用户信息和头像 */}
} size="small" />
{user?.name || '访客'} {user?.modules?.length || 0} 个模块
) } export default TopNavigation