blob: 56989ff2ac2987a5f0555a623a120ca5effdc84d [file] [log] [blame]
BirdNETM11aacb92025-06-07 23:17:03 +08001import React, { useState, useEffect } from 'react';
2import { useModel } from 'umi';
3import { Card, Input, Button, Space, Typography, Row, Col, message } from 'antd';
4import { CopyOutlined, UserAddOutlined, GiftOutlined, ShareAltOutlined } from '@ant-design/icons';
5import { getUserInviteCode, getUserByInviteCode } from './service';
6
7const { Title, Text, Paragraph } = Typography;
8
9const InvitePage: React.FC = () => {
10 const [myInviteCode, setMyInviteCode] = useState<string>('');
11 const [inputInviteCode, setInputInviteCode] = useState<string>('');
12 const [loading, setLoading] = useState<boolean>(false);
13 const [submitting, setSubmitting] = useState<boolean>(false);
14
15 // 获取当前用户信息
16 const { initialState } = useModel('@@initialState');
17 const userId = initialState?.currentUser?.userId || '';
18
19 // 获取我的邀请码
20 const fetchMyInviteCode = async () => {
21 if (!userId) {
22 message.error('用户信息获取失败');
23 return;
24 }
25
26 setLoading(true);
27 try {
28 const response = await getUserInviteCode();
29 if (response.code === 200) {
30 // 根据后端返回的数据结构调整
31 setMyInviteCode(response.msg);
32 } else {
33 message.error(response.msg || '获取邀请码失败');
34 }
35 } catch (error) {
36 message.error('获取邀请码失败');
37 } finally {
38 setLoading(false);
39 }
40 };
41
42 // 复制邀请码
43 const copyInviteCode = () => {
44 if (myInviteCode) {
45 navigator.clipboard.writeText(myInviteCode).then(() => {
46 message.success('邀请码已复制到剪贴板');
47 }).catch(() => {
48 message.error('复制失败,请手动复制');
49 });
50 }
51 };
52
53 // 查询邀请码对应的用户
54 const handleSubmitInviteCode = async () => {
55 if (!inputInviteCode.trim()) {
56 message.warning('请输入邀请码');
57 return;
58 }
59
60 setSubmitting(true);
61 try {
62 const response = await getUserByInviteCode(inputInviteCode.trim());
63
64 if (response.code === 200) {
65 message.success(`邀请码有效,对应用户ID: ${response.data}`);
66 setInputInviteCode('');
67 // 这里可以根据业务需求进行后续操作,比如关注用户等
68 } else {
69 message.error(response.msg || '邀请码无效');
70 }
71 } catch (error) {
72 message.error('查询邀请码失败');
73 } finally {
74 setSubmitting(false);
75 }
76 };
77
78 useEffect(() => {
79 fetchMyInviteCode();
80 }, []);
81
82 return (
83 <div
84 style={{
85 padding: '32px 24px',
86 maxWidth: '800px',
87 margin: '0 auto',
88 minHeight: '100vh',
89 background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
90 }}
91 >
92 {/* 头部标题区域 */}
93 <div style={{ textAlign: 'center', marginBottom: '40px' }}>
94 <GiftOutlined
95 style={{
96 fontSize: '48px',
97 color: '#fff',
98 marginBottom: '16px',
99 filter: 'drop-shadow(0 4px 8px rgba(0,0,0,0.2))'
100 }}
101 />
102 <Title
103 level={1}
104 style={{
105 color: '#fff',
106 margin: 0,
107 fontSize: '32px',
108 fontWeight: 600,
109 textShadow: '0 2px 4px rgba(0,0,0,0.3)'
110 }}
111 >
112 邀请码管理
113 </Title>
114 <Paragraph
115 style={{
116 color: 'rgba(255,255,255,0.8)',
117 fontSize: '16px',
118 margin: '8px 0 0 0'
119 }}
120 >
121 邀请好友,共享美好体验
122 </Paragraph>
123 </div>
124
125 <Row gutter={[24, 24]}>
126 {/* 我的邀请码 */}
127 <Col xs={24} lg={12}>
128 <Card
129 title={
130 <Space>
131 <ShareAltOutlined style={{ color: '#1890ff' }} />
132 <span style={{ color: '#1890ff', fontWeight: 600 }}>我的邀请码</span>
133 </Space>
134 }
135 style={{
136 height: '100%',
137 borderRadius: '16px',
138 boxShadow: '0 8px 32px rgba(0,0,0,0.12)',
139 border: 'none',
140 background: 'rgba(255,255,255,0.95)',
141 backdropFilter: 'blur(10px)'
142 }}
143 bodyStyle={{ padding: '24px' }}
144 >
145 <Space direction="vertical" style={{ width: '100%' }} size="large">
146 <div style={{
147 padding: '20px',
148 background: 'linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%)',
149 borderRadius: '12px',
150 textAlign: 'center'
151 }}>
152 <Text
153 type="secondary"
154 style={{
155 fontSize: '14px',
156 display: 'block',
157 marginBottom: '16px'
158 }}
159 >
160 分享下方邀请码给好友,邀请他们加入
161 </Text>
162 <div style={{
163 background: '#fff',
164 padding: '16px',
165 borderRadius: '8px',
166 border: '2px dashed #d9d9d9',
167 marginBottom: '16px'
168 }}>
169 <Text
170 copyable={false}
171 style={{
172 fontSize: '24px',
173 fontWeight: 'bold',
174 color: '#1890ff',
175 fontFamily: 'Monaco, monospace',
176 letterSpacing: '2px'
177 }}
178 >
179 {loading ? '加载中...' : (myInviteCode || '暂无邀请码')}
180 </Text>
181 </div>
182 <Button
183 type="primary"
184 size="large"
185 icon={<CopyOutlined />}
186 onClick={copyInviteCode}
187 disabled={!myInviteCode}
188 style={{
189 borderRadius: '8px',
190 height: '44px',
191 fontSize: '16px',
192 fontWeight: 500,
193 boxShadow: '0 4px 12px rgba(24, 144, 255, 0.3)'
194 }}
195 block
196 >
197 复制邀请码
198 </Button>
199 </div>
200 </Space>
201 </Card>
202 </Col>
203
204 {/* 输入邀请码 */}
205 <Col xs={24} lg={12}>
206 <Card
207 title={
208 <Space>
209 <UserAddOutlined style={{ color: '#52c41a' }} />
210 <span style={{ color: '#52c41a', fontWeight: 600 }}>查询邀请码</span>
211 </Space>
212 }
213 style={{
214 height: '100%',
215 borderRadius: '16px',
216 boxShadow: '0 8px 32px rgba(0,0,0,0.12)',
217 border: 'none',
218 background: 'rgba(255,255,255,0.95)',
219 backdropFilter: 'blur(10px)'
220 }}
221 bodyStyle={{ padding: '24px' }}
222 >
223 <Space direction="vertical" style={{ width: '100%' }} size="large">
224 <div style={{
225 padding: '20px',
226 background: 'linear-gradient(135deg, #f6ffed 0%, #e6f7ff 100%)',
227 borderRadius: '12px'
228 }}>
229 <Text
230 type="secondary"
231 style={{
232 fontSize: '14px',
233 display: 'block',
234 marginBottom: '20px',
235 textAlign: 'center'
236 }}
237 >
238 输入邀请码
239 </Text>
240 <Space direction="vertical" style={{ width: '100%' }} size="middle">
241 <Input
242 value={inputInviteCode}
243 onChange={(e) => setInputInviteCode(e.target.value)}
244 placeholder="请输入邀请码"
245 size="large"
246 style={{
247 borderRadius: '8px',
248 fontSize: '16px',
249 height: '48px',
250 textAlign: 'center',
251 fontFamily: 'Monaco, monospace',
252 letterSpacing: '1px'
253 }}
254 onPressEnter={handleSubmitInviteCode}
255 />
256 <Button
257 type="primary"
258 size="large"
259 icon={<UserAddOutlined />}
260 onClick={handleSubmitInviteCode}
261 loading={submitting}
262 disabled={!inputInviteCode.trim()}
263 style={{
264 borderRadius: '8px',
265 height: '44px',
266 fontSize: '16px',
267 fontWeight: 500,
268 background: '#52c41a',
269 borderColor: '#52c41a',
270 boxShadow: '0 4px 12px rgba(82, 196, 26, 0.3)'
271 }}
272 block
273 >
274 邀请码
275 </Button>
276 </Space>
277 </div>
278 </Space>
279 </Card>
280 </Col>
281 </Row>
282
283 {/* 底部装饰 */}
284 <div style={{
285 textAlign: 'center',
286 marginTop: '40px',
287 padding: '20px',
288 background: 'rgba(255,255,255,0.1)',
289 borderRadius: '12px',
290 backdropFilter: 'blur(10px)'
291 }}>
292 <Text style={{ color: 'rgba(255,255,255,0.7)', fontSize: '14px' }}>
293 通过邀请码连接更多朋友,一起享受精彩体验
294 </Text>
295 </div>
296 </div>
297 );
298};
299
300export default InvitePage;