ujs_zhengfang_v9.0.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. // 江苏大学(ujs.edu.cn) 拾光课程表适配脚本
  2. // 基于正方教务系统接口适配
  3. // 出现问题请联系作者或者提交直接pr更改,这更加快速
  4. // 基于GSMC修改
  5. // 作者:洛初 Github@gongfuture
  6. /**
  7. * 解析周次字符串,处理单双周和周次范围。
  8. */
  9. function parseWeeks(weekStr) {
  10. if (!weekStr) return [];
  11. const weekSets = weekStr.split(',');
  12. let weeks = [];
  13. for (const set of weekSets) {
  14. const trimmedSet = set.trim();
  15. const rangeMatch = trimmedSet.match(/(\d+)-(\d+)周/);
  16. const singleMatch = trimmedSet.match(/^(\d+)周/); // 匹配以数字周结束的
  17. let start = 0;
  18. let end = 0;
  19. let processed = false;
  20. if (rangeMatch) { // 范围, 如 "1-5周"
  21. start = Number(rangeMatch[1]);
  22. end = Number(rangeMatch[2]);
  23. processed = true;
  24. } else if (singleMatch) { // 单个周, 如 "6周"
  25. start = end = Number(singleMatch[1]);
  26. processed = true;
  27. }
  28. if (processed) {
  29. // 确定单双周
  30. const isSingle = trimmedSet.includes('(单)');
  31. const isDouble = trimmedSet.includes('(双)');
  32. for (let w = start; w <= end; w++) {
  33. if (isSingle && w % 2 === 0) continue; // 单周跳过偶数
  34. if (isDouble && w % 2 !== 0) continue; // 双周跳过奇数
  35. weeks.push(w);
  36. }
  37. }
  38. }
  39. // 去重并排序
  40. return [...new Set(weeks)].sort((a, b) => a - b);
  41. }
  42. /**
  43. * 解析 API 返回的 JSON 数据。
  44. */
  45. function parseJsonData(jsonData) {
  46. console.log("JS: parseJsonData 正在解析 JSON 数据...");
  47. // 检查JSON结构:新的数据在 kbList 字段中
  48. if (!jsonData || !Array.isArray(jsonData.kbList)) {
  49. console.warn("JS: JSON 数据结构错误或缺少 kbList 字段。");
  50. return [];
  51. }
  52. const rawCourseList = jsonData.kbList;
  53. const finalCourseList = [];
  54. for (const rawCourse of rawCourseList) {
  55. // 关键字段检查: kcmc(课名), xm(教师), cdmc(教室), xqj(星期), jcs(节次范围), zcd(周次描述)
  56. if (!rawCourse.kcmc || !rawCourse.xm || !rawCourse.cdmc ||
  57. !rawCourse.xqj || !rawCourse.jcs || !rawCourse.zcd) {
  58. continue;
  59. }
  60. const weeksArray = parseWeeks(rawCourse.zcd);
  61. // 周次有效性检查
  62. if (weeksArray.length === 0) {
  63. continue;
  64. }
  65. // 解析节次范围,例如 "1-2"
  66. const sectionParts = rawCourse.jcs.split('-');
  67. const startSection = Number(sectionParts[0]);
  68. const endSection = Number(sectionParts[sectionParts.length - 1]);
  69. const day = Number(rawCourse.xqj); // xqj: 星期几 (周一为1, 周日为7)
  70. // 数字有效性检查
  71. if (isNaN(day) || isNaN(startSection) || isNaN(endSection) || day < 1 || day > 7 || startSection > endSection) {
  72. // console.warn(`JS: 课程 ${rawCourse.kcmc} 星期或节次数据无效,跳过。`);
  73. continue;
  74. }
  75. finalCourseList.push({
  76. name: rawCourse.kcmc.trim(),
  77. teacher: rawCourse.xm.trim(),
  78. position: rawCourse.cdmc.trim(),
  79. day: day,
  80. startSection: startSection,
  81. endSection: endSection,
  82. weeks: weeksArray
  83. });
  84. }
  85. finalCourseList.sort((a, b) =>
  86. a.day - b.day ||
  87. a.startSection - b.startSection ||
  88. a.name.localeCompare(b.name)
  89. );
  90. console.log(`JS: JSON 数据解析完成,共找到 ${finalCourseList.length} 门课程。`);
  91. return finalCourseList;
  92. }
  93. /**
  94. * 检查当前是否处于夏令时作息时间段。
  95. * @returns true 夏令时 false 冬令时
  96. */
  97. async function whetherSummerTimeSlot() {
  98. // // 教务处 校历/作息时间 公告页
  99. // const url = "https://jwc.ujs.edu.cn/index/xl_zuo_xi_shi_jian.htm";
  100. // let title = "";
  101. // try {
  102. // const response = await fetch(url);
  103. // if (!response.ok) {
  104. // throw new Error(`网络请求失败。状态码: ${response.status} (${response.statusText})`);
  105. // }
  106. // const html = await response.text();
  107. // const doc = new DOMParser().parseFromString(html, "text/html");
  108. // // 优先按页面固定 id 读取:#line_u8_0, #line_u8_1 ...
  109. // for (let i = 0; i < 30; i++) {
  110. // const a = doc.querySelector(`#line_u8_${i} > a`);
  111. // if (!a) continue;
  112. // title = (a.getAttribute("title") || a.textContent || "").trim();
  113. // if (title.includes("作息时间表")) {
  114. // break;
  115. // }
  116. // }
  117. // // 若固定 id 没取到,则扫描所有链接文本
  118. // if (title.trim().length === 0) {
  119. // const links = doc.querySelectorAll("a");
  120. // for (const link of links) {
  121. // title = (link.getAttribute("title") || link.textContent || "").trim();
  122. // if (title.includes("作息时间表")) {
  123. // break;
  124. // }
  125. // }
  126. // }
  127. // // 从公告中提取日期
  128. // if (title.trim().length === 0) {
  129. // throw new Error("未找到作息时间公告标题。");
  130. // }
  131. // const match = title.match(/[((]\s*(\d{4})年(\d{1,2})月(\d{1,2})日起执行\s*[))]/);
  132. // if (!match) {
  133. // throw new Error("公告标题格式不匹配,无法提取执行日期。");
  134. // }
  135. // const y = Number(match[1]);
  136. // const m = Number(match[2]);
  137. // const d = Number(match[3]);
  138. // const changeDate = new Date(y, m - 1, d);
  139. // const now = new Date();
  140. // if (changeDate.getMonth() === 3 && now >= changeDate ) { // 4月7日开始夏令时
  141. // return true;
  142. // } else if (changeDate.getMonth() === 9 && now < changeDate) { // 10月7日开始冬令时
  143. // return true;
  144. // } else {
  145. // return false;
  146. // }
  147. // } catch (error) {
  148. // console.error('JS: 获取作息时间公告失败:', error);
  149. // AndroidBridge.showToast("无法获取作息时间公告,智能选择回退到预设时间。");
  150. // // 预设日期
  151. // const summerStart = new Date(new Date().getFullYear(), 3, 7); // 4月7日
  152. // const winterStart = new Date(new Date().getFullYear(), 9, 7); // 10月7日
  153. // const now = new Date();
  154. // if (now >= summerStart && now < winterStart) {
  155. // return true; // 夏令时
  156. // } else {
  157. // return false; // 冬令时
  158. // }
  159. // }
  160. // CORS 问题导致无法获取公告页,智能选择回退到预设时间。
  161. // 预设日期
  162. const summerStart = new Date(new Date().getFullYear(), 3, 7); // 4月7日
  163. const winterStart = new Date(new Date().getFullYear(), 9, 8); // 10月8日
  164. const now = new Date();
  165. if (now >= summerStart && now < winterStart) {
  166. return true; // 夏令时
  167. } else {
  168. return false; // 冬令时
  169. }
  170. }
  171. /**
  172. * 检查是否在登录页面。
  173. * 只有当 URL 精确匹配时,才返回 true。
  174. */
  175. function isLoginPage() {
  176. const url = window.location.href;
  177. const loginUrl = "http://jwxt.ujs.edu.cn/sso/jziotlogin";
  178. // 如果当前 URL 与指定的登录 URL 完全一致,则返回 true (是登录页)
  179. return url === loginUrl;
  180. }
  181. function validateYearInput(input) {
  182. console.log("JS: validateYearInput 被调用,输入: " + input);
  183. if (/^[0-9]{4}$/.test(input)) {
  184. console.log("JS: validateYearInput 验证通过。");
  185. return false;
  186. } else {
  187. console.log("JS: validateYearInput 验证失败。");
  188. return "请输入四位数字的学年!";
  189. }
  190. }
  191. async function promptUserToStart() {
  192. console.log("JS: 流程开始:显示公告。");
  193. return await window.AndroidBridgePromise.showAlert(
  194. "教务系统课表导入",
  195. "导入前请确保您已在浏览器中成功登录教务系统",
  196. "好的,开始导入"
  197. );
  198. }
  199. async function getAcademicYear() {
  200. const currentYear = new Date().getFullYear().toString();
  201. const currentMonth = new Date().getMonth() + 1; // 月份从0开始,所以加1
  202. // 如果当前月份在8月或之后,默认学年是当前年份-下一年份,否则是上一年份-当前年份
  203. const defaultYear = currentMonth >= 8 ? currentYear : (Number(currentYear) - 1).toString();
  204. console.log("JS: 提示用户输入学年。");
  205. return await window.AndroidBridgePromise.showPrompt(
  206. "选择学年",
  207. "请输入要导入课程的起始学年(如2025-2026 应该填2025):",
  208. defaultYear,
  209. "validateYearInput"
  210. );
  211. }
  212. async function selectSemester() {
  213. const semesters = ["第一学期", "第二学期"];
  214. const currentMonth = new Date().getMonth() + 1; // 月份从0开始,所以加1
  215. const defaultSemesterIndex = currentMonth >= 8 ? 0 : 1; // 如果当前月份在8月或之后,默认选择第一学期,否则选择第二学期
  216. console.log("JS: 提示用户选择学期。");
  217. const semesterIndex = await window.AndroidBridgePromise.showSingleSelection(
  218. "选择学期",
  219. JSON.stringify(semesters),
  220. defaultSemesterIndex
  221. );
  222. return semesterIndex;
  223. }
  224. async function selectTimeSlot() {
  225. const timeSlots = ["智能选择" ,"夏令时", "冬令时"];
  226. console.log("JS: 提示用户选择作息类型。");
  227. const timeSlotIndex = await window.AndroidBridgePromise.showSingleSelection(
  228. "选择作息时间",
  229. JSON.stringify(timeSlots),
  230. 0
  231. );
  232. return timeSlotIndex;
  233. }
  234. async function reselectTimeSlot(selectedTimeSlot) {
  235. const options = ["对的对的,就是这个", "不对不对,应该是另外一个"];
  236. const dialogTitle = "当前智能选择结果为: " + (selectedTimeSlot ? "夏令时" : "冬令时") + ",是否更改选择?";
  237. const selectedIndex = await window.AndroidBridgePromise.showSingleSelection(
  238. dialogTitle,
  239. JSON.stringify(options),
  240. 0
  241. );
  242. if (selectedIndex === null || selectedIndex === -1) {
  243. return false;
  244. }
  245. // 选中第 2 项(索引 1)表示“需要改成另外一个”。
  246. return selectedIndex === 1;
  247. }
  248. /**
  249. * 将选择索引转换为 API 所需的学期码。
  250. */
  251. function getSemesterCode(semesterIndex) {
  252. // semesterIndex 3 (第一学期), 12 (第二学期)
  253. return semesterIndex === 0 ? "3" : "12";
  254. }
  255. /**
  256. * 请求和解析课程数据
  257. */
  258. async function fetchAndParseCourses(academicYear, semesterIndex) {
  259. AndroidBridge.showToast("正在请求课表数据...");
  260. const semesterCode = getSemesterCode(semesterIndex);
  261. // API URL 和请求体
  262. const xnmXqmBody = `xnm=${academicYear}&xqm=${semesterCode}&kzlx=ck&xsdm=&kclbdm=`;
  263. const url = "http://jwxt.ujs.edu.cn/kbcx/xskbcx_cxXsgrkb.html?gnmkdm=N2151";
  264. console.log(`JS: 发送请求到 ${url}, body: ${xnmXqmBody}`);
  265. const requestOptions = {
  266. "headers": {
  267. "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
  268. },
  269. "body": xnmXqmBody,
  270. "method": "POST",
  271. "credentials": "include"
  272. };
  273. try {
  274. const response = await fetch(url, requestOptions);
  275. if (!response.ok) {
  276. throw new Error(`网络请求失败。状态码: ${response.status} (${response.statusText})`);
  277. }
  278. const jsonText = await response.text();
  279. let jsonData;
  280. try {
  281. jsonData = JSON.parse(jsonText);
  282. } catch (e) {
  283. console.error('JS: JSON 解析失败,可能是会话过期:', e);
  284. AndroidBridge.showToast("数据返回格式错误,可能是您未成功登录或会话已过期。");
  285. return null;
  286. }
  287. const courses = parseJsonData(jsonData);
  288. if (courses.length === 0) {
  289. AndroidBridge.showToast("未找到任何课程数据,请检查所选学年学期是否正确或本学期无课,或教务系统需要二次登录。");
  290. return null;
  291. }
  292. console.log(`JS: 课程数据解析成功,共找到 ${courses.length} 门课程。`);
  293. console.log("JS: 课程列表预览:", courses.slice(0, 5)); // 预览前5门课程
  294. // 默认总周数为 20,周一为一周第一天。
  295. const config = {
  296. semesterTotalWeeks: 20,
  297. firstDayOfWeek: 1
  298. };
  299. // 返回课程列表和配置信息
  300. return { courses: courses, config: config };
  301. } catch (error) {
  302. AndroidBridge.showToast(`请求或解析失败: ${error.message}`);
  303. console.error('JS: Fetch/Parse Error:', error);
  304. return null;
  305. }
  306. }
  307. async function saveCourses(parsedCourses) {
  308. AndroidBridge.showToast(`正在保存 ${parsedCourses.length} 门课程...`);
  309. console.log(`JS: 尝试保存 ${parsedCourses.length} 门课程...`);
  310. try {
  311. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses, null, 2));
  312. console.log("JS: 课程保存成功!");
  313. return true;
  314. } catch (error) {
  315. AndroidBridge.showToast(`课程保存失败: ${error.message}`);
  316. console.error('JS: Save Courses Error:', error);
  317. return false;
  318. }
  319. }
  320. // 上午作息时间
  321. // 北固及本部主楼、主A楼、生环楼、汽车能动楼、京江楼
  322. const AMorningTimeSlots = [
  323. { number: 1, startTime: "08:00", endTime: "08:45" },
  324. { number: 2, startTime: "08:55", endTime: "09:40" },
  325. { number: 3, startTime: "10:00", endTime: "10:45" },
  326. { number: 4, startTime: "10:55", endTime: "11:40" },
  327. ];
  328. // 三江楼、材料楼、机械楼、新校区各教学楼
  329. const BMorningTimeSlots = [
  330. { number: 1, startTime: "08:00", endTime: "08:45" },
  331. { number: 2, startTime: "08:55", endTime: "09:40" },
  332. { number: 3, startTime: "10:10", endTime: "10:55" },
  333. { number: 4, startTime: "11:05", endTime: "11:50" },
  334. ];
  335. // 三山楼、讲堂群、实践楼
  336. const CMorningTimeSlots = [
  337. { number: 1, startTime: "08:00", endTime: "08:45" },
  338. { number: 2, startTime: "08:55", endTime: "09:40" },
  339. { number: 3, startTime: "10:20", endTime: "11:05" },
  340. { number: 4, startTime: "11:15", endTime: "12:00" },
  341. ];
  342. // 夏令时
  343. // 下午作息时间
  344. // 北固
  345. const DSummerAfternoonTimeSlots = [
  346. { number: 5, startTime: "14:00", endTime: "14:45" },
  347. { number: 6, startTime: "14:55", endTime: "15:40" },
  348. { number: 7, startTime: "15:50", endTime: "16:35" },
  349. { number: 8, startTime: "16:45", endTime: "17:30" },
  350. ];
  351. // 本部
  352. const ESummerAfternoonTimeSlots = [
  353. { number: 5, startTime: "14:00", endTime: "14:45" },
  354. { number: 6, startTime: "14:55", endTime: "15:40" },
  355. { number: 7, startTime: "16:00", endTime: "16:45" },
  356. { number: 8, startTime: "16:55", endTime: "17:40" },
  357. ];
  358. // 晚上作息时间
  359. const SummerEveningTimeSlots = [
  360. { number: 9, startTime: "19:00", endTime: "19:45" },
  361. { number: 10, startTime: "19:55", endTime: "20:40" },
  362. { number: 11, startTime: "20:50", endTime: "21:35" },
  363. ];
  364. // 冬令时
  365. // 下午作息时间
  366. // 北固
  367. const DWinterAfternoonTimeSlots = [
  368. { number: 5, startTime: "13:30", endTime: "14:15" },
  369. { number: 6, startTime: "14:25", endTime: "15:10" },
  370. { number: 7, startTime: "15:20", endTime: "16:05" },
  371. { number: 8, startTime: "16:15", endTime: "17:00" },
  372. ];
  373. // 本部
  374. const EWinterAfternoonTimeSlots = [
  375. { number: 5, startTime: "13:30", endTime: "14:15" },
  376. { number: 6, startTime: "14:25", endTime: "15:10" },
  377. { number: 7, startTime: "15:30", endTime: "16:15" },
  378. { number: 8, startTime: "16:25", endTime: "17:10" },
  379. ];
  380. // 晚上作息时间
  381. const WinterEveningTimeSlots = [
  382. { number: 9, startTime: "18:30", endTime: "19:15" },
  383. { number: 10, startTime: "19:25", endTime: "20:10" },
  384. { number: 11, startTime: "20:20", endTime: "21:05" },
  385. ];
  386. // 全局默认作息
  387. // 夏令时
  388. const SummerTimeSlots = [...AMorningTimeSlots, ...ESummerAfternoonTimeSlots, ...SummerEveningTimeSlots];
  389. // 冬令时
  390. const WinterTimeSlots = [...AMorningTimeSlots, ...EWinterAfternoonTimeSlots, ...WinterEveningTimeSlots];
  391. function getCampusTypeFromPosition(position) {
  392. const normalized = String(position || "").replace(/\s+/g, " ").trim();
  393. if (!normalized) return null;
  394. // const firstPart = normalized.split(" ")[0] || "";
  395. // if (firstPart.includes("北固")) return "D";
  396. // if (firstPart.includes("本部")) return "E";
  397. // // 兜底:有些数据可能不按空格分段,补充全文匹配。
  398. // if (normalized.includes("北固")) return "D";
  399. // if (normalized.includes("本部")) return "E";
  400. // api好像不返回前缀了,我也不确定北固是怎么样的格式,只能这么写了()
  401. const firstPart = normalized.split(" ")[0] || "";
  402. if (firstPart.includes("北固") || normalized.includes("北固")) return "D";
  403. return "E"; // 其他默认本部
  404. // return null;
  405. }
  406. function getMorningTypeFromPosition(position) {
  407. const text = String(position || "").trim();
  408. if (text.includes("主A楼") || text.includes("京江楼")) return "A";
  409. if (text.includes("三江楼")) return "B";
  410. if (text.includes("三山楼") || text.includes("讲堂群")) return "C";
  411. return null;
  412. }
  413. function buildCourseTimeSlotsByPosition(position, isSummerTime) {
  414. const morningType = getMorningTypeFromPosition(position);
  415. const campusType = getCampusTypeFromPosition(position);
  416. // 仅对指定楼宇做自定义。
  417. if (!morningType || !campusType) {
  418. return null;
  419. }
  420. const morningTimeSlots = morningType === "A"
  421. ? AMorningTimeSlots
  422. : morningType === "B"
  423. ? BMorningTimeSlots
  424. : CMorningTimeSlots;
  425. const afternoonTimeSlots = isSummerTime
  426. ? (campusType === "D" ? DSummerAfternoonTimeSlots : ESummerAfternoonTimeSlots)
  427. : (campusType === "D" ? DWinterAfternoonTimeSlots : EWinterAfternoonTimeSlots);
  428. const eveningTimeSlots = isSummerTime ? SummerEveningTimeSlots : WinterEveningTimeSlots;
  429. return [...morningTimeSlots, ...afternoonTimeSlots, ...eveningTimeSlots];
  430. }
  431. function applyCustomTimeToCourses(courses, isSummerTime) {
  432. let customizedCount = 0;
  433. let skippedCount = 0;
  434. const updatedCourses = courses.map((course) => {
  435. const courseTimeSlots = buildCourseTimeSlotsByPosition(course.position, isSummerTime);
  436. if (!courseTimeSlots) {
  437. skippedCount += 1;
  438. return course;
  439. }
  440. const slotMap = new Map(courseTimeSlots.map((slot) => [slot.number, slot]));
  441. const startSlot = slotMap.get(course.startSection);
  442. const endSlot = slotMap.get(course.endSection);
  443. if (!startSlot || !endSlot) {
  444. skippedCount += 1;
  445. console.warn(`JS: 课程 ${course.name} 的节次(${course.startSection}-${course.endSection})未命中自定义时间映射,回退为普通节次。`);
  446. return course;
  447. }
  448. customizedCount += 1;
  449. return {
  450. ...course,
  451. isCustomTime: true,
  452. customStartTime: startSlot.startTime,
  453. customEndTime: endSlot.endTime,
  454. };
  455. });
  456. console.log(`JS: 自定义时间处理完成,命中 ${customizedCount} 门,跳过 ${skippedCount} 门。`);
  457. return updatedCourses;
  458. }
  459. async function importPresetTimeSlots(timeSlots) {
  460. console.log(`JS: 准备导入 ${timeSlots.length} 个预设时间段。`);
  461. if (timeSlots.length > 0) {
  462. AndroidBridge.showToast(`正在导入 ${timeSlots.length} 个预设时间段...`);
  463. try {
  464. await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  465. AndroidBridge.showToast("预设时间段导入成功!");
  466. console.log("JS: 预设时间段导入成功。");
  467. } catch (error) {
  468. AndroidBridge.showToast("导入时间段失败: " + error.message);
  469. console.error('JS: Save Time Slots Error:', error);
  470. }
  471. } else {
  472. AndroidBridge.showToast("警告:时间段为空,未导入时间段信息。");
  473. console.warn("JS: 警告:传入时间段为空,未导入时间段信息。");
  474. }
  475. }
  476. async function runImportFlow() {
  477. if (isLoginPage()) {
  478. AndroidBridge.showToast("导入失败:请先登录教务系统!");
  479. console.log("JS: 检测到当前在登录页面,终止导入。");
  480. return;
  481. }
  482. const alertConfirmed = await promptUserToStart();
  483. if (!alertConfirmed) {
  484. AndroidBridge.showToast("用户取消了导入。");
  485. console.log("JS: 用户取消了导入流程。");
  486. return;
  487. }
  488. // // 与后续流程并发执行,提前缓存智能选择结果。
  489. // const smartTimeSlotPromise = whetherSummerTimeSlot();
  490. // console.log("JS: 智能作息判定已并发启动。");
  491. const academicYear = await getAcademicYear();
  492. if (academicYear === null) {
  493. AndroidBridge.showToast("导入已取消。");
  494. console.log("JS: 获取学年失败/取消,流程终止。");
  495. return;
  496. }
  497. console.log(`JS: 已选择学年: ${academicYear}`);
  498. const semesterIndex = await selectSemester();
  499. if (semesterIndex === null || semesterIndex === -1) {
  500. AndroidBridge.showToast("导入已取消。");
  501. console.log("JS: 选择学期失败/取消,流程终止。");
  502. return;
  503. }
  504. console.log(`JS: 已选择学期索引: ${semesterIndex}`);
  505. const timeSlotIndex = await selectTimeSlot();
  506. if (timeSlotIndex === null || timeSlotIndex === -1) {
  507. AndroidBridge.showToast("导入已取消。");
  508. console.log("JS: 选择作息类型失败/取消,流程终止。");
  509. return;
  510. }
  511. let isSummerTime = false;
  512. if (timeSlotIndex === 1) {
  513. isSummerTime = true;
  514. } else if (timeSlotIndex === 2) {
  515. isSummerTime = false;
  516. } else {
  517. // try {
  518. // isSummerTime = await smartTimeSlotPromise;
  519. // } catch (error) {
  520. // console.error("JS: 智能作息判定异常,回退重新判定:", error);
  521. // isSummerTime = await whetherSummerTimeSlot();
  522. // }
  523. isSummerTime = await whetherSummerTimeSlot();
  524. const shouldReselect = await reselectTimeSlot(isSummerTime);
  525. if (shouldReselect) {
  526. isSummerTime = !isSummerTime;
  527. }
  528. }
  529. console.log(`JS: 作息类型: ${isSummerTime ? "夏令时" : "冬令时"}`);
  530. const result = await fetchAndParseCourses(academicYear, semesterIndex);
  531. if (result === null) {
  532. console.log("JS: 课程获取或解析失败,流程终止。");
  533. return;
  534. }
  535. const { courses, config } = result;
  536. const coursesWithCustomTime = applyCustomTimeToCourses(courses, isSummerTime);
  537. // 只匹配了主A楼、京江楼、三江楼、三山楼、讲堂群这几个教学楼的作息时间,其他课程都使用默认时间
  538. const timeSlotAlert = await window.AndroidBridgePromise.showAlert(
  539. "楼栋自定义时间提示",
  540. "脚本已根据课程所在位置智能匹配了作息时间,部分课程可能与预设时间不符。\n" +
  541. "请在课表页面核对课程时间,如有错误请手动修改课程所在位置或节次信息。\n" +
  542. "(目前仅主A楼、京江楼、三江楼、三山楼、讲堂群的课程会应用自定义时间,其他课程默认时间不变)\n" +
  543. "欢迎其他楼栋的同学提供课程时间信息以完善脚本!",
  544. "我知道了"
  545. );
  546. const saveResult = await saveCourses(coursesWithCustomTime);
  547. if (!saveResult) {
  548. console.log("JS: 课程保存失败,流程终止。");
  549. return;
  550. }
  551. try {
  552. await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
  553. AndroidBridge.showToast(`课表配置更新成功!总周数:${config.semesterTotalWeeks}周。`);
  554. } catch (error) {
  555. AndroidBridge.showToast(`课表配置保存失败: ${error.message}`);
  556. console.error('JS: Save Config Error:', error);
  557. }
  558. await importPresetTimeSlots(isSummerTime ? SummerTimeSlots : WinterTimeSlots);
  559. AndroidBridge.showToast(`课程导入成功,共导入 ${coursesWithCustomTime.length} 门课程!`);
  560. console.log("JS: 整个导入流程执行完毕并成功。");
  561. AndroidBridge.notifyTaskCompletion();
  562. }
  563. runImportFlow();