You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
2.8 KiB
121 lines
2.8 KiB
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'
|
|
});
|
|
});
|
|
}
|
|
}); |