hnnu_01.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // 淮南师范学院 (xxmh.hnnu.edu.cn) 库表图融合门户 拾光课程表适配脚本
  2. // ==================== 工具函数 ====================
  3. // 解析周次字符串 -> 数字数组
  4. function parseWeeks(weekStr) {
  5. const weeks = new Set();
  6. if (!weekStr) return [];
  7. const parts = weekStr.split(",");
  8. for (const part of parts) {
  9. const trimmed = part.trim();
  10. if (trimmed === "") continue;
  11. if (trimmed.includes("-")) {
  12. const [start, end] = trimmed.split("-").map(n => parseInt(n));
  13. if (!isNaN(start) && !isNaN(end)) {
  14. for (let i = start; i <= end; i++) {
  15. weeks.add(i);
  16. }
  17. }
  18. } else {
  19. const n = parseInt(trimmed);
  20. if (!isNaN(n) && n >= 1 && n <= 30) {
  21. weeks.add(n);
  22. }
  23. }
  24. }
  25. return Array.from(weeks).sort((a, b) => a - b);
  26. }
  27. // 检查是否已登录
  28. function isUserLoggedIn() {
  29. const token = localStorage.getItem("token");
  30. return token !== null && token !== "";
  31. }
  32. // 合并课程(相同课程合并周次)
  33. function mergeCourses(allCourses) {
  34. const merged = {};
  35. for (const course of allCourses) {
  36. const key = `${course.day}-${course.startSection}-${course.name}`;
  37. if (merged[key]) {
  38. merged[key].weeks = Array.from(new Set([...merged[key].weeks, ...course.weeks])).sort((a, b) => a - b);
  39. } else {
  40. merged[key] = { ...course, weeks: [...course.weeks] };
  41. }
  42. }
  43. return Object.values(merged);
  44. }
  45. // ==================== 预设时间段 ====================
  46. const presetTimeSlots = [
  47. { number: 1, startTime: "08:00", endTime: "08:45" },
  48. { number: 2, startTime: "08:55", endTime: "09:40" },
  49. { number: 3, startTime: "10:00", endTime: "10:45" },
  50. { number: 4, startTime: "10:55", endTime: "11:40" },
  51. { number: 5, startTime: "14:00", endTime: "14:45" },
  52. { number: 6, startTime: "14:55", endTime: "15:40" },
  53. { number: 7, startTime: "16:00", endTime: "16:45" },
  54. { number: 8, startTime: "16:55", endTime: "17:40" },
  55. { number: 9, startTime: "18:30", endTime: "19:15" },
  56. { number: 10, startTime: "19:25", endTime: "20:10" },
  57. { number: 11, startTime: "20:20", endTime: "21:05" }
  58. ];
  59. async function importPresetTimeSlots() {
  60. try {
  61. await window.shiguangBridgePromise.savePresetTimeSlots(JSON.stringify(presetTimeSlots));
  62. window.shiguangBridge.showToast("预设时间段导入成功!");
  63. } catch (error) {
  64. window.shiguangBridge.showToast("导入时间段失败: " + error.message);
  65. }
  66. }
  67. // ==================== 主流程 ====================
  68. async function runImportFlow() {
  69. try {
  70. // 1. 检查是否已登录
  71. if (!isUserLoggedIn()) {
  72. window.shiguangBridge.showToast("请先登录教务系统后再导入课程!");
  73. return;
  74. }
  75. // 2. 获取当前学期信息
  76. window.shiguangBridge.showToast("正在获取学期信息...");
  77. const nowTermRes = await fetch("/zhxyApi/term/rhptTerm/get/nowTerm", {
  78. method: "GET",
  79. headers: {
  80. "X-Access-Token": localStorage.getItem("token")
  81. }
  82. });
  83. const nowTermData = await nowTermRes.json();
  84. if (!nowTermData.success || !nowTermData.result) {
  85. window.shiguangBridge.showToast("获取学期信息失败,请重试!");
  86. return;
  87. }
  88. const { schoolYearKey, nowDate } = nowTermData.result;
  89. // 3. 让用户选择学期(因为API不返回termCode)
  90. const semesterOptions = ["第一学期 (autumn)", "第二学期 (spring)"];
  91. const semesterIndex = await window.shiguangBridgePromise.showSingleSelection(
  92. "选择学期",
  93. JSON.stringify(semesterOptions),
  94. 0
  95. );
  96. if (semesterIndex === null || semesterIndex === undefined) {
  97. window.shiguangBridge.showToast("导入已取消。");
  98. return;
  99. }
  100. const termCode = semesterIndex === 0 ? "autumn" : "spring";
  101. // 4. 遍历所有周次(1-20周)获取课表
  102. window.shiguangBridge.showToast("正在遍历获取所有周次课表...");
  103. const allCourses = [];
  104. const totalWeeks = 20;
  105. for (let week = 1; week <= totalWeeks; week++) {
  106. try {
  107. const scheduleUrl = `/zhxyApi/workhall/api/weeklySchedule?_t=${Date.now()}&schoolYear=${schoolYearKey}&termCode=${termCode}&count=${week}`;
  108. const scheduleRes = await fetch(scheduleUrl, {
  109. method: "GET",
  110. headers: {
  111. "X-Access-Token": localStorage.getItem("token")
  112. }
  113. });
  114. const scheduleData = await scheduleRes.json();
  115. if (scheduleData.success && Array.isArray(scheduleData.result)) {
  116. for (const item of scheduleData.result) {
  117. const day = parseInt(item.WEEK);
  118. if (isNaN(day) || day < 1 || day > 7) continue;
  119. const startSection = parseInt(item.SESSION);
  120. if (isNaN(startSection) || startSection < 1) continue;
  121. const continuedSession = parseInt(item.continuedSession) || 1;
  122. const endSection = startSection + continuedSession - 1;
  123. const weeks = parseWeeks(item.count);
  124. if (weeks.length === 0) continue;
  125. allCourses.push({
  126. name: item.courseName || "",
  127. teacher: item.teacherName || "",
  128. position: item.address || "",
  129. day: day,
  130. startSection: startSection,
  131. endSection: endSection,
  132. weeks: weeks
  133. });
  134. }
  135. }
  136. } catch (e) {
  137. console.warn(`第${week}周获取失败:`, e);
  138. }
  139. }
  140. // 5. 合并课程
  141. const mergedCourses = mergeCourses(allCourses);
  142. if (mergedCourses.length === 0) {
  143. window.shiguangBridge.showToast("未找到任何课程数据!");
  144. return;
  145. }
  146. // 6. 按星期和节次排序
  147. mergedCourses.sort((a, b) => a.day - b.day || a.startSection - b.startSection);
  148. // 7. 保存课程
  149. window.shiguangBridge.showToast(`共解析到${mergedCourses.length}门课程,正在导入...`);
  150. await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(mergedCourses));
  151. await importPresetTimeSlots();
  152. window.shiguangBridge.showToast(`课程导入成功!共${mergedCourses.length}门课程`);
  153. window.shiguangBridge.notifyTaskCompletion();
  154. } catch (error) {
  155. console.error("导入失败:", error);
  156. window.shiguangBridge.showToast("导入失败:" + error.message);
  157. }
  158. }
  159. // 启动
  160. runImportFlow();