blob: 65de07e427070925f9e2abb861d99f60d6afbbc7 [file] [log] [blame]
ybt02e716d2025-04-15 17:19:32 +08001import React, { useState } from 'react';
2import { useNavigate, Link } from 'react-router-dom';
3import { Form, Input, Button, Card, message, Typography, Divider } from 'antd';
4import { UserOutlined, LockOutlined, MailOutlined } from '@ant-design/icons';
5import axios from 'axios';
6// 删除 import './Register.css';
7
8const { Title, Text } = Typography;
9
10const Register = () => {
11 const [loading, setLoading] = useState(false);
12 const navigate = useNavigate();
13
14 const onFinish = async (values) => {
15 setLoading(true);
16 try {
17 const response = await axios.post('/api/auth/register', {
18 username: values.username,
19 email: values.email,
20 password: values.password
21 });
22
23 if (response.data.code === 200) {
24 message.success('注册成功!');
25 navigate('/login');
26 } else {
27 message.error(response.data.message || '注册失败');
28 }
29 } catch (error) {
30 console.error('注册错误:', error);
31 message.error('注册失败,请检查网络连接');
32 } finally {
33 setLoading(false);
34 }
35 };
36
37 return (
38 <div className="flex justify-center items-center min-h-screen bg-gray-100">
39 <Card className="w-full max-w-md">
40 <div className="text-center mb-6">
41 <Title level={2} className="mb-3">注册账号</Title>
42 <Text type="secondary">创建您的PT网站账号</Text>
43 </div>
44
45 <Form
46 name="register"
47 onFinish={onFinish}
48 size="large"
49 layout="vertical"
50 >
51 <Form.Item
52 name="username"
53 rules={[
54 { required: true, message: '请输入用户名!' },
55 { min: 3, message: '用户名至少3个字符' },
56 { max: 20, message: '用户名最多20个字符' }
57 ]}
58 >
59 <Input prefix={<UserOutlined />} placeholder="用户名" />
60 </Form.Item>
61
62 <Form.Item
63 name="email"
64 rules={[
65 { required: true, message: '请输入邮箱!' },
66 { type: 'email', message: '请输入有效的邮箱地址!' }
67 ]}
68 >
69 <Input prefix={<MailOutlined />} placeholder="邮箱" />
70 </Form.Item>
71
72 <Form.Item
73 name="password"
74 rules={[
75 { required: true, message: '请输入密码!' },
76 { min: 6, message: '密码至少6个字符' }
77 ]}
78 >
79 <Input.Password prefix={<LockOutlined />} placeholder="密码" />
80 </Form.Item>
81
82 <Form.Item
83 name="confirm"
84 dependencies={['password']}
85 rules={[
86 { required: true, message: '请确认密码!' },
87 ({ getFieldValue }) => ({
88 validator(_, value) {
89 if (!value || getFieldValue('password') === value) {
90 return Promise.resolve();
91 }
92 return Promise.reject(new Error('两次输入的密码不一致!'));
93 },
94 }),
95 ]}
96 >
97 <Input.Password prefix={<LockOutlined />} placeholder="确认密码" />
98 </Form.Item>
99
100 <Form.Item>
101 <Button type="primary" htmlType="submit" className="w-full" loading={loading}>
102 注册
103 </Button>
104 </Form.Item>
105
106 <Divider plain>或者</Divider>
107
108 <div className="text-center mt-2">
109 <Text type="secondary">已有账号?</Text>
110 <Link to="/login">立即登录</Link>
111 </div>
112 </Form>
113 </Card>
114 </div>
115 );
116};
117
118export default Register;