Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 1 | // import React, { useEffect, useState } from 'react'; |
| 2 | // import axios from 'axios'; |
| 3 | // import Header from '../../components/Header'; |
| 4 | // import './UserProfile.css'; // 复用个人资料页面的样式 |
| 5 | // import UserNav from './UserNav'; |
| 6 | |
| 7 | // const NewbieTasks = () => { |
| 8 | // const [tasks, setTasks] = useState([]); |
| 9 | // const [experience, setExperience] = useState({}); |
| 10 | // const [currentStep, setCurrentStep] = useState({}); |
| 11 | // const [loading, setLoading] = useState(true); |
| 12 | |
| 13 | // const baseURL = '/echo/task/tutorial'; |
| 14 | // const userId = 1; |
| 15 | |
| 16 | // // 获取所有任务 |
| 17 | // const fetchTasks = async () => { |
| 18 | // try { |
| 19 | // const response = await axios.get(`${baseURL}/getAllTasks`, { |
| 20 | // params: { user_id: userId }, |
| 21 | // }); |
| 22 | // setTasks(response.data.tasks || []); |
| 23 | // } catch (error) { |
| 24 | // console.error('获取任务失败:', error); |
| 25 | // } |
| 26 | // }; |
| 27 | |
| 28 | // // 获取经验与等级信息 |
| 29 | // const fetchExperience = async () => { |
| 30 | // try { |
| 31 | // const response = await axios.get(`${baseURL}/getAllTasks`, { |
| 32 | // params: { user_id: userId }, |
| 33 | // }); |
| 34 | // setExperience(response.data || {}); |
| 35 | // } catch (error) { |
| 36 | // console.error('获取经验失败:', error); |
| 37 | // } |
| 38 | // }; |
| 39 | |
| 40 | // // 获取当前引导步骤 |
| 41 | // const fetchCurrentStep = async () => { |
| 42 | // try { |
| 43 | // const response = await axios.get(`${baseURL}/getNewStep`, { |
| 44 | // params: { user_id: userId }, |
| 45 | // }); |
| 46 | // setCurrentStep(response.data || {}); |
| 47 | // } catch (error) { |
| 48 | // console.error('获取引导步骤失败:', error); |
| 49 | // } |
| 50 | // }; |
| 51 | |
| 52 | // // 更新任务状态 |
| 53 | // const updateStatus = async (taskId) => { |
| 54 | // try { |
| 55 | // await axios.post(`${baseURL}/updateStatus`, { |
| 56 | // user_id: userId, |
| 57 | // task_id: taskId, |
| 58 | // }); |
| 59 | // await fetchTasks(); |
| 60 | // await fetchExperience(); |
| 61 | // } catch (error) { |
| 62 | // console.error('更新状态失败:', error); |
| 63 | // } |
| 64 | // }; |
| 65 | |
| 66 | // // 提交任务进度 |
| 67 | // const updateProgress = async (taskId, progress) => { |
| 68 | // try { |
| 69 | // await axios.post(`${baseURL}/updateProgress`, { |
| 70 | // user_id: userId, |
| 71 | // task_id: taskId, |
| 72 | // progress, |
| 73 | // }); |
| 74 | // await fetchTasks(); |
| 75 | // } catch (error) { |
| 76 | // console.error('更新进度失败:', error); |
| 77 | // } |
| 78 | // }; |
| 79 | |
| 80 | // // 领取奖励 |
| 81 | // const claimReward = async (taskId) => { |
| 82 | // try { |
| 83 | // await axios.post(`${baseURL}/rewardClaim`, { |
| 84 | // user_id: userId, |
| 85 | // task_id: taskId, |
| 86 | // }); |
| 87 | // await fetchTasks(); |
| 88 | // await fetchExperience(); |
| 89 | // } catch (error) { |
| 90 | // console.error('领取奖励失败:', error); |
| 91 | // } |
| 92 | // }; |
| 93 | |
| 94 | // // 查看奖励是否已发放 |
| 95 | // const checkRewardStatus = async (taskId) => { |
| 96 | // try { |
| 97 | // const response = await axios.post(`${baseURL}/rewardReview`, { |
| 98 | // user_id: userId, |
| 99 | // task_id: taskId, |
| 100 | // }); |
| 101 | // alert(response.data.message || '已完成'); |
| 102 | // } catch (error) { |
| 103 | // console.error('查看失败:', error); |
| 104 | // } |
| 105 | // }; |
| 106 | |
| 107 | // // 页面初始化 |
| 108 | // useEffect(() => { |
| 109 | // const init = async () => { |
| 110 | // await fetchTasks(); |
| 111 | // await fetchExperience(); |
| 112 | // await fetchCurrentStep(); |
| 113 | // setLoading(false); |
| 114 | // }; |
| 115 | // init(); |
| 116 | // }, []); |
| 117 | |
| 118 | // if (loading) return <p className="loading">加载中...</p>; |
| 119 | |
| 120 | // return ( |
| 121 | // <div className="user-profile-container"> |
| 122 | // <Header /> |
| 123 | // <div className="user-center"> |
| 124 | // <div className="user-nav-container"> |
| 125 | // <UserNav activeKey="newbieTasks" onSelect={() => {}} /> |
| 126 | // </div> |
| 127 | |
| 128 | // <div className="common-card right-content"> |
| 129 | // <div className="profile-header"> |
| 130 | // <h1>🎯 新手考核</h1> |
| 131 | // </div> |
| 132 | |
| 133 | // <div className="profile-details"> |
| 134 | // <p><strong>当前经验:</strong>{experience.current_experience}</p> |
| 135 | // <p><strong>等级:</strong>{experience.level}</p> |
| 136 | // <p><strong>奖励经验:</strong>{experience.reward?.experience || 0}</p> |
| 137 | // <p><strong>奖励积分:</strong>{experience.reward?.points || 0}</p> |
| 138 | // </div> |
| 139 | |
| 140 | // <div className="profile-details"> |
| 141 | // <p><strong>🧭 当前引导步骤:</strong>{currentStep.current_step}</p> |
| 142 | // <p>{currentStep.step_description}</p> |
| 143 | // </div> |
| 144 | |
| 145 | // <div className="profile-details"> |
| 146 | // <h2>任务列表</h2> |
| 147 | // <ul> |
| 148 | // {tasks.map(task => ( |
| 149 | // <li key={task.task_id} style={{ marginBottom: '20px' }}> |
| 150 | // <p><strong>任务:</strong>{task.title}</p> |
| 151 | // <p><strong>描述:</strong>{task.description}</p> |
| 152 | // <p><strong>进度:</strong>{task.progress}%</p> |
| 153 | // <p><strong>状态:</strong>{task.status}</p> |
| 154 | // <p><strong>奖励:</strong>经验 {task.reward.experience},积分 {task.reward.points}</p> |
| 155 | |
| 156 | // <div style={{ marginTop: '10px' }}> |
| 157 | // <button className="task-btn" onClick={() => updateStatus(task.task_id)}>更新状态</button> |
| 158 | // <button className="task-btn" onClick={() => updateProgress(task.task_id, 100)}>提交进度</button> |
| 159 | // <button className="task-btn" onClick={() => claimReward(task.task_id)}>领取奖励</button> |
| 160 | // <button className="task-btn" onClick={() => checkRewardStatus(task.task_id)}>查看奖励是否已发放</button> |
| 161 | // </div> |
| 162 | // </li> |
| 163 | // ))} |
| 164 | // </ul> |
| 165 | // </div> |
| 166 | // </div> |
| 167 | // </div> |
| 168 | // </div> |
| 169 | // ); |
| 170 | // }; |
| 171 | |
| 172 | // export default NewbieTasks; |
| 173 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 174 | import React, { useEffect, useState } from 'react'; |
| 175 | import axios from 'axios'; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 176 | import './UserProfile.css'; // 继续复用样式 |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 177 | |
| 178 | const NewbieTasks = () => { |
| 179 | const [tasks, setTasks] = useState([]); |
| 180 | const [experience, setExperience] = useState({}); |
| 181 | const [currentStep, setCurrentStep] = useState({}); |
| 182 | const [loading, setLoading] = useState(true); |
| 183 | |
| 184 | const baseURL = '/echo/task/tutorial'; |
| 185 | const userId = 1; |
| 186 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 187 | const fetchTasks = async () => { |
| 188 | try { |
| 189 | const response = await axios.get(`${baseURL}/getAllTasks`, { |
| 190 | params: { user_id: userId }, |
| 191 | }); |
| 192 | setTasks(response.data.tasks || []); |
| 193 | } catch (error) { |
| 194 | console.error('获取任务失败:', error); |
| 195 | } |
| 196 | }; |
| 197 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 198 | const fetchExperience = async () => { |
| 199 | try { |
| 200 | const response = await axios.get(`${baseURL}/getAllTasks`, { |
| 201 | params: { user_id: userId }, |
| 202 | }); |
| 203 | setExperience(response.data || {}); |
| 204 | } catch (error) { |
| 205 | console.error('获取经验失败:', error); |
| 206 | } |
| 207 | }; |
| 208 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 209 | const fetchCurrentStep = async () => { |
| 210 | try { |
| 211 | const response = await axios.get(`${baseURL}/getNewStep`, { |
| 212 | params: { user_id: userId }, |
| 213 | }); |
| 214 | setCurrentStep(response.data || {}); |
| 215 | } catch (error) { |
| 216 | console.error('获取引导步骤失败:', error); |
| 217 | } |
| 218 | }; |
| 219 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 220 | const updateStatus = async (taskId) => { |
| 221 | try { |
| 222 | await axios.post(`${baseURL}/updateStatus`, { |
| 223 | user_id: userId, |
| 224 | task_id: taskId, |
| 225 | }); |
| 226 | await fetchTasks(); |
| 227 | await fetchExperience(); |
| 228 | } catch (error) { |
| 229 | console.error('更新状态失败:', error); |
| 230 | } |
| 231 | }; |
| 232 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 233 | const updateProgress = async (taskId, progress) => { |
| 234 | try { |
| 235 | await axios.post(`${baseURL}/updateProgress`, { |
| 236 | user_id: userId, |
| 237 | task_id: taskId, |
| 238 | progress, |
| 239 | }); |
| 240 | await fetchTasks(); |
| 241 | } catch (error) { |
| 242 | console.error('更新进度失败:', error); |
| 243 | } |
| 244 | }; |
| 245 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 246 | const claimReward = async (taskId) => { |
| 247 | try { |
| 248 | await axios.post(`${baseURL}/rewardClaim`, { |
| 249 | user_id: userId, |
| 250 | task_id: taskId, |
| 251 | }); |
| 252 | await fetchTasks(); |
| 253 | await fetchExperience(); |
| 254 | } catch (error) { |
| 255 | console.error('领取奖励失败:', error); |
| 256 | } |
| 257 | }; |
| 258 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 259 | const checkRewardStatus = async (taskId) => { |
| 260 | try { |
| 261 | const response = await axios.post(`${baseURL}/rewardReview`, { |
| 262 | user_id: userId, |
| 263 | task_id: taskId, |
| 264 | }); |
| 265 | alert(response.data.message || '已完成'); |
| 266 | } catch (error) { |
| 267 | console.error('查看失败:', error); |
| 268 | } |
| 269 | }; |
| 270 | |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 271 | useEffect(() => { |
| 272 | const init = async () => { |
| 273 | await fetchTasks(); |
| 274 | await fetchExperience(); |
| 275 | await fetchCurrentStep(); |
| 276 | setLoading(false); |
| 277 | }; |
| 278 | init(); |
| 279 | }, []); |
| 280 | |
| 281 | if (loading) return <p className="loading">加载中...</p>; |
| 282 | |
| 283 | return ( |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 284 | <div className="common-card right-content"> |
| 285 | <div className="profile-header"> |
| 286 | <h1>🎯 新手考核</h1> |
| 287 | </div> |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 288 | |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 289 | <div className="profile-details"> |
| 290 | <p><strong>当前经验:</strong>{experience.current_experience}</p> |
| 291 | <p><strong>等级:</strong>{experience.level}</p> |
| 292 | <p><strong>奖励经验:</strong>{experience.reward?.experience || 0}</p> |
| 293 | <p><strong>奖励积分:</strong>{experience.reward?.points || 0}</p> |
| 294 | </div> |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 295 | |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 296 | <div className="profile-details"> |
| 297 | <p><strong>🧭 当前引导步骤:</strong>{currentStep.current_step}</p> |
| 298 | <p>{currentStep.step_description}</p> |
| 299 | </div> |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 300 | |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 301 | <div className="profile-details"> |
| 302 | <h2>任务列表</h2> |
| 303 | <ul> |
| 304 | {tasks.map(task => ( |
| 305 | <li key={task.task_id} style={{ marginBottom: '20px', borderBottom: '1px solid #eee', paddingBottom: '10px' }}> |
| 306 | <p><strong>任务:</strong>{task.title}</p> |
| 307 | <p><strong>描述:</strong>{task.description}</p> |
| 308 | <p><strong>进度:</strong>{task.progress}%</p> |
| 309 | <p><strong>状态:</strong>{task.status}</p> |
| 310 | <p><strong>奖励:</strong>经验 {task.reward.experience},积分 {task.reward.points}</p> |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 311 | |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 312 | <div className="task-btn-group"> |
| 313 | <button className="task-btn" onClick={() => updateStatus(task.task_id)}>更新状态</button> |
| 314 | <button className="task-btn" onClick={() => updateProgress(task.task_id, 100)}>提交进度</button> |
| 315 | <button className="task-btn" onClick={() => claimReward(task.task_id)}>领取奖励</button> |
| 316 | <button className="task-btn" onClick={() => checkRewardStatus(task.task_id)}>查看奖励状态</button> |
| 317 | </div> |
| 318 | </li> |
| 319 | ))} |
| 320 | </ul> |
Krishya | 2e0f49a | 2025-05-29 10:59:01 +0800 | [diff] [blame] | 321 | </div> |
| 322 | </div> |
| 323 | ); |
| 324 | }; |
| 325 | |
| 326 | export default NewbieTasks; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 327 | |