hnsf_01.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.AndroidBridgePromise.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.AndroidBridgePromise.showPrompt(
  91.         "选择学年",
  92.         "请输入要导入课程的学年(例如 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.AndroidBridgePromise.showSingleSelection(
  101.         "选择学期",
  102.         JSON.stringify(semesters),
  103.         0
  104.     );
  105.     return semesterIndex;
  106. }
  107. async function fetchAndParseCourses(academicYear, semesterIndex) {
  108. AndroidBridge.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. AndroidBridge.showToast("数据返回格式错误,可能是您未成功登录或会话已过期。");
  135. return null;
  136. }
  137. const courses = parseJsonData(jsonData);
  138. if (courses.length === 0) {
  139. AndroidBridge.showToast("未找到任何课程数据,请检查所选学年学期是否正确或本学期无课。");
  140. return null;
  141. }
  142. console.log(`JS: 课程数据解析成功,共找到 ${courses.length} 门课程。`);
  143. return { courses: courses };
  144. } catch (error) {
  145. AndroidBridge.showToast(`请求或解析失败: ${error.message}`);
  146. console.error('JS: Fetch/Parse Error:', error);
  147. return null;
  148. }
  149. }
  150. async function saveCourses(parsedCourses) {
  151.     AndroidBridge.showToast(`正在保存 ${parsedCourses.length} 门课程...`);
  152.     console.log(`JS: 尝试保存 ${parsedCourses.length} 门课程...`);
  153.     try {
  154.         await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses, null, 2));
  155.         console.log("JS: 课程保存成功!");
  156.         return true;
  157.     } catch (error) {
  158.         AndroidBridge.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. async function selectTimeSlotsType() {
  188.     const timeSlotsOptions = ["非夏季作息", "夏季作息"];
  189.     console.log("JS: 提示用户选择作息时间类型。");
  190.     const selectedIndex = await window.AndroidBridgePromise.showSingleSelection(
  191.         "选择作息时间",
  192.         JSON.stringify(timeSlotsOptions),
  193.         0
  194.     );
  195.     return selectedIndex;
  196. }
  197. async function importPresetTimeSlots(timeSlots) {
  198.     console.log(`JS: 准备导入 ${timeSlots.length} 个预设时间段。`);
  199.     if (timeSlots.length > 0) {
  200.         AndroidBridge.showToast(`正在导入 ${timeSlots.length} 个预设时间段...`);
  201.         try {
  202.             await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  203.             AndroidBridge.showToast("预设时间段导入成功!");
  204.             console.log("JS: 预设时间段导入成功。");
  205.         } catch (error) {
  206.             AndroidBridge.showToast("导入时间段失败: " + error.message);
  207.             console.error('JS: Save Time Slots Error:', error);
  208.         }
  209.     } else {
  210.         AndroidBridge.showToast("警告:时间段为空,未导入时间段信息。");
  211.         console.warn("JS: 警告:传入时间段为空,未导入时间段信息。");
  212.     }
  213. }
  214. async function runImportFlow() {
  215.     if (isLoginPage()) {
  216.         AndroidBridge.showToast("导入失败:请先登录教务系统!");
  217.         console.log("JS: 检测到当前在登录页面,终止导入。");
  218.         return;
  219.     }
  220.     // 1. 公告和前置检查。
  221.     const alertConfirmed = await promptUserToStart();
  222.     if (!alertConfirmed) {
  223.         AndroidBridge.showToast("用户取消了导入。");
  224.         console.log("JS: 用户取消了导入流程。");
  225.         return;
  226.     }
  227.     const academicYear = await getAcademicYear();
  228.     if (academicYear === null) {
  229.         AndroidBridge.showToast("导入已取消。");
  230.         console.log("JS: 获取学年失败/取消,流程终止。");
  231.         return;
  232.     }
  233.     console.log(`JS: 已选择学年: ${academicYear}`);
  234.     const semesterIndex = await selectSemester();
  235.     if (semesterIndex === null || semesterIndex === -1) {
  236.         AndroidBridge.showToast("导入已取消。");
  237.         console.log("JS: 选择学期失败/取消,流程终止。");
  238.         return;
  239.     }
  240.     console.log(`JS: 已选择学期索引: ${semesterIndex}`);
  241.     // 2. 获取并解析课程
  242.     const result = await fetchAndParseCourses(academicYear, semesterIndex);
  243.     if (result === null) {
  244.         console.log("JS: 课程获取或解析失败,流程终止。");
  245.         return;
  246.     }
  247.     const { courses } = result;
  248.     // 3. 课程数据保存。
  249.     const saveResult = await saveCourses(courses);
  250.     if (!saveResult) {
  251.         console.log("JS: 课程保存失败,流程终止。");
  252.         return;
  253.     }
  254.     // 4. 作息时间选择与导入
  255.     const timeSlotsIndex = await selectTimeSlotsType();
  256.     let selectedTimeSlots = [];
  257.     if (timeSlotsIndex === 0) {
  258.         // 0: 非夏季作息
  259.         selectedTimeSlots = Non_summerTimeSlots;
  260.         console.log("JS: 已选择非夏季作息。");
  261.     } else if (timeSlotsIndex === 1) {
  262.         // 1: 夏季作息
  263.         selectedTimeSlots = SummerTimeSlots;
  264.         console.log("JS: 已选择夏季作息。");
  265.     } else {
  266.         selectedTimeSlots = Non_summerTimeSlots;
  267.         console.warn("JS: 作息时间选择失败/取消,使用非夏季作息作为默认值。");
  268.     }
  269.     await importPresetTimeSlots(selectedTimeSlots);
  270.     AndroidBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
  271.     console.log("JS: 整个导入流程执行完毕并成功。");
  272.     AndroidBridge.notifyTaskCompletion();
  273. }
  274. runImportFlow();