blob: a10657c7b8cf81dfa364fe96c468895a0e7fd6a6 [file] [log] [blame]
2230100977253112025-04-15 21:30:39 +08001import React, { useEffect, useState } from 'react';
2import axios from 'axios';
3import './UserProfile.css';
2230100937ebec12025-06-03 21:17:04 +08004import UserNav from './UserNav';
Krishyac0f7e9b2025-04-22 15:28:28 +08005import Header from '../../components/Header';
2230100937ebec12025-06-03 21:17:04 +08006import { useUser } from '../../context/UserContext';
2230100977253112025-04-15 21:30:39 +08007
223010097ff51f22025-04-15 21:35:28 +08008const UserProfile = () => {
2230100937ebec12025-06-03 21:17:04 +08009 const { user, loading } = useUser(); // 从上下文拿用户和加载状态
2230100977253112025-04-15 21:30:39 +080010 const [userProfile, setUserProfile] = useState(null);
2230100977253112025-04-15 21:30:39 +080011 const [error, setError] = useState(null);
12
2230100977253112025-04-15 21:30:39 +080013 useEffect(() => {
2230100937ebec12025-06-03 21:17:04 +080014 if (loading) return; // 用户信息还没加载完,先不请求
15 console.log('用户:', user, '加载:', loading);
16
17 if (!user || !user.userId) {
18 setError('未登录或用户信息缺失');
19 setUserProfile(null);
20 return;
21 }
22
2230100977253112025-04-15 21:30:39 +080023 const fetchUserProfile = async () => {
24 try {
2230100937ebec12025-06-03 21:17:04 +080025 setError(null);
26 const response = await axios.get(`/echo/user/${user.userId}/getProfile`);
2230100977253112025-04-15 21:30:39 +080027
2230100937ebec12025-06-03 21:17:04 +080028 console.log('响应数据:', response); // 调试用
29 const raw = response.data;
30 console.log('raw:', raw); // 调试用
31
32 if (!raw) {
33 setError('用户数据为空');
34 setUserProfile(null);
35 return;
2230100977253112025-04-15 21:30:39 +080036 }
2230100937ebec12025-06-03 21:17:04 +080037
38 const profile = {
39 avatar_url: raw.avatarUrl || 'https://example.com/default-avatar.jpg',
40 nickname: raw.username || '未知用户',
41 email: raw.email || '未填写',
42 gender: raw.gender || '保密',
43 bio: raw.description || '无',
44 interests: raw.hobbies ? raw.hobbies.split(',') : [],
45 level: raw.level || '未知',
46 experience: raw.experience ?? 0,
47 upload_amount: raw.uploadCount ?? 0,
48 download_amount: raw.downloadCount ?? 0,
49 share_rate: raw.shareRate ?? 0,
50 joined_date: raw.registrationTime,
51 };
52
53 setUserProfile(profile);
2230100977253112025-04-15 21:30:39 +080054 } catch (err) {
2230100937ebec12025-06-03 21:17:04 +080055 if (err.response?.status === 404) {
56 setError('用户不存在');
57 } else {
58 setError('请求失败,请稍后再试');
59 }
60 setUserProfile(null);
2230100977253112025-04-15 21:30:39 +080061 }
62 };
63
64 fetchUserProfile();
2230100937ebec12025-06-03 21:17:04 +080065 }, [user, loading]);
2230100977253112025-04-15 21:30:39 +080066
2230100937ebec12025-06-03 21:17:04 +080067 if (loading) return <p>正在加载用户信息...</p>;
68 if (error) return <p className="error">{error}</p>;
69 if (!userProfile) return null;
2230100977253112025-04-15 21:30:39 +080070
71 return (
Krishyac0f7e9b2025-04-22 15:28:28 +080072 <div className="user-profile-container">
2230100937ebec12025-06-03 21:17:04 +080073 <Header />
74 <div className="user-center">
75 <div className="user-nav-container">
76 <UserNav />
Krishyac0f7e9b2025-04-22 15:28:28 +080077 </div>
2230100937ebec12025-06-03 21:17:04 +080078 <div className="common-card">
79 <div className="right-content">
80 <div className="profile-header">
81 <img
82 src={userProfile.avatar_url}
83 alt={userProfile.nickname}
84 className="avatar"
85 />
86 <h1>{userProfile.nickname}</h1>
87 </div>
2230100977253112025-04-15 21:30:39 +080088
2230100937ebec12025-06-03 21:17:04 +080089 <div className="profile-details">
90 <p><strong>邮箱:</strong>{userProfile.email}</p>
91 <p><strong>性别:</strong>{userProfile.gender}</p>
92 <p><strong>个人简介:</strong>{userProfile.bio}</p>
93 <p><strong>兴趣:</strong>{userProfile.interests.length > 0 ? userProfile.interests.join(', ') : '无'}</p>
94 <p><strong>等级:</strong>{userProfile.level}</p>
95 <p><strong>经验:</strong>{userProfile.experience}</p>
96 <p><strong>上传量:</strong>{userProfile.upload_amount}</p>
97 <p><strong>下载量:</strong>{userProfile.download_amount}</p>
98 <p><strong>分享率:</strong>{(userProfile.share_rate * 100).toFixed(2)}%</p>
99 <p><strong>加入时间:</strong>{new Date(userProfile.joined_date).toLocaleDateString()}</p>
100 </div>
101 </div>
Krishyac0f7e9b2025-04-22 15:28:28 +0800102 </div>
103 </div>
2230100977253112025-04-15 21:30:39 +0800104 </div>
105 );
106};
107
2230100937ebec12025-06-03 21:17:04 +0800108export default UserProfile;