|
@@ -1,10 +1,103 @@
|
|
|
-// 齐齐哈尔工程学院(qqhrit.com)拾光课程表适配脚本
|
|
|
|
|
|
|
+// 齐齐哈尔工程学院(qqhrit.com) 拾光课程表适配脚本
|
|
|
// 基于正方教务系统接口适配
|
|
// 基于正方教务系统接口适配
|
|
|
// 非该大学开发者适配,开发者无法及时发现问题
|
|
// 非该大学开发者适配,开发者无法及时发现问题
|
|
|
// 出现问题请提issues或者提交pr更改,这更加快速
|
|
// 出现问题请提issues或者提交pr更改,这更加快速
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 解析周次字符串,处理单双周和周次范围。
|
|
|
|
|
|
|
+ * 节次与周次合并去重函数
|
|
|
|
|
+ * @param {Array<Object>} courses 原始解析课程数组
|
|
|
|
|
+ * @returns {Array<Object>} 合并去重后的课程数组
|
|
|
|
|
+ */
|
|
|
|
|
+function mergeAndDistinctCourses(courses) {
|
|
|
|
|
+ if (!Array.isArray(courses) || courses.length <= 1) return courses;
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 深拷贝并规范周次数据,过滤无效项
|
|
|
|
|
+ const list = courses.map(c => ({
|
|
|
|
|
+ ...c,
|
|
|
|
|
+ name: c.name || '',
|
|
|
|
|
+ teacher: c.teacher || '',
|
|
|
|
|
+ position: c.position || '',
|
|
|
|
|
+ weeks: Array.isArray(c.weeks) ? [...c.weeks].sort((a, b) => a - b) : []
|
|
|
|
|
+ }));
|
|
|
|
|
+
|
|
|
|
|
+ // 阶段 1:合并连续节次与完全重复记录(前提:名称、教师、地点、星期、周次一致)
|
|
|
|
|
+ list.sort((a, b) => {
|
|
|
|
|
+ return a.name.localeCompare(b.name) ||
|
|
|
|
|
+ a.teacher.localeCompare(b.teacher) ||
|
|
|
|
|
+ a.position.localeCompare(b.position) ||
|
|
|
|
|
+ (a.day || 0) - (b.day || 0) ||
|
|
|
|
|
+ a.weeks.join(',').localeCompare(b.weeks.join(',')) ||
|
|
|
|
|
+ (a.startSection || 0) - (b.startSection || 0);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const step1Merged = [];
|
|
|
|
|
+ let current = list[0];
|
|
|
|
|
+
|
|
|
|
|
+ for (let i = 1; i < list.length; i++) {
|
|
|
|
|
+ const next = list[i];
|
|
|
|
|
+
|
|
|
|
|
+ const isSameCourseAndWeeks =
|
|
|
|
|
+ current.name === next.name &&
|
|
|
|
|
+ current.teacher === next.teacher &&
|
|
|
|
|
+ current.position === next.position &&
|
|
|
|
|
+ current.day === next.day &&
|
|
|
|
|
+ current.weeks.join(',') === next.weeks.join(',');
|
|
|
|
|
+
|
|
|
|
|
+ const isContinuous = current.endSection + 1 === next.startSection;
|
|
|
|
|
+ const isDuplicate = current.startSection === next.startSection && current.endSection === next.endSection;
|
|
|
|
|
+
|
|
|
|
|
+ if (isSameCourseAndWeeks && isContinuous) {
|
|
|
|
|
+ // 节次连续:延长结束节次 (如 1-2 节 + 3-4 节 -> 1-4 节)
|
|
|
|
|
+ current.endSection = next.endSection;
|
|
|
|
|
+ } else if (isSameCourseAndWeeks && isDuplicate) {
|
|
|
|
|
+ // 完全重复:跳过
|
|
|
|
|
+ continue;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ step1Merged.push(current);
|
|
|
|
|
+ current = next;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ step1Merged.push(current);
|
|
|
|
|
+
|
|
|
|
|
+ // 阶段 2:合并同节次的周次(前提:名称、教师、地点、星期、开始/结束节次一致)
|
|
|
|
|
+ step1Merged.sort((a, b) => {
|
|
|
|
|
+ return a.name.localeCompare(b.name) ||
|
|
|
|
|
+ a.teacher.localeCompare(b.teacher) ||
|
|
|
|
|
+ a.position.localeCompare(b.position) ||
|
|
|
|
|
+ (a.day || 0) - (b.day || 0) ||
|
|
|
|
|
+ (a.startSection || 0) - (b.startSection || 0) ||
|
|
|
|
|
+ (a.endSection || 0) - (b.endSection || 0);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const step2Merged = [];
|
|
|
|
|
+ let cur = step1Merged[0];
|
|
|
|
|
+
|
|
|
|
|
+ for (let i = 1; i < step1Merged.length; i++) {
|
|
|
|
|
+ const nxt = step1Merged[i];
|
|
|
|
|
+
|
|
|
|
|
+ const isSameCourseAndSection =
|
|
|
|
|
+ cur.name === nxt.name &&
|
|
|
|
|
+ cur.teacher === nxt.teacher &&
|
|
|
|
|
+ cur.position === nxt.position &&
|
|
|
|
|
+ cur.day === nxt.day &&
|
|
|
|
|
+ cur.startSection === nxt.startSection &&
|
|
|
|
|
+ cur.endSection === nxt.endSection;
|
|
|
|
|
+
|
|
|
|
|
+ if (isSameCourseAndSection) {
|
|
|
|
|
+ // 周次合并去重 (如 1-8 周 + 9-16 周 -> 1-16 周)
|
|
|
|
|
+ cur.weeks = Array.from(new Set([...cur.weeks, ...nxt.weeks])).sort((a, b) => a - b);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ step2Merged.push(cur);
|
|
|
|
|
+ cur = nxt;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ step2Merged.push(cur);
|
|
|
|
|
+
|
|
|
|
|
+ return step2Merged;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 解析周次字符串,处理单双周和周次范围
|
|
|
*/
|
|
*/
|
|
|
function parseWeeks(weekStr) {
|
|
function parseWeeks(weekStr) {
|
|
|
if (!weekStr) return [];
|
|
if (!weekStr) return [];
|
|
@@ -16,39 +109,36 @@ function parseWeeks(weekStr) {
|
|
|
const trimmedSet = set.trim();
|
|
const trimmedSet = set.trim();
|
|
|
|
|
|
|
|
const rangeMatch = trimmedSet.match(/(\d+)-(\d+)周/);
|
|
const rangeMatch = trimmedSet.match(/(\d+)-(\d+)周/);
|
|
|
- const singleMatch = trimmedSet.match(/^(\d+)周/); // 匹配以数字周结束的
|
|
|
|
|
|
|
+ const singleMatch = trimmedSet.match(/^(\d+)周/);
|
|
|
|
|
|
|
|
let start = 0;
|
|
let start = 0;
|
|
|
let end = 0;
|
|
let end = 0;
|
|
|
let processed = false;
|
|
let processed = false;
|
|
|
|
|
|
|
|
- if (rangeMatch) { // 范围, 如 "1-5周"
|
|
|
|
|
|
|
+ if (rangeMatch) {
|
|
|
start = Number(rangeMatch[1]);
|
|
start = Number(rangeMatch[1]);
|
|
|
end = Number(rangeMatch[2]);
|
|
end = Number(rangeMatch[2]);
|
|
|
processed = true;
|
|
processed = true;
|
|
|
- } else if (singleMatch) { // 单个周, 如 "6周"
|
|
|
|
|
|
|
+ } else if (singleMatch) {
|
|
|
start = end = Number(singleMatch[1]);
|
|
start = end = Number(singleMatch[1]);
|
|
|
processed = true;
|
|
processed = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (processed) {
|
|
if (processed) {
|
|
|
- // 确定单双周
|
|
|
|
|
const isSingle = trimmedSet.includes('(单)');
|
|
const isSingle = trimmedSet.includes('(单)');
|
|
|
const isDouble = trimmedSet.includes('(双)');
|
|
const isDouble = trimmedSet.includes('(双)');
|
|
|
|
|
|
|
|
for (let w = start; w <= end; w++) {
|
|
for (let w = start; w <= end; w++) {
|
|
|
- if (isSingle && w % 2 === 0) continue; // 单周跳过偶数
|
|
|
|
|
- if (isDouble && w % 2 !== 0) continue; // 双周跳过奇数
|
|
|
|
|
|
|
+ if (isSingle && w % 2 === 0) continue;
|
|
|
|
|
+ if (isDouble && w % 2 !== 0) continue;
|
|
|
weeks.push(w);
|
|
weeks.push(w);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 去重并排序
|
|
|
|
|
return [...new Set(weeks)].sort((a, b) => a - b);
|
|
return [...new Set(weeks)].sort((a, b) => a - b);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 独立处理课程时间逻辑的函数
|
|
* 独立处理课程时间逻辑的函数
|
|
|
*/
|
|
*/
|
|
@@ -80,21 +170,13 @@ function applyCustomTimeLogic(courses) {
|
|
|
{ number: 4, startTime: "10:50", endTime: "11:35" }
|
|
{ number: 4, startTime: "10:50", endTime: "11:35" }
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- // 辅助函数:根据节次从方案中提取时间
|
|
|
|
|
- const getTimeFromScheme = (scheme, section) => {
|
|
|
|
|
- const slot = scheme.find(s => s.number === section);
|
|
|
|
|
- return slot ? [slot.startTime, slot.endTime] : null;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
return courses.map(course => {
|
|
return courses.map(course => {
|
|
|
const is23Sharp = /(2#|3#)/.test(course.position);
|
|
const is23Sharp = /(2#|3#)/.test(course.position);
|
|
|
const isLibMachine = /(图|馆|齐三机床)/.test(course.position);
|
|
const isLibMachine = /(图|馆|齐三机床)/.test(course.position);
|
|
|
const isWeekend = (course.day === 6 || course.day === 7);
|
|
const isWeekend = (course.day === 6 || course.day === 7);
|
|
|
const isSpecialPos = (is23Sharp || isLibMachine);
|
|
const isSpecialPos = (is23Sharp || isLibMachine);
|
|
|
-
|
|
|
|
|
const isEndpoint3or4 = (course.startSection === 3 || course.startSection === 4 ||
|
|
const isEndpoint3or4 = (course.startSection === 3 || course.startSection === 4 ||
|
|
|
course.endSection === 3 || course.endSection === 4);
|
|
course.endSection === 3 || course.endSection === 4);
|
|
|
-
|
|
|
|
|
const shouldApplyCustom = isWeekend || (isSpecialPos && isEndpoint3or4);
|
|
const shouldApplyCustom = isWeekend || (isSpecialPos && isEndpoint3or4);
|
|
|
|
|
|
|
|
if (shouldApplyCustom) {
|
|
if (shouldApplyCustom) {
|
|
@@ -107,7 +189,6 @@ function applyCustomTimeLogic(courses) {
|
|
|
// 工作日特殊教室逻辑
|
|
// 工作日特殊教室逻辑
|
|
|
const scheme = is23Sharp ? TIME_SCHEME_2 : TIME_SCHEME_3;
|
|
const scheme = is23Sharp ? TIME_SCHEME_2 : TIME_SCHEME_3;
|
|
|
|
|
|
|
|
- // 辅助函数:如果是 3 或 4 节则取特殊方案,否则取全局标准时间
|
|
|
|
|
const getSectionTime = (sec) => {
|
|
const getSectionTime = (sec) => {
|
|
|
if (sec === 3 || sec === 4) {
|
|
if (sec === 3 || sec === 4) {
|
|
|
return scheme.find(s => s.number === sec);
|
|
return scheme.find(s => s.number === sec);
|
|
@@ -119,7 +200,6 @@ function applyCustomTimeLogic(courses) {
|
|
|
endSlot = getSectionTime(course.endSection);
|
|
endSlot = getSectionTime(course.endSection);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 如果成功找到了对应的开始和结束时间,则应用自定义时间
|
|
|
|
|
if (startSlot && endSlot) {
|
|
if (startSlot && endSlot) {
|
|
|
return {
|
|
return {
|
|
|
...course,
|
|
...course,
|
|
@@ -134,90 +214,54 @@ function applyCustomTimeLogic(courses) {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 解析 API 返回的 JSON 数据。
|
|
|
|
|
|
|
+ * 解析 API 返回的 JSON 数据
|
|
|
*/
|
|
*/
|
|
|
function parseJsonData(jsonData) {
|
|
function parseJsonData(jsonData) {
|
|
|
- console.log("JS: parseJsonData 正在解析 JSON 数据...");
|
|
|
|
|
-
|
|
|
|
|
- // 检查JSON结构:新的数据在 kbList 字段中
|
|
|
|
|
if (!jsonData || !Array.isArray(jsonData.kbList)) {
|
|
if (!jsonData || !Array.isArray(jsonData.kbList)) {
|
|
|
- console.warn("JS: JSON 数据结构错误或缺少 kbList 字段。");
|
|
|
|
|
- return [];
|
|
|
|
|
|
|
+ return [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const rawCourseList = jsonData.kbList;
|
|
const rawCourseList = jsonData.kbList;
|
|
|
- const finalCourseList = [];
|
|
|
|
|
|
|
+ const initialCourseList = [];
|
|
|
|
|
|
|
|
for (const rawCourse of rawCourseList) {
|
|
for (const rawCourse of rawCourseList) {
|
|
|
- // 关键字段检查: kcmc(课名), xm(教师), cdmc(教室), xqj(星期), jcs(节次范围), zcd(周次描述)
|
|
|
|
|
if (!rawCourse.kcmc || !rawCourse.xm || !rawCourse.cdmc ||
|
|
if (!rawCourse.kcmc || !rawCourse.xm || !rawCourse.cdmc ||
|
|
|
!rawCourse.xqj || !rawCourse.jcs || !rawCourse.zcd) {
|
|
!rawCourse.xqj || !rawCourse.jcs || !rawCourse.zcd) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const weeksArray = parseWeeks(rawCourse.zcd);
|
|
const weeksArray = parseWeeks(rawCourse.zcd);
|
|
|
-
|
|
|
|
|
- // 周次有效性检查
|
|
|
|
|
if (weeksArray.length === 0) {
|
|
if (weeksArray.length === 0) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 解析节次范围,例如 "1-2"
|
|
|
|
|
const sectionParts = rawCourse.jcs.split('-');
|
|
const sectionParts = rawCourse.jcs.split('-');
|
|
|
const startSection = Number(sectionParts[0]);
|
|
const startSection = Number(sectionParts[0]);
|
|
|
- const endSection = Number(sectionParts[sectionParts.length - 1]);
|
|
|
|
|
-
|
|
|
|
|
- const day = Number(rawCourse.xqj); // xqj: 星期几 (周一为1, 周日为7)
|
|
|
|
|
|
|
+ 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) {
|
|
|
|
|
- // console.warn(`JS: 课程 ${rawCourse.kcmc} 星期或节次数据无效,跳过。`);
|
|
|
|
|
|
|
+ if (isNaN(day) || isNaN(startSection) || isNaN(endSection) ||
|
|
|
|
|
+ day < 1 || day > 7 || startSection > endSection) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- finalCourseList.push({
|
|
|
|
|
|
|
+ initialCourseList.push({
|
|
|
name: rawCourse.kcmc.trim(),
|
|
name: rawCourse.kcmc.trim(),
|
|
|
teacher: rawCourse.xm.trim(),
|
|
teacher: rawCourse.xm.trim(),
|
|
|
position: rawCourse.cdmc.trim(),
|
|
position: rawCourse.cdmc.trim(),
|
|
|
- day: day,
|
|
|
|
|
|
|
+ day: day,
|
|
|
startSection: startSection,
|
|
startSection: startSection,
|
|
|
- endSection: endSection,
|
|
|
|
|
|
|
+ endSection: endSection,
|
|
|
weeks: weeksArray
|
|
weeks: weeksArray
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- finalCourseList.sort((a, b) =>
|
|
|
|
|
- a.day - b.day ||
|
|
|
|
|
- a.startSection - b.startSection ||
|
|
|
|
|
- a.name.localeCompare(b.name)
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- // 课程时间特殊处理
|
|
|
|
|
- const processedCourses = applyCustomTimeLogic(finalCourseList);
|
|
|
|
|
-
|
|
|
|
|
- console.log(`JS: JSON 数据解析完成,共找到 ${finalCourseList.length} 门课程。`);
|
|
|
|
|
- return processedCourses;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-function validateYearInput(input) {
|
|
|
|
|
- console.log("JS: validateYearInput 被调用,输入: " + input);
|
|
|
|
|
- if (/^[0-9]{4}$/.test(input)) {
|
|
|
|
|
- console.log("JS: validateYearInput 验证通过。");
|
|
|
|
|
- return false;
|
|
|
|
|
- } else {
|
|
|
|
|
- console.log("JS: validateYearInput 验证失败。");
|
|
|
|
|
- return "请输入四位数字的学年!";
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const mergedCourses = mergeAndDistinctCourses(initialCourseList);
|
|
|
|
|
+ return applyCustomTimeLogic(mergedCourses);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function promptUserToStart() {
|
|
async function promptUserToStart() {
|
|
|
- console.log("JS: 流程开始:显示公告。");
|
|
|
|
|
return await window.shiguangBridgePromise.showAlert(
|
|
return await window.shiguangBridgePromise.showAlert(
|
|
|
"教务系统课表导入",
|
|
"教务系统课表导入",
|
|
|
"导入前请确保您已在浏览器中成功登录教务系统",
|
|
"导入前请确保您已在浏览器中成功登录教务系统",
|
|
@@ -225,57 +269,186 @@ async function promptUserToStart() {
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function getAcademicYear() {
|
|
|
|
|
- const currentYear = new Date().getFullYear().toString();
|
|
|
|
|
- console.log("JS: 提示用户输入学年。");
|
|
|
|
|
- return await window.shiguangBridgePromise.showPrompt(
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 从教务系统获取学年学期选项
|
|
|
|
|
+ * 学年:以选中项为中心,取前2年+后2年,共5个选项
|
|
|
|
|
+ */
|
|
|
|
|
+async function fetchAcademicOptions() {
|
|
|
|
|
+ const url = "http://jwxt.qqhrit.com:20266/jwglxt/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N2151";
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ const response = await fetch(url, {
|
|
|
|
|
+ method: "GET",
|
|
|
|
|
+ credentials: "include"
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (!response.ok) return null;
|
|
|
|
|
+
|
|
|
|
|
+ const htmlText = await response.text();
|
|
|
|
|
+ const parser = new DOMParser();
|
|
|
|
|
+ const doc = parser.parseFromString(htmlText, "text/html");
|
|
|
|
|
+
|
|
|
|
|
+ const allYearOptions = Array.from(doc.querySelectorAll("#xnm option"))
|
|
|
|
|
+ .filter(opt => opt.value !== "")
|
|
|
|
|
+ .map(opt => ({
|
|
|
|
|
+ value: opt.value,
|
|
|
|
|
+ text: opt.textContent.trim(),
|
|
|
|
|
+ selected: opt.selected
|
|
|
|
|
+ }));
|
|
|
|
|
+
|
|
|
|
|
+ const semesterOptions = Array.from(doc.querySelectorAll("#xqm option"))
|
|
|
|
|
+ .filter(opt => opt.value !== "")
|
|
|
|
|
+ .map(opt => ({
|
|
|
|
|
+ value: opt.value,
|
|
|
|
|
+ text: opt.textContent.trim(),
|
|
|
|
|
+ selected: opt.selected
|
|
|
|
|
+ }));
|
|
|
|
|
+
|
|
|
|
|
+ if (allYearOptions.length === 0 || semesterOptions.length === 0) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const selectedIndex = allYearOptions.findIndex(opt => opt.selected);
|
|
|
|
|
+
|
|
|
|
|
+ if (selectedIndex === -1) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ yearOptions: allYearOptions.slice(0, 5),
|
|
|
|
|
+ semesterOptions,
|
|
|
|
|
+ defaultYearIndex: 0,
|
|
|
|
|
+ defaultSemesterIndex: semesterOptions.findIndex(opt => opt.selected) !== -1
|
|
|
|
|
+ ? semesterOptions.findIndex(opt => opt.selected)
|
|
|
|
|
+ : 0
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const start = Math.max(0, selectedIndex - 2);
|
|
|
|
|
+ const end = Math.min(allYearOptions.length, selectedIndex + 3);
|
|
|
|
|
+ const yearOptions = allYearOptions.slice(start, end);
|
|
|
|
|
+ const newDefaultIndex = selectedIndex - start;
|
|
|
|
|
+ const defaultSemesterIndex = semesterOptions.findIndex(opt => opt.selected);
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ yearOptions,
|
|
|
|
|
+ semesterOptions,
|
|
|
|
|
+ defaultYearIndex: newDefaultIndex,
|
|
|
|
|
+ defaultSemesterIndex: defaultSemesterIndex !== -1 ? defaultSemesterIndex : 0
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 提示用户选择学年和学期
|
|
|
|
|
+ */
|
|
|
|
|
+async function selectAcademicYearAndSemester() {
|
|
|
|
|
+ const optionsData = await fetchAcademicOptions();
|
|
|
|
|
+
|
|
|
|
|
+ if (!optionsData) {
|
|
|
|
|
+ window.shiguangBridge.showToast("从教务系统读取学年学期失败,请确保登录状态。");
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const { yearOptions, semesterOptions, defaultYearIndex, defaultSemesterIndex } = optionsData;
|
|
|
|
|
+
|
|
|
|
|
+ const yearTexts = yearOptions.map(item => item.text);
|
|
|
|
|
+ const yearIndex = await window.shiguangBridgePromise.showSingleSelection(
|
|
|
"选择学年",
|
|
"选择学年",
|
|
|
- "请输入要导入课程的起始学年(例如 2025-2026 应输入2025):",
|
|
|
|
|
- currentYear,
|
|
|
|
|
- "validateYearInput"
|
|
|
|
|
|
|
+ JSON.stringify(yearTexts),
|
|
|
|
|
+ defaultYearIndex
|
|
|
);
|
|
);
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-async function selectSemester() {
|
|
|
|
|
- const semesters = ["第一学期", "第二学期"];
|
|
|
|
|
- console.log("JS: 提示用户选择学期。");
|
|
|
|
|
|
|
+ if (yearIndex === null || yearIndex === -1) return null;
|
|
|
|
|
+ const selectedYearCode = yearOptions[yearIndex].value;
|
|
|
|
|
+
|
|
|
|
|
+ const semesterTexts = semesterOptions.map(item => item.text);
|
|
|
const semesterIndex = await window.shiguangBridgePromise.showSingleSelection(
|
|
const semesterIndex = await window.shiguangBridgePromise.showSingleSelection(
|
|
|
"选择学期",
|
|
"选择学期",
|
|
|
- JSON.stringify(semesters),
|
|
|
|
|
- -1
|
|
|
|
|
|
|
+ JSON.stringify(semesterTexts),
|
|
|
|
|
+ defaultSemesterIndex
|
|
|
);
|
|
);
|
|
|
- return semesterIndex;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (semesterIndex === null || semesterIndex === -1) return null;
|
|
|
|
|
+ const selectedSemesterCode = semesterOptions[semesterIndex].value;
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ academicYear: selectedYearCode,
|
|
|
|
|
+ semesterCode: selectedSemesterCode
|
|
|
|
|
+ };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 将选择索引转换为 API 所需的学期码。
|
|
|
|
|
|
|
+ * 获取学期开学日期
|
|
|
*/
|
|
*/
|
|
|
-function getSemesterCode(semesterIndex) {
|
|
|
|
|
- // semesterIndex 3 (第一学期), 12 (第二学期)
|
|
|
|
|
- return semesterIndex === 0 ? "3" : "12";
|
|
|
|
|
-}
|
|
|
|
|
|
|
+async function fetchSemesterStartDate(academicYear, semesterCode) {
|
|
|
|
|
+ const url = "http://jwxt.qqhrit.com:20266/jwglxt/kbcx/xskbcxZccx_cxZcByXnxq.html?gnmkdm=N2154";
|
|
|
|
|
+ const requestBody = `xnm=${academicYear}&xqm=${semesterCode}`;
|
|
|
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ const response = await fetch(url, {
|
|
|
|
|
+ method: "POST",
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ "accept": "application/json, text/javascript, */*; q=0.01",
|
|
|
|
|
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
|
|
|
+ "x-requested-with": "XMLHttpRequest"
|
|
|
|
|
+ },
|
|
|
|
|
+ body: requestBody,
|
|
|
|
|
+ credentials: "include"
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (response.ok) {
|
|
|
|
|
+ const json = await response.json();
|
|
|
|
|
+ if (Array.isArray(json) && json.length > 0) {
|
|
|
|
|
+ // 优先找第1周,否则取第一项
|
|
|
|
|
+ const firstWeekObj = json.find(item => String(item.zs) === "1" || String(item.zsmc) === "1") || json[0];
|
|
|
|
|
+
|
|
|
|
|
+ if (firstWeekObj.rq) {
|
|
|
|
|
+ const startDateStr = firstWeekObj.rq.split('/')[0];
|
|
|
|
|
+ if (/^\d{4}-\d{2}-\d{2}$/.test(startDateStr)) {
|
|
|
|
|
+ return startDateStr;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (firstWeekObj.zcrq) {
|
|
|
|
|
+ const match = firstWeekObj.zcrq.match(/(\d{4}-\d{2}-\d{2})/);
|
|
|
|
|
+ if (match) return match[1];
|
|
|
|
|
+ }
|
|
|
|
|
+ // 某些系统用 ksrq 字段
|
|
|
|
|
+ if (firstWeekObj.ksrq) {
|
|
|
|
|
+ const match = firstWeekObj.ksrq.match(/(\d{4}-\d{2}-\d{2})/);
|
|
|
|
|
+ if (match) return match[1];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ // 获取失败不影响主流程
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 请求和解析课程数据
|
|
* 请求和解析课程数据
|
|
|
*/
|
|
*/
|
|
|
-async function fetchAndParseCourses(academicYear, semesterIndex) {
|
|
|
|
|
- const semesterCode = getSemesterCode(semesterIndex);
|
|
|
|
|
|
|
+async function fetchAndParseCourses(academicYear, semesterCode) {
|
|
|
const requestBody = `xnm=${academicYear}&xqm=${semesterCode}&kzlx=ck&xsdm=&kclbdm=`;
|
|
const requestBody = `xnm=${academicYear}&xqm=${semesterCode}&kzlx=ck&xsdm=&kclbdm=`;
|
|
|
const targetUrl = "http://jwxt.qqhrit.com:20266/jwglxt/kbcx/xskbcx_cxXsgrkb.html?gnmkdm=N2151";
|
|
const targetUrl = "http://jwxt.qqhrit.com:20266/jwglxt/kbcx/xskbcx_cxXsgrkb.html?gnmkdm=N2151";
|
|
|
|
|
|
|
|
- try {
|
|
|
|
|
- const response = await fetch(targetUrl, {
|
|
|
|
|
|
|
+ // 并行获取课程数据和开学日期
|
|
|
|
|
+ const [courseResponse, semesterStartDate] = await Promise.all([
|
|
|
|
|
+ fetch(targetUrl, {
|
|
|
method: "POST",
|
|
method: "POST",
|
|
|
- headers: {
|
|
|
|
|
- "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
|
|
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
|
|
|
},
|
|
},
|
|
|
body: requestBody,
|
|
body: requestBody,
|
|
|
credentials: "include"
|
|
credentials: "include"
|
|
|
- });
|
|
|
|
|
|
|
+ }),
|
|
|
|
|
+ fetchSemesterStartDate(academicYear, semesterCode)
|
|
|
|
|
+ ]);
|
|
|
|
|
|
|
|
- if (response.ok) {
|
|
|
|
|
- const jsonText = await response.text();
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (courseResponse.ok) {
|
|
|
|
|
+ const jsonText = await courseResponse.text();
|
|
|
const jsonData = JSON.parse(jsonText);
|
|
const jsonData = JSON.parse(jsonText);
|
|
|
if (jsonData && jsonData.kbList) {
|
|
if (jsonData && jsonData.kbList) {
|
|
|
const parsedCourses = parseJsonData(jsonData);
|
|
const parsedCourses = parseJsonData(jsonData);
|
|
@@ -283,7 +456,7 @@ async function fetchAndParseCourses(academicYear, semesterIndex) {
|
|
|
return {
|
|
return {
|
|
|
courses: parsedCourses,
|
|
courses: parsedCourses,
|
|
|
config: {
|
|
config: {
|
|
|
- semesterStartDate: null,
|
|
|
|
|
|
|
+ semesterStartDate: semesterStartDate,
|
|
|
semesterTotalWeeks: 20
|
|
semesterTotalWeeks: 20
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -291,7 +464,7 @@ async function fetchAndParseCourses(academicYear, semesterIndex) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- console.error(`Request failed: ${targetUrl}`, e);
|
|
|
|
|
|
|
+ // 请求失败
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
window.shiguangBridge.showToast("未能获取课表数据,请检查网络环境或登录状态。");
|
|
window.shiguangBridge.showToast("未能获取课表数据,请检查网络环境或登录状态。");
|
|
@@ -299,21 +472,15 @@ async function fetchAndParseCourses(academicYear, semesterIndex) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function saveCourses(parsedCourses) {
|
|
async function saveCourses(parsedCourses) {
|
|
|
- window.shiguangBridge.showToast(`正在保存 ${parsedCourses.length} 门课程...`);
|
|
|
|
|
- console.log(`JS: 尝试保存 ${parsedCourses.length} 门课程...`);
|
|
|
|
|
try {
|
|
try {
|
|
|
- await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses, null, 2));
|
|
|
|
|
- console.log("JS: 课程保存成功!");
|
|
|
|
|
|
|
+ await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses));
|
|
|
return true;
|
|
return true;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
window.shiguangBridge.showToast(`课程保存失败: ${error.message}`);
|
|
window.shiguangBridge.showToast(`课程保存失败: ${error.message}`);
|
|
|
- console.error('JS: Save Courses Error:', error);
|
|
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// 统一作息时间
|
|
|
|
|
-// 3,4节作息采用1#、5#版本
|
|
|
|
|
const TimeSlots = [
|
|
const TimeSlots = [
|
|
|
{ number: 1, startTime: "08:00", endTime: "08:45" },
|
|
{ number: 1, startTime: "08:00", endTime: "08:45" },
|
|
|
{ number: 2, startTime: "08:55", endTime: "09:40" },
|
|
{ number: 2, startTime: "08:55", endTime: "09:40" },
|
|
@@ -323,84 +490,66 @@ const TimeSlots = [
|
|
|
{ number: 6, startTime: "14:25", endTime: "15:10" },
|
|
{ number: 6, startTime: "14:25", endTime: "15:10" },
|
|
|
{ number: 7, startTime: "15:20", endTime: "16:05" },
|
|
{ number: 7, startTime: "15:20", endTime: "16:05" },
|
|
|
{ number: 8, startTime: "16:05", endTime: "16:50" },
|
|
{ number: 8, startTime: "16:05", endTime: "16:50" },
|
|
|
- { number: 9, startTime: "18:00", "endTime": "18:45" },
|
|
|
|
|
- { number: 10, startTime: "18:45", "endTime": "19:30" },
|
|
|
|
|
- { number: 11, startTime: "19:30", "endTime": "20:15" }
|
|
|
|
|
|
|
+ { number: 9, startTime: "18:00", endTime: "18:45" },
|
|
|
|
|
+ { number: 10, startTime: "18:45", endTime: "19:30" },
|
|
|
|
|
+ { number: 11, startTime: "19:30", endTime: "20:15" }
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
async function importPresetTimeSlots(timeSlots) {
|
|
async function importPresetTimeSlots(timeSlots) {
|
|
|
- console.log(`JS: 准备导入 ${timeSlots.length} 个预设时间段。`);
|
|
|
|
|
-
|
|
|
|
|
- if (timeSlots.length > 0) {
|
|
|
|
|
- window.shiguangBridge.showToast(`正在导入 ${timeSlots.length} 个预设时间段...`);
|
|
|
|
|
- try {
|
|
|
|
|
- await window.shiguangBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
|
|
|
|
|
- window.shiguangBridge.showToast("预设时间段导入成功!");
|
|
|
|
|
- console.log("JS: 预设时间段导入成功。");
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- window.shiguangBridge.showToast("导入时间段失败: " + error.message);
|
|
|
|
|
- console.error('JS: Save Time Slots Error:', error);
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
|
|
+ if (timeSlots.length === 0) {
|
|
|
window.shiguangBridge.showToast("警告:时间段为空,未导入时间段信息。");
|
|
window.shiguangBridge.showToast("警告:时间段为空,未导入时间段信息。");
|
|
|
- console.warn("JS: 警告:传入时间段为空,未导入时间段信息。");
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await window.shiguangBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
|
|
|
|
|
+ window.shiguangBridge.showToast("预设时间段导入成功!");
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ window.shiguangBridge.showToast("导入时间段失败: " + error.message);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
async function runImportFlow() {
|
|
async function runImportFlow() {
|
|
|
-
|
|
|
|
|
const alertConfirmed = await promptUserToStart();
|
|
const alertConfirmed = await promptUserToStart();
|
|
|
if (!alertConfirmed) {
|
|
if (!alertConfirmed) {
|
|
|
window.shiguangBridge.showToast("用户取消了导入。");
|
|
window.shiguangBridge.showToast("用户取消了导入。");
|
|
|
- console.log("JS: 用户取消了导入流程。");
|
|
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const academicYear = await getAcademicYear();
|
|
|
|
|
- if (academicYear === null) {
|
|
|
|
|
- window.shiguangBridge.showToast("导入已取消。");
|
|
|
|
|
- console.log("JS: 获取学年失败/取消,流程终止。");
|
|
|
|
|
|
|
+ const selection = await selectAcademicYearAndSemester();
|
|
|
|
|
+ if (!selection) {
|
|
|
|
|
+ window.shiguangBridge.showToast("未选择学年学期,导入流程终止。");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- console.log(`JS: 已选择学年: ${academicYear}`);
|
|
|
|
|
|
|
|
|
|
|
|
+ const { academicYear, semesterCode } = selection;
|
|
|
|
|
|
|
|
- const semesterIndex = await selectSemester();
|
|
|
|
|
- if (semesterIndex === null || semesterIndex === -1) {
|
|
|
|
|
- window.shiguangBridge.showToast("导入已取消。");
|
|
|
|
|
- console.log("JS: 选择学期失败/取消,流程终止。");
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- console.log(`JS: 已选择学期索引: ${semesterIndex}`);
|
|
|
|
|
-
|
|
|
|
|
- const result = await fetchAndParseCourses(academicYear, semesterIndex);
|
|
|
|
|
|
|
+ const result = await fetchAndParseCourses(academicYear, semesterCode);
|
|
|
if (result === null) {
|
|
if (result === null) {
|
|
|
- console.log("JS: 课程获取或解析失败,流程终止。");
|
|
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
const { courses, config } = result;
|
|
const { courses, config } = result;
|
|
|
|
|
|
|
|
const saveResult = await saveCourses(courses);
|
|
const saveResult = await saveCourses(courses);
|
|
|
if (!saveResult) {
|
|
if (!saveResult) {
|
|
|
- console.log("JS: 课程保存失败,流程终止。");
|
|
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
await window.shiguangBridgePromise.saveCourseConfig(JSON.stringify(config));
|
|
await window.shiguangBridgePromise.saveCourseConfig(JSON.stringify(config));
|
|
|
- window.shiguangBridge.showToast(`课表配置更新成功!总周数:${config.semesterTotalWeeks}周。`);
|
|
|
|
|
|
|
+ let configMsg = "课表配置更新成功!";
|
|
|
|
|
+ if (config.semesterStartDate) {
|
|
|
|
|
+ configMsg += ` 开学日期:${config.semesterStartDate}`;
|
|
|
|
|
+ }
|
|
|
|
|
+ window.shiguangBridge.showToast(configMsg);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
window.shiguangBridge.showToast(`课表配置保存失败: ${error.message}`);
|
|
window.shiguangBridge.showToast(`课表配置保存失败: ${error.message}`);
|
|
|
- console.error('JS: Save Config Error:', error);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
await importPresetTimeSlots(TimeSlots);
|
|
await importPresetTimeSlots(TimeSlots);
|
|
|
|
|
|
|
|
-
|
|
|
|
|
window.shiguangBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
|
|
window.shiguangBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
|
|
|
- console.log("JS: 整个导入流程执行完毕并成功。");
|
|
|
|
|
window.shiguangBridge.notifyTaskCompletion();
|
|
window.shiguangBridge.notifyTaskCompletion();
|
|
|
}
|
|
}
|
|
|
|
|
|