Update AddClass.js

main
p6fxi93qh 4 months ago
parent 7bf45f2a06
commit ca7534f6c6

@ -45,78 +45,102 @@ Page({
}); });
}, },
// 浮窗2选择文件的处理函数 // 浮窗2选择文件的处理函数
onSelectFile() { onSelectFile() {
wx.chooseMessageFile({ wx.chooseMessageFile({
count: 1, count: 1,
type: 'file', type: 'file',
success: (res) => { success: (res) => {
const filePath = res.tempFiles[0].path; // 依然使用相对路径 const filePath = res.tempFiles[0].path; // 获取文件的临时路径
this.setData({ this.setData({
fileName: filePath.split('/').pop() // 取文件名部分 fileName: filePath
}); });
console.log("选中的文件路径:", filePath); // 可以在这里使用相对路径进行后续处理 console.log("选中的文件路径:", filePath);
}, },
fail: (err) => { fail: (err) => {
console.error("选择文件失败:", err); console.error("选择文件失败:", err);
} }
}); });
}, },
// 浮窗2确认按钮的点击事件处理 // 浮窗2确认按钮的点击事件处理
onConfirm2() { onConfirm2() {
// 处理确认逻辑 // 处理确认逻辑
console.log('确认导入文件:', this.data.fileName); console.log('确认导入文件:', this.data.fileName);
this.setData({ this.setData({
modalVisible2: false, // 隐藏浮窗 modalVisible2: false, // 隐藏浮窗
fileName:null, modalVisible3: true // 显示浮窗3
modalVisible3: true //显示浮窗3 });
}); },
},
// 浮窗3确认按钮的点击事件处理
// 浮窗2取消按钮的点击事件处理 onConfirm3() {
onCancel2() { const { className, cards } = this.data;
this.setData({ // 检查班级名是否已存在
modalVisible2: false, // 隐藏浮窗 const classExists = cards.some(card => card.name === className);
className:null, if (classExists) {
fileName: null // 清空选择的文件
});
},
// 浮窗3确认按钮的点击事件处理
onConfirm3() {
const { className, cards } = this.data;
// 检查班级名是否已存在
const classExists = cards.some(card => card.name === className);
if (classExists) {
// 如果班级名已经存在,弹出错误提示或进行其他处理 // 如果班级名已经存在,弹出错误提示或进行其他处理
this.setData({ this.setData({
modalVisible3:false, modalVisible3: false,
modalVisible4:true, modalVisible4: true,
className:null, className: null,
fileName: null fileName: null
}); });
return; // 结束函数执行 return; // 结束函数执行
} }
// 创建新班级对象 // 确保文件路径和班级名称有效
const newCard = { if (!this.data.fileName || !className) {
console.error('文件路径或班级名称无效');
return;
}
// 上传Excel文件和班级名称给后端
this.uploadExcelFileAndClassName(className, this.data.fileName);
// 创建新班级对象
const newCard = {
name: className || '新课程', // 使用用户输入的班级名,如果为空则默认 name: className || '新课程', // 使用用户输入的班级名,如果为空则默认
studentCount: 30, studentCount: 30,
creationDate: new Date().toLocaleDateString() // 当前日期 creationDate: new Date().toLocaleDateString() // 当前日期
}; };
// 更新数据状态
// 更新数据状态 this.setData({
this.setData({
cards: [...cards, newCard], // 将新卡片加入数组 cards: [...cards, newCard], // 将新卡片加入数组
modalVisible3: false, // 隐藏浮窗 modalVisible3: false, // 隐藏浮窗
className:null, className: null,
fileName: 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取消按钮的点击事件处理 // 浮窗3取消按钮的点击事件处理
onCancel3(){ onCancel3(){
this.setData({ this.setData({

Loading…
Cancel
Save