blob: 1ae6c85d1903150038aa1dd9d57da848dfe6ca07 [file] [log] [blame]
2230101462240ab2025-06-07 09:28:16 +08001import { Loading3QuartersOutlined, LockOutlined, MailOutlined } from '@ant-design/icons';
2import { Button, Checkbox, Flex, Form, Input } from 'antd';
3import { NavLink, useNavigate } from 'react-router';
4import { useAppDispatch, useAppSelector } from '../../store/hooks';
5import { loginUser } from './authSlice';
22301014b1477f72025-06-07 22:54:40 +08006import { useEffect } from 'react';
2230101462240ab2025-06-07 09:28:16 +08007import useMessage from 'antd/es/message/useMessage';
22301014bc4616f2025-06-03 16:59:44 +08008
2230101462240ab2025-06-07 09:28:16 +08009// 定义 Form 表单的字段类型
10interface FormValues {
11 email: string;
12 password: string;
13 remember: boolean;
14}
15
22301014bc4616f2025-06-03 16:59:44 +080016function Login() {
2230101462240ab2025-06-07 09:28:16 +080017 const dispatch = useAppDispatch();
18 const auth = useAppSelector(state => (state.auth));
19 const [messageApi, Message] = useMessage()
22301014b1477f72025-06-07 22:54:40 +080020 const nav = useNavigate()
2230101462240ab2025-06-07 09:28:16 +080021
22 useEffect(() => {
23 if (auth.isAuth) {
22301014b1477f72025-06-07 22:54:40 +080024 nav('/', { replace: true });
2230101462240ab2025-06-07 09:28:16 +080025 }
26 if (!auth.loading && auth.error) {
27 messageApi.error(auth.error);
28 }
29 }, [auth, messageApi, nav])
22301014b1477f72025-06-07 22:54:40 +080030
2230101462240ab2025-06-07 09:28:16 +080031 // 给 onFinish 参数添加类型
32 const onFinish = async (values: FormValues) => {
33 try {
34 await dispatch(loginUser({ email: values.email, password: values.password }));
35 } catch (error) {
36 console.error('登录失败', error);
37 }
22301014bc4616f2025-06-03 16:59:44 +080038 };
39
40 return (
41 <Form
42 name="login"
43 initialValues={{ remember: true }}
22301014bc4616f2025-06-03 16:59:44 +080044 onFinish={onFinish}
45 >
2230101462240ab2025-06-07 09:28:16 +080046 {Message}
22301014bc4616f2025-06-03 16:59:44 +080047 <h2>登录</h2>
48 <Form.Item
49 name="email"
50 rules={[{ required: true, message: '请输入你的账号(注册邮箱)!' }, { type: 'email', message: '请输入正确的邮箱' }]}
51 >
52 <Input prefix={<MailOutlined />} placeholder="账号(注册邮箱)" />
53 </Form.Item>
54 <Form.Item
55 name="password"
56 rules={[{ required: true, message: '请输入你的密码!' }]}
57 >
58 <Input prefix={<LockOutlined />} type="password" placeholder="密码" />
59 </Form.Item>
60 <Form.Item>
61 <Flex justify="space-between" align="center">
62 <Form.Item name="remember" valuePropName="checked" noStyle>
63 <Checkbox>自动登录</Checkbox>
64 </Form.Item>
2230101462240ab2025-06-07 09:28:16 +080065 <NavLink to="/forget"> 忘记密码 </NavLink>
22301014bc4616f2025-06-03 16:59:44 +080066 </Flex>
67 </Form.Item>
68
69 <Form.Item>
70 <Button block type="primary" htmlType="submit">
2230101462240ab2025-06-07 09:28:16 +080071 {auth.loading ? (
72 <><Loading3QuartersOutlined /></>
73 ) : (
74 <>登录</>
75 )
76 }
22301014bc4616f2025-06-03 16:59:44 +080077 </Button>
2230101462240ab2025-06-07 09:28:16 +080078 <NavLink to="/register">注册</NavLink>
22301014bc4616f2025-06-03 16:59:44 +080079 </Form.Item>
80 </Form>
81 );
2230101462240ab2025-06-07 09:28:16 +080082}
22301014bc4616f2025-06-03 16:59:44 +080083
2230101462240ab2025-06-07 09:28:16 +080084export default Login;