ybt | da5978b | 2025-05-31 15:58:05 +0800 | [diff] [blame] | 1 | import React from "react"; |
| 2 | import { Layout, Menu, Avatar, Dropdown } from "antd"; |
| 3 | import { |
| 4 | UserOutlined, |
| 5 | LogoutOutlined, |
| 6 | HomeOutlined, |
| 7 | AppstoreOutlined, |
| 8 | SettingOutlined, |
| 9 | CommentOutlined, |
| 10 | CloudDownloadOutlined, |
| 11 | UploadOutlined, |
| 12 | ToolOutlined, |
| 13 | } from "@ant-design/icons"; |
| 14 | import { useNavigate, Link, useLocation } from "react-router-dom"; |
| 15 | import { useAuth } from "../features/auth/contexts/AuthContext"; |
| 16 | |
| 17 | const { Header, Content, Footer } = Layout; |
| 18 | |
| 19 | // 路径与菜单key的映射 |
| 20 | const pathToMenuKey = { |
| 21 | "/": "1", |
| 22 | "/forum": "2", |
| 23 | "/pt": "3", |
| 24 | "/torrents": "4", |
| 25 | "/upload": "5", |
| 26 | "/tools": "6", |
| 27 | "/admin": "7", |
| 28 | "/profile": "profile", |
| 29 | }; |
| 30 | |
| 31 | const MainLayout = ({ children }) => { |
| 32 | const { user, logout } = useAuth(); |
| 33 | const location = useLocation(); |
| 34 | |
| 35 | // 根据当前路径获取对应的菜单key |
| 36 | const getSelectedKey = () => { |
| 37 | const path = location.pathname; |
| 38 | return pathToMenuKey[path] || "1"; // 默认选中首页 |
| 39 | }; |
| 40 | |
| 41 | const handleLogout = async () => { |
| 42 | await logout(); // logout 函数内已有消息提示和导航逻辑 |
| 43 | }; |
| 44 | |
| 45 | // 用户菜单项 |
| 46 | const userMenuItems = [ |
| 47 | { |
| 48 | key: "profile", |
| 49 | icon: <UserOutlined />, |
| 50 | label: <Link to="/profile">个人资料</Link>, |
| 51 | }, |
| 52 | { |
| 53 | type: "divider", |
| 54 | }, |
| 55 | { |
| 56 | key: "logout", |
| 57 | icon: <LogoutOutlined />, |
| 58 | label: "退出登录", |
| 59 | onClick: handleLogout, |
| 60 | }, |
| 61 | ]; |
| 62 | |
| 63 | // 主导航菜单项 |
| 64 | const menuItems = [ |
| 65 | { |
| 66 | key: "1", |
| 67 | icon: <HomeOutlined />, |
| 68 | label: <Link to="/">首页</Link>, |
| 69 | }, |
| 70 | { |
| 71 | key: "2", |
| 72 | icon: <CommentOutlined />, |
| 73 | label: <Link to="/forum">论坛</Link>, |
| 74 | }, |
| 75 | { |
| 76 | key: "3", |
| 77 | icon: <CloudDownloadOutlined />, |
| 78 | label: <Link to="/pt">PT</Link>, |
| 79 | }, |
| 80 | { |
| 81 | key: "4", |
| 82 | icon: <AppstoreOutlined />, |
| 83 | label: <Link to="/torrents">种子列表</Link>, |
| 84 | }, |
| 85 | { |
| 86 | key: "5", |
| 87 | icon: <UploadOutlined />, |
| 88 | label: <Link to="/upload">发布种子</Link>, |
| 89 | }, |
| 90 | { |
| 91 | key: "6", |
| 92 | icon: <ToolOutlined />, |
| 93 | label: <Link to="/tools">工具箱</Link>, |
| 94 | } |
| 95 | ]; |
| 96 | |
| 97 | // 如果用户是管理员,添加管理面板菜单项 |
| 98 | if (user?.role === "admin") { |
| 99 | menuItems.push({ |
| 100 | key: "7", |
| 101 | icon: <SettingOutlined />, |
| 102 | label: <Link to="/admin">管理面板</Link>, |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | return ( |
| 107 | <Layout className="min-h-screen"> |
| 108 | <Header className="flex items-center justify-between px-6"> |
| 109 | <div className="text-white text-xl font-bold">PT网站</div> |
| 110 | <Menu |
| 111 | theme="dark" |
| 112 | mode="horizontal" |
| 113 | selectedKeys={[getSelectedKey()]} |
| 114 | className="flex-1 justify-center" |
| 115 | style={{ maxWidth: "800px" }} |
| 116 | overflowedIndicator={null} |
| 117 | items={menuItems} |
| 118 | /> |
| 119 | <div> |
| 120 | <Dropdown menu={{ items: userMenuItems }} placement="bottomRight"> |
| 121 | <span className="flex items-center cursor-pointer text-white"> |
| 122 | <Avatar src={user?.avatar} icon={<UserOutlined />} /> |
| 123 | <span className="ml-2">{user?.username || "用户"}</span> |
| 124 | <span className="ml-1 text-xs opacity-80"> |
| 125 | ({user?.role || "游客"}) |
| 126 | </span> |
| 127 | </span> |
| 128 | </Dropdown> |
| 129 | </div> |
| 130 | </Header> |
| 131 | <Content className="p-6"> |
| 132 | <div className="bg-white p-6 min-h-[280px] rounded">{children}</div> |
| 133 | </Content> |
| 134 | <Footer style={{ textAlign: "center" }}> |
| 135 | PT网站 ©{new Date().getFullYear()} Created by G12-Team |
| 136 | </Footer> |
| 137 | </Layout> |
| 138 | ); |
| 139 | }; |
| 140 | |
| 141 | export default MainLayout; |