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