gxdlxy_01.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // 广西电力职业技术学院(gxdlxy.com) 拾光课程表适配脚本
  2. // 非该大学开发者适配,开发者无法及时发现问题
  3. // 出现问题请提联系开发者或者提交pr更改,这更加快速
  4. window.validateYearInput = function (input) {
  5. if (/^[0-9]{4}$/.test(input)) {
  6. return false; // 验证通过
  7. } else {
  8. return "请输入四位数字的起始学年(如 2025)";
  9. }
  10. };
  11. function parseWeeks(weekStr) {
  12. const weeks = [];
  13. if (!weekStr) return weeks;
  14. const pureWeekData = weekStr.replace(/周|\(.*?\)/g, '');
  15. const segments = pureWeekData.split(',');
  16. segments.forEach(seg => {
  17. if (seg.includes('-')) {
  18. const range = seg.split('-').map(Number);
  19. const start = range[0];
  20. const end = range[1];
  21. if (!isNaN(start) && !isNaN(end)) {
  22. for (let i = start; i <= end; i++) {
  23. weeks.push(i);
  24. }
  25. }
  26. } else {
  27. const w = parseInt(seg);
  28. if (!isNaN(w)) {
  29. weeks.push(w);
  30. }
  31. }
  32. });
  33. return [...new Set(weeks)].sort((a, b) => a - b);
  34. }
  35. function mergeAndDistinctCourses(courses) {
  36. if (courses.length <= 1) return courses;
  37. courses.sort((a, b) => {
  38. if (a.name !== b.name) return a.name.localeCompare(b.name);
  39. if (a.day !== b.day) return a.day - b.day;
  40. if (a.startSection !== b.startSection) return a.startSection - b.startSection;
  41. if (a.teacher !== b.teacher) return a.teacher.localeCompare(b.teacher);
  42. if (a.position !== b.position) return a.position.localeCompare(b.position);
  43. return a.weeks.join(',').localeCompare(b.weeks.join(','));
  44. });
  45. const merged = [];
  46. let current = courses[0];
  47. for (let i = 1; i < courses.length; i++) {
  48. const next = courses[i];
  49. const isSameCourse =
  50. current.name === next.name &&
  51. current.teacher === next.teacher &&
  52. current.position === next.position &&
  53. current.day === next.day &&
  54. current.weeks.join(',') === next.weeks.join(',');
  55. const isContinuous = (current.endSection + 1 === next.startSection);
  56. if (isSameCourse && isContinuous) {
  57. current.endSection = next.endSection;
  58. } else if (isSameCourse && current.startSection === next.startSection && current.endSection === next.endSection) {
  59. continue;
  60. } else {
  61. merged.push(current);
  62. current = next;
  63. }
  64. }
  65. merged.push(current);
  66. return merged;
  67. }
  68. /**
  69. * 将 HTML 源码解析为课程模型
  70. */
  71. function parseTimetableToModel(htmlString) {
  72. const doc = new DOMParser().parseFromString(htmlString, "text/html");
  73. const timetable = doc.getElementById('kbtable');
  74. if (!timetable) return [];
  75. let rawCourses = [];
  76. const rows = Array.from(timetable.querySelectorAll('tr')).filter(r => r.querySelector('td'));
  77. rows.forEach(row => {
  78. const cells = row.querySelectorAll('td');
  79. cells.forEach((cell, dayIndex) => {
  80. const day = dayIndex + 1; // 星期
  81. const detailDiv = cell.querySelector('div.kbcontent');
  82. if (!detailDiv) return;
  83. const rawHtml = detailDiv.innerHTML.trim();
  84. if (!rawHtml || rawHtml === "&nbsp;" || detailDiv.innerText.trim().length < 2) return;
  85. // 分割同一个格子内的多门课程
  86. const blocks = rawHtml.split(/---------------------|----------------------/);
  87. blocks.forEach(block => {
  88. if (!block.trim()) return;
  89. const tempDiv = document.createElement('div');
  90. tempDiv.innerHTML = block;
  91. // 1. 提取课程名
  92. let name = "";
  93. for (let node of tempDiv.childNodes) {
  94. if (node.nodeType === 3 && node.textContent.trim() !== "") {
  95. name = node.textContent.trim();
  96. break;
  97. }
  98. }
  99. // 2. 提取教师
  100. const teacher = tempDiv.querySelector('font[title="老师"]')?.innerText.trim() || "未知教师";
  101. // 3. 提取地点与精准节次 (关键点)
  102. const locationFull = tempDiv.querySelector('font[title="教室"]')?.innerText || "未知地点";
  103. let startSection = 0;
  104. let endSection = 0;
  105. // 匹配内部的 [01-02]节 格式
  106. const sectionMatch = locationFull.match(/\[(\d+)-(\d+)\]节/);
  107. if (sectionMatch) {
  108. startSection = parseInt(sectionMatch[1], 10);
  109. endSection = parseInt(sectionMatch[2], 10);
  110. }
  111. // 清洗教室名
  112. const position = locationFull.replace(/\[\d+-\d+\]节$/, "").trim();
  113. // 4. 提取周次
  114. const weekStr = tempDiv.querySelector('font[title="周次(节次)"]')?.innerText || "";
  115. if (name && startSection > 0) {
  116. rawCourses.push({
  117. "name": name,
  118. "teacher": teacher,
  119. "weeks": parseWeeks(weekStr),
  120. "position": position,
  121. "day": day,
  122. "startSection": startSection,
  123. "endSection": endSection
  124. });
  125. }
  126. });
  127. });
  128. });
  129. // 调用合并函数
  130. return mergeAndDistinctCourses(rawCourses);
  131. }
  132. // 交互封装模块
  133. async function showWelcomeAlert() {
  134. return await window.AndroidBridgePromise.showAlert(
  135. "导入提示",
  136. "请确保已在内置浏览器中成功登录广西电力职业技术学院教务系统。",
  137. "开始导入"
  138. );
  139. }
  140. async function getSemesterParamsFromUser() {
  141. const currentYear = new Date().getFullYear();
  142. const year = await window.AndroidBridgePromise.showPrompt(
  143. "选择学年",
  144. "请输入起始学年(如2025代表2025-2026学年):",
  145. String(currentYear),
  146. "validateYearInput"
  147. );
  148. if (!year) return null;
  149. const semesterIndex = await window.AndroidBridgePromise.showSingleSelection(
  150. "选择学期",
  151. JSON.stringify(["第一学期", "第二学期"]),
  152. 0
  153. );
  154. if (semesterIndex === null) return null;
  155. // 拼接教务系统识别码
  156. return `${year}-${parseInt(year) + 1}-${semesterIndex + 1}`;
  157. }
  158. // 网络与存储封装模块
  159. async function fetchCourseHtml(semesterId) {
  160. AndroidBridge.showToast("正在请求课表数据,请稍候...");
  161. const response = await fetch("https://jw.vpn.gxdlxy.com/jsxsd/xskb/xskb_list.do", {
  162. method: "POST",
  163. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  164. body: `cj0701id=&zc=&demo=&xnxq01id=${semesterId}`,
  165. credentials: "include"
  166. });
  167. return await response.text();
  168. }
  169. async function saveCourseDataToApp(courses) {
  170. await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify({
  171. "semesterTotalWeeks": 20,
  172. "firstDayOfWeek": 1
  173. }));
  174. const timeSlots = [
  175. { "number": 1, "startTime": "08:30", "endTime": "09:10" },
  176. { "number": 2, "startTime": "09:20", "endTime": "10:00" },
  177. { "number": 3, "startTime": "10:20", "endTime": "11:00" },
  178. { "number": 4, "startTime": "11:10", "endTime": "11:50" },
  179. { "number": 5, "startTime": "14:30", "endTime": "15:10" },
  180. { "number": 6, "startTime": "15:20", "endTime": "16:00" },
  181. { "number": 7, "startTime": "16:10", "endTime": "16:50" },
  182. { "number": 8, "startTime": "16:50", "endTime": "17:30" },
  183. { "number": 9, "startTime": "19:40", "endTime": "20:20" },
  184. { "number": 10, "startTime": "20:30", "endTime": "21:10" }
  185. ];
  186. await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  187. // 保存最终课程
  188. return await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
  189. }
  190. // 流程控制模块
  191. async function runImportFlow() {
  192. try {
  193. const start = await showWelcomeAlert();
  194. if (!start) return;
  195. const semesterId = await getSemesterParamsFromUser();
  196. if (!semesterId) return;
  197. const html = await fetchCourseHtml(semesterId);
  198. const finalCourses = parseTimetableToModel(html);
  199. if (finalCourses.length === 0) {
  200. AndroidBridge.showToast("未发现课程,请检查学期选择或登录状态。");
  201. return;
  202. }
  203. await saveCourseDataToApp(finalCourses);
  204. AndroidBridge.showToast(`成功导入 ${finalCourses.length} 门课程`);
  205. AndroidBridge.notifyTaskCompletion();
  206. } catch (error) {
  207. console.error(error);
  208. AndroidBridge.showToast("导入异常: " + error.message);
  209. }
  210. }
  211. // 启动执行
  212. runImportFlow();