|
|
|
|
// pages/addStudent/addStudent.js
|
|
|
|
|
Page({
|
|
|
|
|
data: {
|
|
|
|
|
className:"K班",
|
|
|
|
|
input: "",
|
|
|
|
|
students:[
|
|
|
|
|
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onLoad: function(options) {
|
|
|
|
|
console.log(options);
|
|
|
|
|
// options 参数包含了从上一个页面传递过来的数据
|
|
|
|
|
const receivedClassName = options.className; // 获取传递过来的 className,默认为空字符串
|
|
|
|
|
|
|
|
|
|
// 更新页面的数据
|
|
|
|
|
this.setData({
|
|
|
|
|
className: receivedClassName // 将接收到的 className 赋值给页面的 data 属性
|
|
|
|
|
});
|
|
|
|
|
console.log(this.data.className);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//获取输入框内容
|
|
|
|
|
onInput: function(e) {
|
|
|
|
|
console.log(e.detail.value)
|
|
|
|
|
this.setData({
|
|
|
|
|
input: e.detail.value//保存输入
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Excel文件导入
|
|
|
|
|
importExcel: function() {
|
|
|
|
|
const that = this;
|
|
|
|
|
wx.chooseMessageFile({
|
|
|
|
|
count: 1, // 默认为1,设置选择文件的数量
|
|
|
|
|
type: 'file',
|
|
|
|
|
extension: ['xlsx'], // 指定文件的后缀名
|
|
|
|
|
success(res) {
|
|
|
|
|
const tempFiles = res.tempFiles; // 临时文件路径
|
|
|
|
|
const filePath = tempFiles[0].path; // 获取文件的路径
|
|
|
|
|
console.log(tempFiles);
|
|
|
|
|
that.readExcel(filePath); // 读取文件内容
|
|
|
|
|
},
|
|
|
|
|
fail(err) {
|
|
|
|
|
console.error('选择文件失败:', err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 读取Excel文件内容
|
|
|
|
|
readExcel(filePath) {
|
|
|
|
|
// 可以使用微信小程序的API或将文件上传到服务器解析
|
|
|
|
|
const that = this;
|
|
|
|
|
wx.uploadFile({
|
|
|
|
|
url: '暂无', // 你的服务器上传地址
|
|
|
|
|
filePath: filePath,
|
|
|
|
|
name: 'file',
|
|
|
|
|
success: uploadRes => {
|
|
|
|
|
// 假设服务器返回解析后的 JSON 数据
|
|
|
|
|
const data = JSON.parse(uploadRes.data);
|
|
|
|
|
that.addStudents(data.students);
|
|
|
|
|
},
|
|
|
|
|
fail: uploadErr => {
|
|
|
|
|
console.error('文件上传失败:', uploadErr);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 将解析后的学生数据添加到现有数组中
|
|
|
|
|
addStudents(newStudents) {
|
|
|
|
|
const currentStudents = this.data.students;
|
|
|
|
|
const allStudents = currentStudents.concat(newStudents); // 合并数组
|
|
|
|
|
this.setData({
|
|
|
|
|
students: allStudents
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//点击输入框提示可以输入
|
|
|
|
|
focus: function(){
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '可以开始输入了',
|
|
|
|
|
icon: 'none',
|
|
|
|
|
duration: 2000
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
cantNull: function(){
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '输入框不可为空',
|
|
|
|
|
icon: 'none',
|
|
|
|
|
duration: 2000
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
addProcess: function(){
|
|
|
|
|
if(this.data.input != ""){
|
|
|
|
|
//分割字符串为数组
|
|
|
|
|
let firstArray = this.data.input.split("|");
|
|
|
|
|
console.log(firstArray);
|
|
|
|
|
let secondArray = firstArray.map(str => str.split(" "));
|
|
|
|
|
console.log(secondArray)
|
|
|
|
|
//遍历secondArray
|
|
|
|
|
secondArray.forEach(subArray => {
|
|
|
|
|
// 结构法从子数组中提取id和name
|
|
|
|
|
//等同于 const id = subArray[0]; const name = subArray[1];
|
|
|
|
|
const { [0]: id, [1]: name } = subArray;
|
|
|
|
|
|
|
|
|
|
// 创建一个新的对象来存储这些信息
|
|
|
|
|
const newStu = {
|
|
|
|
|
id: id,
|
|
|
|
|
name: name,
|
|
|
|
|
point: 10,
|
|
|
|
|
clazz: this.data.className,
|
|
|
|
|
};
|
|
|
|
|
const isDuplicate = this.data.students.some(stu => stu.id === id);
|
|
|
|
|
if(!isDuplicate){
|
|
|
|
|
// 将新对象发送到后端服务器
|
|
|
|
|
const classstu = newStu;
|
|
|
|
|
//classstu.className = this.data.className;//此时 classstu = {id:"102201",name:"Jhon", className:`${this.data.className}` }
|
|
|
|
|
wx.request({
|
|
|
|
|
url: 'http://192.168.152.1:8090/student/set',
|
|
|
|
|
method: 'POST', // 请求方法,通常是POST用于发送数据
|
|
|
|
|
header: {
|
|
|
|
|
'Content-Type': 'application/json' // 设置请求头,指定数据格式为JSON
|
|
|
|
|
},
|
|
|
|
|
data: newStu, // 要发送的数据,这里直接传入对象,wx.request会自动将其转换为JSON字符串(后端接收json字符串)
|
|
|
|
|
success: function(res) {
|
|
|
|
|
// 处理后端返回的响应
|
|
|
|
|
console.log('请求成功', res.data);
|
|
|
|
|
// 根据需要更新小程序页面的数据或执行其他操作
|
|
|
|
|
},
|
|
|
|
|
fail: function(err) {
|
|
|
|
|
// 处理请求失败的情况
|
|
|
|
|
console.error('请求失败', err);
|
|
|
|
|
// 可以向用户显示错误信息或执行其他错误处理操作
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// 将新对象添加到data.stu数组中
|
|
|
|
|
this.setData({
|
|
|
|
|
['students[' + this.data.students.length + ']']: newStu //直接将新对象加在数组后面
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.log('id重复:${id}');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
console.log(this.data.students);
|
|
|
|
|
} else {
|
|
|
|
|
this.cantNull();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
goToclass_manage: function(){
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: '/pages/class_manage/class_manage'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|