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