Krishya | 2e0f49a | 2025-05-29 10:59:01 +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; |