import React, { useState, useEffect } from 'react'; import { Form, Input, Button, Card, message, Typography, Tabs, Divider, Space, Modal, Alert } from 'antd'; import { UserOutlined, LockOutlined, MailOutlined, PhoneOutlined, SafetyCertificateOutlined, RobotOutlined } from '@ant-design/icons'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; const { Title, Text } = Typography; const Login = () => { const [loading, setLoading] = useState(false); const [isFirstUser, setIsFirstUser] = useState(false); const [registerMode, setRegisterMode] = useState(false); const { login, register, checkAdmin } = useAuth(); const navigate = useNavigate(); useEffect(() => { checkIsFirstUser(); }, []); const checkIsFirstUser = async () => { try { const response = await checkAdmin(); if (response.success) { setIsFirstUser(!response.data.hasAdmin); if (!response.data.hasAdmin) { setRegisterMode(true); } } } catch (error) { console.error('检查用户状态失败:', error); } }; const onFinishLogin = async (values) => { setLoading(true); try { const result = await login(values.username, values.password); if (result.success) { message.success('登录成功'); navigate('/'); } else { message.error(result.message || '登录失败'); } } catch (error) { message.error(error || '登录失败'); } finally { setLoading(false); } }; const onFinishRegister = async (values) => { if (values.password !== values.confirmPassword) { message.error('两次输入的密码不一致'); return; } setLoading(true); try { const result = await register({ username: values.username, password: values.password, email: values.email, phone: values.phone, realName: values.realName }); if (result.success) { message.success(result.isFirstUser ? '注册成功,已为您创建管理员账户' : '注册成功'); navigate('/'); } else { message.error(result.message || '注册失败'); } } catch (error) { message.error(error || '注册失败'); } finally { setLoading(false); } }; const containerStyle = { minHeight: '100vh', display: 'flex', justifyContent: 'center', alignItems: 'center', background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)', padding: '24px' }; const cardStyle = { width: '100%', maxWidth: isFirstUser ? 450 : 400, borderRadius: '16px', boxShadow: '0 20px 60px rgba(0,0,0,0.3)' }; const headerStyle = { textAlign: 'center', marginBottom: '32px' }; const titleStyle = { fontSize: '28px', fontWeight: '700', color: '#1a1a2e', marginBottom: '8px' }; const subtitleStyle = { fontSize: '14px', color: '#666' }; const formStyle = { marginTop: '24px' }; const submitButtonStyle = { width: '100%', height: '48px', fontSize: '16px', fontWeight: '600', borderRadius: '8px' }; const footerStyle = { textAlign: 'center', marginTop: '24px' }; return (
{isFirstUser ? '创建管理员账户' : 'IDC设备管理系统'} {isFirstUser ? '首次使用,请创建系统管理员账户' : '请登录您的账户'}
{isFirstUser && ( )}
{registerMode ? ( <> } placeholder="用户名" /> } placeholder="真实姓名" /> } placeholder="邮箱" /> } placeholder="手机号(可选)" /> } placeholder="密码" /> ({ validator(_, value) { if (!value || getFieldValue('password') === value) { return Promise.resolve(); } return Promise.reject(new Error('两次输入的密码不一致')); }, }), ]} > } placeholder="确认密码" /> ) : ( <> } placeholder="用户名" /> } placeholder="密码" /> )}
{!isFirstUser && (
}>
)}
); }; export default Login;