blob: 3a8cdbc7c324da5ce6b9dce50aa47128c74b2b7a [file] [log] [blame]
ybtda5978b2025-05-31 15:58:05 +08001// src/features/auth/pages/LoginPage.jsx
2import React, { useState } from "react";
3import { useNavigate, Link } from "react-router-dom";
4import {
5 Form,
6 Input,
7 Button,
8 Checkbox,
9 Card,
10 Typography,
11 Space,
12 Divider,
13 message,
14} from "antd";
15import { UserOutlined, LockOutlined } from "@ant-design/icons";
16import { useAuth } from "../contexts/AuthContext"; // 使用新的 AuthContext
ybtda5978b2025-05-31 15:58:05 +080017
18const { Title, Text } = Typography;
19
20const LoginPage = () => {
21 const [loading, setLoading] = useState(false);
22 const navigate = useNavigate();
23 const { login, isAuthenticated, user } = useAuth(); // 从 Context 获取 login 方法等
24
25 React.useEffect(() => {
26 // 如果已经登录,并且有用户信息,则重定向到首页
27 if (isAuthenticated && user) {
28 navigate("/");
29 }
30 }, [isAuthenticated, user, navigate]);
31
32 const onFinish = async (values) => {
33 setLoading(true);
34 try {
ybtbac75f22025-06-08 22:31:15 +080035 const params = {
36 username: values.username,
37 password: values.password,
38 };
39 const userData = await login(params);
40
41 // 根据用户角色导航到不同页面
42 if (userData && userData.role === 'admin') {
43 navigate("/admin"); // 管理员导航到管理面板
44 } else {
45 navigate("/"); // 普通用户导航到首页
46 }
ybtda5978b2025-05-31 15:58:05 +080047 } catch (error) {
48 // 错误消息由 AuthContext 中的 login 方法或 request 拦截器处理
49 console.error("Login page error:", error);
50 } finally {
51 setLoading(false);
52 }
53 };
54
55 return (
56 <div className="flex justify-center items-center min-h-screen bg-slate-100 p-4">
57 {" "}
58 {/* Tailwind: bg-gray-100 -> bg-slate-100 */}
59 <Card className="w-full max-w-md shadow-lg rounded-lg">
60 {" "}
61 {/* Tailwind: rounded-lg */}
62 <div className="text-center mb-8">
63 {" "}
64 {/* Tailwind: mb-6 -> mb-8 */}
65 <Title level={2} className="!mb-2 text-slate-700">
66 PT站登录
67 </Title>{" "}
68 {/* Tailwind: text-slate-700 */}
69 <Text type="secondary">欢迎回来,请登录您的账号</Text>
70 </div>
71 <Form
72 name="login_form" // 最好给表单一个唯一的名字
73 initialValues={{ remember: true }}
74 onFinish={onFinish}
75 size="large"
76 layout="vertical"
77 className="space-y-6" // Tailwind: 间距控制
78 >
79 <Form.Item
80 name="username"
81 rules={[{ required: true, message: "请输入您的用户名!" }]}
82 >
83 <Input
84 prefix={<UserOutlined className="site-form-item-icon" />}
85 placeholder="用户名"
86 />
87 </Form.Item>
88 <Form.Item
89 name="password"
90 rules={[{ required: true, message: "请输入您的密码!" }]}
91 >
92 <Input.Password
93 prefix={<LockOutlined className="site-form-item-icon" />}
94 placeholder="密码"
95 />
96 </Form.Item>
97 <Form.Item className="!mb-0">
98 {" "}
99 {/* Tailwind: !mb-0 覆盖antd默认margin */}
100 <div className="flex justify-between items-center">
101 <Form.Item name="remember" valuePropName="checked" noStyle>
102 <Checkbox>记住我</Checkbox>
103 </Form.Item>
104 <Link
105 to="/forgot-password"
106 className="text-blue-600 hover:text-blue-700 hover:underline"
107 >
108 {" "}
109 {/* Tailwind: hover:underline */}
110 忘记密码?
111 </Link>
112 </div>
113 </Form.Item>
114 <Form.Item>
115 <Button
116 type="primary"
117 htmlType="submit"
118 className="w-full !text-base"
119 loading={loading}
120 >
121 {" "}
122 {/* Tailwind: !text-base (示例) */}登
123 </Button>
124 </Form.Item>
125 <Divider plain>
126 <span className="text-slate-500">或</span>
127 </Divider>{" "}
128 {/* Tailwind: text-slate-500 */}
129 <div className="text-center">
130 <Text type="secondary" className="mr-1">
131 还没有账号?
132 </Text>
133 <Link
134 to="/register"
135 className="font-medium text-blue-600 hover:text-blue-700 hover:underline"
136 >
137 立即注册
138 </Link>
139 </div>
140 </Form>
141 {/* 提示信息部分可以保留或移除 */}
142 <div className="mt-8 p-4 bg-slate-50 rounded-md border border-slate-200">
143 {" "}
144 {/* Tailwind: border, border-slate-200 */}
145 <Text
146 type="secondary"
147 className="block mb-2 font-semibold text-slate-600"
148 >
149 测试账号提示
150 </Text>
151 <ul className="space-y-1 text-sm text-slate-500 list-disc list-inside">
152 <li>管理员: admin / admin123</li>
153 <li>普通用户: user / user123</li>
154 {/* ...其他测试账号 */}
155 </ul>
156 </div>
157 </Card>
158 </div>
159 );
160};
161
162export default LoginPage;