hbguhx_01.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // 河北地质大学华信学院强智教务 (61.182.88.214:8090) 拾光课程表适配脚本
  2. // 非该大学开发者适配,开发者无法及时发现问题
  3. // 出现问题请联系开发者或者提交 pr 更改,这更加快速
  4. /**
  5. * 解析周次字符串为数组
  6. */
  7. function parseWeeks(weekStr) {
  8. const weeks = [];
  9. if (!weekStr) return weeks;
  10. // 移除"周"字、括号和节次信息
  11. const pureWeekData = weekStr.replace(/周|\(.*?\)|\[\d+-\d+ 节\]/g, '').trim();
  12. if (!pureWeekData) return weeks;
  13. // 分割并处理每个段
  14. const segments = pureWeekData.split(',');
  15. segments.forEach(seg => {
  16. seg = seg.trim();
  17. if (!seg) return;
  18. if (seg.includes('-')) {
  19. const [start, end] = seg.split('-').map(Number);
  20. if (!isNaN(start) && !isNaN(end)) {
  21. for (let i = start; i <= end; i++) {
  22. weeks.push(i);
  23. }
  24. }
  25. } else {
  26. const w = parseInt(seg);
  27. if (!isNaN(w)) {
  28. weeks.push(w);
  29. }
  30. }
  31. });
  32. return [...new Set(weeks)].sort((a, b) => a - b);
  33. }
  34. /**
  35. * 合并连续节次的相同课程
  36. */
  37. function mergeAndDistinctCourses(courses) {
  38. if (courses.length <= 1) return courses;
  39. // 排序以便合并
  40. courses.sort((a, b) => {
  41. if (a.name !== b.name) return a.name.localeCompare(b.name);
  42. if (a.day !== b.day) return a.day - b.day;
  43. if (a.startSection !== b.startSection) return a.startSection - b.startSection;
  44. if (a.teacher !== b.teacher) return a.teacher.localeCompare(b.teacher);
  45. if (a.position !== b.position) return a.position.localeCompare(b.position);
  46. return a.weeks.join(',').localeCompare(b.weeks.join(','));
  47. });
  48. const merged = [];
  49. let current = courses[0];
  50. for (let i = 1; i < courses.length; i++) {
  51. const next = courses[i];
  52. // 判断是否为同一门课程
  53. const isSameCourse =
  54. current.name === next.name &&
  55. current.teacher === next.teacher &&
  56. current.position === next.position &&
  57. current.day === next.day &&
  58. current.weeks.join(',') === next.weeks.join(',');
  59. // 判断节次是否连续
  60. const isContinuous = (current.endSection + 1 === next.startSection);
  61. if (isSameCourse && isContinuous) {
  62. // 合并连续节次
  63. current.endSection = next.endSection;
  64. } else if (!(isSameCourse && current.startSection === next.startSection && current.endSection === next.endSection)) {
  65. merged.push(current);
  66. current = next;
  67. }
  68. }
  69. merged.push(current);
  70. return merged;
  71. }
  72. /**
  73. * 将 HTML 源码解析为课程模型
  74. */
  75. function parseTimetableToModel(htmlString) {
  76. const doc = new DOMParser().parseFromString(htmlString, "text/html");
  77. const timetable = doc.getElementById('kbtable');
  78. if (!timetable) {
  79. return [];
  80. }
  81. let rawCourses = [];
  82. const rows = Array.from(timetable.querySelectorAll('tr')).filter(r => r.querySelector('td'));
  83. rows.forEach((row) => {
  84. const cells = row.querySelectorAll('td');
  85. cells.forEach((cell, dayIndex) => {
  86. const day = dayIndex + 1; // 星期几(1-7)
  87. // 获取所有课程详情 div,包括所有状态的
  88. const detailDivs = Array.from(cell.querySelectorAll('div.kbcontent'));
  89. detailDivs.forEach((detailDiv) => {
  90. const rawHtml = detailDiv.innerHTML.trim();
  91. const innerText = detailDiv.innerText.trim();
  92. if (!rawHtml || rawHtml === "&nbsp;" || innerText.length < 2) return;
  93. // 分割同一个格子内的多门课程
  94. const blocks = rawHtml.split(/---------------------|----------------------/);
  95. blocks.forEach((block) => {
  96. if (!block.trim()) return;
  97. const tempDiv = document.createElement('div');
  98. tempDiv.innerHTML = block;
  99. // 1. 提取课程名(跳过开头的空行,处理 <br> 开头的分隔块)
  100. let name = "";
  101. const htmlLines = tempDiv.innerHTML.split('<br>');
  102. for (const line of htmlLines) {
  103. const text = line.replace(/<[^>]*>/g, '').trim();
  104. if (text) {
  105. name = text;
  106. break;
  107. }
  108. }
  109. if (!name) return;
  110. // 2. 提取周次和节次信息
  111. const weekFont = tempDiv.querySelector('font[title="周次(节次)"]');
  112. const weekFull = weekFont?.innerText || "";
  113. let startSection = 0;
  114. let endSection = 0;
  115. let weekStr = "";
  116. // 匹配 "1-17(周)[01-02节]" 格式
  117. const weekSectionMatch = weekFull.match(/(.+?)\(周\)\[(\d+)-(\d+)节\]/);
  118. if (weekSectionMatch) {
  119. weekStr = weekSectionMatch[1]; // "1-17"
  120. startSection = parseInt(weekSectionMatch[2], 10);
  121. endSection = parseInt(weekSectionMatch[3], 10);
  122. } else {
  123. // 尝试匹配 "1-17(周)[01-02 节]" 格式(带空格)
  124. const weekSectionMatchWithSpace = weekFull.match(/(.+?)\(周\)\[(\d+)-(\d+) 节\]/);
  125. if (weekSectionMatchWithSpace) {
  126. weekStr = weekSectionMatchWithSpace[1];
  127. startSection = parseInt(weekSectionMatchWithSpace[2], 10);
  128. endSection = parseInt(weekSectionMatchWithSpace[3], 10);
  129. } else {
  130. // 尝试匹配其他格式
  131. const altMatch = weekFull.match(/(\d+)-(\d+)/);
  132. if (altMatch) {
  133. weekStr = altMatch[0];
  134. // 假设是第1-2节
  135. startSection = 1;
  136. endSection = 2;
  137. }
  138. }
  139. }
  140. // 3. 提取教师信息
  141. const teacher = tempDiv.querySelector('font[title="老师"]')?.innerText.trim() || "未知教师";
  142. // 4. 提取教室地点
  143. const position = tempDiv.querySelector('font[title="教室"]')?.innerText.trim() || "未知地点";
  144. if (name && startSection > 0) {
  145. const course = {
  146. "name": name,
  147. "teacher": teacher,
  148. "weeks": parseWeeks(weekStr),
  149. "position": position,
  150. "day": day,
  151. "startSection": startSection,
  152. "endSection": endSection
  153. };
  154. rawCourses.push(course);
  155. }
  156. });
  157. });
  158. });
  159. });
  160. return mergeAndDistinctCourses(rawCourses);
  161. }
  162. /**
  163. * 从网页中提取学期选项列表
  164. */
  165. function extractSemesterOptions(htmlString) {
  166. const doc = new DOMParser().parseFromString(htmlString, "text/html");
  167. const semesterSelect = doc.getElementById('xnxq01id');
  168. if (!semesterSelect) {
  169. return [];
  170. }
  171. const options = Array.from(semesterSelect.querySelectorAll('option')).map(opt => ({
  172. value: opt.value,
  173. text: opt.text
  174. }));
  175. return options;
  176. }
  177. /**
  178. * 从网页中提取作息时间
  179. */
  180. function extractTimeSlots(htmlString) {
  181. const doc = new DOMParser().parseFromString(htmlString, "text/html");
  182. const timetable = doc.getElementById('kbtable');
  183. if (!timetable) return null;
  184. const timeSlots = [];
  185. const rows = Array.from(timetable.querySelectorAll('tr'));
  186. rows.forEach((row) => {
  187. const th = row.querySelector('th');
  188. if (!th) return;
  189. // 提取时间范围,如 "08:30-10:05"
  190. const timeText = th.innerText.trim();
  191. const timeMatch = timeText.match(/(\d{2}:\d{2})-(\d{2}:\d{2})/);
  192. if (timeMatch) {
  193. const startTime = timeMatch[1];
  194. const endTime = timeMatch[2];
  195. // 判断是否是有效的时间段(排除"中午"等)
  196. const sectionName = timeText.split('\n')[0].trim();
  197. if (startTime && endTime && sectionName !== '中午') {
  198. // 为每个大节创建两个时间段
  199. const sections = [
  200. {
  201. number: timeSlots.length + 1,
  202. startTime: startTime,
  203. endTime: `${startTime.split(':')[0]}:45`
  204. },
  205. {
  206. number: timeSlots.length + 2,
  207. startTime: `${startTime.split(':')[0]}:55`,
  208. endTime: endTime
  209. }
  210. ];
  211. timeSlots.push(...sections);
  212. }
  213. }
  214. });
  215. return timeSlots.length > 0 ? timeSlots : null;
  216. }
  217. /**
  218. * 根据学期字符串获取学期第一周周一日期
  219. * 学期格式如 "2025-2026-2"
  220. * 春季学期:3月第二个周一,秋季学期:9月1日所在周的周一
  221. */
  222. function getSemesterStartMonday(semesterId) {
  223. if (!semesterId) return null;
  224. const parts = semesterId.split('-');
  225. if (parts.length < 3) return null;
  226. const year1 = parseInt(parts[0], 10);
  227. const year2 = parseInt(parts[1], 10);
  228. const semester = parseInt(parts[2], 10);
  229. if (isNaN(year1) || isNaN(year2) || isNaN(semester)) return null;
  230. // 估算学期开始日期
  231. let startDate;
  232. if (semester === 1) {
  233. startDate = new Date(year1, 8, 1); // 秋季学期:约9月1日
  234. } else {
  235. // 春季学期:3月第二个周一
  236. const march1Day = new Date(year2, 2, 1).getDay();
  237. const firstMondayDate = march1Day === 1 ? 1 : march1Day === 0 ? 2 : 9 - march1Day;
  238. startDate = new Date(year2, 2, firstMondayDate + 7); // 第二个周一
  239. }
  240. // 找到学期开始日期所在周的周一
  241. const startDay = startDate.getDay(); // 0=周日
  242. const monday = new Date(startDate);
  243. monday.setDate(startDate.getDate() + (startDay === 0 ? -6 : 1 - startDay));
  244. return monday;
  245. }
  246. /**
  247. * 格式化日期为 YYYY-MM-DD
  248. */
  249. function formatDate(date) {
  250. const y = date.getFullYear();
  251. const m = String(date.getMonth() + 1).padStart(2, '0');
  252. const d = String(date.getDate()).padStart(2, '0');
  253. return `${y}-${m}-${d}`;
  254. }
  255. /**
  256. * 根据学期字符串计算当前周数
  257. */
  258. function calculateCurrentWeek(semesterId) {
  259. const monday = getSemesterStartMonday(semesterId);
  260. if (!monday) return 1;
  261. const now = new Date();
  262. const diffMs = now.getTime() - monday.getTime();
  263. const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
  264. return Math.max(1, Math.floor(diffDays / 7) + 1);
  265. }
  266. /**
  267. * 显示欢迎提示
  268. */
  269. async function showWelcomeAlert() {
  270. return await window.AndroidBridgePromise.showAlert(
  271. "导入提示",
  272. "请确保已在学期理论课表页面,(首页课表是本周课表不可导入,请进入学期理论课表页面)",
  273. "开始导入"
  274. );
  275. }
  276. /**
  277. * 获取用户选择的学期参数
  278. */
  279. async function getSemesterParamsFromUser(semesterOptions) {
  280. if (!semesterOptions || semesterOptions.length === 0) {
  281. AndroidBridge.showToast("未获取到学期列表");
  282. return null;
  283. }
  284. // 直接显示所有学期选项,让用户一次性选择
  285. const semesterLabels = semesterOptions.map(opt => opt.text);
  286. const semesterIndex = await window.AndroidBridgePromise.showSingleSelection(
  287. "选择学期",
  288. JSON.stringify(semesterLabels),
  289. 0 // 默认选择第一个(最新学期)
  290. );
  291. if (semesterIndex === null) return null;
  292. return semesterOptions[semesterIndex].value;
  293. }
  294. /**
  295. * 请求课表 HTML 数据
  296. */
  297. async function fetchCourseHtml() {
  298. try {
  299. const response = await fetch("http://61.182.88.214:8090/jsxsd/xskb/xskb_list.do", {
  300. method: "GET",
  301. credentials: "include",
  302. headers: {
  303. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
  304. }
  305. });
  306. if (!response.ok) {
  307. throw new Error(`HTTP error! status: ${response.status}`);
  308. }
  309. const text = await response.text();
  310. return text;
  311. } catch (error) {
  312. console.error('获取课表页面失败:', error);
  313. throw error;
  314. }
  315. }
  316. /**
  317. * 保存课程数据到 App
  318. */
  319. async function saveCourseDataToApp(courses, timeSlots, semesterId) {
  320. // 计算学期开始日期和当前周数
  321. const startMonday = getSemesterStartMonday(semesterId);
  322. const currentWeek = calculateCurrentWeek(semesterId);
  323. // 保存学期配置
  324. await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify({
  325. "semesterStartDate": startMonday ? formatDate(startMonday) : null,
  326. "currentWeek": currentWeek,
  327. "semesterTotalWeeks": 20,
  328. "firstDayOfWeek": 1
  329. }));
  330. // 保存作息时间(从网页提取)
  331. if (timeSlots && timeSlots.length > 0) {
  332. await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  333. }
  334. // 保存课程数据
  335. return await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
  336. }
  337. /**
  338. * 主流程控制
  339. */
  340. async function runImportFlow() {
  341. try {
  342. // 1. 显示欢迎提示
  343. const start = await showWelcomeAlert();
  344. if (!start) return;
  345. // 2. 获取课表 HTML(包含学期选项和作息时间)
  346. const html = await fetchCourseHtml();
  347. // 3. 从网页中提取学期选项
  348. const semesterOptions = extractSemesterOptions(html);
  349. // 4. 从网页中提取作息时间
  350. let timeSlots = extractTimeSlots(html);
  351. if (!timeSlots || timeSlots.length === 0) {
  352. // 设置默认作息时间
  353. timeSlots = [
  354. { number: 1, startTime: "08:30", endTime: "09:15" },
  355. { number: 2, startTime: "09:25", endTime: "10:10" },
  356. { number: 3, startTime: "10:15", endTime: "11:00" },
  357. { number: 4, startTime: "11:10", endTime: "11:55" },
  358. { number: 5, startTime: "14:30", endTime: "15:15" },
  359. { number: 6, startTime: "15:25", endTime: "16:10" },
  360. { number: 7, startTime: "16:15", endTime: "17:00" },
  361. { number: 8, startTime: "17:10", endTime: "17:55" },
  362. { number: 9, startTime: "19:00", endTime: "19:45" },
  363. { number: 10, startTime: "19:55", endTime: "20:40" }
  364. ];
  365. }
  366. // 5. 让用户选择学期
  367. const semesterId = await getSemesterParamsFromUser(semesterOptions);
  368. if (!semesterId) return;
  369. // 6. 根据选择的学期重新请求课表数据
  370. const response = await fetch("http://61.182.88.214:8090/jsxsd/xskb/xskb_list.do", {
  371. method: "POST",
  372. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  373. body: `cj0701id=&zc=&demo=&xnxq01id=${semesterId}`,
  374. credentials: "include"
  375. });
  376. const courseHtml = await response.text();
  377. // 7. 解析课程数据
  378. const finalCourses = parseTimetableToModel(courseHtml);
  379. if (finalCourses.length === 0) {
  380. AndroidBridge.showToast("未发现课程,请检查学期选择或登录状态。");
  381. // 尝试直接从初始 HTML 中解析课程
  382. const initialCourses = parseTimetableToModel(html);
  383. if (initialCourses.length > 0) {
  384. await saveCourseDataToApp(initialCourses, timeSlots, semesterId);
  385. AndroidBridge.showToast(`成功导入 ${initialCourses.length} 门课程`);
  386. AndroidBridge.notifyTaskCompletion();
  387. }
  388. return;
  389. }
  390. // 8. 保存课程数据
  391. await saveCourseDataToApp(finalCourses, timeSlots, semesterId);
  392. AndroidBridge.showToast(`成功导入 ${finalCourses.length} 门课程(当前第 ${calculateCurrentWeek(semesterId)} 周)`);
  393. AndroidBridge.notifyTaskCompletion();
  394. } catch (error) {
  395. console.error('导入异常:', error);
  396. AndroidBridge.showToast("导入异常:" + error.message);
  397. }
  398. }
  399. // 启动执行
  400. runImportFlow();