Jelajahi Sumber

fix: 优化获取作息时间表的逻辑,增加有效性检查和去重处理

XingHeYuZhuan 2 minggu lalu
induk
melakukan
4570e79a51
1 mengubah file dengan 31 tambahan dan 11 penghapusan
  1. 31 11
      resources/JSEI/jsei_01.js

+ 31 - 11
resources/JSEI/jsei_01.js

@@ -403,20 +403,40 @@ async function fetchTimeSlots(academicYear, semesterCode) {
         if (!response.ok) return null;
 
         const json = await response.json();
-        if (!Array.isArray(json)) return null;
-
-        // 过滤有效节次并转换格式
-        const timeSlots = json
-            .filter(item => item.jcmc && item.qssj && item.jssj)
-            .map(item => ({
-                number: Number(item.jcmc),
-                startTime: item.qssj.substring(0, 5), // "08:30:00" -> "08:30"
-                endTime: item.jssj.substring(0, 5)
-            }))
-            .sort((a, b) => a.number - b.number);
 
+        if (!Array.isArray(json) || json.length === 0) {
+            return null;
+        }
+
+        const seen = {};
+        const timeSlots = [];
+
+        for (let i = 0; i < json.length; i++) {
+            const item = json[i];
+            if (!item || !item.jcmc || !item.qssj || !item.jssj) {
+                continue;
+            }
+
+            const num = Number(item.jcmc);
+            if (isNaN(num) || seen[num]) {
+                continue;
+            }
+            seen[num] = true;
+
+            timeSlots.push({
+                number: num,
+                startTime: String(item.qssj).substring(0, 5),
+                endTime: String(item.jssj).substring(0, 5)
+            });
+        }
+
+        Array.prototype.sort.call(timeSlots, (a, b) => a.number - b.number);
+
+        console.log("[作息] 解析到", timeSlots.length, "条:", timeSlots);
         return timeSlots.length > 0 ? timeSlots : null;
+
     } catch (e) {
+        console.error("[作息] 异常:", e);
         return null;
     }
 }