刘嘉昕 | b845fa0 | 2025-06-09 17:34:59 +0800 | [diff] [blame^] | 1 | import React from 'react'; |
| 2 | import { Link, useNavigate, useLocation } from 'react-router-dom'; |
| 3 | import { Layout, Menu, Avatar } from 'antd'; |
| 4 | import { |
| 5 | HomeOutlined, |
| 6 | CloudUploadOutlined, |
| 7 | ThunderboltOutlined, |
| 8 | UsergroupAddOutlined |
| 9 | } from '@ant-design/icons'; |
| 10 | |
| 11 | const { Header } = Layout; |
| 12 | |
| 13 | const Navbar = () => { |
| 14 | const navigate = useNavigate(); |
| 15 | const location = useLocation(); |
| 16 | |
| 17 | // 从localStorage获取用户数据 |
| 18 | const userData = JSON.parse(localStorage.getItem('user')) || {}; |
| 19 | const { username, image } = userData; |
| 20 | |
| 21 | // 点击头像或用户名跳转到首页 |
| 22 | const handleUserClick = () => { |
| 23 | navigate('/usercenter'); |
| 24 | }; |
| 25 | |
| 26 | return ( |
| 27 | <Header |
| 28 | style={{ |
| 29 | backgroundColor: '#f5be42', |
| 30 | boxShadow: '0 2px 8px rgba(0,0,0,0.15)', |
| 31 | padding: '0 40px', |
| 32 | width: '100%', // 确保宽度扩展到100% |
| 33 | display: 'flex', |
| 34 | alignItems: 'center', |
| 35 | justifyContent: 'space-between' |
| 36 | }} |
| 37 | > |
| 38 | {/* Logo */} |
| 39 | <div style={{ color: '#fff', fontSize: '20px', fontWeight: 'bold' }}> |
| 40 | <Link to="/" style={{ color: '#fff', textDecoration: 'none' }}> |
| 41 | 社交互动平台 |
| 42 | </Link> |
| 43 | </div> |
| 44 | |
| 45 | {/* Menu */} |
| 46 | <Menu |
| 47 | mode="horizontal" |
| 48 | theme="dark" |
| 49 | selectedKeys={[location.pathname]} |
| 50 | style={{ |
| 51 | backgroundColor: 'transparent', |
| 52 | flex: 1, |
| 53 | justifyContent: 'center', |
| 54 | borderBottom: 'none' |
| 55 | }} |
| 56 | > |
| 57 | <Menu.Item key="/mainpage" icon={<HomeOutlined />}> |
| 58 | <Link to="/home" style={{ color: '#fff' }}>首页</Link> |
| 59 | </Menu.Item> |
| 60 | <Menu.Item key="/" icon={<HomeOutlined />}> |
| 61 | <Link to="/community" style={{ color: '#fff' }}>社区</Link> |
| 62 | </Menu.Item> |
| 63 | <Menu.Item key="/torrents" icon={<ThunderboltOutlined />}> |
| 64 | <Link to="/torrents" style={{ color: '#fff' }}>种子</Link> |
| 65 | </Menu.Item> |
| 66 | <Menu.Item key="/upload" icon={<CloudUploadOutlined />}> |
| 67 | <Link to="/upload" style={{ color: '#fff' }}>上传种子</Link> |
| 68 | </Menu.Item> |
| 69 | <Menu.Item key="/friend" icon={<UsergroupAddOutlined />}> |
| 70 | <Link to="/friend" style={{ color: '#fff' }}>好友</Link> |
| 71 | </Menu.Item> |
| 72 | <Menu.Item key="/shop" icon={<UsergroupAddOutlined />}> |
| 73 | <Link to="/shop" style={{ color: '#fff' }}>商城</Link> |
| 74 | </Menu.Item> |
| 75 | </Menu> |
| 76 | |
| 77 | {/* 用户名和头像 - 添加点击事件 */} |
| 78 | <div |
| 79 | style={{ |
| 80 | display: 'flex', |
| 81 | alignItems: 'center', |
| 82 | gap: '8px', |
| 83 | cursor: 'pointer' // 添加指针样式表示可点击 |
| 84 | }} |
| 85 | onClick={handleUserClick} |
| 86 | > |
| 87 | <Avatar |
| 88 | src={image} |
| 89 | size="default" |
| 90 | style={{ backgroundColor: '#f56a00' }} |
| 91 | /> |
| 92 | <span style={{ color: '#fff' }}>{username}</span> |
| 93 | </div> |
| 94 | </Header> |
| 95 | ); |
| 96 | }; |
| 97 | |
| 98 | export default Navbar; |