소스 검색

fix: 支持单双周课程解析 feat: 补充每节课的具体上课时间

sLingli 1 개월 전
부모
커밋
0c653191f7
1개의 변경된 파일36개의 추가작업 그리고 20개의 파일을 삭제
  1. 36 20
      resources/TSU/tsu_01.js

+ 36 - 20
resources/TSU/tsu_01.js

@@ -1,4 +1,17 @@
-function parseWeeks(weekStr) {
+const SECTION_TIMES = [
+    { number: 1,  startTime: "08:00", endTime: "08:45" },
+    { number: 2,  startTime: "08:55", endTime: "09:40" },
+    { number: 3,  startTime: "10:00", endTime: "10:45" },
+    { number: 4,  startTime: "10:55", endTime: "11:40" },
+    { number: 5,  startTime: "14:30", endTime: "15:15" },
+    { number: 6,  startTime: "15:25", endTime: "16:10" },
+    { number: 7,  startTime: "16:30", endTime: "17:15" },
+    { number: 8,  startTime: "17:25", endTime: "18:10" },
+    { number: 9,  startTime: "19:00", endTime: "19:45" },
+    { number: 10, startTime: "19:55", endTime: "20:40" },
+];
+
+function parseWeeks(weekStr, parity) {
     const weeks = [];
     weekStr.split(',').forEach(part => {
         if (part.includes('-')) {
@@ -9,6 +22,8 @@ function parseWeeks(weekStr) {
             if (!isNaN(w)) weeks.push(w);
         }
     });
+    if (parity === '单') return weeks.filter(w => w % 2 === 1);
+    if (parity === '双') return weeks.filter(w => w % 2 === 0);
     return weeks;
 }
 
@@ -49,22 +64,21 @@ async function fetchAndParseCourses() {
             // 找出所有时间的行号(锚点)
             const timeIndices = [];
             lines.forEach((line, index) => {
-                if (/([\d\-,]+)\[(\d+)-(\d+)\]/.test(line)) {
+                if (/([\d\-,]+)\s*(?:单|双)?\s*\[(\d+)-(\d+)\]/.test(line)) {
                     timeIndices.push(index);
                 }
             });
 
-            // 遍历每个锚点,精准抓取
             timeIndices.forEach((currTimeIdx, k) => {
                 const nextTimeIdx = (k + 1 < timeIndices.length) ? timeIndices[k + 1] : lines.length;
-                const match = lines[currTimeIdx].match(/([\d\-,]+)\[(\d+)-(\d+)\]/);
-                
+                const match = lines[currTimeIdx].match(/([\d\-,]+)\s*(单|双)?\s*\[(\d+)-(\d+)\]/);
+
                 if (match) {
                     // 1. 抓取课名和老师
                     let name = "未知课程";
                     let teacher = "未知教师";
                     let nameTeacherLines = (k === 0) ? lines.slice(0, currTimeIdx) : lines.slice(timeIndices[k - 1] + 1, currTimeIdx);
-                    
+
                     if (nameTeacherLines.length > 2) {
                         nameTeacherLines = nameTeacherLines.slice(nameTeacherLines.length - 2);
                     }
@@ -74,23 +88,22 @@ async function fetchAndParseCourses() {
                         teacher = nameTeacherLines[1];
                     } else if (nameTeacherLines.length === 1) {
                         name = nameTeacherLines[0];
-                        teacher = ""; // 没写老师
+                        teacher = "";
                     }
 
                     // 2. 抓取地点
                     let position = "";
-                    const gap = nextTimeIdx - currTimeIdx - 1; // 距离下个时间(或结尾)差几行
-                    
+                    const gap = nextTimeIdx - currTimeIdx - 1;
+
                     if (k === timeIndices.length - 1) {
-                        // 最后一门课,后面全是地点
                         if (gap > 0) position = lines.slice(currTimeIdx + 1).join(' ');
                     } else {
                         if (gap === 3) {
-                            position = lines[currTimeIdx + 1]; // 刚好3行,第1行必是地点
+                            position = lines[currTimeIdx + 1];
                         } else if (gap > 3) {
-                            position = lines.slice(currTimeIdx + 1, nextTimeIdx - 2).join(' '); // 地点占了多行
+                            position = lines.slice(currTimeIdx + 1, nextTimeIdx - 2).join(' ');
                         } else {
-                            position = ""; // <= 2行说明全被下节课的名字占了,这节课没地点(网课)
+                            position = "";
                         }
                     }
 
@@ -99,9 +112,9 @@ async function fetchAndParseCourses() {
                         teacher: teacher,
                         position: position,
                         day: day,
-                        startSection: parseInt(match[2]),
-                        endSection: parseInt(match[3]),
-                        weeks: parseWeeks(match[1])
+                        startSection: parseInt(match[3]),
+                        endSection: parseInt(match[4]),
+                        weeks: parseWeeks(match[1], match[2])
                     });
                 }
             });
@@ -162,11 +175,14 @@ async function runImportFlow() {
             AndroidBridge.notifyTaskCompletion();
             return;
         }
+
         const saveResult = await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
-        if (saveResult === true) {
-            AndroidBridge.showToast(`导入大成功!合并生成 ${courses.length} 个课块`);
-            AndroidBridge.notifyTaskCompletion();
-        }
+        if (saveResult !== true) return;
+
+        await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(SECTION_TIMES));
+
+        AndroidBridge.showToast(`导入大成功!合并生成 ${courses.length} 个课块`);
+        AndroidBridge.notifyTaskCompletion();
     } catch (error) {
         AndroidBridge.showToast("⚠️ " + error.message);
         AndroidBridge.notifyTaskCompletion();