| ybt | 0d010e5 | 2025-06-09 00:29:36 +0800 | [diff] [blame] | 1 | import React, { useState } from "react"; |
| 2 | import { useNavigate, Link } from "react-router-dom"; |
| 3 | import { |
| 4 | Form, |
| 5 | Input, |
| 6 | Button, |
| 7 | Card, |
| 8 | Typography, |
| 9 | Space, |
| 10 | Divider, |
| 11 | message, |
| 12 | Alert, |
| 13 | } from "antd"; |
| 14 | import { UserOutlined, LockOutlined, CrownOutlined } from "@ant-design/icons"; |
| 15 | import { useAuth } from "../contexts/AuthContext"; |
| 16 | |
| 17 | const { Title, Text } = Typography; |
| 18 | |
| 19 | const AdminLoginPage = () => { |
| 20 | const [loading, setLoading] = useState(false); |
| 21 | const navigate = useNavigate(); |
| 22 | const { adminLogin, isAuthenticated, user } = useAuth(); |
| 23 | |
| 24 | React.useEffect(() => { |
| 25 | // 如果已经登录且是管理员,重定向到管理面板 |
| 26 | if (isAuthenticated && user && user.role === 'admin') { |
| 27 | navigate("/admin"); |
| 28 | } |
| 29 | }, [isAuthenticated, user, navigate]); |
| 30 | |
| 31 | const onFinish = async (values) => { |
| 32 | setLoading(true); |
| 33 | try { |
| 34 | const params = { |
| 35 | username: values.username, |
| 36 | password: values.password, |
| 37 | }; |
| 38 | const userData = await adminLogin(params); |
| 39 | |
| 40 | console.log("admin login userData:", userData); |
| 41 | // 管理员登录成功后直接导航到管理面板 |
| 42 | if (userData && userData.uid.includes('admin')) { |
| 43 | navigate("/admin"); |
| 44 | } else { |
| 45 | message.error("该账号不是管理员账号"); |
| 46 | } |
| 47 | } catch (error) { |
| 48 | console.error("Admin login page error:", error); |
| 49 | } finally { |
| 50 | setLoading(false); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | return ( |
| 55 | <div className="flex justify-center items-center min-h-screen bg-gradient-to-br from-orange-100 to-red-100 p-4"> |
| 56 | <Card className="w-full max-w-md shadow-xl rounded-lg border-t-4 border-t-orange-500"> |
| 57 | <div className="text-center mb-8"> |
| 58 | <div className="mb-4"> |
| 59 | <CrownOutlined className="text-4xl text-orange-500" /> |
| 60 | </div> |
| 61 | <Title level={2} className="!mb-2 text-orange-700"> |
| 62 | 管理员登录 |
| 63 | </Title> |
| 64 | <Text type="secondary">请使用管理员账号登录系统</Text> |
| 65 | </div> |
| 66 | |
| 67 | <Alert |
| 68 | message="管理员登录" |
| 69 | description="此页面仅供系统管理员使用,请确认您拥有管理员权限。" |
| 70 | type="warning" |
| 71 | showIcon |
| 72 | className="mb-6" |
| 73 | /> |
| 74 | |
| 75 | <Form |
| 76 | name="admin_login_form" |
| 77 | initialValues={{ remember: true }} |
| 78 | onFinish={onFinish} |
| 79 | size="large" |
| 80 | layout="vertical" |
| 81 | className="space-y-6" |
| 82 | > |
| 83 | <Form.Item |
| 84 | name="username" |
| 85 | rules={[{ required: true, message: "请输入管理员用户名!" }]} |
| 86 | > |
| 87 | <Input |
| 88 | prefix={<UserOutlined className="site-form-item-icon" />} |
| 89 | placeholder="管理员用户名" |
| 90 | /> |
| 91 | </Form.Item> |
| 92 | <Form.Item |
| 93 | name="password" |
| 94 | rules={[{ required: true, message: "请输入管理员密码!" }]} |
| 95 | > |
| 96 | <Input.Password |
| 97 | prefix={<LockOutlined className="site-form-item-icon" />} |
| 98 | placeholder="管理员密码" |
| 99 | /> |
| 100 | </Form.Item> |
| 101 | <Form.Item> |
| 102 | <Button |
| 103 | type="primary" |
| 104 | htmlType="submit" |
| 105 | className="w-full !text-base bg-orange-500 border-orange-500 hover:bg-orange-600 hover:border-orange-600" |
| 106 | loading={loading} |
| 107 | > |
| 108 | 管理员登录 |
| 109 | </Button> |
| 110 | </Form.Item> |
| 111 | </Form> |
| 112 | |
| 113 | <Divider plain> |
| 114 | <span className="text-slate-500">其他选项</span> |
| 115 | </Divider> |
| 116 | |
| 117 | <div className="text-center space-y-3"> |
| 118 | <div> |
| 119 | <Link |
| 120 | to="/login" |
| 121 | className="font-medium text-blue-600 hover:text-blue-700 hover:underline" |
| 122 | > |
| 123 | 普通用户登录 |
| 124 | </Link> |
| 125 | </div> |
| 126 | <div> |
| 127 | <Text type="secondary" className="mr-1"> |
| 128 | 还没有账号? |
| 129 | </Text> |
| 130 | <Link |
| 131 | to="/register" |
| 132 | className="font-medium text-blue-600 hover:text-blue-700 hover:underline" |
| 133 | > |
| 134 | 立即注册 |
| 135 | </Link> |
| 136 | </div> |
| 137 | </div> |
| 138 | |
| 139 | {/* 管理员测试账号提示 */} |
| 140 | <div className="mt-8 p-4 bg-orange-50 rounded-md border border-orange-200"> |
| 141 | <Text |
| 142 | type="secondary" |
| 143 | className="block mb-2 font-semibold text-orange-700" |
| 144 | > |
| 145 | 管理员测试账号 |
| 146 | </Text> |
| 147 | <ul className="space-y-1 text-sm text-orange-600 list-disc list-inside"> |
| 148 | <li>admin / admin123</li> |
| 149 | <li>IcyIron / 111111</li> |
| 150 | </ul> |
| 151 | </div> |
| 152 | </Card> |
| 153 | </div> |
| 154 | ); |
| 155 | }; |
| 156 | |
| 157 | export default AdminLoginPage; |