qqhrit.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // 齐齐哈尔工程学院(qqhrit.com)拾光课程表适配脚本
  2. // 基于正方教务系统接口适配
  3. // 非该大学开发者适配,开发者无法及时发现问题
  4. // 出现问题请提issues或者提交pr更改,这更加快速
  5. /**
  6. * 解析周次字符串,处理单双周和周次范围。
  7. */
  8. function parseWeeks(weekStr) {
  9. if (!weekStr) return [];
  10. const weekSets = weekStr.split(',');
  11. let weeks = [];
  12. for (const set of weekSets) {
  13. const trimmedSet = set.trim();
  14. const rangeMatch = trimmedSet.match(/(\d+)-(\d+)周/);
  15. const singleMatch = trimmedSet.match(/^(\d+)周/); // 匹配以数字周结束的
  16. let start = 0;
  17. let end = 0;
  18. let processed = false;
  19. if (rangeMatch) { // 范围, 如 "1-5周"
  20. start = Number(rangeMatch[1]);
  21. end = Number(rangeMatch[2]);
  22. processed = true;
  23. } else if (singleMatch) { // 单个周, 如 "6周"
  24. start = end = Number(singleMatch[1]);
  25. processed = true;
  26. }
  27. if (processed) {
  28. // 确定单双周
  29. const isSingle = trimmedSet.includes('(单)');
  30. const isDouble = trimmedSet.includes('(双)');
  31. for (let w = start; w <= end; w++) {
  32. if (isSingle && w % 2 === 0) continue; // 单周跳过偶数
  33. if (isDouble && w % 2 !== 0) continue; // 双周跳过奇数
  34. weeks.push(w);
  35. }
  36. }
  37. }
  38. // 去重并排序
  39. return [...new Set(weeks)].sort((a, b) => a - b);
  40. }
  41. /**
  42. * 独立处理课程时间逻辑的函数
  43. */
  44. function applyCustomTimeLogic(courses) {
  45. // 方案1:周末全天
  46. const TIME_SCHEME_1 = [
  47. { number: 1, startTime: "08:20", endTime: "09:05" },
  48. { number: 2, startTime: "09:05", endTime: "09:50" },
  49. { number: 3, startTime: "10:00", endTime: "10:45" },
  50. { number: 4, startTime: "10:45", endTime: "11:30" },
  51. { number: 5, startTime: "13:30", endTime: "14:15" },
  52. { number: 6, startTime: "14:15", endTime: "15:00" },
  53. { number: 7, startTime: "15:20", endTime: "16:05" },
  54. { number: 8, startTime: "16:05", endTime: "16:50" },
  55. { number: 9, startTime: "18:00", endTime: "18:45" },
  56. { number: 10, startTime: "18:45", endTime: "19:30" },
  57. { number: 11, startTime: "19:30", endTime: "20:15" }
  58. ];
  59. // 方案2:2#/3# 专用(3-4节)
  60. const TIME_SCHEME_2 = [
  61. { number: 3, startTime: "10:20", endTime: "11:05" },
  62. { number: 4, startTime: "11:15", endTime: "12:00" }
  63. ];
  64. // 方案3:图/馆/齐三机床 专用(3-4节)
  65. const TIME_SCHEME_3 = [
  66. { number: 3, startTime: "09:55", endTime: "10:40" },
  67. { number: 4, startTime: "10:50", endTime: "11:35" }
  68. ];
  69. // 辅助函数:根据节次从方案中提取时间
  70. const getTimeFromScheme = (scheme, section) => {
  71. const slot = scheme.find(s => s.number === section);
  72. return slot ? [slot.startTime, slot.endTime] : null;
  73. };
  74. return courses.map(course => {
  75. const is23Sharp = /(2#|3#)/.test(course.position);
  76. const isLibMachine = /(图|馆|齐三机床)/.test(course.position);
  77. const isWeekend = (course.day === 6 || course.day === 7);
  78. const isSpecialPos = (is23Sharp || isLibMachine);
  79. const isEndpoint3or4 = (course.startSection === 3 || course.startSection === 4 ||
  80. course.endSection === 3 || course.endSection === 4);
  81. const shouldApplyCustom = isWeekend || (isSpecialPos && isEndpoint3or4);
  82. if (shouldApplyCustom) {
  83. let startSlot, endSlot;
  84. if (isWeekend) {
  85. startSlot = TIME_SCHEME_1.find(s => s.number === course.startSection);
  86. endSlot = TIME_SCHEME_1.find(s => s.number === course.endSection);
  87. } else {
  88. // 工作日特殊教室逻辑
  89. const scheme = is23Sharp ? TIME_SCHEME_2 : TIME_SCHEME_3;
  90. // 辅助函数:如果是 3 或 4 节则取特殊方案,否则取全局标准时间
  91. const getSectionTime = (sec) => {
  92. if (sec === 3 || sec === 4) {
  93. return scheme.find(s => s.number === sec);
  94. }
  95. return TimeSlots.find(s => s.number === sec);
  96. };
  97. startSlot = getSectionTime(course.startSection);
  98. endSlot = getSectionTime(course.endSection);
  99. }
  100. // 如果成功找到了对应的开始和结束时间,则应用自定义时间
  101. if (startSlot && endSlot) {
  102. return {
  103. ...course,
  104. isCustomTime: true,
  105. customStartTime: startSlot.startTime,
  106. customEndTime: endSlot.endTime
  107. };
  108. }
  109. }
  110. return course;
  111. });
  112. }
  113. /**
  114. * 解析 API 返回的 JSON 数据。
  115. */
  116. function parseJsonData(jsonData) {
  117. console.log("JS: parseJsonData 正在解析 JSON 数据...");
  118. // 检查JSON结构:新的数据在 kbList 字段中
  119. if (!jsonData || !Array.isArray(jsonData.kbList)) {
  120. console.warn("JS: JSON 数据结构错误或缺少 kbList 字段。");
  121. return [];
  122. }
  123. const rawCourseList = jsonData.kbList;
  124. const finalCourseList = [];
  125. for (const rawCourse of rawCourseList) {
  126. // 关键字段检查: kcmc(课名), xm(教师), cdmc(教室), xqj(星期), jcs(节次范围), zcd(周次描述)
  127. if (!rawCourse.kcmc || !rawCourse.xm || !rawCourse.cdmc ||
  128. !rawCourse.xqj || !rawCourse.jcs || !rawCourse.zcd) {
  129. continue;
  130. }
  131. const weeksArray = parseWeeks(rawCourse.zcd);
  132. // 周次有效性检查
  133. if (weeksArray.length === 0) {
  134. continue;
  135. }
  136. // 解析节次范围,例如 "1-2"
  137. const sectionParts = rawCourse.jcs.split('-');
  138. const startSection = Number(sectionParts[0]);
  139. const endSection = Number(sectionParts[sectionParts.length - 1]);
  140. const day = Number(rawCourse.xqj); // xqj: 星期几 (周一为1, 周日为7)
  141. // 数字有效性检查
  142. if (isNaN(day) || isNaN(startSection) || isNaN(endSection) || day < 1 || day > 7 || startSection > endSection) {
  143. // console.warn(`JS: 课程 ${rawCourse.kcmc} 星期或节次数据无效,跳过。`);
  144. continue;
  145. }
  146. finalCourseList.push({
  147. name: rawCourse.kcmc.trim(),
  148. teacher: rawCourse.xm.trim(),
  149. position: rawCourse.cdmc.trim(),
  150. day: day,
  151. startSection: startSection,
  152. endSection: endSection,
  153. weeks: weeksArray
  154. });
  155. }
  156. finalCourseList.sort((a, b) =>
  157. a.day - b.day ||
  158. a.startSection - b.startSection ||
  159. a.name.localeCompare(b.name)
  160. );
  161. // 课程时间特殊处理
  162. const processedCourses = applyCustomTimeLogic(finalCourseList);
  163. console.log(`JS: JSON 数据解析完成,共找到 ${finalCourseList.length} 门课程。`);
  164. return processedCourses;
  165. }
  166. function validateYearInput(input) {
  167. console.log("JS: validateYearInput 被调用,输入: " + input);
  168. if (/^[0-9]{4}$/.test(input)) {
  169. console.log("JS: validateYearInput 验证通过。");
  170. return false;
  171. } else {
  172. console.log("JS: validateYearInput 验证失败。");
  173. return "请输入四位数字的学年!";
  174. }
  175. }
  176. async function promptUserToStart() {
  177. console.log("JS: 流程开始:显示公告。");
  178. return await window.AndroidBridgePromise.showAlert(
  179. "教务系统课表导入",
  180. "导入前请确保您已在浏览器中成功登录教务系统",
  181. "好的,开始导入"
  182. );
  183. }
  184. async function getAcademicYear() {
  185. const currentYear = new Date().getFullYear().toString();
  186. console.log("JS: 提示用户输入学年。");
  187. return await window.AndroidBridgePromise.showPrompt(
  188. "选择学年",
  189. "请输入要导入课程的起始学年(例如 2025-2026 应输入2025):",
  190. currentYear,
  191. "validateYearInput"
  192. );
  193. }
  194. async function selectSemester() {
  195. const semesters = ["第一学期", "第二学期"];
  196. console.log("JS: 提示用户选择学期。");
  197. const semesterIndex = await window.AndroidBridgePromise.showSingleSelection(
  198. "选择学期",
  199. JSON.stringify(semesters),
  200. -1
  201. );
  202. return semesterIndex;
  203. }
  204. /**
  205. * 将选择索引转换为 API 所需的学期码。
  206. */
  207. function getSemesterCode(semesterIndex) {
  208. // semesterIndex 3 (第一学期), 12 (第二学期)
  209. return semesterIndex === 0 ? "3" : "12";
  210. }
  211. /**
  212. * 请求和解析课程数据
  213. */
  214. async function fetchAndParseCourses(academicYear, semesterIndex) {
  215. const semesterCode = getSemesterCode(semesterIndex);
  216. const requestBody = `xnm=${academicYear}&xqm=${semesterCode}&kzlx=ck&xsdm=&kclbdm=`;
  217. const targetUrl = "http://jwxt.qqhrit.com:20266/jwglxt/kbcx/xskbcx_cxXsgrkb.html?gnmkdm=N2151";
  218. try {
  219. const response = await fetch(targetUrl, {
  220. method: "POST",
  221. headers: {
  222. "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
  223. },
  224. body: requestBody,
  225. credentials: "include"
  226. });
  227. if (response.ok) {
  228. const jsonText = await response.text();
  229. const jsonData = JSON.parse(jsonText);
  230. if (jsonData && jsonData.kbList) {
  231. const parsedCourses = parseJsonData(jsonData);
  232. if (parsedCourses.length > 0) {
  233. return {
  234. courses: parsedCourses,
  235. config: {
  236. semesterStartDate: null,
  237. semesterTotalWeeks: 20
  238. }
  239. };
  240. }
  241. }
  242. }
  243. } catch (e) {
  244. console.error(`Request failed: ${targetUrl}`, e);
  245. }
  246. AndroidBridge.showToast("未能获取课表数据,请检查网络环境或登录状态。");
  247. return null;
  248. }
  249. async function saveCourses(parsedCourses) {
  250. AndroidBridge.showToast(`正在保存 ${parsedCourses.length} 门课程...`);
  251. console.log(`JS: 尝试保存 ${parsedCourses.length} 门课程...`);
  252. try {
  253. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses, null, 2));
  254. console.log("JS: 课程保存成功!");
  255. return true;
  256. } catch (error) {
  257. AndroidBridge.showToast(`课程保存失败: ${error.message}`);
  258. console.error('JS: Save Courses Error:', error);
  259. return false;
  260. }
  261. }
  262. // 统一作息时间
  263. // 3,4节作息采用1#、5#版本
  264. const TimeSlots = [
  265. { number: 1, startTime: "08:00", endTime: "08:45" },
  266. { number: 2, startTime: "08:55", endTime: "09:40" },
  267. { number: 3, startTime: "10:05", endTime: "10:50" },
  268. { number: 4, startTime: "11:00", endTime: "11:45" },
  269. { number: 5, startTime: "13:30", endTime: "14:15" },
  270. { number: 6, startTime: "14:25", endTime: "15:10" },
  271. { number: 7, startTime: "15:20", endTime: "16:05" },
  272. { number: 8, startTime: "16:05", endTime: "16:50" },
  273. { number: 9, startTime: "18:00", "endTime": "18:45" },
  274. { number: 10, startTime: "18:45", "endTime": "19:30" },
  275. { number: 11, startTime: "19:30", "endTime": "20:15" }
  276. ];
  277. async function importPresetTimeSlots(timeSlots) {
  278. console.log(`JS: 准备导入 ${timeSlots.length} 个预设时间段。`);
  279. if (timeSlots.length > 0) {
  280. AndroidBridge.showToast(`正在导入 ${timeSlots.length} 个预设时间段...`);
  281. try {
  282. await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  283. AndroidBridge.showToast("预设时间段导入成功!");
  284. console.log("JS: 预设时间段导入成功。");
  285. } catch (error) {
  286. AndroidBridge.showToast("导入时间段失败: " + error.message);
  287. console.error('JS: Save Time Slots Error:', error);
  288. }
  289. } else {
  290. AndroidBridge.showToast("警告:时间段为空,未导入时间段信息。");
  291. console.warn("JS: 警告:传入时间段为空,未导入时间段信息。");
  292. }
  293. }
  294. async function runImportFlow() {
  295. const alertConfirmed = await promptUserToStart();
  296. if (!alertConfirmed) {
  297. AndroidBridge.showToast("用户取消了导入。");
  298. console.log("JS: 用户取消了导入流程。");
  299. return;
  300. }
  301. const academicYear = await getAcademicYear();
  302. if (academicYear === null) {
  303. AndroidBridge.showToast("导入已取消。");
  304. console.log("JS: 获取学年失败/取消,流程终止。");
  305. return;
  306. }
  307. console.log(`JS: 已选择学年: ${academicYear}`);
  308. const semesterIndex = await selectSemester();
  309. if (semesterIndex === null || semesterIndex === -1) {
  310. AndroidBridge.showToast("导入已取消。");
  311. console.log("JS: 选择学期失败/取消,流程终止。");
  312. return;
  313. }
  314. console.log(`JS: 已选择学期索引: ${semesterIndex}`);
  315. const result = await fetchAndParseCourses(academicYear, semesterIndex);
  316. if (result === null) {
  317. console.log("JS: 课程获取或解析失败,流程终止。");
  318. return;
  319. }
  320. const { courses, config } = result;
  321. const saveResult = await saveCourses(courses);
  322. if (!saveResult) {
  323. console.log("JS: 课程保存失败,流程终止。");
  324. return;
  325. }
  326. try {
  327. await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
  328. AndroidBridge.showToast(`课表配置更新成功!总周数:${config.semesterTotalWeeks}周。`);
  329. } catch (error) {
  330. AndroidBridge.showToast(`课表配置保存失败: ${error.message}`);
  331. console.error('JS: Save Config Error:', error);
  332. }
  333. await importPresetTimeSlots(TimeSlots);
  334. AndroidBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
  335. console.log("JS: 整个导入流程执行完毕并成功。");
  336. AndroidBridge.notifyTaskCompletion();
  337. }
  338. runImportFlow();