|
|
Page({
|
|
|
data: {
|
|
|
showModal: false,
|
|
|
mystudentName: '',
|
|
|
mystudentId: '',
|
|
|
myscore: 0,
|
|
|
studentName: '',
|
|
|
studentId: '',
|
|
|
socket: null, // 保存WebSocket连接
|
|
|
},
|
|
|
goBack() {
|
|
|
wx.navigateTo({
|
|
|
url: '/pages/student/student',
|
|
|
});
|
|
|
},
|
|
|
|
|
|
onLoad(options) {
|
|
|
// 获取传递的数据
|
|
|
this.setData({
|
|
|
mystudentId: options.studentId,
|
|
|
mystudentName: options.studentName,
|
|
|
myscore: options.score,
|
|
|
});
|
|
|
this.connectWebSocket();
|
|
|
this.fetchStudentInfo();
|
|
|
},
|
|
|
|
|
|
// 连接 WebSocket
|
|
|
connectWebSocket() {
|
|
|
const socket = wx.connectSocket({
|
|
|
url: 'ws://127.0.0.1:8000/ws/classroom/',
|
|
|
});
|
|
|
|
|
|
socket.onMessage((message) => {
|
|
|
const data = JSON.parse(message.data);
|
|
|
if (data.action === 'student_picked') {
|
|
|
this.setData({
|
|
|
showModal: true,
|
|
|
studentName: data.name,
|
|
|
studentId: data.student_id,
|
|
|
});
|
|
|
// 弹窗显示被点中的学生
|
|
|
wx.showModal({
|
|
|
title: '点名结果',
|
|
|
content: `被点中的同学:姓名: ${this.data.studentName} 学号: ${this.data.studentId}`,
|
|
|
showCancel: false,
|
|
|
success: (res) => {
|
|
|
if (res.confirm) {
|
|
|
this.setData({ showModal: false });
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
this.setData({ socket });
|
|
|
},
|
|
|
|
|
|
// 豁免卡机制
|
|
|
useExemptionCard() {
|
|
|
wx.showModal({
|
|
|
title: '使用豁免卡',
|
|
|
content: `确认使用豁免卡吗?
|
|
|
购买点名豁免卡,可以实现点名豁免一次,一张需要10积分。(只有提问时可使用)`,
|
|
|
success: (res) => {
|
|
|
if (res.confirm) {
|
|
|
// 更新积分
|
|
|
const newScore = this.data.myscore - 10;
|
|
|
this.setData({ myscore: newScore });
|
|
|
|
|
|
// 发送豁免卡信息给教师端
|
|
|
this.sendExemptionCardInfo();
|
|
|
} else {
|
|
|
// 用户选择不使用豁免卡
|
|
|
wx.showToast({ title: '未使用豁免卡', icon: 'none' });
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
sendExemptionCardInfo() {
|
|
|
const { mystudentId, mystudentName, myscore, socket } = this.data;
|
|
|
|
|
|
// 通过 WebSocket 向教师端发送豁免卡信息
|
|
|
const message = {
|
|
|
action: 'use_exemption_card',
|
|
|
studentId: mystudentId,
|
|
|
studentName: mystudentName,
|
|
|
newScore: myscore, // 更新后的积分
|
|
|
};
|
|
|
|
|
|
if (socket) {
|
|
|
socket.send({
|
|
|
data: JSON.stringify(message),
|
|
|
success: () => {
|
|
|
wx.showToast({ title: '豁免卡信息已发送', icon: 'success' });
|
|
|
},
|
|
|
fail: () => {
|
|
|
wx.showToast({ title: '发送信息失败', icon: 'none' });
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
wx.showToast({ title: 'WebSocket连接未建立', icon: 'none' });
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 下拉刷新
|
|
|
onPullDownRefresh() {
|
|
|
this.fetchStudentInfo(); // 获取最新的学生信息
|
|
|
},
|
|
|
|
|
|
// 从服务器获取学生最新信息
|
|
|
fetchStudentInfo() {
|
|
|
const { mystudentId } = this.data;
|
|
|
|
|
|
wx.request({
|
|
|
url: `http://127.0.0.1:8000/api/get_student_info/${mystudentId}/`, // 替换为你的 API 地址
|
|
|
method: 'GET',
|
|
|
success: (res) => {
|
|
|
if (res.statusCode === 200) {
|
|
|
const { name, score } = res.data;
|
|
|
this.setData({
|
|
|
mystudentName: name,
|
|
|
myscore: score, // 更新当前积分
|
|
|
});
|
|
|
wx.showToast({ title: '信息更新成功', icon: 'success' });
|
|
|
} else {
|
|
|
wx.showToast({ title: '获取信息失败', icon: 'none' });
|
|
|
}
|
|
|
wx.stopPullDownRefresh(); // 停止下拉刷新
|
|
|
},
|
|
|
fail: () => {
|
|
|
wx.showToast({ title: '请求失败,请检查网络', icon: 'none' });
|
|
|
wx.stopPullDownRefresh(); // 停止下拉刷新
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
}); |