Header.jsx
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { useAuth } from '../../hooks/useAuth';  // 修正路径
import { useCart } from '../../hooks/useCart';  // 修正路径
const Header = () => {
  const { user, logout, isAuthenticated } = useAuth();
  const { getCartItemsCount } = useCart();
  const navigate = useNavigate();
  const handleLogout = () => {
    logout();
    navigate('/');
  };
  return (
    <header style={headerStyle}>
      <div style={containerStyle}>
        <Link to="/" style={logoStyle}>
          <h1 style={logoTitleStyle}>普罗米修甄选</h1>
        </Link>
        
        <nav style={navStyle}>
          <Link to="/" style={linkStyle}>首页</Link>
          <Link to="/products" style={linkStyle}>产品</Link>
          
          {isAuthenticated ? (
            <>
              <Link to="/cart" style={linkStyle}>
                购物车 ({getCartItemsCount()})
              </Link>
              <Link to="/profile" style={linkStyle}>个人中心</Link>
              <Link to="/orders" style={linkStyle}>我的订单</Link>
              <button onClick={handleLogout} style={logoutButtonStyle}>
                退出
              </button>
            </>
          ) : (
            <>
              <Link to="/login" style={linkStyle}>登录</Link>
              <Link to="/register" style={linkStyle}>注册</Link>
            </>
          )}
        </nav>
      </div>
    </header>
  );
};
const headerStyle = {
  backgroundColor: '#ff5722', // 橙色红色
  padding: '0.5rem 0',
  color: 'white',
  boxShadow: '0 2px 10px rgba(255, 87, 34, 0.3)',
  position: 'sticky',
  top: 0,
  zIndex: 1000,
  borderBottom: '2px solid #ff8a65',
};
const containerStyle = {
  maxWidth: '1200px',
  margin: '0 auto',
  padding: '0 1rem',
  display: 'flex',
  justifyContent: 'space-between',
  alignItems: 'center',
};
const logoStyle = {
  color: 'white',
  textDecoration: 'none',
};
const logoTitleStyle = {
  fontSize: '1.25rem',
  fontWeight: 'bold',
  margin: 0,
};
const navStyle = {
  display: 'flex',
  alignItems: 'center',
  gap: '1rem',
};
const linkStyle = {
  color: 'white',
  textDecoration: 'none',
  padding: '0.4rem 0.8rem',
  borderRadius: '4px',
  transition: 'all 0.3s ease',
  fontSize: '0.9rem',
};
const logoutButtonStyle = {
  backgroundColor: 'transparent',
  color: 'white',
  border: '1px solid white',
  padding: '0.4rem 0.8rem',
  borderRadius: '4px',
  cursor: 'pointer',
  transition: 'all 0.3s ease',
  fontSize: '0.9rem',
};
// 添加悬停效果
Object.assign(linkStyle, {
  ':hover': {
    backgroundColor: 'rgba(255, 255, 255, 0.2)',
    transform: 'translateY(-1px)',
  }
});
Object.assign(logoutButtonStyle, {
  ':hover': {
    backgroundColor: 'rgba(255, 255, 255, 0.2)',
    transform: 'translateY(-1px)',
  }
});
export default Header;