feat: 添加本地配置文件支持,允许用户上传自定义考试配置并清除本地配置

This commit is contained in:
MKStoler1024 2025-03-30 16:39:02 +00:00
parent 504954619a
commit 188bd95897
7 changed files with 197 additions and 17 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 MiB

After

Width:  |  Height:  |  Size: 3.5 MiB

View File

@ -11,6 +11,24 @@ document.addEventListener("DOMContentLoaded", () => {
let offsetTime = getCookie("offsetTime") || 0;
function fetchData() {
// 优先使用本地配置
const localConfig = localStorage.getItem('localExamConfig');
if (localConfig) {
try {
const data = JSON.parse(localConfig);
displayExamInfo(data);
updateCurrentTime();
updateExamInfo(data);
setInterval(() => updateCurrentTime(), 1000);
setInterval(() => updateExamInfo(data), 1000);
return Promise.resolve();
} catch (error) {
localStorage.removeItem('localExamConfig');
errorSystem.show('本地配置无效,已切换至默认配置');
}
}
// 使用默认配置
return fetch('exam_config.json', { cache: "no-store" })
.then(response => response.json())
.then(data => {

View File

@ -9,6 +9,8 @@ document.addEventListener("DOMContentLoaded", () => {
const zoomInput = document.getElementById("zoom-input");
const themeToggle = document.getElementById("theme-toggle");
const themeLink = document.getElementById("theme-link");
const configFileInput = document.getElementById("config-file");
const clearConfigBtn = document.getElementById("clear-config-btn");
let offsetTime = getCookie("offsetTime") || 0;
let room = getCookie("room") || "";
@ -79,6 +81,58 @@ document.addEventListener("DOMContentLoaded", () => {
themeLink.href = theme === "light" ? "Styles/light.css" : "Styles/dark.css";
});
configFileInput.addEventListener("change", (event) => {
try {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
try {
const config = JSON.parse(e.target.result);
// 验证配置文件格式
if (!config.examInfos || !Array.isArray(config.examInfos)) {
throw new Error("无效的配置文件格式");
}
// 验证每个考试信息
config.examInfos.forEach(exam => {
if (!exam.name || !exam.start || !exam.end) {
throw new Error("考试信息不完整");
}
// 验证日期格式
if (isNaN(new Date(exam.start).getTime()) || isNaN(new Date(exam.end).getTime())) {
throw new Error("无效的日期格式");
}
});
// 保存配置到本地存储
localStorage.setItem('localExamConfig', JSON.stringify(config));
errorSystem.show('配置文件已加载,将在下次启动时生效');
} catch (error) {
errorSystem.show('配置文件格式错误: ' + error.message);
}
};
reader.readAsText(file);
} catch (e) {
errorSystem.show('读取文件失败: ' + e.message);
}
});
clearConfigBtn.addEventListener("click", () => {
try {
if (confirm("确定要清除本地配置吗?这将恢复使用默认配置文件。")) {
localStorage.removeItem('localExamConfig');
configFileInput.value = ''; // 清空文件选择
errorSystem.show('本地配置已清除,将在下次启动时生效');
}
} catch (e) {
errorSystem.show('清除配置失败: ' + e.message);
}
});
try {
document.body.style.zoom = zoomLevel;
} catch (e) {

View File

@ -410,3 +410,54 @@ input:checked + .slider:before {
justify-content: space-between;
gap: 10px;
}
.config-file-container {
margin: 12px 0;
padding: 10px;
border: 1px solid #555;
border-radius: 5px;
background-color: rgba(31, 31, 31, 0.5);
}
.config-file-container label {
display: block;
margin-bottom: 8px;
color: #e0e0e0;
}
.config-file-container input[type="file"] {
display: block;
width: 100%;
padding: 8px;
border: 1px solid #555;
border-radius: 4px;
background-color: #222;
color: #e0e0e0;
cursor: pointer;
}
.config-file-container input[type="file"]:hover {
background-color: #333;
}
.file-hint {
margin-top: 4px;
font-size: 12px;
color: #888;
}
.config-control-btn {
margin-top: 10px;
padding: 8px 16px;
background-color: #d9534f;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.3s ease;
}
.config-control-btn:hover {
background-color: #c9302c;
}

View File

@ -415,3 +415,54 @@ input:checked + .slider:before {
justify-content: space-between;
gap: 10px;
}
.config-file-container {
margin: 12px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: rgba(255, 255, 255, 0.5);
}
.config-file-container label {
display: block;
margin-bottom: 8px;
color: #333;
}
.config-file-container input[type="file"] {
display: block;
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
background-color: #fff;
color: #333;
cursor: pointer;
}
.config-file-container input[type="file"]:hover {
background-color: #f5f5f5;
}
.file-hint {
margin-top: 4px;
font-size: 12px;
color: #666;
}
.config-control-btn {
margin-top: 10px;
padding: 8px 16px;
background-color: #d9534f;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.3s ease;
}
.config-control-btn:hover {
background-color: #c9302c;
}

View File

@ -5,43 +5,43 @@
"examInfos": [
{
"name": "语文",
"start": "2025-03-27T07:20:00",
"end": "2025-03-27T09:50:00"
"start": "2025-03-30T07:20:00",
"end": "2025-03-30T09:50:00"
},
{
"name": "物理",
"start": "2025-03-27T10:20:00",
"end": "2025-03-27T11:50:00"
"start": "2025-03-30T10:20:00",
"end": "2025-03-30T11:50:00"
},
{
"name": "英语",
"start": "2025-03-27T14:10:00",
"end": "2025-03-27T16:10:00"
"start": "2025-03-30T14:10:00",
"end": "2025-03-30T23:50:00"
},
{
"name": "历史",
"start": "2025-03-27T16:30:00",
"end": "2025-03-27T18:00:00"
"start": "2025-03-30T23:55:00",
"end": "2025-03-31T01:00:00"
},
{
"name": "数学",
"start": "2025-03-28T07:50:00",
"end": "2025-03-28T09:50:00"
"start": "2025-03-31T07:50:00",
"end": "2025-03-31T09:50:00"
},
{
"name": "化学",
"start": "2025-03-28T10:20:00",
"end": "2025-03-28T11:50:00"
"start": "2025-03-31T10:20:00",
"end": "2025-03-31T11:50:00"
},
{
"name": "生物政治",
"start": "2025-03-28T14:10:00",
"end": "2025-03-28T15:40:00"
"name": "生物/政治",
"start": "2025-03-31T14:10:00",
"end": "2025-03-31T15:40:00"
},
{
"name": "地理",
"start": "2025-03-28T16:10:00",
"end": "2025-03-28T17:40:00"
"start": "2025-03-31T16:10:00",
"end": "2025-03-31T17:40:00"
}
]
}

View File

@ -59,6 +59,12 @@
<input type="text" id="room-input" name="room-input" value="">
<label for="zoom-input">页面缩放倍数:</label>
<input type="number" id="zoom-input" step="0.1" min="0.5" max="2">
<div class="config-file-container">
<label for="config-file">配置文件:</label>
<input type="file" id="config-file" accept=".json">
<div class="file-hint">支持.json格式文件</div>
<button id="clear-config-btn" class="config-control-btn">清除本地配置</button>
</div>
<div class="theme-toggle-container">
<label for="theme-toggle">亮/暗色模式:</label>
<label class="switch">