Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame^] | 1 | // // src/pages/UserInfo.js |
| 2 | // import React, { useEffect, useState } from 'react'; |
| 3 | // import axios from 'axios'; |
| 4 | // import { useLocation } from 'wouter'; |
| 5 | |
| 6 | // const DEFAULT_AVATAR_URL = '/default-avatar.png'; // 替换为你的默认头像地址 |
| 7 | |
| 8 | // const UserInfo = () => { |
| 9 | // const [location] = useLocation(); |
| 10 | // const userId = location.split('/').pop(); |
| 11 | // const [userInfo, setUserInfo] = useState(null); |
| 12 | // const [error, setError] = useState(null); |
| 13 | |
| 14 | // useEffect(() => { |
| 15 | // const fetchUserInfo = async () => { |
| 16 | // try { |
| 17 | // setError(null); |
| 18 | // const { data: raw } = await axios.get(`/echo/user/${userId}/getProfile`); |
| 19 | |
| 20 | // if (!raw) { |
| 21 | // setError('用户数据为空'); |
| 22 | // setUserInfo(null); |
| 23 | // return; |
| 24 | // } |
| 25 | |
| 26 | // const profile = { |
| 27 | // avatarUrl: raw.avatarUrl |
| 28 | // ? `${process.env.REACT_APP_AVATAR_BASE_URL}${raw.avatarUrl}` |
| 29 | // : DEFAULT_AVATAR_URL, |
| 30 | // nickname: raw.username || '未知用户', |
| 31 | // email: raw.email || '未填写', |
| 32 | // gender: raw.gender || '保密', |
| 33 | // bio: raw.description || '无', |
| 34 | // interests: raw.hobbies ? raw.hobbies.split(',') : [], |
| 35 | // level: raw.level || '未知', |
| 36 | // experience: raw.experience ?? 0, |
| 37 | // uploadAmount: raw.uploadCount ?? 0, |
| 38 | // downloadAmount: raw.downloadCount ?? 0, |
| 39 | // shareRate: raw.shareRate ?? 0, |
| 40 | // joinedDate: raw.registrationTime, |
| 41 | // }; |
| 42 | |
| 43 | // setUserInfo(profile); |
| 44 | // } catch (err) { |
| 45 | // setError(err.response?.status === 404 ? '用户不存在' : '请求失败,请稍后再试'); |
| 46 | // setUserInfo(null); |
| 47 | // } |
| 48 | // }; |
| 49 | |
| 50 | // fetchUserInfo(); |
| 51 | // }, [userId]); |
| 52 | |
| 53 | // if (error) return <p>{error}</p>; |
| 54 | // if (!userInfo) return <p>加载中...</p>; |
| 55 | |
| 56 | // return ( |
| 57 | // <div className="user-profile"> |
| 58 | // <img src={userInfo.avatarUrl} alt="头像" /> |
| 59 | // <h2>{userInfo.nickname}</h2> |
| 60 | // <p>邮箱:{userInfo.email}</p> |
| 61 | // <p>性别:{userInfo.gender}</p> |
| 62 | // <p>简介:{userInfo.bio}</p> |
| 63 | // <p>兴趣爱好:{userInfo.interests.join('、')}</p> |
| 64 | // <p>等级:{userInfo.level}</p> |
| 65 | // <p>经验值:{userInfo.experience}</p> |
| 66 | // <p>上传量:{userInfo.uploadAmount}</p> |
| 67 | // <p>下载量:{userInfo.downloadAmount}</p> |
| 68 | // <p>分享率:{userInfo.shareRate}</p> |
| 69 | // <p>注册时间:{userInfo.joinedDate}</p> |
| 70 | // </div> |
| 71 | // ); |
| 72 | // }; |
| 73 | |
| 74 | // export default UserInfo; |
| 75 | |
| 76 | |
| 77 | // src/pages/UserInfo.jsx |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 78 | import React, { useEffect, useState } from 'react'; |
| 79 | import axios from 'axios'; |
| 80 | import { useLocation } from 'wouter'; |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame^] | 81 | import './UserInfo.css'; |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 82 | |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame^] | 83 | const DEFAULT_AVATAR_URL = '/default-avatar.png'; |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 84 | |
| 85 | const UserInfo = () => { |
| 86 | const [location] = useLocation(); |
| 87 | const userId = location.split('/').pop(); |
| 88 | const [userInfo, setUserInfo] = useState(null); |
| 89 | const [error, setError] = useState(null); |
| 90 | |
| 91 | useEffect(() => { |
| 92 | const fetchUserInfo = async () => { |
| 93 | try { |
| 94 | setError(null); |
| 95 | const { data: raw } = await axios.get(`/echo/user/${userId}/getProfile`); |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 96 | if (!raw) { |
| 97 | setError('用户数据为空'); |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
| 101 | const profile = { |
| 102 | avatarUrl: raw.avatarUrl |
| 103 | ? `${process.env.REACT_APP_AVATAR_BASE_URL}${raw.avatarUrl}` |
| 104 | : DEFAULT_AVATAR_URL, |
| 105 | nickname: raw.username || '未知用户', |
| 106 | email: raw.email || '未填写', |
| 107 | gender: raw.gender || '保密', |
| 108 | bio: raw.description || '无', |
| 109 | interests: raw.hobbies ? raw.hobbies.split(',') : [], |
| 110 | level: raw.level || '未知', |
| 111 | experience: raw.experience ?? 0, |
| 112 | uploadAmount: raw.uploadCount ?? 0, |
| 113 | downloadAmount: raw.downloadCount ?? 0, |
| 114 | shareRate: raw.shareRate ?? 0, |
| 115 | joinedDate: raw.registrationTime, |
| 116 | }; |
| 117 | |
| 118 | setUserInfo(profile); |
| 119 | } catch (err) { |
| 120 | setError(err.response?.status === 404 ? '用户不存在' : '请求失败,请稍后再试'); |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 121 | } |
| 122 | }; |
| 123 | |
| 124 | fetchUserInfo(); |
| 125 | }, [userId]); |
| 126 | |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame^] | 127 | if (error) return <p className="error">{error}</p>; |
| 128 | if (!userInfo) return <p className="loading">加载中...</p>; |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 129 | |
| 130 | return ( |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame^] | 131 | <div className="user-info-container"> |
| 132 | <div className="user-card"> |
| 133 | <img className="avatar" src={userInfo.avatarUrl} alt="用户头像" /> |
| 134 | <h2 className="nickname">{userInfo.nickname}</h2> |
| 135 | <p className="bio">{userInfo.bio}</p> |
| 136 | <div className="details"> |
| 137 | <p><strong>邮箱:</strong>{userInfo.email}</p> |
| 138 | <p><strong>性别:</strong>{userInfo.gender}</p> |
| 139 | <p><strong>兴趣爱好:</strong>{userInfo.interests.join('、')}</p> |
| 140 | <p><strong>等级:</strong>{userInfo.level}</p> |
| 141 | <p><strong>经验值:</strong>{userInfo.experience}</p> |
| 142 | <p><strong>上传量:</strong>{userInfo.uploadAmount}</p> |
| 143 | <p><strong>下载量:</strong>{userInfo.downloadAmount}</p> |
| 144 | <p><strong>分享率:</strong>{userInfo.shareRate}</p> |
| 145 | <p><strong>注册时间:</strong>{userInfo.joinedDate}</p> |
| 146 | </div> |
| 147 | </div> |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 148 | </div> |
| 149 | ); |
| 150 | }; |
| 151 | |
| 152 | export default UserInfo; |