ccit.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // 长春工程学院(ccit.edu.cn) 拾光课程表适配脚本
  2. // 非该大学开发者适配,开发者无法及时发现问题
  3. // 出现问题请提联系开发者或者提交pr更改,这更加快速
  4. // 工具函数
  5. window.validateYearInput = function(input) {
  6. return /^[0-9]{4}$/.test(input) ? false : "请输入四位数字的学年!"; //[cite: 1]
  7. };
  8. function parseWeeks(weekStr) {
  9. const weeks = [];
  10. if (!weekStr) return weeks;
  11. const pureWeekData = weekStr.split('(')[0];
  12. pureWeekData.split(',').forEach(seg => {
  13. if (seg.includes('-')) {
  14. const [s, e] = seg.split('-').map(Number);
  15. if (!isNaN(s) && !isNaN(e)) {
  16. for (let i = s; i <= e; i++) weeks.push(i);
  17. }
  18. } else {
  19. const w = parseInt(seg);
  20. if (!isNaN(w)) weeks.push(w);
  21. }
  22. });
  23. return [...new Set(weeks)].sort((a, b) => a - b);
  24. }
  25. /**
  26. * 节次合并与去重
  27. */
  28. function mergeAndDistinctCourses(courses) {
  29. if (courses.length <= 1) return courses;
  30. courses.sort((a, b) => {
  31. return a.name.localeCompare(b.name) ||
  32. a.day - b.day ||
  33. a.startSection - b.startSection ||
  34. a.weeks.join(',').localeCompare(b.weeks.join(','));
  35. });
  36. const merged = [];
  37. let current = courses[0];
  38. for (let i = 1; i < courses.length; i++) {
  39. const next = courses[i];
  40. const isSameCourse =
  41. current.name === next.name &&
  42. current.teacher === next.teacher &&
  43. current.position === next.position &&
  44. current.day === next.day &&
  45. current.weeks.join(',') === next.weeks.join(',');
  46. const isContinuous = current.endSection + 1 === next.startSection;
  47. if (isSameCourse && isContinuous) {
  48. current.endSection = next.endSection;
  49. } else if (isSameCourse && current.startSection === next.startSection && current.endSection === next.endSection) {
  50. continue;
  51. } else {
  52. merged.push(current);
  53. current = next;
  54. }
  55. }
  56. merged.push(current);
  57. return merged;
  58. }
  59. /**
  60. * 错峰上下课时间适配逻辑
  61. */
  62. function applyCustomTimeSplitting(courses) {
  63. const SPLIT_CONFIG = [
  64. {
  65. // 组1
  66. // 匹配:DJ1(11-12层), DJ2(21-23层), XJ3(31-32层), XJ5(51-52层), XJ6(61-62层), XJ7(71-74层)
  67. regex: /(DJ1-1[12])|(DJ2-2[1-3])|(XJ3-3[12])|(XJ5-5[12])|(XJ6-6[12])|(XJ7-7[1-4])/,
  68. timeMap: {
  69. 1: ["07:50", "08:35"], 2: ["08:35", "09:20"],
  70. 3: ["10:10", "10:55"], 4: ["10:55", "11:40"],
  71. 5: ["13:20", "14:05"], 6: ["14:05", "14:50"],
  72. 7: ["15:40", "16:25"], 8: ["16:25", "17:10"],
  73. 9: ["18:00", "18:45"], 10: ["18:50", "19:35"]
  74. }
  75. },
  76. {
  77. // 组2
  78. // 匹配:DJ1(13-15层), DJ2(24-26层), XJ3(33-34层), XJ6(63层), XJ7(75-77层), DSx, XSx
  79. regex: /(DJ1-1[3-5])|(DJ2-2[4-6])|(XJ3-3[34])|(XJ6-63)|(XJ7-7[5-7])|(DS)|(XS)/,
  80. timeMap: {
  81. 1: ["08:10", "08:55"], 2: ["08:55", "09:40"],
  82. 3: ["10:30", "11:15"], 4: ["11:15", "12:00"],
  83. 5: ["13:40", "14:25"], 6: ["14:25", "15:10"],
  84. 7: ["16:00", "16:45"], 8: ["16:45", "17:30"],
  85. 9: ["18:00", "18:45"], 10: ["18:50", "19:35"]
  86. }
  87. }
  88. ];
  89. return courses.map(course => {
  90. for (const config of SPLIT_CONFIG) {
  91. // 使用正则匹配 course.position (上课地点)
  92. if (config.regex.test(course.position)) {
  93. const startTimes = config.timeMap[course.startSection];
  94. const endTimes = config.timeMap[course.endSection];
  95. if (startTimes && endTimes) {
  96. // 必须设为 true 以激活自定义时间模式
  97. course.isCustomTime = true;
  98. // 格式必须为 HH:mm
  99. course.customStartTime = startTimes[0];
  100. course.customEndTime = endTimes[1];
  101. }
  102. break;
  103. }
  104. }
  105. return course;
  106. });
  107. }
  108. // 核心解析逻辑
  109. function parseTimetableToModel(doc) {
  110. const timetable = doc.getElementById('kbtable');
  111. if (!timetable) return [];
  112. let rawCourses = [];
  113. const rows = Array.from(timetable.querySelectorAll('tr')).filter(r => r.querySelector('td'));
  114. rows.forEach(row => {
  115. const cells = row.querySelectorAll('td');
  116. cells.forEach((cell, dayIndex) => {
  117. const day = dayIndex + 1;
  118. const detailDivs = cell.querySelectorAll('div.kbcontent');
  119. detailDivs.forEach(div => {
  120. const rawHtml = div.innerHTML.trim();
  121. if (!rawHtml || rawHtml === "&nbsp;" || div.innerText.trim().length < 2) return;
  122. const blocks = rawHtml.split(/---------------------|----------------------/);
  123. blocks.forEach(block => {
  124. if (!block.trim()) return;
  125. const tempDiv = document.createElement('div');
  126. tempDiv.innerHTML = block;
  127. let name = "";
  128. for (let node of tempDiv.childNodes) {
  129. if (node.nodeType === 3 && node.textContent.trim() !== "") {
  130. name = node.textContent.trim();
  131. break;
  132. }
  133. }
  134. const teacherRaw = tempDiv.querySelector('font[title="老师"], font[title="教师"]')?.innerText || "";
  135. const teacher = teacherRaw.replace("任课教师:", "").trim();
  136. const position = tempDiv.querySelector('font[title="教室"]')?.innerText || "未知地点";
  137. const weekStr = tempDiv.querySelector('font[title="周次(节次)"]')?.innerText || "";
  138. let startSection = 0;
  139. let endSection = 0;
  140. if (weekStr) {
  141. const sectionPart = weekStr.match(/\[(.*?)节\]/);
  142. if (sectionPart && sectionPart[1]) {
  143. const sections = sectionPart[1].split('-').map(Number).filter(n => !isNaN(n));
  144. if (sections.length > 0) {
  145. startSection = sections[0];
  146. endSection = sections[sections.length - 1];
  147. }
  148. }
  149. }
  150. if (name && startSection > 0) {
  151. rawCourses.push({
  152. "name": name,
  153. "teacher": teacher || "未知教师",
  154. "weeks": parseWeeks(weekStr),
  155. "position": position,
  156. "day": day,
  157. "startSection": startSection,
  158. "endSection": endSection
  159. });
  160. }
  161. });
  162. });
  163. });
  164. });
  165. // 1. 先进行标准节次合并
  166. const merged = mergeAndDistinctCourses(rawCourses);
  167. // 2. 根据地点注入自定义时间(合并后注入)
  168. return applyCustomTimeSplitting(merged);
  169. }
  170. // 配置与流程
  171. async function saveAppConfig() {
  172. const config = { "semesterTotalWeeks": 20, "firstDayOfWeek": 1 };
  173. return await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
  174. }
  175. async function saveAppTimeSlots() {
  176. const timeSlots = [
  177. { "number": 1, "startTime": "08:00", "endTime": "08:45" },
  178. { "number": 2, "startTime": "08:50", "endTime": "09:35" },
  179. { "number": 3, "startTime": "10:05", "endTime": "10:50" },
  180. { "number": 4, "startTime": "10:55", "endTime": "11:40" },
  181. { "number": 5, "startTime": "13:30", "endTime": "14:15" },
  182. { "number": 6, "startTime": "14:20", "endTime": "15:05" },
  183. { "number": 7, "startTime": "15:35", "endTime": "16:20" },
  184. { "number": 8, "startTime": "16:25", "endTime": "17:10" },
  185. { "number": 9, "startTime": "18:00", "endTime": "18:45" },
  186. { "number": 10, "startTime": "18:50", "endTime": "19:35" },
  187. { "number": 11, "startTime": "19:40", "endTime": "20:25" },
  188. { "number": 12, "startTime": "20:30", "endTime": "21:15" }
  189. ];
  190. return await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  191. }
  192. async function runImportFlow() {
  193. try {
  194. const confirmed = await window.AndroidBridgePromise.showAlert("提示", "请确保已成功登录教务系统。是否开始导入?", "开始");
  195. if (!confirmed) return;
  196. const currentYear = new Date().getFullYear();
  197. const year = await window.AndroidBridgePromise.showPrompt("选择学年", "请输入要导入课程的起始学年(例如 2025-2026 应输入2025):", String(currentYear), "validateYearInput");
  198. if (!year) return;
  199. const semesterIndex = await window.AndroidBridgePromise.showSingleSelection("选择学期", JSON.stringify(["第一学期", "第二学期"]), 0);
  200. if (semesterIndex === null) return;
  201. const semesterId = `${year}-${parseInt(year) + 1}-${semesterIndex + 1}`;
  202. AndroidBridge.showToast("正在请求数据...");
  203. const response = await fetch("https://http-10-198-47-148-8080.webvpn.ccit.edu.cn/jsxsd/xskb/xskb_list.do", {
  204. method: "POST",
  205. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  206. body: `jx0404id=&cj0701id=&zc=&demo=&xnxq01id=${semesterId}`,
  207. credentials: "include"
  208. });
  209. const html = await response.text();
  210. const finalCourses = parseTimetableToModel(new DOMParser().parseFromString(html, "text/html"));
  211. if (finalCourses.length === 0) {
  212. AndroidBridge.showToast("未发现课程,请检查学期选择或登录状态。");
  213. return;
  214. }
  215. await saveAppConfig();
  216. await saveAppTimeSlots();
  217. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(finalCourses)); //[cite: 1]
  218. AndroidBridge.showToast(`成功导入 ${finalCourses.length} 门课程`);
  219. AndroidBridge.notifyTaskCompletion();
  220. } catch (error) {
  221. AndroidBridge.showToast("异常: " + error.message);
  222. }
  223. }
  224. runImportFlow();