blob: 5b90c59f28b844d2b539b122b8a2153833c6f686 [file] [log] [blame]
22301014bc4616f2025-06-03 16:59:44 +08001import { useEffect, useState } from 'react';
2import { LockOutlined, MailOutlined, NumberOutlined, UserOutlined } from '@ant-design/icons';
3import { Button, Checkbox, Form, Input, Space } from 'antd';
4import { NavLink } from 'react-router';
5
6interface FormValues {
7 name: string;
8 email: string;
9 verifyCode: string;
10 password: string;
11 confirmPassword: string;
12 agreement: boolean;
13}
14
15function Register() {
16 const [countdown, setCountdown] = useState(0);
17 const [form] = Form.useForm<FormValues>();
18 const [emailStatues] = useState(false);
19
20 const sendVerificationCode = () => {
21 setCountdown(60)
22 }
23
24 // 发送表单倒计时
25 useEffect(() => {
26 if (countdown > 0) {
27 const timer = setTimeout(() => {
28 setCountdown(prev => prev - 1);
29 }, 1000);
30
31 return () => clearTimeout(timer);
32 }
33 }, [countdown]);
34
35
36 // 表单提交
37 const onFinish = (values: FormValues) => {
38 console.log('注册成功:', values);
39 };
40
41 return (
42 <Form
43 form={form}
44 style={{ maxWidth: 360 }}
45 onFinish={onFinish}
46 scrollToFirstError
47 >
48 <h2>注册</h2>
49
50 <Form.Item
51 name="name"
52 rules={[{ required: true, message: '请输入用户名' }]}
53 >
54 <Input prefix={<UserOutlined />} placeholder="请输入用户名" />
55 </Form.Item>
56
57 <Form.Item
58 name="email"
59
60 rules={[{ required: true, message: '请输入邮箱' }, { type: 'email', message: '邮箱格式错误' }]}
61 >
62 <Input
63 prefix={<MailOutlined />}
64 placeholder="请输入邮箱"
65 />
66 </Form.Item>
67
68 <Form.Item
69 name="verifyCode"
70 rules={[{ required: true, message: '请填写验证码' }]}
71 >
72 <Space direction="horizontal" size="small">
73 <Input
74 prefix={<NumberOutlined />}
75 placeholder="请输入验证码"
76 style={{ flex: 1 }}
77 />
78 <Button
79 disabled={countdown > 0 || !emailStatues}
80 onClick={sendVerificationCode}
81 style={{ width: 120 }}
82 >
83 {countdown > 0 ? `${countdown}s后重试` : '发送验证码'}
84 </Button>
85 </Space>
86 </Form.Item>
87
88 <Form.Item
89 name="password"
90 rules={[
91 { required: true, message: '请输入密码' },
92 { min: 6, message: '密码长度至少为6位' },
93 ]}
94 >
95 <Input.Password
96 prefix={<LockOutlined />}
97 placeholder="请输入密码"
98 />
99 </Form.Item>
100
101 <Form.Item
102 name="confirmPassword"
103 dependencies={['password']}
104 rules={[
105 { required: true, message: '请确认密码' },
106 ({ getFieldValue }) => ({
107 validator(_, value) {
108 if (!value || getFieldValue('password') === value) {
109 return Promise.resolve();
110 }
111 return Promise.reject('两次输入的密码不一致');
112 },
113 }),
114 ]}
115 >
116 <Input.Password
117 prefix={<LockOutlined />}
118 placeholder="请确认密码"
119 />
120 </Form.Item>
121
122 <Form.Item
123 name="agreement"
124 valuePropName="checked"
125 rules={[
126 {
127 validator: (_, value) =>
128 value ? Promise.resolve() : Promise.reject('请阅读并同意用户协议'),
129 },
130 ]}
131 >
132 <Checkbox>我已阅读并同意用户协议:<a>《创驿平台用户协议》</a></Checkbox>
133 </Form.Item>
134
135 <Form.Item>
136 <Button
137 type="primary"
138 htmlType="submit"
139 style={{ width: '100%' }}
140 >
141 注册
142 </Button>
143 <NavLink to='/login'>返回登录</NavLink>
144 </Form.Item>
145 </Form>
146 );
147}
148
149export default Register;