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.

204 lines
5.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

let globalStudent = null;
let globalnumber =0;
let houduan = "10.133.48.140:8000";
const generatedStrings = [];
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.48.140:8000/${globalnumber}/upload`, {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error('文件上传失败。');
}
const data = await response.json();
alert(data.message);
}
catch (error) {
console.error('错误:', error);
alert('上传失败: ' + error.message);
}
}
async function getRandomStudent() {
const urlObj = new URL(url);
// 使用 URLSearchParams 获取查询参数
const params = new URLSearchParams(urlObj.search);
// 获取 'number' 参数的值
globalnumber = params.get('number');
console.log('haha');
const response = await fetch(`http://10.133.48.140:8000/${globalnumber}/random-call`,
{
method: 'POST',
});
const student = await response.json();
globalStudent=student;
console.log(student);
if(!generatedStrings.includes(student.id))
{
generatedStrings.push(student.id);
changepoints(1);
}
document.getElementById('picked').innerText = `${student.name} \n 学号:${student.id} \n积分:${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.48.140: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 charoom() {
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.48.140:8000/${globalnumber}/search-room`, {
method: 'POST',
});
if (!response.ok) {
throw new Error('连接失败。');
}
const data = await response.json(); // 解析响应数据(假设返回的是 JSON 格式)
alert(data.message);
if(data.message=="班级存在")
{
return 1;
}
else
{
return 2;
}
} catch (error) {
console.error('发生错误:', error.message); // 输出错误信息
alert(`连接后端服务器失败: ${error.message}`); // 弹窗显示错误信息
}
}
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.48.140: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 = `${globalStudent.name} \n 学号:${globalStudent.id} \n积分:${globalStudent.points}`;
}
function animateImage() {
const imageContainer = document.querySelector('.image-container');
// Step 1: 从右边弹出
imageContainer.style.right = '50%'; // 中间位置
imageContainer.style.transform = 'translate(50%, 0) rotate(360deg)'; // 转动
// Step 2: 等待旋转后从左边出去
setTimeout(() => {
imageContainer.style.right = '100%'; // 从左边出去
imageContainer.style.transform = 'translate(0, 0)'; // 重置旋转
}, 2000); // 旋转时间与动画匹配
}