let globalStudent = null; let globalnumber =0; async function uploadFile() { const fileInput = document.getElementById('fileInput'); // 获取文件输入元素 const url = window.location.href; console.log(fileInput); // 创建一个 URL 对象 const urlObj = new URL(url); // 使用 URLSearchParams 获取查询参数 const params = new URLSearchParams(urlObj.search); // 获取 'number' 参数的值 globalnumber = params.get('number'); // 检查是否选中了文件 if (fileInput.files.length === 0) { alert('请先选择一个文件!'); return; } const file = fileInput.files[0]; // 获取第一个文件 // 检查文件类型 const allowedTypes = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel']; // 允许的文件类型 if (!allowedTypes.includes(file.type)) { alert('请上传有效的 Excel 文件!'); return; } // 检查文件大小(例如:限制为 5MB) const maxSize = 5 * 1024 * 1024; // 5MB if (file.size > maxSize) { alert('文件大小不能超过 5MB!'); return; } const formData = new FormData(); formData.append('file', file); try { const response = await fetch(`http://10.133.39.58:8000/${globalnumber}/upload`, { method: 'POST', body: formData }); if (!response.ok) { throw new Error('文件上传失败。'); } const data = await response.json(); console.log(data); } catch (error) { console.error('错误:', error); alert('上传失败: ' + error.message); } } async function getRandomStudent() { console.log('haha'); const response = await fetch(`http://10.133.39.58:8000/${globalnumber}/random-call`, { method: 'POST', }); const student = await response.json(); globalStudent=student; console.log(student); document.getElementById('picked').innerText = `Selected: ${student.name} (ID: ${student.id}) (points: ${student.points})`; } async function createroom() { const roomNumber = document.getElementById('roomNumber').value; if (roomNumber.trim() === '') { alert('房间号码不能为空!'); this.isModalVisible = false; return; } globalnumber = roomNumber; console.log(roomNumber); try { const response = await fetch(`http://10.133.39.58:8000/create-class/${globalnumber}`, { method: 'POST', }); if (!response.ok) { throw new Error('文件上传失败。'); } const data = await response.json(); // 解析响应数据(假设返回的是 JSON 格式) console.log('房间创建成功:', data); // 输出成功消息或处理数据 alert(data.message); } catch (error) { console.error('发生错误:', error.message); // 输出错误信息 alert(`连接后端服务器失败: ${error.message}`); // 弹窗显示错误信息 } setTimeout(() => { window.location.href = `index.html?number=${roomNumber}`; }, 300); // 等待1秒 } async function changepoints(chan) { const idi = globalStudent.id; const namei = globalStudent.name; const pointsi = chan; globalStudent.points+=chan; const response = await fetch(`http://10.133.39.58:8000/${globalnumber}/change-points`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ id: String(idi), name: String(namei), points: parseFloat(pointsi) }), }); response.json().then(result => { console.log(result); // 输出 Promise 的结果 }).catch(error => { console.error("发生错误:", error); }); document.getElementById('picked').innerText = `Selected: ${globalStudent.name} (ID: ${globalStudent.id}) (points: ${globalStudent.points})`; }