|
|
@@ -0,0 +1,278 @@
|
|
|
+// 江西航空职业技术学院(jhzyedu.cn) 拾光课程表适配脚本
|
|
|
+// 正方教务系统V9.0 API适配方案
|
|
|
+// 参考:GDUST广东科技学院适配案例
|
|
|
+
|
|
|
+/**
|
|
|
+ * 解析周次字符串,处理单双周和周次范围。
|
|
|
+ */
|
|
|
+function parseWeeks(weekStr) {
|
|
|
+ if (!weekStr) return [];
|
|
|
+
|
|
|
+ const weekSets = weekStr.split(',');
|
|
|
+ let weeks = [];
|
|
|
+
|
|
|
+ for (const set of weekSets) {
|
|
|
+ const trimmedSet = set.trim();
|
|
|
+
|
|
|
+ const rangeMatch = trimmedSet.match(/(\d+)-(\d+)周/);
|
|
|
+ const singleMatch = trimmedSet.match(/^(\d+)周/);
|
|
|
+
|
|
|
+ let start = 0;
|
|
|
+ let end = 0;
|
|
|
+ let processed = false;
|
|
|
+
|
|
|
+ if (rangeMatch) {
|
|
|
+ start = Number(rangeMatch[1]);
|
|
|
+ end = Number(rangeMatch[2]);
|
|
|
+ processed = true;
|
|
|
+ } else if (singleMatch) {
|
|
|
+ start = end = Number(singleMatch[1]);
|
|
|
+ processed = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (processed) {
|
|
|
+ const isSingle = trimmedSet.includes('(单)');
|
|
|
+ const isDouble = trimmedSet.includes('(双)');
|
|
|
+
|
|
|
+ for (let w = start; w <= end; w++) {
|
|
|
+ if (isSingle && w % 2 === 0) continue;
|
|
|
+ if (isDouble && w % 2 !== 0) continue;
|
|
|
+ weeks.push(w);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [...new Set(weeks)].sort((a, b) => a - b);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 解析 API 返回的 JSON 数据。
|
|
|
+ */
|
|
|
+function parseJsonData(jsonData) {
|
|
|
+ console.log("JS: parseJsonData 正在解析 JSON 数据...");
|
|
|
+
|
|
|
+ if (!jsonData || !Array.isArray(jsonData.kbList)) {
|
|
|
+ console.warn("JS: JSON 数据结构错误或缺少 kbList 字段。");
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
+ const rawCourseList = jsonData.kbList;
|
|
|
+ const finalCourseList = [];
|
|
|
+
|
|
|
+ for (const rawCourse of rawCourseList) {
|
|
|
+ if (!rawCourse.kcmc || !rawCourse.xm || !rawCourse.cdmc ||
|
|
|
+ !rawCourse.xqj || !rawCourse.jcs || !rawCourse.zcd) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ const weeksArray = parseWeeks(rawCourse.zcd);
|
|
|
+
|
|
|
+ if (weeksArray.length === 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ const sectionParts = rawCourse.jcs.split('-');
|
|
|
+ const startSection = Number(sectionParts[0]);
|
|
|
+ const endSection = Number(sectionParts[sectionParts.length - 1]);
|
|
|
+
|
|
|
+ const day = Number(rawCourse.xqj);
|
|
|
+
|
|
|
+ if (isNaN(day) || isNaN(startSection) || isNaN(endSection) || day < 1 || day > 7 || startSection > endSection) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ finalCourseList.push({
|
|
|
+ name: rawCourse.kcmc.trim(),
|
|
|
+ teacher: rawCourse.xm.trim(),
|
|
|
+ position: rawCourse.cdmc.trim(),
|
|
|
+ day: day,
|
|
|
+ startSection: startSection,
|
|
|
+ endSection: endSection,
|
|
|
+ weeks: weeksArray
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ finalCourseList.sort((a, b) =>
|
|
|
+ a.day - b.day ||
|
|
|
+ a.startSection - b.startSection ||
|
|
|
+ a.name.localeCompare(b.name)
|
|
|
+ );
|
|
|
+
|
|
|
+ console.log(`JS: JSON 数据解析完成,共找到 ${finalCourseList.length} 门课程。`);
|
|
|
+ return finalCourseList;
|
|
|
+}
|
|
|
+
|
|
|
+function validateYearInput(input) {
|
|
|
+ if (/^[0-9]{4}$/.test(input)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return "请输入四位数字的学年!";
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+async function promptUserToStart() {
|
|
|
+ return await window.AndroidBridgePromise.showAlert(
|
|
|
+ "教务系统课表导入",
|
|
|
+ "导入前请确保您已在浏览器中成功登录教务系统。\n将自动通过API获取课表数据,无需手动打开课表页面。",
|
|
|
+ "好的,开始导入"
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+async function getAcademicYear() {
|
|
|
+ const currentYear = new Date().getFullYear().toString();
|
|
|
+ return await window.AndroidBridgePromise.showPrompt(
|
|
|
+ "选择学年",
|
|
|
+ "请输入要导入课程的起始学年(例如 2025-2026 学年应输入 2025):",
|
|
|
+ currentYear,
|
|
|
+ "validateYearInput"
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+async function selectSemester() {
|
|
|
+ const semesters = ["第一学期", "第二学期"];
|
|
|
+ const semesterIndex = await window.AndroidBridgePromise.showSingleSelection(
|
|
|
+ "选择学期",
|
|
|
+ JSON.stringify(semesters),
|
|
|
+ 0
|
|
|
+ );
|
|
|
+ return semesterIndex;
|
|
|
+}
|
|
|
+
|
|
|
+function getSemesterCode(semesterIndex) {
|
|
|
+ // 正方教务V9.0: 3=第一学期, 12=第二学期
|
|
|
+ return semesterIndex === 0 ? "3" : "12";
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通过 API 请求课表数据
|
|
|
+ */
|
|
|
+async function fetchAndParseCourses(academicYear, semesterIndex) {
|
|
|
+ const semesterCode = getSemesterCode(semesterIndex);
|
|
|
+ const requestBody = "xnm=" + academicYear + "&xqm=" + semesterCode + "&kzlx=ck&xsdm=&kclbdm=";
|
|
|
+
|
|
|
+ const apiUrl = "https://jw.jhzyedu.cn/kbcx/xskbcx_cxXsgrkb.html?gnmkdm=N2151";
|
|
|
+
|
|
|
+ AndroidBridge.showToast("正在通过API获取课表数据...");
|
|
|
+
|
|
|
+ try {
|
|
|
+ const response = await fetch(apiUrl, {
|
|
|
+ method: "POST",
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
|
|
|
+ },
|
|
|
+ body: requestBody,
|
|
|
+ credentials: "include"
|
|
|
+ });
|
|
|
+
|
|
|
+ if (response.ok) {
|
|
|
+ const jsonText = await response.text();
|
|
|
+ const jsonData = JSON.parse(jsonText);
|
|
|
+ if (jsonData && jsonData.kbList) {
|
|
|
+ const parsedCourses = parseJsonData(jsonData);
|
|
|
+ if (parsedCourses.length > 0) {
|
|
|
+ return {
|
|
|
+ courses: parsedCourses,
|
|
|
+ config: {
|
|
|
+ semesterStartDate: null,
|
|
|
+ semesterTotalWeeks: 20
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ AndroidBridge.showToast("API 返回数据异常,请检查登录状态或学年学期选择。");
|
|
|
+ return null;
|
|
|
+ } catch (e) {
|
|
|
+ console.error("API fetch error: " + e.message);
|
|
|
+ AndroidBridge.showToast("请求课表数据失败,请确认已登录教务系统。");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+async function saveCourses(parsedCourses) {
|
|
|
+ AndroidBridge.showToast("正在保存 " + parsedCourses.length + " 门课程...");
|
|
|
+ try {
|
|
|
+ await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses, null, 2));
|
|
|
+ console.log("JS: 课程保存成功!");
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ AndroidBridge.showToast("课程保存失败: " + error.message);
|
|
|
+ console.error('JS: Save Courses Error:', error);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 江西航空职业技术学院作息时间(每节40分钟)
|
|
|
+const TimeSlots = [
|
|
|
+ { number: 1, startTime: "09:00", endTime: "09:40" },
|
|
|
+ { number: 2, startTime: "09:45", endTime: "10:25" },
|
|
|
+ { number: 3, startTime: "10:35", endTime: "11:15" },
|
|
|
+ { number: 4, startTime: "11:20", endTime: "12:00" },
|
|
|
+ { number: 5, startTime: "13:30", endTime: "14:10" },
|
|
|
+ { number: 6, startTime: "14:15", endTime: "14:55" },
|
|
|
+ { number: 7, startTime: "15:05", endTime: "15:45" },
|
|
|
+ { number: 8, startTime: "15:50", endTime: "16:30" }
|
|
|
+];
|
|
|
+
|
|
|
+async function importPresetTimeSlots(timeSlots) {
|
|
|
+ if (timeSlots.length > 0) {
|
|
|
+ AndroidBridge.showToast("正在导入 " + timeSlots.length + " 个预设时间段...");
|
|
|
+ try {
|
|
|
+ await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
|
|
|
+ AndroidBridge.showToast("预设时间段导入成功!");
|
|
|
+ console.log("JS: 预设时间段导入成功。");
|
|
|
+ } catch (error) {
|
|
|
+ AndroidBridge.showToast("导入时间段失败: " + error.message);
|
|
|
+ console.error('JS: Save Time Slots Error:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+async function runImportFlow() {
|
|
|
+
|
|
|
+ const alertConfirmed = await promptUserToStart();
|
|
|
+ if (!alertConfirmed) {
|
|
|
+ AndroidBridge.showToast("用户取消了导入。");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const academicYear = await getAcademicYear();
|
|
|
+ if (academicYear === null) {
|
|
|
+ AndroidBridge.showToast("导入已取消。");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ const semesterIndex = await selectSemester();
|
|
|
+ if (semesterIndex === null || semesterIndex === -1) {
|
|
|
+ AndroidBridge.showToast("导入已取消。");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const result = await fetchAndParseCourses(academicYear, semesterIndex);
|
|
|
+ if (result === null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const { courses, config } = result;
|
|
|
+
|
|
|
+ const saveResult = await saveCourses(courses);
|
|
|
+ if (!saveResult) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
|
|
|
+ AndroidBridge.showToast("课表配置更新成功!总周数:" + config.semesterTotalWeeks + "周。");
|
|
|
+ } catch (error) {
|
|
|
+ AndroidBridge.showToast("课表配置保存失败: " + error.message);
|
|
|
+ }
|
|
|
+
|
|
|
+ await importPresetTimeSlots(TimeSlots);
|
|
|
+
|
|
|
+ AndroidBridge.showToast("课程导入成功,共导入 " + courses.length + " 门课程!");
|
|
|
+ console.log("JS: 整个导入流程执行完毕并成功。");
|
|
|
+ AndroidBridge.notifyTaskCompletion();
|
|
|
+}
|
|
|
+
|
|
|
+runImportFlow();
|