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.
295 lines
7.1 KiB
295 lines
7.1 KiB
// pages/test3/test3.js
|
|
const db = wx.cloud.database();
|
|
Page({
|
|
data: {
|
|
students: [], // 存储所有学生信息
|
|
currentStudent: null, // 当前选中的学生信息
|
|
score: '', // 用户输入的分数
|
|
effects: [ // 特殊效果数组
|
|
{ name: '+0分', value: 0 },
|
|
{ name: '+1分', value: 1 },
|
|
{ name: '+2分', value: 2 },
|
|
{ name: '+3分', value: 3 },
|
|
{ name: '+4分', value: 4 },
|
|
{ name: '+5分', value: 5 },
|
|
]
|
|
},
|
|
|
|
onLoad: function() {
|
|
this.loadStudents();
|
|
},
|
|
|
|
// 加载学生数据
|
|
loadStudents: 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
|
|
});
|
|
},
|
|
fail: err => {
|
|
console.error('学生数据加载失败:', err);
|
|
}
|
|
});
|
|
},
|
|
|
|
// 按积分加权随机点名
|
|
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
|
|
});
|
|
}
|
|
},
|
|
|
|
// 减少1分
|
|
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 // 设置抽取的效果以便显示
|
|
});
|
|
|
|
// 更新选中学生的分数
|
|
this.updateStudentScore(effect.value);
|
|
wx.showToast({
|
|
title: `应用效果:${effect.name}`,
|
|
icon: 'success',
|
|
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'
|
|
});
|
|
}
|
|
}); |