blob: a5d2d97afe1a56e8dfd746e1d5def5fcd7d1502c [file] [log] [blame]
zhaoyumaof8f81842025-06-09 00:00:46 +08001import { useEffect, useState } from 'react';
2import { Card, List, Button, message, Carousel, Modal } from 'antd';
3import { getUserInfo } from '@/services/session';
4import { getPlanetList, updateUserPlanet, getUserPlanet, getPlanetInfo, PlanetEntity, getRandomUserPlanets } from '@/services/planets/api';
5import { useRequest } from '@umijs/max';
6import { getUser } from '@/services/system/user';
7
8const RelationPage = () => {
9 const [planetList, setPlanetList] = useState<PlanetEntity[]>([]);
10 const [relatedUsers, setRelatedUsers] = useState<any[]>([]);
11 const [selectedUser, setSelectedUser] = useState<any>(null);
12 const [userModalVisible, setUserModalVisible] = useState(false);
13 // 获取用户信息
14 const { data: userInfo, loading: userLoading } = useRequest(async () => {
15 const userRes = await getUserInfo();
16 // 假设 userId 为字符串类型,需要将其转换为数字类型,同时处理可能的 undefined 情况
17 const userId = userRes.user.userId ? parseInt(userRes.user.userId, 10) : 1;
18 const UserplanetRes = await getUserPlanet({ userId });
19 console.log(UserplanetRes);
20 const planetId = UserplanetRes.planetId || 1;
21 const planetRes = await getPlanetInfo({ planetId });
22
23 return {
24 data: {
25 ...userRes.user,
26 planet: planetRes
27 }
28 };
29 });
30
31
32
33
34 console.log(userInfo);
35 const [listLoading, setListLoading] = useState(false);
36 // 获取所有GIF列表
37 const fetchPlanetList = async () => {
38 try {
39 setListLoading(true);
40 const res = await getPlanetList({ pageNum: 1, pageSize: 100 });
41 console.log(res);
42 setPlanetList(res);
43 } catch (error) {
44 message.error('获取GIF列表失败');
45 } finally {
46 setListLoading(false);
47 }
48 };
49
50 // 获取相关用户信息
51 const fetchRelatedUsers = async () => {
52 try {
53 // 获取随机用户星球列表
54 const randomPlanetsNo = await getRandomUserPlanets();
55 const userId = userInfo?.userId ? parseInt(userInfo.userId, 10) : 1;
56 const randomPlanets = randomPlanetsNo.filter((item) => item.userId !== userId);
57 // 获取完整用户信息并关联星球数据
58 const usersWithPlanets = await Promise.all(
59 randomPlanets.map(async (item) => {
60
61 // 确保 item.userId 为 number 类型,若为 undefined 则使用默认值 1
62 const userIdToUse = typeof item.userId === 'number' ? item.userId : 1;
63 const userRes = await getUser(userIdToUse);
64 const planetRes = await getPlanetInfo({ planetId: item.planetId });
65 return {
66 user: userRes.data,
67 planet: planetRes
68 };
69 })
70 );
71
72 setRelatedUsers(usersWithPlanets);
73 console.log(usersWithPlanets);
74 } catch (error) {
75 message.error('获取相关用户失败');
76 console.error(error);
77 }
78 };
79
80 useEffect(() => {
81 fetchPlanetList();
82 fetchRelatedUsers();
83 }, []);
84 // 显示用户详情
85 const showUserDetails = (user: any) => {
86 setSelectedUser(user);
87 setUserModalVisible(true);
88 };
89 console.log(planetList);
90 // 更新用户形象
91 const handleUpdateAvatar = async (id: number | undefined) => {
92 if (!id) {
93 message.error('无效的星球ID');
94 return;
95 }
96 try {
97 console.log(id);
98 // 处理 userInfo.userId 可能为 undefined 的情况,若为 undefined 则使用默认值 '1'
99 const userIdStr = userInfo?.userId || '1';
100 console.log(userIdStr);
101 await updateUserPlanet({ planetId: id, userId: parseInt(userIdStr, 10) });
102 message.success('更新形象成功');
103 window.location.reload();
104 } catch (error) {
105 message.error('更新形象失败');
106 }
107 };
108 if (userLoading) {
109 return <div>Loading...</div>;
110 }
111
112 return (
113 <div>
114 <Card title="当前形象" style={{ background: 'transparent', borderColor: '#3a3a5c' }}>
115 {userInfo?.planet && (
116 <div style={{ textAlign: 'center' }}>
117 <img
118 src={`/images/planets/${userInfo.planet.filename}.gif`}
119 alt="用户形象"
120 style={{
121 width: 200,
122 height: 200,
123 borderRadius: '50%',
124 border: '2px solid #4a4a7a',
125 boxShadow: '0 0 15px #6a5acd'
126 }}
127 />
128 <div style={{
129 marginTop: 16,
130 color: '#a8b2d1',
131 fontSize: 16
132 }}>
133 当前形象: {userInfo.planet.name}
134 </div>
135 </div>
136 )}
137 </Card>
138
139 <Card title="选择新形象" style={{
140 marginTop: 24,
141 background: 'transparent',
142 borderColor: '#3a3a5c'
143 }}>
144 <Carousel
145 dots={false}
146 arrows={true}
147 style={{ width: '100%', maxWidth: 800, margin: '0 auto' }}
148 >
149 {planetList.map((item) => (
150 <div key={item.planetId} style={{ textAlign: 'center', padding: '0 40px' }}>
151 <div style={{ marginBottom: 16 }}>
152 <img
153 src={`/images/planets/${item.filename}.gif`}
154 alt={item.name}
155 style={{
156 width: 200,
157 height: 200,
158 borderRadius: '50%',
159 border: '2px solid #4a4a7a',
160 boxShadow: '0 0 15px #6a5acd',
161 marginTop: 16,
162 margin: '0 auto'
163 }}
164 />
165 </div>
166 <div style={{
167 color: '#a8b2d1',
168 marginBottom: 16
169 }}>
170 <div style={{ fontSize: 18, fontWeight: 'bold' }}>{item.name}</div>
171 <div style={{ margin: '8px 0' }}>{item.description}</div>
172 <div>创建时间: {item.createTime}</div>
173 </div>
174 <Button
175 type="primary"
176 onClick={() => handleUpdateAvatar(item.planetId)}
177 style={{
178 background: 'linear-gradient(45deg, #6a5acd, #9370db)',
179 border: 'none'
180 }}
181 >
182 选择
183 </Button>
184 </div>
185 ))}
186 </Carousel>
187 </Card>
188
189 <Card title="与你相关的人" style={{
190 marginTop: 24,
191 background: 'transparent',
192 borderColor: '#3a3a5c'
193 }}>
194 <Carousel
195 dots={false}
196 arrows={true}
197 style={{ width: '100%', maxWidth: 800, margin: '0 auto' }}
198 >
199 {relatedUsers.map((item) => (
200 <div key={item.userId} style={{ textAlign: 'center', padding: '0 40px' }}>
201 <div style={{ marginBottom: 16 }}>
202 <img
203 src={`/images/planets/${item.planet.filename}.gif`}
204 alt={item.planet.name}
205 style={{
206 width: 200,
207 height: 200,
208 borderRadius: '50%',
209 border: '2px solid #4a4a7a',
210 boxShadow: '0 0 15px #6a5acd',
211 margin: '0 auto'
212 }}
213 />
214 </div>
215 <div style={{
216 color: '#a8b2d1',
217 marginBottom: 16
218 }}>
219 <div style={{ fontSize: 18, fontWeight: 'bold' }}>昵称:{item.user.nickName}</div>
220 <div style={{ margin: '8px 0' }}>邮箱:{item.user.email}</div>
221 <div>星球形象:{item.planet.name}</div>
222 </div>
223 <Button
224 type="primary"
225 onClick={() => showUserDetails(item.user)}
226 style={{
227 background: 'linear-gradient(45deg, #6a5acd, #9370db)',
228 border: 'none'
229 }}
230 >
231 查看详情
232 </Button>
233 </div>
234 ))}
235 </Carousel>
236 </Card>
237
238 <Modal
239 title="用户详情"
240 visible={userModalVisible}
241 onCancel={() => setUserModalVisible(false)}
242 footer={null}
243 style={{ color: '#a8b2d1' }}
244 >
245 {selectedUser && (
246 <div>
247 <p>昵称: {selectedUser.nickName}</p>
248 <p>邮箱: {selectedUser.email}</p>
249 <p>电话: {selectedUser.phonenumber}</p>
250
251 </div>
252 )}
253 </Modal>
254 </div>
255 );
256};
257
258export default RelationPage;