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.

350 lines
8.6 KiB

// pages/rollCall/rollCall.js
const db = wx.cloud.database();
Page({
data: {
students: [],
currentStudent: null,
score: '',
effects: [
{ name: '+0分', value: 0, type: 'add' },
{ name: '+1分', value: 1, type: 'add' },
{ name: '+2分', value: 2, type: 'add' },
{ name: '+3分', value: 3, type: 'add' },
{ name: '+4分', value: 4, type: 'add' },
{ name: '+5分', value: 5, type: 'add' },
{ name: '-1分', value: -1, type: 'add' },
{ name: '-2分', value: -2, type: 'add' },
{ name: '积分乘2', value: 2, type: 'multiply' },
{ name: '积分除2', value: 0.5, type: 'divide' }
],
page: 1,
pageSize: 10,
hasMore: true
},
loadAllStudents: function() {
const that = this;
const loadNextPage = function() {
const studentsCollection = db.collection('users');
studentsCollection.skip((that.data.page - 1) * that.data.pageSize).limit(that.data.pageSize).get({
success: res => {
if (res.data.length === 0) {
that.setData({
hasMore: false
});
return;
}
let newStudents = that.data.students.concat(res.data);
that.setData({
students: newStudents,
page: that.data.page + 1
});
// 如果还有更多数据,继续加载下一页
if (that.data.hasMore) {
loadNextPage();
}
},
fail: err => {
console.error('学生数据加载失败:', err);
}
});
};
loadNextPage();
},
onLoad: function() {
this.loadAllStudents();
},
// 按积分加权随机点名
weightedRandomRollCall: function() {
const students = this.data.students;
if (students.length === 0) {
wx.showToast({
title: '没有可点名的学生',
icon: 'none',
duration: 2000
});
return;
}
const weights = students.map(student => 1 / (student.score + 1));
const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
const randomWeight = Math.random() * totalWeight;
let cumulativeWeight = 0;
let selectedStudent = null;
for (let i = 0; i < weights.length; i++) {
cumulativeWeight += weights[i];
if (cumulativeWeight >= randomWeight) {
selectedStudent = students[i];
break;
}
}
if (selectedStudent) {
this.setData({
currentStudent: selectedStudent
});
console.log(this.data.currentStudent)
wx.showToast({
title: `点到:${selectedStudent.name}`,
icon: 'none',
duration: 2000
});
} else {
wx.showToast({
title: '未抽到学生',
icon: 'none',
duration: 2000
});
}
},
// 用户输入分数
inputScore: function(e) {
this.setData({
score: e.detail.value
});
},
// 更新分数
updatescore: function() {
let db = wx.cloud.database()
let userCollection = db.collection('users')
const xuehao = this.data.currentStudent.num;
const inputScore = this.data.score;
if (!xuehao || inputScore === '') {
wx.showToast({
title: '信息不完整',
icon: 'none',
duration: 2000
});
return;
}
const newScore = parseFloat(this.data.currentStudent.score) + parseFloat(inputScore);
userCollection.where({
num: xuehao
}).update({
data: {
score: newScore
}
}).then(res => {
console.log('更新成功');
wx.showToast({
title: '分数更新成功',
icon: 'success',
duration: 2000
});
}).catch(err => {
console.log('更新失败', err);
})
},
addHalfScore: function() {
if (this.data.currentStudent) {
this.updateStudentScore(0.5);
} else {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
}
},
addoneScore: function() {
if (this.data.currentStudent) {
this.updateStudentScore(1);
} else {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
}
},
addoneandhalfScore: function() {
if (this.data.currentStudent) {
this.updateStudentScore(1.5);
} else {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
}
},
addtwoScore: function() {
if (this.data.currentStudent) {
this.updateStudentScore(2);
} else {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
}
},
addtwoandhalfScore: function() {
if (this.data.currentStudent) {
this.updateStudentScore(2.5);
} else {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
}
},
addthreeScore: function() {
if (this.data.currentStudent) {
this.updateStudentScore(3);
} else {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
}
},
subtractOneScore: function() {
if (this.data.currentStudent) {
this.updateStudentScore(-1);
} else {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
}
},
// 更新选中学生的分数
updateStudentScore: function(amount) {
const newScore = parseFloat(this.data.currentStudent.score) + amount;
const studentId = this.data.currentStudent._id;
const userCollection = db.collection('users');
userCollection.doc(studentId).update({
data: {
score: newScore
}
}).then(res => {
console.log('分数更新成功');
wx.showToast({
title: '分数更新成功',
icon: 'success',
duration: 2000
});
this.setData({
currentStudent: {...this.data.currentStudent, score: newScore.toString()}
});
}).catch(err => {
console.error('分数更新失败', err);
wx.showToast({
title: '分数更新失败',
icon: 'none',
duration: 2000
});
});
},
applyEffect: function() {
if (!this.data.currentStudent) {
wx.showToast({
title: '请先随机点名一个学生',
icon: 'none',
duration: 2000
});
return;
}
const effect = this.data.effects[Math.floor(Math.random() * this.data.effects.length)];
this.setData({
effect: effect
});
let newScore;
switch (effect.type) {
case 'add':
newScore = parseFloat(this.data.currentStudent.score) + effect.value;
break;
case 'multiply':
newScore = parseFloat(this.data.currentStudent.score) * effect.value;
break;
case 'divide':
if (this.data.currentStudent.score === 0) {
wx.showToast({
title: '分数不能为0',
icon: 'none',
duration: 2000
});
return;
}
newScore = parseFloat(this.data.currentStudent.score) * effect.value;
break;
default:
newScore = this.data.currentStudent.score;
}
const studentId = this.data.currentStudent._id;
const userCollection = db.collection('users');
userCollection.doc(studentId).update({
data: {
score: newScore
}
}).then(res => {
console.log('分数更新成功');
wx.showToast({
title: `应用效果:${effect.name}`,
icon: 'success',
duration: 2000
});
this.setData({
currentStudent: {...this.data.currentStudent, score: newScore.toString()}
});
}).catch(err => {
console.error('分数更新失败', err);
wx.showToast({
title: '分数更新失败',
icon: 'none',
duration: 2000
});
});
},
//刷新
refreshData: function() {
this.loadStudents1();
},
// 加载学生数据
loadStudents1: function() {
const studentsCollection = db.collection('users');
studentsCollection.get({
success: res => {
if (res.data.length === 0) {
wx.showToast({
title: '没有学生数据',
icon: 'none',
duration: 2000
});
return;
}
this.setData({
students: res.data,
currentStudent: null,
effect: null
});
},
fail: err => {
console.error('学生数据加载失败:', err);
}
});
},
goBack: function() {
wx.navigateTo({
url: '/pages/teacherHomePage/teacherHomePage'
});
}
});