| ybt | da5978b | 2025-05-31 15:58:05 +0800 | [diff] [blame] | 1 | // src/features/auth/pages/LoginPage.jsx |
| 2 | import React, { useState } from "react"; |
| 3 | import { useNavigate, Link } from "react-router-dom"; |
| 4 | import { |
| 5 | Form, |
| 6 | Input, |
| 7 | Button, |
| 8 | Checkbox, |
| 9 | Card, |
| 10 | Typography, |
| 11 | Space, |
| 12 | Divider, |
| 13 | message, |
| 14 | } from "antd"; |
| 15 | import { UserOutlined, LockOutlined } from "@ant-design/icons"; |
| 16 | import { useAuth } from "../contexts/AuthContext"; // 使用新的 AuthContext |
| ybt | da5978b | 2025-05-31 15:58:05 +0800 | [diff] [blame] | 17 | |
| 18 | const { Title, Text } = Typography; |
| 19 | |
| 20 | const 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 { |
| ybt | bac75f2 | 2025-06-08 22:31:15 +0800 | [diff] [blame] | 35 | 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 | } |
| ybt | da5978b | 2025-05-31 15:58:05 +0800 | [diff] [blame] | 47 | } 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 */} |
| ybt | 0d010e5 | 2025-06-09 00:29:36 +0800 | [diff] [blame] | 129 | <div className="text-center space-y-3"> |
| 130 | <div> |
| 131 | <Text type="secondary" className="mr-1"> |
| 132 | 还没有账号? |
| 133 | </Text> |
| 134 | <Link |
| 135 | to="/register" |
| 136 | className="font-medium text-blue-600 hover:text-blue-700 hover:underline" |
| 137 | > |
| 138 | 立即注册 |
| 139 | </Link> |
| 140 | </div> |
| 141 | <div> |
| 142 | <Link |
| 143 | to="/admin/login" |
| 144 | className="font-medium text-orange-600 hover:text-orange-700 hover:underline" |
| 145 | > |
| 146 | 管理员登录 |
| 147 | </Link> |
| 148 | </div> |
| ybt | da5978b | 2025-05-31 15:58:05 +0800 | [diff] [blame] | 149 | </div> |
| 150 | </Form> |
| 151 | {/* 提示信息部分可以保留或移除 */} |
| 152 | <div className="mt-8 p-4 bg-slate-50 rounded-md border border-slate-200"> |
| 153 | {" "} |
| 154 | {/* Tailwind: border, border-slate-200 */} |
| 155 | <Text |
| 156 | type="secondary" |
| 157 | className="block mb-2 font-semibold text-slate-600" |
| 158 | > |
| 159 | 测试账号提示 |
| 160 | </Text> |
| 161 | <ul className="space-y-1 text-sm text-slate-500 list-disc list-inside"> |
| 162 | <li>管理员: admin / admin123</li> |
| 163 | <li>普通用户: user / user123</li> |
| 164 | {/* ...其他测试账号 */} |
| 165 | </ul> |
| 166 | </div> |
| 167 | </Card> |
| 168 | </div> |
| 169 | ); |
| 170 | }; |
| 171 | |
| 172 | export default LoginPage; |