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.

160 lines
4.6 KiB

Page({
onCardTap: function() {
wx.switchTab({
url: '/pages/RollClass/RollClass',
})
},
data: {
cards: [], // 班级数据数组,存储当前已有的班级
modalVisible1: false, // 控制浮窗1的显隐状态
modalVisible2: false, // 控制浮窗2的显隐状态
modalVisible3: false, // 控制浮窗3的显隐状态
modalVisible4: false, // 控制浮窗4的显隐状态
className: '', // 存储输入的班级名称(浮窗1)
fileName: '' // 存储选择的文件名(浮窗2)
},
// 点击加号按钮后触发,显示浮窗
onAddCard() {
this.setData({
modalVisible1: true // 设置浮窗可见
});
},
// 处理输入框的输入事件,更新班级名称
onInput(e) {
this.setData({
className: e.detail.value // 更新输入的班级名称为用户输入
});
},
// 浮窗1确认按钮的点击事件处理
onConfirm1() {
this.setData({
modalVisible1: false, // 隐藏浮窗
modalVisible2: true //显示浮窗2
});
},
// 浮窗1取消按钮的点击事件处理
onCancel1() {
this.setData({
modalVisible1: false, // 隐藏浮窗
className: null // 重置输入框内容
});
},
// 浮窗2选择文件的处理函数
onSelectFile() {
wx.chooseMessageFile({
count: 1,
type: 'file',
success: (res) => {
const filePath = res.tempFiles[0].path; // 获取文件的临时路径
this.setData({
fileName: filePath
});
console.log("选中的文件路径:", filePath);
},
fail: (err) => {
console.error("选择文件失败:", err);
}
});
},
// 浮窗2确认按钮的点击事件处理
onConfirm2() {
// 处理确认逻辑
console.log('确认导入文件:', this.data.fileName);
this.setData({
modalVisible2: false, // 隐藏浮窗
modalVisible3: true // 显示浮窗3
});
},
// 浮窗3确认按钮的点击事件处理
onConfirm3() {
const { className, cards } = this.data;
// 检查班级名是否已存在
const classExists = cards.some(card => card.name === className);
if (classExists) {
// 如果班级名已经存在,弹出错误提示或进行其他处理
this.setData({
modalVisible3: false,
modalVisible4: true,
className: null,
fileName: null
});
return; // 结束函数执行
}
// 确保文件路径和班级名称有效
if (!this.data.fileName || !className) {
console.error('文件路径或班级名称无效');
return;
}
// 上传Excel文件和班级名称给后端
this.uploadExcelFileAndClassName(className, this.data.fileName);
// 创建新班级对象
const newCard = {
name: className || '新课程', // 使用用户输入的班级名,如果为空则默认
studentCount: 30,
creationDate: new Date().toLocaleDateString() // 当前日期
};
// 更新数据状态
this.setData({
cards: [...cards, newCard], // 将新卡片加入数组
modalVisible3: false, // 隐藏浮窗
className: null,
fileName: null
});
},
// 上传Excel文件和班级名称给后端的方法
uploadExcelFileAndClassName(className, fileName) {
if (fileName && className) {
wx.uploadFile({
url: 'http://10.133.8.121:8000/get/information/', // 确保URL正确
filePath: fileName, // 使用完整的临时文件路径
name: 'file', // 后端用来解析文件的字段名
formData: {
'class_name': className // 同时发送班级名称
},
success: (res) => {
const data = res.data;
console.log('文件上传成功,响应:', data);
if (data.message === 1) {
// 文件处理成功,班级创建成功
} else if (data.message === 0) {
// 文件处理失败或班级已存在
console.error('上传失败或班级已存在:', data.error);
}
},
fail: (err) => {
console.error('文件上传失败:', err);
}
});
} else {
console.error('文件路径或班级名称无效');
}
},
// 浮窗3取消按钮的点击事件处理
onCancel3(){
this.setData({
modalVisible3:false,
className:null,
fileName: null
});
},
onCancel4(){
this.setData({
modalVisible4:false,
className:null,
fileName: null
});
}
});