22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 1 | // export default NewbieTasks; |
| 2 | import React, { useEffect, useState } from 'react'; |
| 3 | import axios from 'axios'; |
| 4 | |
| 5 | const NewbieTasks = () => { |
| 6 | const [tasks, setTasks] = useState([]); |
| 7 | const [experience, setExperience] = useState({}); |
| 8 | const [currentStep, setCurrentStep] = useState({}); |
| 9 | const [loading, setLoading] = useState(true); |
| 10 | |
| 11 | const baseURL = '/echo/task/tutorial'; |
| 12 | const userId = 1; |
| 13 | |
| 14 | const fetchTasks = async () => { |
| 15 | try { |
| 16 | const response = await axios.get(`${baseURL}/getAllTasks`, { |
| 17 | params: { user_id: userId }, |
| 18 | }); |
| 19 | setTasks(response.data.tasks || []); |
| 20 | } catch (error) { |
| 21 | console.error('获取任务失败:', error); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | const fetchExperience = async () => { |
| 26 | try { |
| 27 | const response = await axios.get(`${baseURL}/getAllTasks`, { |
| 28 | params: { user_id: userId }, |
| 29 | }); |
| 30 | setExperience(response.data || {}); |
| 31 | } catch (error) { |
| 32 | console.error('获取经验失败:', error); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | const fetchCurrentStep = async () => { |
| 37 | try { |
| 38 | const response = await axios.get(`${baseURL}/getNewStep`, { |
| 39 | params: { user_id: userId }, |
| 40 | }); |
| 41 | setCurrentStep(response.data || {}); |
| 42 | } catch (error) { |
| 43 | console.error('获取引导步骤失败:', error); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | const updateStatus = async (taskId) => { |
| 48 | try { |
| 49 | await axios.post(`${baseURL}/updateStatus`, { |
| 50 | user_id: userId, |
| 51 | task_id: taskId, |
| 52 | }); |
| 53 | await fetchTasks(); |
| 54 | await fetchExperience(); |
| 55 | } catch (error) { |
| 56 | console.error('更新状态失败:', error); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | const updateProgress = async (taskId, progress) => { |
| 61 | try { |
| 62 | await axios.post(`${baseURL}/updateProgress`, { |
| 63 | user_id: userId, |
| 64 | task_id: taskId, |
| 65 | progress, |
| 66 | }); |
| 67 | await fetchTasks(); |
| 68 | } catch (error) { |
| 69 | console.error('更新进度失败:', error); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | const claimReward = async (taskId) => { |
| 74 | try { |
| 75 | await axios.post(`${baseURL}/rewardClaim`, { |
| 76 | user_id: userId, |
| 77 | task_id: taskId, |
| 78 | }); |
| 79 | await fetchTasks(); |
| 80 | await fetchExperience(); |
| 81 | } catch (error) { |
| 82 | console.error('领取奖励失败:', error); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | const checkRewardStatus = async (taskId) => { |
| 87 | try { |
| 88 | const response = await axios.post(`${baseURL}/rewardReview`, { |
| 89 | user_id: userId, |
| 90 | task_id: taskId, |
| 91 | }); |
| 92 | alert(response.data.message || '已完成'); |
| 93 | } catch (error) { |
| 94 | console.error('查看失败:', error); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | useEffect(() => { |
| 99 | const init = async () => { |
| 100 | await fetchTasks(); |
| 101 | await fetchExperience(); |
| 102 | await fetchCurrentStep(); |
| 103 | setLoading(false); |
| 104 | }; |
| 105 | init(); |
| 106 | }, []); |
| 107 | |
| 108 | if (loading) return <p className="loading">加载中...</p>; |
| 109 | |
| 110 | return ( |
| 111 | <div className="common-card right-content"> |
| 112 | <div className="profile-header"> |
| 113 | <h1>🎯 成长任务</h1> |
| 114 | </div> |
| 115 | |
| 116 | <div className="profile-details"> |
| 117 | <p><strong>当前经验:</strong>{experience.current_experience}</p> |
| 118 | <p><strong>等级:</strong>{experience.level}</p> |
| 119 | <p><strong>奖励经验:</strong>{experience.reward?.experience || 0}</p> |
| 120 | <p><strong>奖励积分:</strong>{experience.reward?.points || 0}</p> |
| 121 | </div> |
| 122 | |
| 123 | <div className="profile-details"> |
| 124 | <p><strong>🧭 当前引导步骤:</strong>{currentStep.current_step}</p> |
| 125 | <p>{currentStep.step_description}</p> |
| 126 | </div> |
| 127 | |
| 128 | <div className="profile-details"> |
| 129 | <h2>任务列表</h2> |
| 130 | <ul> |
| 131 | {tasks.map(task => ( |
| 132 | <li key={task.task_id} style={{ marginBottom: '20px', borderBottom: '1px solid #eee', paddingBottom: '10px' }}> |
| 133 | <p><strong>任务:</strong>{task.title}</p> |
| 134 | <p><strong>描述:</strong>{task.description}</p> |
| 135 | <p><strong>进度:</strong>{task.progress}%</p> |
| 136 | <p><strong>状态:</strong>{task.status}</p> |
| 137 | <p><strong>奖励:</strong>经验 {task.reward.experience},积分 {task.reward.points}</p> |
| 138 | |
| 139 | <div className="task-btn-group"> |
| 140 | <button className="task-btn" onClick={() => updateStatus(task.task_id)}>更新状态</button> |
| 141 | <button className="task-btn" onClick={() => updateProgress(task.task_id, 100)}>提交进度</button> |
| 142 | <button className="task-btn" onClick={() => claimReward(task.task_id)}>领取奖励</button> |
| 143 | <button className="task-btn" onClick={() => checkRewardStatus(task.task_id)}>查看奖励状态</button> |
| 144 | </div> |
| 145 | </li> |
| 146 | ))} |
| 147 | </ul> |
| 148 | </div> |
| 149 | </div> |
| 150 | ); |
| 151 | }; |
| 152 | |
| 153 | export default NewbieTasks; |
| 154 | |