smxy.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // 三明学院 (fjsmu.edu.cn) 拾光课程表适配脚本
  2. // 基于正方教务系统V9标准接口适配(CAS SSO版)
  3. function mergeAndDistinctCourses(courses) {
  4. if (!Array.isArray(courses) || courses.length <= 1) return courses;
  5. const list = courses.map(c => ({
  6. ...c, name: c.name || '', teacher: c.teacher || '', position: c.position || '',
  7. weeks: Array.isArray(c.weeks) ? [...c.weeks].sort((a, b) => a - b) : []
  8. }));
  9. list.sort((a, b) => a.name.localeCompare(b.name) || a.teacher.localeCompare(b.teacher) ||
  10. a.position.localeCompare(b.position) || (a.day || 0) - (b.day || 0) ||
  11. a.weeks.join(',').localeCompare(b.weeks.join(',')) || (a.startSection || 0) - (b.startSection || 0));
  12. const step1 = [];
  13. let cur = list[0];
  14. for (let i = 1; i < list.length; i++) {
  15. const nxt = list[i];
  16. const same = cur.name === nxt.name && cur.teacher === nxt.teacher && cur.position === nxt.position &&
  17. cur.day === nxt.day && cur.weeks.join(',') === nxt.weeks.join(',');
  18. if (same && cur.endSection + 1 === nxt.startSection) cur.endSection = nxt.endSection;
  19. else if (same && cur.startSection === nxt.startSection && cur.endSection === nxt.endSection) continue;
  20. else { step1.push(cur); cur = nxt; }
  21. }
  22. step1.push(cur);
  23. step1.sort((a, b) => a.name.localeCompare(b.name) || a.teacher.localeCompare(b.teacher) ||
  24. a.position.localeCompare(b.position) || (a.day || 0) - (b.day || 0) ||
  25. (a.startSection || 0) - (b.startSection || 0) || (a.endSection || 0) - (b.endSection || 0));
  26. const step2 = [];
  27. cur = step1[0];
  28. for (let i = 1; i < step1.length; i++) {
  29. const nxt = step1[i];
  30. if (cur.name === nxt.name && cur.teacher === nxt.teacher && cur.position === nxt.position &&
  31. cur.day === nxt.day && cur.startSection === nxt.startSection && cur.endSection === nxt.endSection) {
  32. cur.weeks = Array.from(new Set([...cur.weeks, ...nxt.weeks])).sort((a, b) => a - b);
  33. } else { step2.push(cur); cur = nxt; }
  34. }
  35. step2.push(cur);
  36. return step2;
  37. }
  38. function parseWeeks(weekStr) {
  39. if (!weekStr) return [];
  40. const weeks = [];
  41. for (const set of weekStr.split(',')) {
  42. const s = set.trim();
  43. const rangeM = s.match(/(\d+)-(\d+)周/);
  44. const singleM = s.match(/^(\d+)周/);
  45. let start = 0, end = 0, ok = false;
  46. if (rangeM) { start = Number(rangeM[1]); end = Number(rangeM[2]); ok = true; }
  47. else if (singleM) { start = end = Number(singleM[1]); ok = true; }
  48. if (ok) {
  49. const isSingle = s.includes('(单)'), isDouble = s.includes('(双)');
  50. for (let w = start; w <= end; w++) {
  51. if (isSingle && w % 2 === 0) continue;
  52. if (isDouble && w % 2 !== 0) continue;
  53. weeks.push(w);
  54. }
  55. }
  56. }
  57. return [...new Set(weeks)].sort((a, b) => a - b);
  58. }
  59. function parseJsonData(jsonData) {
  60. if (!jsonData || !Array.isArray(jsonData.kbList)) return [];
  61. const list = [];
  62. for (const c of jsonData.kbList) {
  63. if (!c.kcmc || !c.xm || !c.cdmc || !c.xqj || !c.jcs || !c.zcd) continue;
  64. const weeks = parseWeeks(c.zcd);
  65. if (!weeks.length) continue;
  66. const parts = c.jcs.split('-');
  67. const start = Number(parts[0]), end = Number(parts[parts.length - 1]);
  68. const day = Number(c.xqj);
  69. if (isNaN(day) || isNaN(start) || isNaN(end) || day < 1 || day > 7 || start > end) continue;
  70. list.push({ name: c.kcmc.trim(), teacher: c.xm.trim(), position: c.cdmc.trim(),
  71. day, startSection: start, endSection: end, weeks });
  72. }
  73. return mergeAndDistinctCourses(list);
  74. }
  75. async function promptUserToStart() {
  76. return await window.shiguangBridgePromise.showAlert(
  77. "教务系统课表导入", "导入前请确保您已在浏览器中成功登录教务系统", "好的,开始导入");
  78. }
  79. async function fetchAcademicOptions() {
  80. const url = "/jwglxt/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N2151";
  81. try {
  82. const resp = await fetch(url, { method: "GET", credentials: "include" });
  83. if (!resp.ok) return null;
  84. const html = await resp.text();
  85. const doc = new DOMParser().parseFromString(html, "text/html");
  86. const years = Array.from(doc.querySelectorAll("#xnm option")).filter(o => o.value !== "")
  87. .map(o => ({ value: o.value, text: o.textContent.trim(), selected: o.hasAttribute("selected") }));
  88. const semesters = Array.from(doc.querySelectorAll("#xqm option")).filter(o => o.value !== "")
  89. .map(o => ({ value: o.value, text: o.textContent.trim(), selected: o.hasAttribute("selected") }));
  90. if (!years.length || !semesters.length) return null;
  91. const selIdx = years.findIndex(o => o.selected);
  92. const start = Math.max(0, (selIdx === -1 ? 0 : selIdx) - 2);
  93. const end = Math.min(years.length, (selIdx === -1 ? 0 : selIdx) + 3);
  94. return { yearOptions: years.slice(start, end), semesterOptions: semesters,
  95. defaultYearIndex: selIdx === -1 ? 0 : selIdx - start,
  96. defaultSemesterIndex: semesters.findIndex(o => o.selected) !== -1 ? semesters.findIndex(o => o.selected) : 0 };
  97. } catch (e) { return null; }
  98. }
  99. async function selectAcademicYearAndSemester() {
  100. const data = await fetchAcademicOptions();
  101. if (!data) { window.shiguangBridge.showToast("从教务系统读取学年学期失败,请确保已登录。"); return null; }
  102. const { yearOptions, semesterOptions, defaultYearIndex, defaultSemesterIndex } = data;
  103. const yearIdx = await window.shiguangBridgePromise.showSingleSelection(
  104. "选择学年", JSON.stringify(yearOptions.map(o => o.text)), defaultYearIndex);
  105. if (yearIdx === null || yearIdx === -1) return null;
  106. const semIdx = await window.shiguangBridgePromise.showSingleSelection(
  107. "选择学期", JSON.stringify(semesterOptions.map(o => o.text)), defaultSemesterIndex);
  108. if (semIdx === null || semIdx === -1) return null;
  109. return { academicYear: yearOptions[yearIdx].value, semesterCode: semesterOptions[semIdx].value };
  110. }
  111. async function fetchSemesterStartDate(academicYear, semesterCode) {
  112. const url = "/jwglxt/kbcx/xskbcxZccx_cxZcByXnxq.html?gnmkdm=N2154";
  113. try {
  114. const resp = await fetch(url, {
  115. method: "POST",
  116. headers: { "accept": "application/json, text/javascript, */*; q=0.01",
  117. "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
  118. "x-requested-with": "XMLHttpRequest" },
  119. body: `xnm=${academicYear}&xqm=${semesterCode}`,
  120. credentials: "include"
  121. });
  122. if (resp.ok) {
  123. const json = await resp.json();
  124. if (Array.isArray(json) && json.length > 0) {
  125. const first = json.find(i => String(i.zs) === "1" || String(i.zsmc) === "1") || json[0];
  126. if (first.rq) { const d = first.rq.split('/')[0]; if (/^\d{4}-\d{2}-\d{2}$/.test(d)) return d; }
  127. if (first.zcrq) { const m = first.zcrq.match(/(\d{4}-\d{2}-\d{2})/); if (m) return m[1]; }
  128. }
  129. }
  130. } catch (e) {}
  131. return null;
  132. }
  133. async function fetchAndParseCourses(academicYear, semesterCode) {
  134. const url = "/jwglxt/kbcx/xskbcx_cxXsgrkb.html?gnmkdm=N2151";
  135. const [resp, startDate] = await Promise.all([
  136. fetch(url, { method: "POST",
  137. headers: { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" },
  138. body: `xnm=${academicYear}&xqm=${semesterCode}&kzlx=ck&xsdm=&kclbdm=`,
  139. credentials: "include" }),
  140. fetchSemesterStartDate(academicYear, semesterCode)
  141. ]);
  142. try {
  143. if (resp.ok) {
  144. const json = await resp.json();
  145. if (json && json.kbList) {
  146. const courses = parseJsonData(json);
  147. if (courses.length > 0) return { courses, config: { semesterStartDate: startDate, semesterTotalWeeks: 20 } };
  148. }
  149. }
  150. } catch (e) {}
  151. window.shiguangBridge.showToast("未能获取课表数据,请检查网络环境或登录状态。");
  152. return null;
  153. }
  154. async function saveCourses(courses) {
  155. try { await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(courses)); return true; }
  156. catch (e) { window.shiguangBridge.showToast("课程保存失败: " + e.message); return false; }
  157. }
  158. async function runImportFlow() {
  159. const ok = await promptUserToStart();
  160. if (!ok) { window.shiguangBridge.showToast("用户取消了导入。"); return; }
  161. const sel = await selectAcademicYearAndSemester();
  162. if (!sel) { window.shiguangBridge.showToast("未选择学年学期,导入终止。"); return; }
  163. const result = await fetchAndParseCourses(sel.academicYear, sel.semesterCode);
  164. if (!result) return;
  165. const saved = await saveCourses(result.courses);
  166. if (!saved) return;
  167. try { await window.shiguangBridgePromise.saveCourseConfig(JSON.stringify(result.config)); } catch (e) {}
  168. window.shiguangBridge.showToast("课程导入成功,共导入 " + result.courses.length + " 门课程!");
  169. window.shiguangBridge.notifyTaskCompletion();
  170. }
  171. runImportFlow();