| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 1 | import { Loading3QuartersOutlined, LockOutlined, MailOutlined } from '@ant-design/icons'; |
| 2 | import { Button, Checkbox, Flex, Form, Input } from 'antd'; |
| 3 | import { NavLink, useNavigate } from 'react-router'; |
| 4 | import { useAppDispatch, useAppSelector } from '../../store/hooks'; |
| 5 | import { loginUser } from './authSlice'; |
| 6 | import { useEffect, useRef } from 'react'; |
| 7 | import useMessage from 'antd/es/message/useMessage'; |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 8 | |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 9 | // 定义 Form 表单的字段类型 |
| 10 | interface FormValues { |
| 11 | email: string; |
| 12 | password: string; |
| 13 | remember: boolean; |
| 14 | } |
| 15 | |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 16 | function Login() { |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 17 | const dispatch = useAppDispatch(); |
| 18 | const auth = useAppSelector(state => (state.auth)); |
| 19 | const [messageApi, Message] = useMessage() |
| 20 | const nav = useRef(useNavigate()) |
| 21 | |
| 22 | useEffect(() => { |
| 23 | if (auth.isAuth) { |
| 24 | nav.current('/'); |
| 25 | } |
| 26 | if (!auth.loading && auth.error) { |
| 27 | messageApi.error(auth.error); |
| 28 | } |
| 29 | }, [auth, messageApi, nav]) |
| 30 | // 给 onFinish 参数添加类型 |
| 31 | const onFinish = async (values: FormValues) => { |
| 32 | try { |
| 33 | await dispatch(loginUser({ email: values.email, password: values.password })); |
| 34 | } catch (error) { |
| 35 | console.error('登录失败', error); |
| 36 | } |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | return ( |
| 40 | <Form |
| 41 | name="login" |
| 42 | initialValues={{ remember: true }} |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 43 | onFinish={onFinish} |
| 44 | > |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 45 | {Message} |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 46 | <h2>登录</h2> |
| 47 | <Form.Item |
| 48 | name="email" |
| 49 | rules={[{ required: true, message: '请输入你的账号(注册邮箱)!' }, { type: 'email', message: '请输入正确的邮箱' }]} |
| 50 | > |
| 51 | <Input prefix={<MailOutlined />} placeholder="账号(注册邮箱)" /> |
| 52 | </Form.Item> |
| 53 | <Form.Item |
| 54 | name="password" |
| 55 | rules={[{ required: true, message: '请输入你的密码!' }]} |
| 56 | > |
| 57 | <Input prefix={<LockOutlined />} type="password" placeholder="密码" /> |
| 58 | </Form.Item> |
| 59 | <Form.Item> |
| 60 | <Flex justify="space-between" align="center"> |
| 61 | <Form.Item name="remember" valuePropName="checked" noStyle> |
| 62 | <Checkbox>自动登录</Checkbox> |
| 63 | </Form.Item> |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 64 | <NavLink to="/forget"> 忘记密码 </NavLink> |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 65 | </Flex> |
| 66 | </Form.Item> |
| 67 | |
| 68 | <Form.Item> |
| 69 | <Button block type="primary" htmlType="submit"> |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 70 | {auth.loading ? ( |
| 71 | <><Loading3QuartersOutlined /></> |
| 72 | ) : ( |
| 73 | <>登录</> |
| 74 | ) |
| 75 | } |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 76 | </Button> |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 77 | 或 <NavLink to="/register">注册</NavLink> |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 78 | </Form.Item> |
| 79 | </Form> |
| 80 | ); |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 81 | } |
| 22301014 | bc4616f | 2025-06-03 16:59:44 +0800 | [diff] [blame] | 82 | |
| 22301014 | 62240ab | 2025-06-07 09:28:16 +0800 | [diff] [blame^] | 83 | export default Login; |