hnsf_01.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // 河南师范大学(htu.edu.cn) 拾光课程表适配脚本
  2. // 非该大学开发者适配,开发者无法及时发现问题
  3. // 出现问题请提联系开发者或者提交pr更改,这更加快速
  4. function parseWeeks(weekStr) {
  5. if (!weekStr) return [];
  6. // 假设新接口的 'zc' 字段已经是逗号分隔的周次数字字符串
  7. const weeks = weekStr.split(',')
  8. .map(w => Number(w.trim())) // 转换为数字
  9. .filter(w => !isNaN(w) && w > 0); // 过滤掉无效数字
  10. // 去重并排序
  11. return [...new Set(weeks)].sort((a, b) => a - b);
  12. }
  13. function parseJsonData(jsonData) {
  14. console.log("JS: parseJsonData 正在解析 JSON 数据...");
  15. // 检查JSON结构
  16. if (!jsonData || jsonData.code !== 0 || !Array.isArray(jsonData.data)) {
  17. console.warn("JS: JSON 数据结构错误或code不为0。");
  18. return [];
  19. }
  20. const rawCourseList = jsonData.data;
  21. const finalCourseList = [];
  22. for (const rawCourse of rawCourseList) {
  23. // 关键字段检查
  24. if (!rawCourse.kcmc || !rawCourse.teaxms || !rawCourse.jxcdmc ||
  25. !rawCourse.xq || !rawCourse.ps || !rawCourse.pe || !rawCourse.zc) {
  26. // console.warn("JS: 发现缺少关键字段的课程记录,跳过。", rawCourse);
  27. continue;
  28. }
  29. const weeksArray = parseWeeks(rawCourse.zc);
  30. // 周次有效性检查
  31. if (weeksArray.length === 0) {
  32. // console.warn(`JS: 课程 ${rawCourse.kcmc} 周次解析失败或为空,跳过。`);
  33. continue;
  34. }
  35. const day = Number(rawCourse.xq); // xq: 星期几 (周一为1)
  36. const startSection = Number(rawCourse.ps);
  37. const endSection = Number(rawCourse.pe);
  38. // 数字有效性检查
  39. if (isNaN(day) || isNaN(startSection) || isNaN(endSection) || day < 1 || day > 7 || startSection > endSection) {
  40. // console.warn(`JS: 课程 ${rawCourse.kcmc} 星期或节次数据无效,跳过。`);
  41. continue;
  42. }
  43. finalCourseList.push({
  44. name: rawCourse.kcmc.trim(),
  45. teacher: rawCourse.teaxms.trim(),
  46. position: rawCourse.jxcdmc.trim(),
  47. day: day,
  48. startSection: startSection,
  49. endSection: endSection,
  50. weeks: weeksArray
  51. });
  52. }
  53. finalCourseList.sort((a, b) =>
  54. a.day - b.day ||
  55. a.startSection - b.startSection ||
  56. a.name.localeCompare(b.name)
  57. );
  58. console.log(`JS: JSON 数据解析完成,共找到 ${finalCourseList.length} 门课程。`);
  59. return finalCourseList;
  60. }
  61. function isLoginPage() {
  62. const url = window.location.href;
  63. const expectedBaseUrl = 'https://jwc.htu.edu.cn';
  64. const cleanUrl = url.endsWith('/') ? url.slice(0, -1) : url;
  65. const isRootLogin = cleanUrl === expectedBaseUrl;
  66. const isKeywordLogin = url.includes('login') || url.includes('cas');
  67. return url.includes('htu.edu.cn') && (isRootLogin || isKeywordLogin);
  68. }
  69. function validateYearInput(input) {
  70.     console.log("JS: validateYearInput 被调用,输入: " + input);
  71.     if (/^[0-9]{4}$/.test(input)) {
  72.         console.log("JS: validateYearInput 验证通过。");
  73.         return false;
  74.     } else {
  75.         console.log("JS: validateYearInput 验证失败。");
  76.         return "请输入四位数字的学年!";
  77.     }
  78. }
  79. async function promptUserToStart() {
  80.     console.log("JS: 流程开始:显示公告。");
  81.     return await window.shiguangBridgePromise.showAlert(
  82.         "教务系统课表导入",
  83.         "导入前请确保您已在浏览器中成功登录教务系统",
  84.         "好的,开始导入"
  85.     );
  86. }
  87. async function getAcademicYear() {
  88.     const currentYear = new Date().getFullYear().toString();
  89.     console.log("JS: 提示用户输入学年。");
  90.     return await window.shiguangBridgePromise.showPrompt(
  91.         "选择学年",
  92.         "请输入要导入课程的起始学年(例如 2025-2026 应输入2025):",
  93.         currentYear,
  94.         "validateYearInput"
  95.     );
  96. }
  97. async function selectSemester() {
  98.     const semesters = ["第一学期 (01)", "第二学期 (02)"];
  99.     console.log("JS: 提示用户选择学期。");
  100.     const semesterIndex = await window.shiguangBridgePromise.showSingleSelection(
  101.         "选择学期",
  102.         JSON.stringify(semesters),
  103.         0
  104.     );
  105.     return semesterIndex;
  106. }
  107. async function fetchAndParseCourses(academicYear, semesterIndex) {
  108. window.shiguangBridge.showToast("正在请求课表数据...");
  109. // 学年学期代码组合:202501 (第一学期), 202502 (第二学期)
  110. // semesterIndex 0 对应 01, 1 对应 02
  111. const semesterCode = semesterIndex === 0 ? "01" : "02";
  112. const xnxqdmBody = `xnxqdm=${academicYear}${semesterCode}`;
  113. const url = "https://jwc.htu.edu.cn/new/student/xsgrkb/getCalendarWeekDatas";
  114. console.log(`JS: 发送请求到 ${url}, body: ${xnxqdmBody}`);
  115. const requestOptions = {
  116. "headers": {
  117. "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  118. },
  119. "body": xnxqdmBody,
  120. "method": "POST",
  121. "credentials": "include"
  122. };
  123. try {
  124. const response = await fetch(url, requestOptions);
  125. if (!response.ok) {
  126. throw new Error(`网络请求失败。状态码: ${response.status} (${response.statusText})`);
  127. }
  128. const jsonText = await response.text();
  129. let jsonData;
  130. try {
  131. jsonData = JSON.parse(jsonText);
  132. } catch (e) {
  133. console.error('JS: JSON 解析失败:', e);
  134. window.shiguangBridge.showToast("数据返回格式错误,可能是您未成功登录或会话已过期。");
  135. return null;
  136. }
  137. const courses = parseJsonData(jsonData);
  138. if (courses.length === 0) {
  139. window.shiguangBridge.showToast("未找到任何课程数据,请检查所选学年学期是否正确或本学期无课。");
  140. return null;
  141. }
  142. console.log(`JS: 课程数据解析成功,共找到 ${courses.length} 门课程。`);
  143. return { courses: courses };
  144. } catch (error) {
  145. window.shiguangBridge.showToast(`请求或解析失败: ${error.message}`);
  146. console.error('JS: Fetch/Parse Error:', error);
  147. return null;
  148. }
  149. }
  150. async function saveCourses(parsedCourses) {
  151.     window.shiguangBridge.showToast(`正在保存 ${parsedCourses.length} 门课程...`);
  152.     console.log(`JS: 尝试保存 ${parsedCourses.length} 门课程...`);
  153.     try {
  154.         await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses, null, 2));
  155.         console.log("JS: 课程保存成功!");
  156.         return true;
  157.     } catch (error) {
  158.         window.shiguangBridge.showToast(`课程保存失败: ${error.message}`);
  159.         console.error('JS: Save Courses Error:', error);
  160.         return false;
  161.     }
  162. }
  163. const Non_summerTimeSlots = [
  164.     { number: 1, startTime: "08:00", endTime: "08:45" },
  165.     { number: 2, startTime: "08:55", endTime: "09:40" },
  166.     { number: 3, startTime: "10:10", endTime: "10:55" },
  167.     { number: 4, startTime: "11:05", endTime: "11:50" },
  168.     { number: 5, startTime: "14:30", endTime: "15:15" },
  169.     { number: 6, startTime: "15:25", endTime: "16:10" },
  170.     { number: 7, startTime: "16:40", endTime: "17:25" },
  171.     { number: 8, startTime: "17:35", "endTime": "18:20" },
  172.     { number: 9, startTime: "19:30", "endTime": "20:15" },
  173.     { number: 10, startTime: "20:25", "endTime": "21:10" }
  174. ];
  175. const SummerTimeSlots = [
  176.     { number: 1, startTime: "08:00", endTime: "08:45" },
  177.     { number: 2, startTime: "08:55", endTime: "09:40" },
  178.     { number: 3, startTime: "10:10", endTime: "10:55" },
  179.     { number: 4, startTime: "11:05", endTime: "11:50" },
  180.     { number: 5, startTime: "15:00", endTime: "15:45" },
  181.     { number: 6, startTime: "15:55", endTime: "16:40" },
  182.     { number: 7, startTime: "17:10", endTime: "17:55" },
  183.     { number: 8, startTime: "18:05", "endTime": "18:50" },
  184.     { number: 9, startTime: "20:00", "endTime": "20:45" },
  185.     { number: 10, startTime: "20:55", "endTime": "21:40" }
  186. ];
  187. const NewCampusTimeSlots = [
  188. { number: 1, startTime: "08:30", endTime: "09:15" },
  189.     { number: 2, startTime: "09:25", endTime: "10:10" },
  190.     { number: 3, startTime: "10:30", endTime: "11:15" },
  191.     { number: 4, startTime: "11:25", endTime: "12:10" },
  192.     { number: 5, startTime: "14:00", endTime: "14:45" },
  193.     { number: 6, startTime: "14:55", endTime: "15:40" },
  194.     { number: 7, startTime: "16:00", endTime: "16:45" },
  195.     { number: 8, startTime: "16:55", "endTime": "17:40" },
  196.     { number: 9, startTime: "18:40", "endTime": "19:25" },
  197.     { number: 10, startTime: "19:35", "endTime": "20:20" }
  198. ]
  199. async function selectCampus() {
  200. const campusOptions = ["建设东路校区/平原湖校区", "创新港校区"]
  201. console.log("JS: 提示用户选择校区。");
  202. const selectedIndex = await window.shiguangBridgePromise.showSingleSelection(
  203. "选择校区",
  204. JSON.stringify(campusOptions),
  205. 0
  206. );
  207. return selectedIndex;
  208. }
  209. async function selectTimeSlotsType() {
  210.     const timeSlotsOptions = ["非夏季作息", "夏季作息"];
  211.     console.log("JS: 提示用户选择作息时间类型。");
  212.     const selectedIndex = await window.shiguangBridgePromise.showSingleSelection(
  213.         "选择作息时间",
  214.         JSON.stringify(timeSlotsOptions),
  215.         0
  216.     );
  217.     return selectedIndex;
  218. }
  219. async function importPresetTimeSlots(timeSlots) {
  220.     console.log(`JS: 准备导入 ${timeSlots.length} 个预设时间段。`);
  221.     if (timeSlots.length > 0) {
  222.         window.shiguangBridge.showToast(`正在导入 ${timeSlots.length} 个预设时间段...`);
  223.         try {
  224.             await window.shiguangBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  225.             window.shiguangBridge.showToast("预设时间段导入成功!");
  226.             console.log("JS: 预设时间段导入成功。");
  227.         } catch (error) {
  228.             window.shiguangBridge.showToast("导入时间段失败: " + error.message);
  229.             console.error('JS: Save Time Slots Error:', error);
  230.         }
  231.     } else {
  232.         window.shiguangBridge.showToast("警告:时间段为空,未导入时间段信息。");
  233.         console.warn("JS: 警告:传入时间段为空,未导入时间段信息。");
  234.     }
  235. }
  236. async function runImportFlow() {
  237.     if (isLoginPage()) {
  238.         window.shiguangBridge.showToast("导入失败:请先登录教务系统!");
  239.         console.log("JS: 检测到当前在登录页面,终止导入。");
  240.         return;
  241.     }
  242.     // 1. 公告和前置检查。
  243.     const alertConfirmed = await promptUserToStart();
  244.     if (!alertConfirmed) {
  245.         window.shiguangBridge.showToast("用户取消了导入。");
  246.         console.log("JS: 用户取消了导入流程。");
  247.         return;
  248.     }
  249.     const academicYear = await getAcademicYear();
  250.     if (academicYear === null) {
  251.         window.shiguangBridge.showToast("导入已取消。");
  252.         console.log("JS: 获取学年失败/取消,流程终止。");
  253.         return;
  254.     }
  255.     console.log(`JS: 已选择学年: ${academicYear}`);
  256.     const semesterIndex = await selectSemester();
  257.     if (semesterIndex === null || semesterIndex === -1) {
  258.         window.shiguangBridge.showToast("导入已取消。");
  259.         console.log("JS: 选择学期失败/取消,流程终止。");
  260.         return;
  261.     }
  262.     console.log(`JS: 已选择学期索引: ${semesterIndex}`);
  263.     // 2. 获取并解析课程
  264.     const result = await fetchAndParseCourses(academicYear, semesterIndex);
  265.     if (result === null) {
  266.         console.log("JS: 课程获取或解析失败,流程终止。");
  267.         return;
  268.     }
  269.     const { courses } = result;
  270.     // 3. 课程数据保存。
  271.     const saveResult = await saveCourses(courses);
  272.     if (!saveResult) {
  273.         console.log("JS: 课程保存失败,流程终止。");
  274.         return;
  275.     }
  276.     // 4. 作息时间选择与导入
  277. const campusIndex = await selectCampus();
  278. let selectedTimeSlots = [];
  279. if (campusIndex === 0) {
  280. // 0: 建设东路校区/平原湖校区
  281. const timeSlotsIndex = await selectTimeSlotsType();
  282. if (timeSlotsIndex === 0) {
  283. // 0: 非夏季作息
  284. selectedTimeSlots = Non_summerTimeSlots;
  285. console.log("JS: 已选择非夏季作息。");
  286. } else if (timeSlotsIndex === 1) {
  287. // 1: 夏季作息
  288. selectedTimeSlots = SummerTimeSlots;
  289. console.log("JS: 已选择夏季作息。");
  290. } else {
  291. selectedTimeSlots = Non_summerTimeSlots;
  292. console.warn("JS: 作息时间选择失败/取消,使用非夏季作息作为默认值。");
  293. }
  294. } else if (campusIndex === 1) {
  295. // 1: 创新港校区
  296. selectedTimeSlots = NewCampusTimeSlots;
  297. console.log("JS: 已选择创新港校区作息。");
  298. }
  299.     await importPresetTimeSlots(selectedTimeSlots);
  300.     window.shiguangBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
  301.     console.log("JS: 整个导入流程执行完毕并成功。");
  302.     window.shiguangBridge.notifyTaskCompletion();
  303. }
  304. runImportFlow();