cqrk_01.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
  123. }
  124. /**
  125. * 自动适配双季作息
  126. * @param {number} semesterIndex 0 代表第一学期, 1 代表第二学期
  127. */
  128. async function saveAppTimeSlots(semesterIndex) {
  129. // 第一学期作息
  130. const timeSlots_1 = [
  131. { "number": 1, "startTime": "08:00", "endTime": "08:45" },
  132. { "number": 2, "startTime": "08:55", "endTime": "09:40" },
  133. { "number": 3, "startTime": "09:50", "endTime": "10:35" },
  134. { "number": 4, "startTime": "10:45", "endTime": "11:30" },
  135. { "number": 5, "startTime": "11:40", "endTime": "12:25" },
  136. { "number": 6, "startTime": "14:30", "endTime": "15:15" },
  137. { "number": 7, "startTime": "15:25", "endTime": "16:10" },
  138. { "number": 8, "startTime": "16:20", "endTime": "17:05" },
  139. { "number": 9, "startTime": "17:15", "endTime": "18:00" },
  140. { "number": 10, "startTime": "19:00", "endTime": "19:45" },
  141. { "number": 11, "startTime": "19:55", "endTime": "20:40" },
  142. { "number": 12, "startTime": "20:50", "endTime": "21:35" }
  143. ];
  144. // 第二学期作息
  145. const timeSlots_2 = [
  146. { "number": 1, "startTime": "08:30", "endTime": "09:15" },
  147. { "number": 2, "startTime": "09:20", "endTime": "10:05" },
  148. { "number": 3, "startTime": "10:20", "endTime": "11:05" },
  149. { "number": 4, "startTime": "11:10", "endTime": "11:55" },
  150. { "number": 5, "startTime": "14:00", "endTime": "14:45" },
  151. { "number": 6, "startTime": "14:50", "endTime": "15:35" },
  152. { "number": 7, "startTime": "15:40", "endTime": "16:25" },
  153. { "number": 8, "startTime": "16:40", "endTime": "17:25" },
  154. { "number": 9, "startTime": "17:30", "endTime": "18:15" },
  155. { "number": 10, "startTime": "19:00", "endTime": "19:45" },
  156. { "number": 11, "startTime": "19:50", "endTime": "20:35" },
  157. { "number": 12, "startTime": "20:40", "endTime": "21:25" }
  158. ];
  159. const selectedSlots = (semesterIndex === 0) ? timeSlots_1 : timeSlots_2;
  160. return await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(selectedSlots));
  161. }
  162. async function runImportFlow() {
  163. try {
  164. const confirmed = await window.AndroidBridgePromise.showAlert("提示", "请确保已成功登录教务系统。是否开始导入?", "开始");
  165. if (!confirmed) return;
  166. // 1. 获取学年
  167. const currentYear = new Date().getFullYear();
  168. const year = await window.AndroidBridgePromise.showPrompt("选择学年", "请输入起始学年:", String(currentYear), "validateYearInput");
  169. if (!year) return;
  170. // 2. 获取学期并记录索引
  171. const semesterIndex = await window.AndroidBridgePromise.showSingleSelection("选择学期", JSON.stringify(["第一学期", "第二学期"]), 0);
  172. if (semesterIndex === null) return;
  173. const semesterId = `${year}-${parseInt(year) + 1}-${semesterIndex + 1}`;
  174. AndroidBridge.showToast("正在请求数据...");
  175. const response = await fetch("http://jwxt.cqrk.edu.cn:18080/jsxsd/xskb/xskb_list.do", {
  176. method: "POST",
  177. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  178. body: `jx0404id=&cj0701id=&zc=&demo=&xnxq01id=${semesterId}`,
  179. credentials: "include"
  180. });
  181. const html = await response.text();
  182. const finalCourses = parseTimetableToModel(new DOMParser().parseFromString(html, "text/html"));
  183. if (finalCourses.length === 0) {
  184. AndroidBridge.showToast("未发现课程,请检查学期选择或登录状态。");
  185. return;
  186. }
  187. await saveAppConfig();
  188. await saveAppTimeSlots(semesterIndex);
  189. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(finalCourses));
  190. AndroidBridge.showToast(`成功导入 ${finalCourses.length} 门课程`);
  191. AndroidBridge.notifyTaskCompletion();
  192. } catch (error) {
  193. AndroidBridge.showToast("异常: " + error.message);
  194. }
  195. }
  196. // 启动
  197. runImportFlow();