hnsf_02.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // ====================== 工具函数 ======================
  2. /**
  3. * 检查用户是否已登录。
  4. * 如果当前URL包含登录关键字,说明用户未登录,返回false。
  5. * 如果不包含登录关键字,说明用户已登录,返回true。
  6. */
  7. function isUserLoggedIn() {
  8. const url = window.location.href;
  9. const loginKeywords = [
  10. "https://jwc.htu.edu.cn/xtgl/login_slogin.html"
  11. ];
  12. // 检查URL是否包含登录关键字
  13. for (const keyword of loginKeywords) {
  14. if (url.includes(keyword)) {
  15. return false; // 包含登录关键字,说明用户未登录
  16. }
  17. }
  18. // 不包含登录关键字,说明用户已登录
  19. return true;
  20. }
  21. // 展开 weeks 字符串 -> 数字数组
  22. function parseWeeks(weeksStr) {
  23. const weeks = new Set();
  24. if (!weeksStr) return [];
  25. const parts = weeksStr.split(",");
  26. for (const part of parts) {
  27. if (part.includes("-")) {
  28. const [start, end] = part.split("-").map(n => parseInt(n));
  29. for (let i = start; i <= end && i <= 20; i++) {
  30. weeks.add(i);
  31. }
  32. } else {
  33. const n = parseInt(part);
  34. if (n >= 1 && n <= 20) weeks.add(n);
  35. }
  36. }
  37. return Array.from(weeks).sort((a, b) => a - b);
  38. }
  39. // 合并重复课程
  40. function mergeDuplicateCourses(courses) {
  41. const merged = [];
  42. const keyMap = {}; // key = name+day+startSection+endSection+position
  43. for (const c of courses) {
  44. const key = `${c.name}|${c.day}|${c.startSection}|${c.endSection}|${c.position}`;
  45. if (!keyMap[key]) {
  46. keyMap[key] = { ...c, weeks: [...c.weeks] };
  47. } else {
  48. keyMap[key].weeks = Array.from(new Set([...keyMap[key].weeks, ...c.weeks]));
  49. }
  50. }
  51. for (const k in keyMap) merged.push(keyMap[k]);
  52. return merged;
  53. }
  54. // ====================== 选择月份 ======================
  55. async function selectMonth() {
  56. try {
  57. // 生成月份选项
  58. const months = [];
  59. const currentYear = new Date().getFullYear();
  60. // 生成当前学年(9月到次年6月)的月份选项
  61. for (let month = 9; month <= 12; month++) {
  62. months.push(`${currentYear}年${month}月`);
  63. }
  64. for (let month = 1; month <= 6; month++) {
  65. months.push(`${currentYear + 1}年${month}月`);
  66. }
  67. const selectedIndex = await window.AndroidBridgePromise.showSingleSelection(
  68. "请选择要获取课程的月份",
  69. JSON.stringify(months),
  70. 0
  71. );
  72. if (selectedIndex === null) return null;
  73. const selectedMonth = months[selectedIndex];
  74. const year = parseInt(selectedMonth.split('年')[0]);
  75. const month = parseInt(selectedMonth.split('年')[1].split('月')[0]);
  76. // 计算该月的开始和结束日期
  77. const startDate = new Date(year, month - 1, 1);
  78. const endDate = new Date(year, month, 0); // 该月最后一天
  79. return {
  80. startDate: startDate,
  81. endDate: endDate,
  82. year: year,
  83. month: month
  84. };
  85. } catch (err) {
  86. console.error("选择月份失败:", err);
  87. return null;
  88. }
  89. }
  90. // ====================== 异步获取课程数据 ======================
  91. async function fetchCoursesData() {
  92. try {
  93. // 让用户选择月份
  94. const monthSelection = await selectMonth();
  95. if (!monthSelection) {
  96. AndroidBridge.showToast("用户取消选择月份!");
  97. return null;
  98. }
  99. // 格式化日期为API需要的格式
  100. const formatDate = (date) => {
  101. const year = date.getFullYear();
  102. const month = String(date.getMonth() + 1).padStart(2, '0');
  103. const day = String(date.getDate()).padStart(2, '0');
  104. return `${year}-${month}-${day}+00%3A00%3A00`;
  105. };
  106. const d1 = formatDate(monthSelection.startDate);
  107. const d2 = formatDate(monthSelection.endDate);
  108. const res = await fetch("https://jwc.htu.edu.cn/new/desktop/getCalendar", {
  109. method: 'POST',
  110. headers: {
  111. 'Content-Type': 'application/x-www-form-urlencoded',
  112. },
  113. body: `d1=${d1}&d2=${d2}`
  114. });
  115. const json = await res.json();
  116. return json; // 直接返回课程数据数组
  117. } catch (err) {
  118. console.error("获取课程数据失败:", err);
  119. AndroidBridge.showToast("获取课程数据失败:" + err.message);
  120. return null;
  121. }
  122. }
  123. // ====================== 处理新格式的课程数据 ======================
  124. async function processCoursesData(coursesData) {
  125. if (!coursesData || !Array.isArray(coursesData)) {
  126. return null;
  127. }
  128. const allCourses = [];
  129. coursesData.forEach((course) => {
  130. // 解析节次代码 (如 "0910" -> 第9节到第10节)
  131. const jcdm = course.jcdm || "";
  132. if (!jcdm || jcdm.length < 4) {
  133. return;
  134. }
  135. const startSection = parseInt(jcdm.substring(0, 2));
  136. const endSection = parseInt(jcdm.substring(2, 4));
  137. // 解析周次 (如 "10" -> [10])
  138. const weeks = parseWeeks(course.zc || "");
  139. if (!weeks.length) {
  140. return;
  141. }
  142. // 解析星期 (如 "3" -> 星期三)
  143. const day = parseInt(course.xq || "1");
  144. if (day < 1 || day > 7) {
  145. return;
  146. }
  147. // 处理地点格式,按照 [校本部]西校区新联楼0303 的格式
  148. let position = course.jxcdmc || "";
  149. const xqmc = course.xqmc || "校本部";
  150. if (position && !position.startsWith("[")) {
  151. position = `[${xqmc}]${position}`;
  152. }
  153. const processedCourse = {
  154. name: course.kcmc || "",
  155. teacher: course.teaxms || "",
  156. position: position,
  157. day: day,
  158. startSection: startSection,
  159. endSection: endSection,
  160. weeks: weeks
  161. };
  162. allCourses.push(processedCourse);
  163. });
  164. const mergedCourses = mergeDuplicateCourses(allCourses);
  165. return mergedCourses.length ? mergedCourses : null;
  166. }
  167. // ====================== 保存课程 ======================
  168. async function saveCourses(courses) {
  169. try {
  170. const result = await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
  171. if (result === true) {
  172. AndroidBridge.showToast("课程导入成功!");
  173. return true;
  174. } else {
  175. AndroidBridge.showToast("课程导入失败,请查看日志!");
  176. return false;
  177. }
  178. } catch (err) {
  179. console.error("保存课程失败:", err);
  180. AndroidBridge.showToast("保存课程失败:" + err.message);
  181. return false;
  182. }
  183. }
  184. // ====================== 导入预设时间段(一天5节大课,10节小课) ======================
  185. async function importPresetTimeSlots() {
  186. // 一天5节大课,10节小课的时间安排
  187. const presetTimeSlots = [
  188. { number: 1, startTime: "08:00", endTime: "08:45" }, // 第1节
  189. { number: 2, startTime: "08:45", endTime: "09:30" }, // 第2节
  190. { number: 3, startTime: "09:50", endTime: "10:35" }, // 第3节
  191. { number: 4, startTime: "10:35", endTime: "11:20" }, // 第4节
  192. { number: 5, startTime: "14:00", endTime: "14:45" }, // 第5节
  193. { number: 6, startTime: "14:45", endTime: "15:30" }, // 第6节
  194. { number: 7, startTime: "15:50", endTime: "16:35" }, // 第7节
  195. { number: 8, startTime: "16:35", endTime: "17:20" }, // 第8节
  196. { number: 9, startTime: "19:00", endTime: "19:45" }, // 第9节
  197. { number: 10, startTime: "19:45", endTime: "20:30" } // 第10节
  198. ];
  199. try {
  200. const result = await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(presetTimeSlots));
  201. if (result === true) {
  202. AndroidBridge.showToast("时间段导入成功!");
  203. } else {
  204. AndroidBridge.showToast("时间段导入失败,请查看日志!");
  205. }
  206. } catch (err) {
  207. console.error("时间段导入失败:", err);
  208. AndroidBridge.showToast("时间段导入失败:" + err.message);
  209. }
  210. }
  211. // ====================== 显示公告 ======================
  212. async function showAnnouncement() {
  213. try {
  214. const announcement = `
  215. 课程导入工具使用说明:
  216. 1. 本工具用于从教务系统获取课程数据
  217. 2. 请确保已登录教务系统
  218. 3. 选择要获取课程的月份
  219. 4. 工具会自动下载原始数据和处理后的数据
  220. 5. 支持一天10节课的时间安排
  221. 6. 自动合并重复课程
  222. 使用前请确认网络连接正常!
  223. `.trim();
  224. await window.AndroidBridgePromise.showAlert(
  225. "课程导入工具公告",
  226. announcement
  227. );
  228. } catch (err) {
  229. console.error("显示公告失败:", err);
  230. // 如果弹窗失败,使用Toast提示
  231. AndroidBridge.showToast("课程导入工具启动中...");
  232. }
  233. }
  234. // ====================== 主流程 ======================
  235. async function runImportFlow() {
  236. // 检查用户是否已登录
  237. if (!isUserLoggedIn()) {
  238. AndroidBridge.showToast("检测到未登录状态,请先登录后再使用课程导入功能!");
  239. return;
  240. }
  241. // 显示公告
  242. await showAnnouncement();
  243. AndroidBridge.showToast("课程导入流程即将开始...");
  244. // 1️⃣ 获取课程数据
  245. const coursesData = await fetchCoursesData();
  246. if (!coursesData) {
  247. return;
  248. }
  249. // 2️⃣ 处理课程数据
  250. const courses = await processCoursesData(coursesData);
  251. if (!courses) {
  252. AndroidBridge.showToast("没有找到有效的课程数据!");
  253. return;
  254. }
  255. // 3️⃣ 保存课程
  256. const saveResult = await saveCourses(courses);
  257. if (!saveResult) {
  258. return;
  259. }
  260. // 4️⃣ 导入预设时间段
  261. await importPresetTimeSlots();
  262. // ✅ 只有所有步骤都成功完成,才通知任务完成
  263. AndroidBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
  264. console.log("JS:整个导入流程执行完毕并成功。");
  265. AndroidBridge.notifyTaskCompletion();
  266. }
  267. // 启动流程
  268. runImportFlow();