From 46265fdd9f17ae0033b7d4b161d971ed1ff36905 Mon Sep 17 00:00:00 2001 From: pg78ac9nr <2401677463@qq.com> Date: Thu, 10 Oct 2024 12:07:44 +0800 Subject: [PATCH] ADD file via upload --- 1.0 pages rollcall.js | 121 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 1.0 pages rollcall.js diff --git a/1.0 pages rollcall.js b/1.0 pages rollcall.js new file mode 100644 index 0000000..4378cec --- /dev/null +++ b/1.0 pages rollcall.js @@ -0,0 +1,121 @@ +const db = wx.cloud.database(); +const app = getApp(); + +Page({ + data: { + students: [], + selectedStudent: null, + score: '' + }, + + onLoad: function() { + this.getStudents(); + }, + + // 获取学生列表 + getStudents: function() { + db.collection('students').get().then(res => { + console.log('Students retrieved:', res.data); + if (res.data && res.data.length > 0) { + this.setData({ + students: res.data + }); + // 将学生列表存储到全局数据中 + app.globalData.students = res.data; + } else { + console.warn('No students found in the database.'); + wx.showToast({ + title: '没有学生信息', + icon: 'none' + }); + } + }).catch(err => { + console.error('Failed to get students:', err); + wx.showToast({ + title: '获取学生信息失败', + icon: 'none' + }); + }); + }, + + // 随机点名 + randomRollCall: function() { + const students = this.data.students; + if (students.length === 0) { + wx.showToast({ + title: '没有学生信息', + icon: 'none' + }); + return; + } + + const totalPoints = students.reduce((sum, student) => sum + Math.max(0, 10 - student.points), 0); + let randomPoint = Math.random() * totalPoints; + let cumulativePoint = 0; + + let selectedStudent = null; + for (const student of students) { + cumulativePoint += Math.max(0, 10 - student.points); + if (randomPoint <= cumulativePoint) { + selectedStudent = student; + break; + } + } + + if (selectedStudent) { + this.setData({ + selectedStudent: selectedStudent + }); + wx.showToast({ + title: `点名成功: ${selectedStudent.name} (${selectedStudent.id})`, + icon: 'success' + }); + } else { + wx.showToast({ + title: '点名失败', + icon: 'none' + }); + } + }, + + // 输入评分 + inputScore: function(e) { + this.setData({ + score: e.detail.value + }); + }, + + // 更新积分 + updatePoints: function() { + const student = this.data.selectedStudent; + const score = parseFloat(this.data.score); + + if (isNaN(score)) { + wx.showToast({ + title: '请输入有效的评分', + icon: 'none' + }); + return; + } + + student.points += score; + + db.collection('students').doc(student._id).update({ + data: { + points: student.points + } + }).then(() => { + this.getStudents(); + wx.showToast({ + title: '评分成功', + icon: 'success' + }); + }).catch(err => { + console.error('Failed to update student points:', err); + wx.showToast({ + title: '评分失败', + icon: 'none' + }); + }); + } +}); \ No newline at end of file