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.

235 lines
6.2 KiB

1 month ago
Page({
data: {
fileList: [],
showModal: false,
exemptionModalVisible: false, // 控制豁免卡信息弹窗
selectedStudent: null,
socket: null,
exemptionCardInfo: {},// 用于存储豁免卡信息
questionScore: 0, // 新增变量,用于存储输入的积分
},
// 返回页面
goBack() {
wx.navigateTo({
url: '/pages/index/index',
});
},
// 选择文件(点击上传学生按钮)
chooseFile: function() {
const that = this;
wx.chooseMessageFile({
count: 1, // 选择文件的数量
type: 'file', // 文件类型
extension: ['xls', 'xlsx'], // 允许选择 Excel 文件
success(res) {
const tempFilePaths = res.tempFiles;
if (tempFilePaths.length > 0) {
// 上传文件
that.uploadFile(tempFilePaths[0].path);
} else {
wx.showToast({
title: '未选择文件',
icon: 'none'
});
}
}
});
},
// 上传文件
uploadFile: function(filePath) {
wx.uploadFile({
url: 'http://127.0.0.1:8000/api/upload-students/', // Django 后端 API
filePath: filePath,
name: 'file', // 后端接收文件的参数名
success(res) {
const data = JSON.parse(res.data);
if (data.message) {
wx.showToast({
title: data.message,
icon: 'success'
});
} else if (data.error) {
wx.showToast({
title: data.error,
icon: 'none'
});
}
},
fail(err) {
wx.showToast({
title: '上传失败',
icon: 'none'
});
}
});
},
onLoad: function() {
this.connectWebSocket();
},
// 建立websock连接
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({
selectedStudent: {
name: data.name,
student_id: data.student_id,
},
showModal: true,
});
}else if (data.action === 'exemption_card_used') {
this.setData({
exemptionCardInfo: {
student_id: data.student_id,
student_name: data.student_name,
new_score: data.new_score,
},
exemptionModalVisible: true, // 显示豁免卡信息弹窗
});
}
});
this.setData({ socket });
},
// 发起点名
pickStudent() {
this.data.socket.send({
data: JSON.stringify({ action: 'pick_student' }),
});
},
onStudentPicked(student) {
this.setData({
selectedStudent: student,
showModal: true,
});
// 发送点名结果到学生组
this.data.socket.send({
data: JSON.stringify({
action: 'student_picked',
name: student.name,
student_id: student.student_id,
}),
});
},
//关闭点名结果弹窗
onModalClose() {
this.setData({
showModal: false,
});
},
// 关闭豁免卡信息弹窗
onExemptionModalClose() {
this.setData({
exemptionModalVisible: false,
});
},
// 进入点到模式
goToRollCall() {
if (!this.data.selectedStudent) {
wx.showToast({
title: '请先选择一个学生',
icon: 'none',
});
return;
}
const { student_id: studentId } = this.data.selectedStudent; // 从 selectedStudent 中解构出 studentId
wx.navigateTo({
url: `/pages/teacher/Mode-arrive/Mode-arrive?studentId=${studentId}`,
});
},
// 进入提问模式
goToQuestionMode() {
if (!this.data.selectedStudent) {
wx.showToast({
title: '请先选择一个学生',
icon: 'none',
});
return;
}
const { student_id: studentId } = this.data.selectedStudent; // 从 selectedStudent 中解构出 studentId
console.log('Navigating to question mode with studentId:', studentId);
wx.navigateTo({
url: `/pages/teacher/Mode-quiz/Mode-quiz?studentId=${studentId}`,
fail: (err) => {
console.log('跳转失败:', err);
},
});
},
// 跳转到排行榜
goTorank() {
console.log('点击了排行榜按钮'); // 添加调试信息
wx.navigateTo({
url: 'pages/teacher/Trank/rank-Mode1/rank-Mode1',
});
},
// 导出数据
exportScores() {
wx.request({
url: 'http://127.0.0.1:8000/api/export_students_scores', // 后端 API 地址
method: 'POST',
responseType: 'arraybuffer', // 确保接收的是文件数据
success(res) {
if (res.statusCode === 200) {
// 将接收到的二进制数据转换为Excel文件并下载
const filePath = `${wx.env.USER_DATA_PATH}/students_scores.xlsx`;
const fs = wx.getFileSystemManager();
fs.writeFile({
filePath,
data: res.data,
encoding: 'binary',
success() {
wx.openDocument({
filePath,
fileType: 'xlsx',
success() {
wx.showToast({
title: 'Excel已下载并打开',
icon: 'success',
});
},
fail() {
wx.showToast({
title: '文件打开失败',
icon: 'none',
});
}
});
},
fail() {
wx.showToast({
title: '文件保存失败',
icon: 'none',
});
}
});
} else {
wx.showToast({
title: res.data.error || '导出失败',
icon: 'none',
});
}
},
fail() {
wx.showToast({
title: '请求失败,请检查网络',
icon: 'none',
});
}
});
},
});