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