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'; |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 4 | import { useUser } from '../../context/UserContext'; |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 5 | import { useLocation } from 'wouter'; |
| 6 | |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 7 | const DEFAULT_AVATAR_URL = `${process.env.PUBLIC_URL}/default-avatar.png`; |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 8 | |
22301009 | 7ff51f2 | 2025-04-15 21:35:28 +0800 | [diff] [blame] | 9 | const UserProfile = () => { |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 10 | const { user, loading, logout } = useUser(); |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 11 | const [userProfile, setUserProfile] = useState(null); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 12 | const [experienceInfo, setExperienceInfo] = useState(null); |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 13 | const [error, setError] = useState(null); |
| 14 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 15 | // 修改密码状态 |
| 16 | const [showPwdModal, setShowPwdModal] = useState(false); |
| 17 | const [oldPassword, setOldPassword] = useState(''); |
| 18 | const [newPassword, setNewPassword] = useState(''); |
| 19 | const [confirmPassword, setConfirmPassword] = useState(''); |
| 20 | |
| 21 | // 退出登录 |
| 22 | const [, setLocation] = useLocation(); |
| 23 | |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 24 | useEffect(() => { |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 25 | if (loading) return; |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 26 | if (!user || !user.userId) { |
| 27 | setError('未登录或用户信息缺失'); |
| 28 | setUserProfile(null); |
| 29 | return; |
| 30 | } |
| 31 | |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 32 | const fetchUserProfile = async () => { |
| 33 | try { |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 34 | setError(null); |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 35 | const { data: raw } = await axios.get(`/echo/user/${user.userId}/getProfile`); |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 36 | if (!raw) { |
| 37 | setError('用户数据为空'); |
| 38 | setUserProfile(null); |
| 39 | return; |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 40 | } |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 41 | |
| 42 | const profile = { |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 43 | avatarUrl: raw.avatarUrl |
| 44 | ? `${process.env.REACT_APP_AVATAR_BASE_URL}${raw.avatarUrl}` |
| 45 | : DEFAULT_AVATAR_URL, |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 46 | nickname: raw.username || '未知用户', |
| 47 | email: raw.email || '未填写', |
| 48 | gender: raw.gender || '保密', |
| 49 | bio: raw.description || '无', |
| 50 | interests: raw.hobbies ? raw.hobbies.split(',') : [], |
| 51 | level: raw.level || '未知', |
| 52 | experience: raw.experience ?? 0, |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 53 | uploadAmount: raw.uploadCount ?? 0, |
| 54 | downloadAmount: raw.downloadCount ?? 0, |
| 55 | shareRate: raw.shareRate ?? 0, |
| 56 | joinedDate: raw.registrationTime, |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | setUserProfile(profile); |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 60 | } catch (err) { |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 61 | setError(err.response?.status === 404 ? '用户不存在' : '请求失败,请稍后再试'); |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 62 | setUserProfile(null); |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 66 | const fetchExperienceInfo = async () => { |
| 67 | try { |
| 68 | const { data } = await axios.get('/echo/level/getExperience', { |
| 69 | params: { user_id: user.userId }, |
| 70 | }); |
| 71 | setExperienceInfo(data); |
| 72 | } catch (err) { |
| 73 | console.error('经验信息获取失败:', err); |
| 74 | } |
| 75 | }; |
| 76 | |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 77 | fetchUserProfile(); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 78 | fetchExperienceInfo(); |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 79 | }, [user, loading]); |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 80 | |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 81 | const handleAvatarUpload = async (e) => { |
| 82 | const file = e.target.files[0]; |
| 83 | if (!file) return; |
| 84 | |
| 85 | const formData = new FormData(); |
| 86 | formData.append('file', file); |
| 87 | |
| 88 | try { |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 89 | const { data } = await axios.post( |
| 90 | `/echo/user/${user.userId}/uploadAvatar`, |
| 91 | formData, |
| 92 | { headers: { 'Content-Type': 'multipart/form-data' } } |
| 93 | ); |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 94 | |
| 95 | if (data?.avatarUrl) { |
| 96 | setUserProfile((prev) => ({ |
| 97 | ...prev, |
| 98 | avatarUrl: `${process.env.REACT_APP_AVATAR_BASE_URL}${data.avatarUrl}`, |
| 99 | })); |
| 100 | alert('头像上传成功'); |
| 101 | } else { |
| 102 | alert('头像上传成功,但未返回新头像地址'); |
| 103 | } |
| 104 | } catch (err) { |
| 105 | console.error('上传失败:', err); |
| 106 | alert('头像上传失败,请重试'); |
| 107 | } |
| 108 | }; |
| 109 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 110 | const handleLogout = () => { |
| 111 | logout(); |
| 112 | setLocation('/auth'); // 退出后跳转登录页 |
| 113 | // window.location.reload(); // 或跳转登录页 |
| 114 | }; |
| 115 | |
| 116 | const handleChangePassword = async () => { |
| 117 | if (!oldPassword || !newPassword || !confirmPassword) { |
| 118 | alert('请填写所有字段'); |
| 119 | return; |
| 120 | } |
| 121 | if (newPassword !== confirmPassword) { |
| 122 | alert('两次输入的新密码不一致'); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | try { |
| 127 | // await axios.post('/echo/user/password', { |
| 128 | // user_id: user.userId, |
| 129 | // oldPassword, |
| 130 | // newPassword, |
| 131 | // }); |
| 132 | await axios.post('/echo/user/password', { |
| 133 | user_id: user.userId, |
| 134 | old_password: oldPassword, |
| 135 | new_password: newPassword, |
| 136 | confirm_password: confirmPassword, |
| 137 | }); |
| 138 | alert('密码修改成功,请重新登录'); |
| 139 | logout(); |
| 140 | window.location.reload(); |
| 141 | } catch (err) { |
| 142 | alert(err.response?.data?.message || '密码修改失败,请检查原密码是否正确'); |
| 143 | } |
| 144 | }; |
| 145 | |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 146 | if (loading) return <p>正在加载用户信息...</p>; |
| 147 | if (error) return <p className="error">{error}</p>; |
| 148 | if (!userProfile) return null; |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 149 | |
22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame] | 150 | const { |
| 151 | avatarUrl, |
| 152 | nickname, |
| 153 | email, |
| 154 | gender, |
| 155 | bio, |
| 156 | interests, |
| 157 | level, |
| 158 | experience, |
| 159 | uploadAmount, |
| 160 | downloadAmount, |
| 161 | shareRate, |
| 162 | joinedDate, |
| 163 | } = userProfile; |
| 164 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 165 | const progressPercent = experienceInfo |
| 166 | ? Math.min( |
| 167 | 100, |
| 168 | ((experienceInfo.current_experience || 0) / |
| 169 | (experienceInfo.next_level_experience || 1)) * |
| 170 | 100 |
| 171 | ).toFixed(2) |
| 172 | : 0; |
| 173 | |
| 174 | const expToNextLevel = experienceInfo |
| 175 | ? (experienceInfo.next_level_experience - experienceInfo.current_experience) |
| 176 | : null; |
| 177 | |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 178 | return ( |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 179 | <div className="common-card"> |
| 180 | <div className="right-content"> |
| 181 | <div className="profile-header"> |
| 182 | <div className="avatar-wrapper"> |
| 183 | <img src={avatarUrl} alt={nickname} className="avatar" /> |
| 184 | <label htmlFor="avatar-upload" className="avatar-upload-label"> |
| 185 | 上传头像 |
| 186 | </label> |
| 187 | <input |
| 188 | type="file" |
| 189 | id="avatar-upload" |
| 190 | accept="image/*" |
| 191 | style={{ display: 'none' }} |
| 192 | onChange={handleAvatarUpload} |
| 193 | /> |
22301009 | 37ebec1 | 2025-06-03 21:17:04 +0800 | [diff] [blame] | 194 | </div> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 195 | <h1>{nickname}</h1> |
| 196 | </div> |
| 197 | |
| 198 | <div className="profile-details"> |
| 199 | <p><strong>邮箱:</strong>{email}</p> |
| 200 | <p><strong>性别:</strong>{gender}</p> |
| 201 | <p><strong>个人简介:</strong>{bio}</p> |
| 202 | <p><strong>兴趣:</strong>{interests.length > 0 ? interests.join(', ') : '无'}</p> |
| 203 | <p><strong>等级:</strong>{level}</p> |
| 204 | <p><strong>经验:</strong>{experience}</p> |
| 205 | <p><strong>上传量:</strong>{uploadAmount}</p> |
| 206 | <p><strong>下载量:</strong>{downloadAmount}</p> |
| 207 | <p><strong>分享率:</strong>{(shareRate * 100).toFixed(2)}%</p> |
| 208 | <p><strong>加入时间:</strong>{new Date(joinedDate).toLocaleDateString()}</p> |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 209 | |
| 210 | {experienceInfo && ( |
| 211 | <> |
| 212 | <p><strong>距离下一等级还需:</strong>{expToNextLevel} 经验值</p> |
| 213 | <div className="exp-bar-wrapper"> |
| 214 | <div className="exp-bar" style={{ width: `${progressPercent}%` }} /> |
| 215 | </div> |
| 216 | <p className="exp-progress-text">{progressPercent}%</p> |
| 217 | </> |
| 218 | )} |
| 219 | |
| 220 | {/* 修改密码与退出登录按钮 */} |
| 221 | <div className="profile-actions"> |
| 222 | <button onClick={() => setShowPwdModal(true)}>修改密码</button> |
| 223 | <button onClick={handleLogout}>退出登录</button> |
| 224 | </div> |
| 225 | |
| 226 | {/* 修改密码弹窗 */} |
| 227 | {showPwdModal && ( |
| 228 | <div className="modal"> |
| 229 | <div className="modal-content"> |
| 230 | <h3>修改密码</h3> |
| 231 | <input |
| 232 | type="password" |
| 233 | placeholder="原密码" |
| 234 | value={oldPassword} |
| 235 | onChange={(e) => setOldPassword(e.target.value)} |
| 236 | /> |
| 237 | <input |
| 238 | type="password" |
| 239 | placeholder="新密码" |
| 240 | value={newPassword} |
| 241 | onChange={(e) => setNewPassword(e.target.value)} |
| 242 | /> |
| 243 | <input |
| 244 | type="password" |
| 245 | placeholder="确认新密码" |
| 246 | value={confirmPassword} |
| 247 | onChange={(e) => setConfirmPassword(e.target.value)} |
| 248 | /> |
| 249 | <div className="modal-buttons"> |
| 250 | <button onClick={handleChangePassword}>确认修改</button> |
| 251 | <button onClick={() => setShowPwdModal(false)}>取消</button> |
| 252 | </div> |
| 253 | </div> |
| 254 | </div> |
| 255 | )} |
Krishya | c0f7e9b | 2025-04-22 15:28:28 +0800 | [diff] [blame] | 256 | </div> |
| 257 | </div> |
22301009 | 7725311 | 2025-04-15 21:30:39 +0800 | [diff] [blame] | 258 | </div> |
| 259 | ); |
| 260 | }; |
| 261 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 262 | export default UserProfile; |
| 263 | |