Skip to content

Commit 188bd95

Browse files
committed
feat: 添加本地配置文件支持,允许用户上传自定义考试配置并清除本地配置
1 parent 5049546 commit 188bd95

File tree

7 files changed

+197
-17
lines changed

7 files changed

+197
-17
lines changed

background.jpg

58.1 KB
Loading

exam/Scripts/examInfo.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ document.addEventListener("DOMContentLoaded", () => {
1111
let offsetTime = getCookie("offsetTime") || 0;
1212

1313
function fetchData() {
14+
// 优先使用本地配置
15+
const localConfig = localStorage.getItem('localExamConfig');
16+
if (localConfig) {
17+
try {
18+
const data = JSON.parse(localConfig);
19+
displayExamInfo(data);
20+
updateCurrentTime();
21+
updateExamInfo(data);
22+
setInterval(() => updateCurrentTime(), 1000);
23+
setInterval(() => updateExamInfo(data), 1000);
24+
return Promise.resolve();
25+
} catch (error) {
26+
localStorage.removeItem('localExamConfig');
27+
errorSystem.show('本地配置无效,已切换至默认配置');
28+
}
29+
}
30+
31+
// 使用默认配置
1432
return fetch('exam_config.json', { cache: "no-store" })
1533
.then(response => response.json())
1634
.then(data => {

exam/Scripts/settings.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ document.addEventListener("DOMContentLoaded", () => {
99
const zoomInput = document.getElementById("zoom-input");
1010
const themeToggle = document.getElementById("theme-toggle");
1111
const themeLink = document.getElementById("theme-link");
12+
const configFileInput = document.getElementById("config-file");
13+
const clearConfigBtn = document.getElementById("clear-config-btn");
1214

1315
let offsetTime = getCookie("offsetTime") || 0;
1416
let room = getCookie("room") || "";
@@ -79,6 +81,58 @@ document.addEventListener("DOMContentLoaded", () => {
7981
themeLink.href = theme === "light" ? "Styles/light.css" : "Styles/dark.css";
8082
});
8183

84+
configFileInput.addEventListener("change", (event) => {
85+
try {
86+
const file = event.target.files[0];
87+
if (!file) return;
88+
89+
const reader = new FileReader();
90+
reader.onload = (e) => {
91+
try {
92+
const config = JSON.parse(e.target.result);
93+
94+
// 验证配置文件格式
95+
if (!config.examInfos || !Array.isArray(config.examInfos)) {
96+
throw new Error("无效的配置文件格式");
97+
}
98+
99+
// 验证每个考试信息
100+
config.examInfos.forEach(exam => {
101+
if (!exam.name || !exam.start || !exam.end) {
102+
throw new Error("考试信息不完整");
103+
}
104+
// 验证日期格式
105+
if (isNaN(new Date(exam.start).getTime()) || isNaN(new Date(exam.end).getTime())) {
106+
throw new Error("无效的日期格式");
107+
}
108+
});
109+
110+
// 保存配置到本地存储
111+
localStorage.setItem('localExamConfig', JSON.stringify(config));
112+
errorSystem.show('配置文件已加载,将在下次启动时生效');
113+
114+
} catch (error) {
115+
errorSystem.show('配置文件格式错误: ' + error.message);
116+
}
117+
};
118+
reader.readAsText(file);
119+
} catch (e) {
120+
errorSystem.show('读取文件失败: ' + e.message);
121+
}
122+
});
123+
124+
clearConfigBtn.addEventListener("click", () => {
125+
try {
126+
if (confirm("确定要清除本地配置吗?这将恢复使用默认配置文件。")) {
127+
localStorage.removeItem('localExamConfig');
128+
configFileInput.value = ''; // 清空文件选择
129+
errorSystem.show('本地配置已清除,将在下次启动时生效');
130+
}
131+
} catch (e) {
132+
errorSystem.show('清除配置失败: ' + e.message);
133+
}
134+
});
135+
82136
try {
83137
document.body.style.zoom = zoomLevel;
84138
} catch (e) {

exam/Styles/dark.css

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,54 @@ input:checked + .slider:before {
410410
justify-content: space-between;
411411
gap: 10px;
412412
}
413+
414+
.config-file-container {
415+
margin: 12px 0;
416+
padding: 10px;
417+
border: 1px solid #555;
418+
border-radius: 5px;
419+
background-color: rgba(31, 31, 31, 0.5);
420+
}
421+
422+
.config-file-container label {
423+
display: block;
424+
margin-bottom: 8px;
425+
color: #e0e0e0;
426+
}
427+
428+
.config-file-container input[type="file"] {
429+
display: block;
430+
width: 100%;
431+
padding: 8px;
432+
border: 1px solid #555;
433+
border-radius: 4px;
434+
background-color: #222;
435+
color: #e0e0e0;
436+
cursor: pointer;
437+
}
438+
439+
.config-file-container input[type="file"]:hover {
440+
background-color: #333;
441+
}
442+
443+
.file-hint {
444+
margin-top: 4px;
445+
font-size: 12px;
446+
color: #888;
447+
}
448+
449+
.config-control-btn {
450+
margin-top: 10px;
451+
padding: 8px 16px;
452+
background-color: #d9534f;
453+
color: white;
454+
border: none;
455+
border-radius: 4px;
456+
cursor: pointer;
457+
font-size: 14px;
458+
transition: background-color 0.3s ease;
459+
}
460+
461+
.config-control-btn:hover {
462+
background-color: #c9302c;
463+
}

exam/Styles/light.css

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,54 @@ input:checked + .slider:before {
415415
justify-content: space-between;
416416
gap: 10px;
417417
}
418+
419+
.config-file-container {
420+
margin: 12px 0;
421+
padding: 10px;
422+
border: 1px solid #ccc;
423+
border-radius: 5px;
424+
background-color: rgba(255, 255, 255, 0.5);
425+
}
426+
427+
.config-file-container label {
428+
display: block;
429+
margin-bottom: 8px;
430+
color: #333;
431+
}
432+
433+
.config-file-container input[type="file"] {
434+
display: block;
435+
width: 100%;
436+
padding: 8px;
437+
border: 1px solid #ccc;
438+
border-radius: 4px;
439+
background-color: #fff;
440+
color: #333;
441+
cursor: pointer;
442+
}
443+
444+
.config-file-container input[type="file"]:hover {
445+
background-color: #f5f5f5;
446+
}
447+
448+
.file-hint {
449+
margin-top: 4px;
450+
font-size: 12px;
451+
color: #666;
452+
}
453+
454+
.config-control-btn {
455+
margin-top: 10px;
456+
padding: 8px 16px;
457+
background-color: #d9534f;
458+
color: white;
459+
border: none;
460+
border-radius: 4px;
461+
cursor: pointer;
462+
font-size: 14px;
463+
transition: background-color 0.3s ease;
464+
}
465+
466+
.config-control-btn:hover {
467+
background-color: #c9302c;
468+
}

exam/exam_config.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,43 @@
55
"examInfos": [
66
{
77
"name": "语文",
8-
"start": "2025-03-27T07:20:00",
9-
"end": "2025-03-27T09:50:00"
8+
"start": "2025-03-30T07:20:00",
9+
"end": "2025-03-30T09:50:00"
1010
},
1111
{
1212
"name": "物理",
13-
"start": "2025-03-27T10:20:00",
14-
"end": "2025-03-27T11:50:00"
13+
"start": "2025-03-30T10:20:00",
14+
"end": "2025-03-30T11:50:00"
1515
},
1616
{
1717
"name": "英语",
18-
"start": "2025-03-27T14:10:00",
19-
"end": "2025-03-27T16:10:00"
18+
"start": "2025-03-30T14:10:00",
19+
"end": "2025-03-30T23:50:00"
2020
},
2121
{
2222
"name": "历史",
23-
"start": "2025-03-27T16:30:00",
24-
"end": "2025-03-27T18:00:00"
23+
"start": "2025-03-30T23:55:00",
24+
"end": "2025-03-31T01:00:00"
2525
},
2626
{
2727
"name": "数学",
28-
"start": "2025-03-28T07:50:00",
29-
"end": "2025-03-28T09:50:00"
28+
"start": "2025-03-31T07:50:00",
29+
"end": "2025-03-31T09:50:00"
3030
},
3131
{
3232
"name": "化学",
33-
"start": "2025-03-28T10:20:00",
34-
"end": "2025-03-28T11:50:00"
33+
"start": "2025-03-31T10:20:00",
34+
"end": "2025-03-31T11:50:00"
3535
},
3636
{
37-
"name": "生物政治",
38-
"start": "2025-03-28T14:10:00",
39-
"end": "2025-03-28T15:40:00"
37+
"name": "生物/政治",
38+
"start": "2025-03-31T14:10:00",
39+
"end": "2025-03-31T15:40:00"
4040
},
4141
{
4242
"name": "地理",
43-
"start": "2025-03-28T16:10:00",
44-
"end": "2025-03-28T17:40:00"
43+
"start": "2025-03-31T16:10:00",
44+
"end": "2025-03-31T17:40:00"
4545
}
4646
]
4747
}

exam/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ <h3>系统设置</h3>
5959
<input type="text" id="room-input" name="room-input" value="">
6060
<label for="zoom-input">页面缩放倍数:</label>
6161
<input type="number" id="zoom-input" step="0.1" min="0.5" max="2">
62+
<div class="config-file-container">
63+
<label for="config-file">配置文件:</label>
64+
<input type="file" id="config-file" accept=".json">
65+
<div class="file-hint">支持.json格式文件</div>
66+
<button id="clear-config-btn" class="config-control-btn">清除本地配置</button>
67+
</div>
6268
<div class="theme-toggle-container">
6369
<label for="theme-toggle">亮/暗色模式:</label>
6470
<label class="switch">

0 commit comments

Comments
 (0)