sddfvc.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 };
  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. "defaultClassDuration": 90,
  71. "defaultBreakDuration": 15,
  72. "firstDayOfWeek": 1
  73. };
  74. return await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
  75. }
  76. /**
  77. * 保存时间段配置
  78. */
  79. async function saveAppTimeSlots() {
  80. const timeSlots = [
  81. { "number": 1, "startTime": "08:30", "endTime": "10:00" },
  82. { "number": 2, "startTime": "10:15", "endTime": "11:45" },
  83. { "number": 3, "startTime": "13:30", "endTime": "15:00" },
  84. { "number": 4, "startTime": "15:15", "endTime": "16:45" }
  85. ];
  86. return await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
  87. }
  88. /**
  89. * 获取并让用户选择学期 ID
  90. */
  91. async function getSelectedSemesterId(apiToken) {
  92. const xqRes = await fetch(`http://jwxt.sddfvc.edu.cn/mobile/student/mobile_kcb_xq?api_token=${apiToken}`);
  93. const xqJson = await xqRes.json();
  94. const xqList = xqJson.data.xq_all;
  95. const currentXq = xqJson.data.xq_current;
  96. const xqNames = xqList.map(item => item.xqmc);
  97. const defaultIdx = xqList.findIndex(item => item.id === currentXq.id);
  98. const selectedIdx = await window.AndroidBridgePromise.showSingleSelection(
  99. "请确认导入学期",
  100. JSON.stringify(xqNames),
  101. defaultIdx !== -1 ? defaultIdx : xqNames.length - 1
  102. );
  103. return selectedIdx !== null ? xqList[selectedIdx].id : null;
  104. }
  105. // 流程控制
  106. async function runImportFlow() {
  107. try {
  108. const tokenMatch = document.cookie.match(/api_token=([^;]+)/);
  109. if (!tokenMatch) {
  110. AndroidBridge.showToast("未检测到登录状态,请先登录");
  111. return;
  112. }
  113. const apiToken = tokenMatch[1];
  114. const semesterId = await getSelectedSemesterId(apiToken);
  115. if (!semesterId) {
  116. AndroidBridge.showToast("导入已取消");
  117. return;
  118. }
  119. AndroidBridge.showToast("正在获取课表数据...");
  120. const kcbRes = await fetch(`http://jwxt.sddfvc.edu.cn/mobile/student/mobile_kcb?api_token=${apiToken}&xq=${semesterId}`);
  121. const kcbJson = await kcbRes.json();
  122. const finalCourses = parseCoursesToModel(kcbJson.data);
  123. if (finalCourses.length === 0) {
  124. AndroidBridge.showToast("该学期暂无课程");
  125. return;
  126. }
  127. AndroidBridge.showToast("正在保存配置...");
  128. await saveAppConfig();
  129. await saveAppTimeSlots();
  130. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(finalCourses));
  131. AndroidBridge.showToast(`成功导入 ${finalCourses.length} 条课程明细`);
  132. AndroidBridge.notifyTaskCompletion();
  133. } catch (error) {
  134. console.error(error);
  135. AndroidBridge.showToast("异常: " + error.message);
  136. }
  137. }
  138. runImportFlow();