Page({ data: { studentId: '', studentName: '', score: 0.0, }, onLoad(options) { this.setData({ studentId: options.studentId, }); this.fetchStudentInfo(); }, // 获取学生姓名和积分 fetchStudentInfo() { console.log("Student ID:", this.data.studentId); // 检查 studentId 的值 wx.request({ url: `http://127.0.0.1:8000/api/get_student_info/${this.data.studentId}/`, // 拼接 student_id method: 'GET', success: (res) => { if (res.statusCode === 200 && res.data) { this.setData({ studentName: res.data.name, score: res.data.score, }); } else if (res.data.error) { wx.showToast({ title: res.data.error, icon: 'none', }); } }, fail: (error) => { console.error('获取学生信息失败:', error); wx.showToast({ title: '获取学生信息失败', icon: 'none', }); }, }); }, // 加分 addScore() { this.updateScore(1); }, // 减分 reduceScore() { this.updateScore(-1); }, // 更新分数到数据库 updateScore(change) { const currentScore = parseFloat(this.data.score); // 转换为数字 const newScore = (currentScore + change).toFixed(1); // 保留一位小数 wx.request({ url: 'http://127.0.0.1:8000/api/update_student_score/', method: 'POST', data: { student_id: this.data.studentId, score: parseFloat(newScore), // 确保发送为浮点数 }, success: (res) => { if (res.data.success) { this.setData({ score: parseFloat(newScore) }); // 更新状态 wx.showToast({ title: change > 0 ? '加分成功' : '扣分成功', icon: 'success', }); } else { wx.showToast({ title: '更新失败', icon: 'none', }); } }, fail: (error) => { console.error('更新分数失败:', error); }, }); }, // 跳转到排行榜 goTorank() { console.log('点击了排行榜按钮'); // 添加调试信息 wx.navigateTo({ url: '/pages/teacher/Trank/rank-Mode1/rank-Mode1', }); }, goBack() { wx.navigateTo({ url: '/pages/teacher/teacher', }); }, });