22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 1 | import React, { useEffect, useState } from 'react'; |
| 2 | import axios from 'axios'; |
| 3 | import './UserProfile.css'; |
| 4 | import UserNav from './UserNav/UserNav'; // 导入 UserNav 组件 |
Krishya | c0f7e9b | 2025-04-22 15:28:28 +0800 | [diff] [blame] | 5 | import Header from '../../components/Header'; |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 6 | |
| 7 | |
| 8 | const API_BASE = process.env.REACT_APP_API_BASE; |
| 9 | |
22301009 | 7ff51f2 | 2025-04-15 21:35:28 +0800 | [diff] [blame] | 10 | const UserProfile = () => { |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 11 | 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 ( |
Krishya | c0f7e9b | 2025-04-22 15:28:28 +0800 | [diff] [blame] | 51 | <div className="user-profile-container"> |
| 52 | <Header /> {/* 引入 Header 组件 */} |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 53 | <div className="user-center"> |
Krishya | c0f7e9b | 2025-04-22 15:28:28 +0800 | [diff] [blame] | 54 | <div className="user-nav-container"> |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 55 | <UserNav /> {/* 引入导航栏 */} |
| 56 | </div> |
Krishya | c0f7e9b | 2025-04-22 15:28:28 +0800 | [diff] [blame] | 57 | <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> |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 67 | |
Krishya | c0f7e9b | 2025-04-22 15:28:28 +0800 | [diff] [blame] | 68 | <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> |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 85 | </div> |
| 86 | </div> |
| 87 | ); |
| 88 | }; |
| 89 | |
Krishya | c0f7e9b | 2025-04-22 15:28:28 +0800 | [diff] [blame] | 90 | export default UserProfile; |