sddfvc.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // 山东药品食品职业学院(sddfvc.edu.cn) 拾光课程表适配脚本
  2. // 非该大学开发者适配,开发者无法及时发现问题
  3. // 出现问题请提联系开发者或者提交pr更改,这更加快速
  4. // 数据解析函数
  5. /**
  6. * 将周次字符串结合单双周标识解析为数字数组
  7. * @param {string} zcString "7-15,17-20"
  8. * @param {number} dsz 0: 全周, 1: 单周, 2: 双周, -1: 全周
  9. */
  10. function parseWeeks(zcString, dsz) {
  11. let weeks = [];
  12. if (!zcString) return weeks;
  13. // 解析基础周次
  14. zcString.split(',').forEach(part => {
  15. if (part.includes('-')) {
  16. const [start, end] = part.split('-').map(Number);
  17. for (let i = start; i <= end; i++) weeks.push(i);
  18. } else {
  19. weeks.push(Number(part));
  20. }
  21. });
  22. // 处理单双周过滤
  23. if (dsz === 1) {
  24. weeks = weeks.filter(w => w % 2 !== 0);
  25. } else if (dsz === 2) {
  26. weeks = weeks.filter(w => w % 2 === 0);
  27. }
  28. return weeks;
  29. }
  30. /**
  31. * 转换课程格式为应用模型
  32. */
  33. function parseCoursesToModel(sourceData) {
  34. const resultCourses = [];
  35. const days = ["xq1", "xq2", "xq3", "xq4", "xq5", "xq6", "xq7"];
  36. const sectionMap = { "1": 1, "3": 2, "5": 3, "7": 4, "9": 5, "11": 6 };
  37. days.forEach((dayKey, index) => {
  38. const dayContent = sourceData[dayKey];
  39. if (!dayContent) return;
  40. Object.keys(dayContent).forEach(slotNum => {
  41. const mappedSection = sectionMap[slotNum];
  42. if (!mappedSection) return;
  43. Object.values(dayContent[slotNum]).forEach(item => {
  44. const courseName = item.skbj[0][1];
  45. Object.values(item.pkmx).forEach(detail => {
  46. if (!detail) return;
  47. const computedWeeks = parseWeeks(detail.zc.zc, detail.zc.dsz);
  48. if (computedWeeks.length === 0) return;
  49. resultCourses.push({
  50. "name": courseName,
  51. "teacher": detail.teacher[0]?.xm || "未知教师",
  52. "position": detail.classroom || "未知地点",
  53. "day": index + 1,
  54. "startSection": mappedSection,
  55. "endSection": mappedSection,
  56. "weeks": computedWeeks
  57. });
  58. });
  59. });
  60. });
  61. });
  62. return resultCourses;
  63. }
  64. // 网络与交互业务函数
  65. /**
  66. * 保存课表全局配置
  67. */
  68. async function saveAppConfig() {
  69. const config = {
  70. "semesterTotalWeeks": 22,
  71. "defaultClassDuration": 90,
  72. "defaultBreakDuration": 15,
  73. "firstDayOfWeek": 1
  74. };
  75. return await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
  76. }
  77. /**
  78. * 保存时间段配置
  79. */
  80. async function saveAppTimeSlots() {
  81. const timeSlots = [
  82. { "number": 1, "startTime": "08:30", "endTime": "10:00" },
  83. { "number": 2, "startTime": "10:15", "endTime": "11:45" },
  84. { "number": 3, "startTime": "13:30", "endTime": "15:00" },
  85. { "number": 4, "startTime": "15:15", "endTime": "16:45" },
  86. { "number": 5, "startTime": "19:00", "endTime": "19:30" },
  87. { "number": 6, "startTime": "20:00", "endTime": "20:45" }
  88. ];
  89. return await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  90. }
  91. /**
  92. * 获取并让用户选择学期 ID
  93. */
  94. async function getSelectedSemesterId(apiToken) {
  95. const xqRes = await fetch(`http://jwxt.sddfvc.edu.cn/mobile/student/mobile_kcb_xq?api_token=${apiToken}`);
  96. const xqJson = await xqRes.json();
  97. const xqList = xqJson.data.xq_all;
  98. const currentXq = xqJson.data.xq_current;
  99. const xqNames = xqList.map(item => item.xqmc);
  100. const defaultIdx = xqList.findIndex(item => item.id === currentXq.id);
  101. const selectedIdx = await window.AndroidBridgePromise.showSingleSelection(
  102. "请确认导入学期",
  103. JSON.stringify(xqNames),
  104. defaultIdx !== -1 ? defaultIdx : xqNames.length - 1
  105. );
  106. return selectedIdx !== null ? xqList[selectedIdx].id : null;
  107. }
  108. /**
  109. * 核心合并逻辑:将多周次数据合并为学期格式
  110. * @param {Array} allWeekCourses 所有周次抓取到的原始课程数组
  111. */
  112. function mergeWeeklyCourses(allWeekCourses) {
  113. const courseMap = new Map();
  114. allWeekCourses.forEach(course => {
  115. // 生成唯一标识:名称+老师+地点+星期+节次
  116. const key = `${course.name}|${course.teacher}|${course.position}|${course.day}|${course.startSection}`;
  117. if (courseMap.has(key)) {
  118. const existing = courseMap.get(key);
  119. // 合并周次并去重排序
  120. const combinedWeeks = [...new Set([...existing.weeks, ...course.weeks])].sort((a, b) => a - b);
  121. existing.weeks = combinedWeeks;
  122. } else {
  123. courseMap.set(key, { ...course });
  124. }
  125. });
  126. return Array.from(courseMap.values());
  127. }
  128. /**
  129. * 遍历抓取 1-22 周的数据
  130. */
  131. async function fetchFullSemesterData(apiToken, semesterId) {
  132. let allCourses = [];
  133. const totalWeeks = 22;
  134. for (let w = 1; w <= totalWeeks; w++) {
  135. AndroidBridge.showToast(`正在获取第 ${w}/${totalWeeks} 周...`);
  136. try {
  137. const res = await fetch(`http://jwxt.sddfvc.edu.cn/mobile/student/mobile_kcb?api_token=${apiToken}&xq=${semesterId}&week=${w}`);
  138. const json = await res.json();
  139. if (json.data) {
  140. const weekCourses = parseCoursesToModel(json.data);
  141. allCourses = allCourses.concat(weekCourses);
  142. }
  143. } catch (e) {
  144. console.warn(`第 ${w} 周数据抓取失败`, e);
  145. }
  146. }
  147. // 调用合并函数
  148. return mergeWeeklyCourses(allCourses);
  149. }
  150. // 流程控制
  151. async function runImportFlow() {
  152. try {
  153. const urlParams = new URLSearchParams(window.location.search);
  154. const apiToken = urlParams.get('api_token');
  155. if (!apiToken) {
  156. console.error("当前 URL 中未找到 api_token 参数:", window.location.href);
  157. AndroidBridge.showToast("未检测到登录 Token,请确保在课表页面运行");
  158. return;
  159. }
  160. const semesterId = await getSelectedSemesterId(apiToken);
  161. if (!semesterId) {
  162. AndroidBridge.showToast("导入已取消");
  163. return;
  164. }
  165. AndroidBridge.showToast("尝试获取学期总表...");
  166. const kcbRes = await fetch(`http://jwxt.sddfvc.edu.cn/mobile/student/mobile_kcb?api_token=${apiToken}&xq=${semesterId}`);
  167. const kcbJson = await kcbRes.json();
  168. let finalCourses = parseCoursesToModel(kcbJson.data);
  169. if (finalCourses.length === 0) {
  170. AndroidBridge.showToast("总表无数据,启动周遍历模式...");
  171. finalCourses = await fetchFullSemesterData(apiToken, semesterId);
  172. }
  173. if (finalCourses.length === 0) {
  174. AndroidBridge.showToast("未发现任何课程数据");
  175. return;
  176. }
  177. // 保存逻辑
  178. AndroidBridge.showToast("正在保存配置...");
  179. await saveAppConfig();
  180. await saveAppTimeSlots();
  181. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(finalCourses));
  182. AndroidBridge.showToast(`成功导入 ${finalCourses.length} 门课程`);
  183. AndroidBridge.notifyTaskCompletion();
  184. } catch (error) {
  185. AndroidBridge.showToast("异常: " + error.message);
  186. }
  187. }
  188. runImportFlow();