Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 1 | // import React, { useState, useEffect } from 'react'; |
| 2 | // import axios from 'axios'; |
| 3 | |
| 4 | // const UserLevelExperience = ({ userId }) => { |
| 5 | // const [experienceInfo, setExperienceInfo] = useState(null); |
| 6 | // const [error, setError] = useState(null); |
| 7 | // const [isLoading, setIsLoading] = useState(false); |
| 8 | // const [upgradeResult, setUpgradeResult] = useState(null); |
| 9 | // const [hasCheckedIn, setHasCheckedIn] = useState(false); |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 10 | // const [isUpgrading, setIsUpgrading] = useState(false); |
| 11 | // const [lastUpgradeTime, setLastUpgradeTime] = useState(null); |
| 12 | // const [justUpgraded, setJustUpgraded] = useState(false); // 新增 |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 13 | |
| 14 | // useEffect(() => { |
| 15 | // if (!userId) return; |
| 16 | // fetchAllLevelData(); |
| 17 | // }, [userId]); |
| 18 | |
| 19 | // useEffect(() => { |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 20 | // if ( |
| 21 | // experienceInfo && |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 22 | // experienceInfo.current_experience >= experienceInfo.next_level_experience && |
| 23 | // !isUpgrading && |
| 24 | // !justUpgraded && |
| 25 | // (!lastUpgradeTime || Date.now() - lastUpgradeTime > 2000) |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 26 | // ) { |
| 27 | // checkUpgrade(); |
| 28 | // } |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 29 | // }, [experienceInfo, isUpgrading, lastUpgradeTime, justUpgraded]); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 30 | |
| 31 | // const fetchAllLevelData = async () => { |
| 32 | // try { |
| 33 | // setIsLoading(true); |
| 34 | // setError(null); |
| 35 | |
| 36 | // const { data } = await axios.get('/echo/level/getExperience', { |
| 37 | // params: { user_id: userId }, |
| 38 | // }); |
| 39 | |
| 40 | // const normalizedData = { |
| 41 | // ...data, |
| 42 | // current_level: data.current_level || data.level, |
| 43 | // }; |
| 44 | |
| 45 | // setExperienceInfo(normalizedData); |
| 46 | |
| 47 | // const today = new Date().toDateString(); |
| 48 | // const lastCheckIn = localStorage.getItem('lastCheckIn'); |
| 49 | // setHasCheckedIn(lastCheckIn === today); |
| 50 | // } catch (err) { |
| 51 | // console.error('经验信息获取失败:', err); |
| 52 | // setError('获取经验信息失败'); |
| 53 | // } finally { |
| 54 | // setIsLoading(false); |
| 55 | // } |
| 56 | // }; |
| 57 | |
| 58 | // const updateExperience = async (source, amount = 10) => { |
| 59 | // try { |
| 60 | // setIsLoading(true); |
| 61 | // setError(null); |
| 62 | |
| 63 | // const { data } = await axios.post('/echo/level/updateExperience', { |
| 64 | // user_id: userId, |
| 65 | // experience: amount, |
| 66 | // source: source, |
| 67 | // }); |
| 68 | |
| 69 | // setExperienceInfo((prev) => ({ |
| 70 | // ...prev, |
| 71 | // current_experience: data.current_experience, |
| 72 | // })); |
| 73 | |
| 74 | // alert(`获得${amount}点经验值!来源:${source}`); |
| 75 | |
| 76 | // if (source === 'check-in') { |
| 77 | // localStorage.setItem('lastCheckIn', new Date().toDateString()); |
| 78 | // setHasCheckedIn(true); |
| 79 | // } |
| 80 | // } catch (err) { |
| 81 | // console.error('更新经验失败:', err); |
| 82 | // setError(err.response?.data?.message || '更新经验失败'); |
| 83 | // } finally { |
| 84 | // setIsLoading(false); |
| 85 | // } |
| 86 | // }; |
| 87 | |
| 88 | // const checkUpgrade = async () => { |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 89 | // if (isUpgrading) return; |
| 90 | |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 91 | // try { |
| 92 | // setIsLoading(true); |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 93 | // setIsUpgrading(true); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 94 | // setError(null); |
| 95 | |
| 96 | // const { data } = await axios.get('/echo/level/upgrade-check', { |
| 97 | // params: { user_id: userId }, |
| 98 | // }); |
| 99 | |
| 100 | // if (data.can_upgrade) { |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 101 | // if (window.confirm('您已满足升级条件,是否要升级?')) { |
| 102 | // await performUpgrade(); |
| 103 | // } |
| 104 | // } else { |
| 105 | // if (data.is_max_level) { |
| 106 | // alert('您已达到最高等级!'); |
| 107 | // } else { |
| 108 | // alert(`还不能升级,还需要${data.next_level_experience - data.current_experience}点经验值`); |
| 109 | // } |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 110 | // } |
| 111 | // } catch (err) { |
| 112 | // console.error('检查升级失败:', err); |
| 113 | // setError(err.response?.data?.message || '检查升级失败'); |
| 114 | // } finally { |
| 115 | // setIsLoading(false); |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 116 | // setIsUpgrading(false); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 117 | // } |
| 118 | // }; |
| 119 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 120 | // const performUpgrade = async () => { |
| 121 | // try { |
| 122 | // setIsUpgrading(true); |
| 123 | // setIsLoading(true); |
| 124 | // setError(null); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 125 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 126 | // const { data } = await axios.post('/echo/level/upgrades', { |
| 127 | // user_id: userId, |
| 128 | // can_upgrade: true, |
| 129 | // }); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 130 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 131 | // console.log('升级响应数据:', data); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 132 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 133 | // if (data.status === 'success') { |
| 134 | // setExperienceInfo((prev) => ({ |
| 135 | // ...prev, |
| 136 | // current_level: data.new_level, |
| 137 | // current_experience: data.current_experience || 0, |
| 138 | // next_level_experience: data.next_level_experience || prev.next_level_experience * 2, |
| 139 | // })); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 140 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 141 | // setUpgradeResult(data); |
| 142 | // setLastUpgradeTime(Date.now()); |
| 143 | // setJustUpgraded(true); // 标记为刚升级过 |
| 144 | // setTimeout(() => setJustUpgraded(false), 3000); // 3 秒冷却期 |
| 145 | // alert(`恭喜!您已升级到等级 ${data.new_level}!`); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 146 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 147 | // // 再次拉取最新经验数据,避免经验值仍然满足升级条件 |
| 148 | // await fetchAllLevelData(); |
| 149 | // } else { |
| 150 | // throw new Error(data.message || '升级失败'); |
| 151 | // } |
| 152 | // } catch (err) { |
| 153 | // console.error('升级失败:', err); |
| 154 | // setError(err.message || '升级失败'); |
| 155 | // alert(err.message || '升级失败,请稍后再试'); |
| 156 | // } finally { |
| 157 | // setIsLoading(false); |
| 158 | // setIsUpgrading(false); |
| 159 | // } |
| 160 | // }; |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 161 | |
| 162 | // if (error) return <p className="error">{error}</p>; |
| 163 | // if (isLoading) return <p>加载中...</p>; |
| 164 | // if (!experienceInfo) return <p>加载经验信息中...</p>; |
| 165 | |
| 166 | // const { current_experience, next_level_experience, current_level } = experienceInfo; |
| 167 | // const progressPercent = Math.min( |
| 168 | // 100, |
| 169 | // (current_experience / (next_level_experience || 1)) * 100 |
| 170 | // ).toFixed(2); |
| 171 | |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 172 | // const expToNextLevel = Math.max(0, next_level_experience - current_experience); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 173 | |
| 174 | // return ( |
| 175 | // <div className="level-experience-section"> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 176 | // <h3>等级与经验</h3> |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 177 | // <p><strong>当前等级:</strong>{current_level || '未知'}</p> |
| 178 | // <p><strong>当前经验:</strong>{current_experience}</p> |
| 179 | // <p><strong>距离下一等级还需:</strong>{expToNextLevel} 经验值</p> |
| 180 | |
| 181 | // <div className="exp-bar-wrapper"> |
| 182 | // <div className="exp-bar" style={{ width: `${progressPercent}%` }} /> |
| 183 | // </div> |
| 184 | // <p className="exp-progress-text">{progressPercent}%</p> |
| 185 | |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 186 | // {upgradeResult && ( |
| 187 | // <div className="upgrade-success"> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 188 | // <p>恭喜!您已成功升级到等级 {upgradeResult.new_level}!</p> |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 189 | // </div> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 190 | // )} |
| 191 | |
| 192 | // {error && ( |
| 193 | // <div className="upgrade-error"> |
| 194 | // <p>{error}</p> |
| 195 | // </div> |
| 196 | // )} |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 197 | |
| 198 | // <div className="level-actions"> |
| 199 | // <button onClick={() => updateExperience('check-in', 15)} disabled={hasCheckedIn}> |
| 200 | // {hasCheckedIn ? '今日已签到' : '每日签到 (+15经验)'} |
| 201 | // </button> |
| 202 | // <button onClick={() => updateExperience('task', 30)}>完成任务 (+30经验)</button> |
| 203 | // <button onClick={() => updateExperience('upload', 50)}>上传种子 (+50经验)</button> |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 204 | // <button onClick={checkUpgrade} disabled={isUpgrading}> |
| 205 | // {isUpgrading ? '升级中...' : '检查升级'} |
| 206 | // </button> |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 207 | // </div> |
| 208 | // </div> |
| 209 | // ); |
| 210 | // }; |
| 211 | |
| 212 | // export default UserLevelExperience; |
| 213 | |