blob: 78701f1dc22dc42f585534cd897040fe00191ade [file] [log] [blame]
2230100977253112025-04-15 21:30:39 +08001import React, { useEffect, useState } from 'react';
2import axios from 'axios';
3import './UserProfile.css';
4import UserNav from './UserNav/UserNav'; // 导入 UserNav 组件
Krishyac0f7e9b2025-04-22 15:28:28 +08005import Header from '../../components/Header';
2230100977253112025-04-15 21:30:39 +08006
7
8const API_BASE = process.env.REACT_APP_API_BASE;
9
223010097ff51f22025-04-15 21:35:28 +080010const UserProfile = () => {
2230100977253112025-04-15 21:30:39 +080011 const [userProfile, setUserProfile] = useState(null);
12 const [loading, setLoading] = useState(true);
13 const [error, setError] = useState(null);
14
15 // 假设用户ID为1,实际应用中应通过认证信息获取
16 const userId = 1;
17
18 useEffect(() => {
19 // 获取用户个人资料
20 const fetchUserProfile = async () => {
21 try {
22 setLoading(true);
23 const response = await axios.get(`${API_BASE}/echo/user/profile`, {
24 params: { user_id: userId }
25 });
26
27 if (response.data.status === 'success') {
28 setUserProfile(response.data);
29 } else {
30 setError('用户不存在');
31 }
32 } catch (err) {
33 setError('请求失败,请稍后再试');
34 } finally {
35 setLoading(false);
36 }
37 };
38
39 fetchUserProfile();
40 }, [userId]);
41
42 if (loading) {
43 return <p>加载中...</p>;
44 }
45
46 if (error) {
47 return <p className="error">{error}</p>;
48 }
49
50 return (
Krishyac0f7e9b2025-04-22 15:28:28 +080051 <div className="user-profile-container">
52 <Header /> {/* 引入 Header 组件 */}
2230100977253112025-04-15 21:30:39 +080053 <div className="user-center">
Krishyac0f7e9b2025-04-22 15:28:28 +080054 <div className="user-nav-container">
2230100977253112025-04-15 21:30:39 +080055 <UserNav /> {/* 引入导航栏 */}
56 </div>
Krishyac0f7e9b2025-04-22 15:28:28 +080057 <div className="common-card">
58 <div className="right-content">
59 <div className="profile-header">
60 <img
61 src={userProfile.avatar_url}
62 alt={userProfile.nickname}
63 className="avatar"
64 />
65 <h1>{userProfile.nickname}</h1>
66 </div>
2230100977253112025-04-15 21:30:39 +080067
Krishyac0f7e9b2025-04-22 15:28:28 +080068 <div className="profile-details">
69 {/* <h2>个人资料</h2> */}
70 <p><strong>邮箱:</strong>{userProfile.email}</p>
71 <p><strong>性别:</strong>{userProfile.gender}</p>
72 <p><strong>个人简介:</strong>{userProfile.bio}</p>
73 <p><strong>兴趣:</strong>{userProfile.interests.join(', ')}</p>
74 <p><strong>等级:</strong>{userProfile.level}</p>
75 <p><strong>经验:</strong>{userProfile.experience}</p>
76 <p><strong>上传量:</strong>{userProfile.upload_amount}</p>
77 <p><strong>下载量:</strong>{userProfile.download_amount}</p>
78 <p><strong>分享率:</strong>{(userProfile.share_rate * 100).toFixed(2)}%</p>
79 <p><strong>VIP:</strong>{userProfile.is_vip ? '是' : '否'}</p>
80 {userProfile.is_vip && <p><strong>VIP等级:</strong>{userProfile.vip_type}</p>}
81 <p><strong>加入时间:</strong>{new Date(userProfile.joined_date).toLocaleDateString()}</p>
82 </div>
83 </div>
84 </div>
2230100977253112025-04-15 21:30:39 +080085 </div>
86 </div>
87 );
88};
89
Krishyac0f7e9b2025-04-22 15:28:28 +080090export default UserProfile;