cqrk_01.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // 重庆人文科技学院(cqrk.edu.cn) 拾光课程表适配脚本
  2. // 非该大学开发者适配,开发者无法及时发现问题
  3. // 出现问题请提联系开发者或者提交pr更改,这更加快速
  4. // 工具函数
  5. window.validateYearInput = function(input) {
  6. return /^[0-9]{4}$/.test(input) ? false : "请输入四位数字的学年!";
  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. function parseTimetableToModel(doc) {
  61. const timetable = doc.getElementById('kbtable');
  62. if (!timetable) return [];
  63. let rawCourses = [];
  64. const rows = Array.from(timetable.querySelectorAll('tr')).filter(r => r.querySelector('td'));
  65. rows.forEach(row => {
  66. const cells = row.querySelectorAll('td');
  67. cells.forEach((cell, dayIndex) => {
  68. const day = dayIndex + 1;
  69. const detailDivs = cell.querySelectorAll('div.kbcontent');
  70. detailDivs.forEach(div => {
  71. const rawHtml = div.innerHTML.trim();
  72. if (!rawHtml || rawHtml === "&nbsp;" || div.innerText.trim().length < 2) return;
  73. const blocks = rawHtml.split(/---------------------|----------------------/);
  74. blocks.forEach(block => {
  75. if (!block.trim()) return;
  76. const tempDiv = document.createElement('div');
  77. tempDiv.innerHTML = block;
  78. let name = "";
  79. for (let node of tempDiv.childNodes) {
  80. if (node.nodeType === 3 && node.textContent.trim() !== "") {
  81. name = node.textContent.trim();
  82. break;
  83. }
  84. }
  85. const teacherRaw = tempDiv.querySelector('font[title="老师"], font[title="教师"]')?.innerText || "";
  86. const teacher = teacherRaw.replace("任课教师:", "").trim();
  87. const position = tempDiv.querySelector('font[title="教室"]')?.innerText || "未知地点";
  88. const weekStr = tempDiv.querySelector('font[title="周次(节次)"]')?.innerText || "";
  89. let startSection = 0;
  90. let endSection = 0;
  91. if (weekStr) {
  92. // 匹配方括号内所有的数字
  93. const sectionPart = weekStr.match(/\[(.*?)节\]/);
  94. if (sectionPart && sectionPart[1]) {
  95. const sections = sectionPart[1].split('-').map(Number).filter(n => !isNaN(n));
  96. if (sections.length > 0) {
  97. startSection = sections[0];
  98. endSection = sections[sections.length - 1];
  99. }
  100. }
  101. }
  102. if (name && startSection > 0) {
  103. rawCourses.push({
  104. "name": name,
  105. "teacher": teacher || "未知教师",
  106. "weeks": parseWeeks(weekStr),
  107. "position": position,
  108. "day": day,
  109. "startSection": startSection,
  110. "endSection": endSection
  111. });
  112. }
  113. });
  114. });
  115. });
  116. });
  117. return mergeAndDistinctCourses(rawCourses);
  118. }
  119. // 配置与流程
  120. async function saveAppConfig() {
  121. const config = { "semesterTotalWeeks": 20, "firstDayOfWeek": 1 };
  122. return await window.shiguangBridgePromise.saveCourseConfig(JSON.stringify(config));
  123. }
  124. /**
  125. * 全年统一作息(不再分学期)
  126. */
  127. async function saveAppTimeSlots() {
  128. // 重庆人文科技学院全年统一作息
  129. const timeSlots = [
  130. { "number": 1, "startTime": "08:30", "endTime": "09:15" },
  131. { "number": 2, "startTime": "09:20", "endTime": "10:05" },
  132. { "number": 3, "startTime": "10:20", "endTime": "11:05" },
  133. { "number": 4, "startTime": "11:10", "endTime": "11:55" },
  134. { "number": 5, "startTime": "14:00", "endTime": "14:45" },
  135. { "number": 6, "startTime": "14:50", "endTime": "15:35" },
  136. { "number": 7, "startTime": "15:40", "endTime": "16:25" },
  137. { "number": 8, "startTime": "16:40", "endTime": "17:25" },
  138. { "number": 9, "startTime": "17:30", "endTime": "18:15" },
  139. { "number": 10, "startTime": "19:00", "endTime": "19:45" },
  140. { "number": 11, "startTime": "19:50", "endTime": "20:35" },
  141. { "number": 12, "startTime": "20:40", "endTime": "21:25" }
  142. ];
  143. return await window.shiguangBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  144. }
  145. async function runImportFlow() {
  146. try {
  147. const confirmed = await window.shiguangBridgePromise.showAlert("提示", "请确保已成功登录教务系统。是否开始导入?", "开始");
  148. if (!confirmed) return;
  149. // 1. 获取学年
  150. const currentYear = new Date().getFullYear();
  151. const year = await window.shiguangBridgePromise.showPrompt("选择学年", "请输入要导入课程的起始学年(例如 2025-2026 应输入2025):", String(currentYear), "validateYearInput");
  152. if (!year) return;
  153. // 2. 获取学期并记录索引
  154. const semesterIndex = await window.shiguangBridgePromise.showSingleSelection("选择学期", JSON.stringify(["第一学期", "第二学期"]), 0);
  155. if (semesterIndex === null) return;
  156. const semesterId = `${year}-${parseInt(year) + 1}-${semesterIndex + 1}`;
  157. window.shiguangBridge.showToast("正在请求数据...");
  158. const response = await fetch("http://jwxt.cqrk.edu.cn:18080/jsxsd/xskb/xskb_list.do", {
  159. method: "POST",
  160. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  161. body: `jx0404id=&cj0701id=&zc=&demo=&xnxq01id=${semesterId}`,
  162. credentials: "include"
  163. });
  164. const html = await response.text();
  165. const finalCourses = parseTimetableToModel(new DOMParser().parseFromString(html, "text/html"));
  166. if (finalCourses.length === 0) {
  167. window.shiguangBridge.showToast("未发现课程,请检查学期选择或登录状态。");
  168. return;
  169. }
  170. await saveAppConfig();
  171. await saveAppTimeSlots();
  172. await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(finalCourses));
  173. window.shiguangBridge.showToast(`成功导入 ${finalCourses.length} 门课程`);
  174. window.shiguangBridge.notifyTaskCompletion();
  175. } catch (error) {
  176. window.shiguangBridge.showToast("异常: " + error.message);
  177. }
  178. }
  179. // 启动
  180. runImportFlow();