[new] new project
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
# resources/SYSU/adapters.yaml
|
||||
adapters:
|
||||
- adapter_id: "SYSU"
|
||||
adapter_name: "中山大学"
|
||||
category: "BACHELOR_AND_ASSOCIATE"
|
||||
asset_js_path: "sysu.js"
|
||||
import_url: "https://jwxt.sysu.edu.cn/"
|
||||
maintainer: "JXCZ"
|
||||
description: "中山大学NetID版本教务系统课程表导入,注:先点击右上角切换为电脑模式再登陆,登陆后可直接导入。"
|
||||
|
||||
- adapter_id: "SYSU_01"
|
||||
adapter_name: "中山大学研究生教务课表导入"
|
||||
category: "POSTGRADUATE"
|
||||
asset_js_path: "sysu_01.js"
|
||||
import_url: "https://cms.sysu.edu.cn/"
|
||||
maintainer: "JR2714"
|
||||
description: "中山大学研究生教务(cms.sysu.edu.cn)"
|
||||
@@ -0,0 +1,622 @@
|
||||
// 中山大学教务系统课表导入器
|
||||
// 功能:获取教务系统课表 -> 转换为目标 JSON -> 通过时光课程表 V2 Bridge 导入
|
||||
|
||||
const API_URL = "/jwxt/timetable-search/stuTimeTabPrint/studentQuery";
|
||||
const CALENDAR_API_URL = "/jwxt/base-info/school-calender";
|
||||
let ACAD_YEAR = "2026-1";
|
||||
|
||||
function validateYearInput(input) {
|
||||
return /^[0-9]{4}$/.test(String(input).trim())
|
||||
? false
|
||||
: "请输入四位数字的学年!";
|
||||
}
|
||||
|
||||
async function getAcademicYear() {
|
||||
const defaultYear = ACAD_YEAR.split("-")[0];
|
||||
|
||||
const yearSelection = await window.shiguangBridgePromise.showPrompt(
|
||||
"选择学年",
|
||||
"请输入要导入课程的学年(如 2026):",
|
||||
defaultYear || "2026",
|
||||
"validateYearInput"
|
||||
);
|
||||
|
||||
return yearSelection;
|
||||
}
|
||||
|
||||
async function selectSemester() {
|
||||
if (!window.shiguangBridgePromise) {
|
||||
throw new Error(
|
||||
"shiguangBridgePromise 不可用,请在时光课程表 App 内运行此适配器。"
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof window.shiguangBridgePromise.showPrompt !== "function") {
|
||||
throw new Error(
|
||||
"shiguangBridgePromise.showPrompt 不可用,请在时光课程表 App 内运行此适配器。"
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof window.shiguangBridgePromise.showSingleSelection !== "function") {
|
||||
throw new Error(
|
||||
"shiguangBridgePromise.showSingleSelection 不可用,请在时光课程表 App 内运行此适配器。"
|
||||
);
|
||||
}
|
||||
|
||||
// 先输入学年
|
||||
const yearSelection = await getAcademicYear();
|
||||
|
||||
if (yearSelection === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const year = String(yearSelection).trim();
|
||||
|
||||
// 再选择学期
|
||||
const semesters = [
|
||||
"1(第一学期)",
|
||||
"2(第二学期)"
|
||||
];
|
||||
|
||||
const [, defaultSemester] = ACAD_YEAR.split("-");
|
||||
|
||||
const semesterIndex =
|
||||
await window.shiguangBridgePromise.showSingleSelection(
|
||||
"选择学期",
|
||||
JSON.stringify(semesters),
|
||||
defaultSemester === "2" ? 1 : 0
|
||||
);
|
||||
|
||||
if (
|
||||
semesterIndex === null ||
|
||||
semesterIndex < 0 ||
|
||||
semesterIndex >= semesters.length
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ACAD_YEAR = `${year}-${semesterIndex + 1}`;
|
||||
|
||||
return ACAD_YEAR;
|
||||
}
|
||||
|
||||
|
||||
// ========================================
|
||||
// 目标课表时间段
|
||||
// ========================================
|
||||
|
||||
const TIME_SLOTS = [
|
||||
{
|
||||
number: 1,
|
||||
startTime: "08:00",
|
||||
endTime: "08:45"
|
||||
},
|
||||
{
|
||||
number: 2,
|
||||
startTime: "08:55",
|
||||
endTime: "09:40"
|
||||
},
|
||||
{
|
||||
number: 3,
|
||||
startTime: "10:10",
|
||||
endTime: "10:55"
|
||||
},
|
||||
{
|
||||
number: 4,
|
||||
startTime: "11:05",
|
||||
endTime: "11:50"
|
||||
},
|
||||
{
|
||||
number: 5,
|
||||
startTime: "14:20",
|
||||
endTime: "15:05"
|
||||
},
|
||||
{
|
||||
number: 6,
|
||||
startTime: "15:15",
|
||||
endTime: "16:00"
|
||||
},
|
||||
{
|
||||
number: 7,
|
||||
startTime: "16:30",
|
||||
endTime: "17:15"
|
||||
},
|
||||
{
|
||||
number: 8,
|
||||
startTime: "17:25",
|
||||
endTime: "18:10"
|
||||
},
|
||||
{
|
||||
number: 9,
|
||||
startTime: "19:00",
|
||||
endTime: "19:45"
|
||||
},
|
||||
{
|
||||
number: 10,
|
||||
startTime: "19:55",
|
||||
endTime: "20:40"
|
||||
},
|
||||
{
|
||||
number: 11,
|
||||
startTime: "20:50",
|
||||
endTime: "21:35"
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
// ========================================
|
||||
// 课表配置
|
||||
// ========================================
|
||||
|
||||
const CONFIG_BASE = {
|
||||
semesterTotalWeeks: 20,
|
||||
defaultClassDuration: 45,
|
||||
defaultBreakDuration: 10
|
||||
};
|
||||
|
||||
|
||||
// ========================================
|
||||
// 自动获取学期实际开学日期
|
||||
// ========================================
|
||||
|
||||
async function fetchSemesterStartDate() {
|
||||
const url =
|
||||
`${CALENDAR_API_URL}?academicYear=${encodeURIComponent(ACAD_YEAR)}` +
|
||||
`&weekly=1&_t=${Math.floor(Date.now() / 1000)}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: "GET",
|
||||
credentials: "include"
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`获取学期开始日期失败:HTTP ${response.status}`
|
||||
);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
const startDate = json?.data?.startTime;
|
||||
|
||||
if (!startDate) {
|
||||
throw new Error(
|
||||
`教务系统未返回 ${ACAD_YEAR} 第 1 周的开始日期。`
|
||||
);
|
||||
}
|
||||
|
||||
return startDate;
|
||||
}
|
||||
|
||||
|
||||
async function buildCourseConfig() {
|
||||
const semesterStartDate =
|
||||
await fetchSemesterStartDate();
|
||||
|
||||
return {
|
||||
semesterStartDate,
|
||||
...CONFIG_BASE
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// 请求课程数据
|
||||
|
||||
async function fetchCourses() {
|
||||
const url = `${API_URL}?_t=${Date.now()}`;
|
||||
const payload = {
|
||||
acadYear: ACAD_YEAR,
|
||||
submitFlag: "1",
|
||||
nothroughCourseFlag: "1"
|
||||
};
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const responseText = await response.text();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`API 请求失败:HTTP ${response.status}\n${responseText}`
|
||||
);
|
||||
}
|
||||
|
||||
let json;
|
||||
|
||||
try {
|
||||
json = JSON.parse(responseText);
|
||||
} catch {
|
||||
throw new Error("教务系统返回的课程数据不是有效的 JSON。\n");
|
||||
}
|
||||
|
||||
if (json?.code !== 200) {
|
||||
throw new Error(
|
||||
`API 返回异常 code:${json?.code}`
|
||||
);
|
||||
}
|
||||
|
||||
const timetable = json?.data?.timetable;
|
||||
|
||||
if (
|
||||
!timetable ||
|
||||
typeof timetable !== "object" ||
|
||||
Array.isArray(timetable)
|
||||
) {
|
||||
throw new Error(
|
||||
"API 返回中不存在有效的 data.timetable"
|
||||
);
|
||||
}
|
||||
|
||||
const courses = [];
|
||||
|
||||
// timetable 的 key:
|
||||
// 第一位 = 星期
|
||||
// 后两位 = 起始节次
|
||||
//
|
||||
// 例如:
|
||||
// 11 -> 周一第1节
|
||||
// 13 -> 周一第3节
|
||||
// 21 -> 周二第1节
|
||||
|
||||
for (const entries of Object.values(timetable)) {
|
||||
if (!Array.isArray(entries) || entries.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const item of entries) {
|
||||
const course = normalizeCourse(item);
|
||||
|
||||
if (course) {
|
||||
courses.push(course);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 自动获取学期配置
|
||||
const config = await buildCourseConfig();
|
||||
|
||||
const result = {
|
||||
courses,
|
||||
timeSlots: TIME_SLOTS,
|
||||
config
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// 课程数据转换
|
||||
|
||||
function normalizeCourse(item) {
|
||||
if (!item || typeof item !== "object") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const name = cleanCourseName(item.courseName);
|
||||
const teacher = cleanText(item.teachingStaffName);
|
||||
const position = cleanText(item.classPlace);
|
||||
const day = toNumber(item.week);
|
||||
const startSection = toNumber(item.startClassTimes);
|
||||
const endSection = toNumber(item.endClassTimes);
|
||||
const startWeek = toNumber(item.startWeek);
|
||||
const weeks = parseWeeks(item.timeDetail, startWeek);
|
||||
|
||||
if (
|
||||
!name ||
|
||||
!day ||
|
||||
!startSection ||
|
||||
!endSection ||
|
||||
weeks.length === 0
|
||||
) {
|
||||
console.warn(
|
||||
"跳过字段不完整的课程记录:",
|
||||
item
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
id: crypto.randomUUID(),
|
||||
|
||||
name,
|
||||
|
||||
teacher,
|
||||
|
||||
position,
|
||||
|
||||
day,
|
||||
|
||||
startSection,
|
||||
|
||||
endSection,
|
||||
|
||||
weeks
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// 清理课程名称
|
||||
|
||||
function cleanCourseName(value) {
|
||||
const text = cleanText(value);
|
||||
|
||||
// 去掉中山大学 API 返回的课程类别前缀
|
||||
//
|
||||
// 本(专必)高等数学一(I)
|
||||
// ↓
|
||||
// 高等数学一(I)
|
||||
//
|
||||
// 本(公必)劳动教育
|
||||
// ↓
|
||||
// 劳动教育
|
||||
|
||||
return text
|
||||
.replace(/^本\([^)]*\)/, "")
|
||||
.trim();
|
||||
}
|
||||
|
||||
|
||||
// 解析上课周次
|
||||
|
||||
function parseWeeks(
|
||||
timeDetail,
|
||||
startWeek = 1
|
||||
) {
|
||||
const text =
|
||||
cleanText(timeDetail);
|
||||
|
||||
if (!text) {
|
||||
return startWeek
|
||||
? [startWeek]
|
||||
: [];
|
||||
}
|
||||
|
||||
// 例如:
|
||||
// 1-17每周
|
||||
|
||||
const rangeMatch =
|
||||
text.match(/(\d+)\s*-\s*(\d+)/);
|
||||
|
||||
if (rangeMatch) {
|
||||
const start =
|
||||
Number(rangeMatch[1]);
|
||||
|
||||
const end =
|
||||
Number(rangeMatch[2]);
|
||||
|
||||
if (
|
||||
Number.isFinite(start) &&
|
||||
Number.isFinite(end) &&
|
||||
end >= start
|
||||
) {
|
||||
return Array.from(
|
||||
{
|
||||
length:
|
||||
end - start + 1
|
||||
},
|
||||
(_, index) =>
|
||||
start + index
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 兼容:
|
||||
//
|
||||
// 1,3,5周
|
||||
// 1、3、5周
|
||||
|
||||
const numbers =
|
||||
text
|
||||
.match(/\d+/g)
|
||||
?.map(Number)
|
||||
.filter(Number.isFinite) || [];
|
||||
|
||||
if (numbers.length > 0) {
|
||||
return [
|
||||
...new Set(numbers)
|
||||
].sort(
|
||||
(a, b) => a - b
|
||||
);
|
||||
}
|
||||
|
||||
return startWeek
|
||||
? [startWeek]
|
||||
: [];
|
||||
}
|
||||
|
||||
|
||||
// 清理字符串
|
||||
|
||||
function cleanText(value) {
|
||||
if (value === null || value === undefined) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return String(value).replace(/\/+$/g, "").trim();
|
||||
}
|
||||
|
||||
|
||||
// 数字转换
|
||||
|
||||
function toNumber(value) {
|
||||
const number = Number(value);
|
||||
return Number.isFinite(number) ? number : null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 主导入流程
|
||||
|
||||
async function runImportFlow() {
|
||||
try {
|
||||
// 提示用户选择学期
|
||||
|
||||
if (
|
||||
window.shiguangBridge &&
|
||||
typeof window.shiguangBridge.showToast ===
|
||||
"function"
|
||||
) {
|
||||
window.shiguangBridge.showToast(
|
||||
"请选择要导入的学期..."
|
||||
);
|
||||
}
|
||||
|
||||
const selectedSemester =
|
||||
await selectSemester();
|
||||
|
||||
if (!selectedSemester) {
|
||||
if (
|
||||
window.shiguangBridge &&
|
||||
typeof window.shiguangBridge.showToast ===
|
||||
"function"
|
||||
) {
|
||||
window.shiguangBridge.showToast(
|
||||
"已取消导入。"
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取课程
|
||||
|
||||
const data =
|
||||
await fetchCourses();
|
||||
|
||||
if (
|
||||
!data ||
|
||||
!Array.isArray(data.courses)
|
||||
) {
|
||||
throw new Error(
|
||||
"课程数据为空或格式不正确。"
|
||||
);
|
||||
}
|
||||
|
||||
// 检查 V2 Bridge
|
||||
|
||||
if (
|
||||
!window.shiguangBridgePromise
|
||||
) {
|
||||
throw new Error(
|
||||
"shiguangBridgePromise 不可用,请在时光课程表 App 内运行此适配器。"
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
typeof window.shiguangBridgePromise
|
||||
.saveImportedCourses !==
|
||||
"function"
|
||||
) {
|
||||
throw new Error(
|
||||
"saveImportedCourses API 不可用。"
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
typeof window.shiguangBridgePromise
|
||||
.savePresetTimeSlots !==
|
||||
"function"
|
||||
) {
|
||||
throw new Error(
|
||||
"savePresetTimeSlots API 不可用。"
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
typeof window.shiguangBridgePromise
|
||||
.saveCourseConfig !==
|
||||
"function"
|
||||
) {
|
||||
throw new Error(
|
||||
"saveCourseConfig API 不可用。"
|
||||
);
|
||||
}
|
||||
|
||||
// 开始导入
|
||||
|
||||
if (
|
||||
window.shiguangBridge &&
|
||||
typeof window.shiguangBridge.showToast ===
|
||||
"function"
|
||||
) {
|
||||
window.shiguangBridge.showToast(
|
||||
`正在导入 ${data.courses.length} 条课程记录...`
|
||||
);
|
||||
}
|
||||
|
||||
// 保存课程
|
||||
|
||||
await window.shiguangBridgePromise
|
||||
.saveImportedCourses(
|
||||
JSON.stringify(
|
||||
data.courses
|
||||
)
|
||||
);
|
||||
|
||||
// 保存时间段
|
||||
|
||||
await window.shiguangBridgePromise
|
||||
.savePresetTimeSlots(
|
||||
JSON.stringify(
|
||||
data.timeSlots
|
||||
)
|
||||
);
|
||||
|
||||
// 保存课表配置
|
||||
|
||||
await window.shiguangBridgePromise
|
||||
.saveCourseConfig(
|
||||
JSON.stringify(
|
||||
data.config
|
||||
)
|
||||
);
|
||||
|
||||
// 导入完成
|
||||
|
||||
if (
|
||||
window.shiguangBridge &&
|
||||
typeof window.shiguangBridge.showToast ===
|
||||
"function"
|
||||
) {
|
||||
window.shiguangBridge.showToast(
|
||||
`成功导入 ${data.courses.length} 条课程记录!`
|
||||
);
|
||||
}
|
||||
|
||||
// 通知 App 导入流程结束
|
||||
|
||||
if (
|
||||
window.shiguangBridge &&
|
||||
typeof window.shiguangBridge
|
||||
.notifyTaskCompletion ===
|
||||
"function"
|
||||
) {
|
||||
window.shiguangBridge
|
||||
.notifyTaskCompletion();
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if (
|
||||
window.shiguangBridge &&
|
||||
typeof window.shiguangBridge.showToast ===
|
||||
"function"
|
||||
) {
|
||||
window.shiguangBridge.showToast(
|
||||
`导入失败:${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 启动
|
||||
|
||||
runImportFlow();
|
||||
@@ -0,0 +1,356 @@
|
||||
// 中山大学研究生教务系统拾光课表适配脚本
|
||||
// 适配系统:cms.sysu.edu.cn /mk/student-web/(研究生课表查询)
|
||||
// 直接调用课表查询接口 selectStudentClassTable 抓取课表(同源 fetch,自动携带登录会话),不解析页面 DOM
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
// ---------- 通用工具 ----------
|
||||
|
||||
function toast(message) {
|
||||
if (window.shiguangBridge && window.shiguangBridge.showToast) {
|
||||
window.shiguangBridge.showToast(message);
|
||||
} else {
|
||||
console.log("[SYSU]", message);
|
||||
}
|
||||
}
|
||||
|
||||
async function alertUser(title, message) {
|
||||
if (window.shiguangBridgePromise && window.shiguangBridgePromise.showAlert) {
|
||||
return await window.shiguangBridgePromise.showAlert(title, message, "确定");
|
||||
}
|
||||
alert(title + "\n" + message);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------- 作息时间(中山大学研究生, 11 节) ----------
|
||||
|
||||
const TIME_SLOTS = [
|
||||
{ number: 1, startTime: "08:00", endTime: "08:45" },
|
||||
{ number: 2, startTime: "08:55", endTime: "09:40" },
|
||||
{ number: 3, startTime: "10:10", endTime: "10:55" },
|
||||
{ number: 4, startTime: "11:05", endTime: "11:50" },
|
||||
{ number: 5, startTime: "14:20", endTime: "15:05" },
|
||||
{ number: 6, startTime: "15:15", endTime: "16:00" },
|
||||
{ number: 7, startTime: "16:30", endTime: "17:15" },
|
||||
{ number: 8, startTime: "17:25", endTime: "18:10" },
|
||||
{ number: 9, startTime: "19:00", endTime: "19:45" },
|
||||
{ number: 10, startTime: "19:55", endTime: "20:40" },
|
||||
{ number: 11, startTime: "20:50", endTime: "21:35" }
|
||||
];
|
||||
|
||||
// ================================================================
|
||||
// 主路径: 接口直取
|
||||
// ================================================================
|
||||
|
||||
const SCHEDULE_API = "/start-class/classTableInfo/selectStudentClassTable";
|
||||
const CALENDAR_API = "/base-info/school-calender"; // 校历
|
||||
const MENU_CODE = "byytxsd_xskbcx_week_query"; // 学生课表查询菜单 code
|
||||
|
||||
// 响应 JSON 里的星期字段名 -> 拾光 day(1=周一..7=周日)
|
||||
const DAY_KEYS = {
|
||||
monday: 1, tuesday: 2, wednesday: 3, thursday: 4,
|
||||
friday: 5, saturday: 6, sunday: 7
|
||||
};
|
||||
|
||||
/** 从当前页面自动探测学年学期, 如 "2026-1"; 找不到返回 null */
|
||||
function detectAcademicYear() {
|
||||
// 1) URL 参数
|
||||
const urlMatch = location.href.match(/academicYear=([^&]+)/);
|
||||
if (urlMatch) return decodeURIComponent(urlMatch[1]);
|
||||
// 2) 页面标题, 如 <p class="title">2026-1学期课程</p>
|
||||
const text = document.body ? document.body.innerText : "";
|
||||
const titleMatch = text.match(/(\d{4}-\d)\s*学期/);
|
||||
if (titleMatch) return titleMatch[1];
|
||||
// 3) 页面上的学年学期下拉框(已选中项)
|
||||
const selects = document.querySelectorAll("select");
|
||||
for (let i = 0; i < selects.length; i++) {
|
||||
const opt = selects[i].selectedOptions && selects[i].selectedOptions[0];
|
||||
if (opt) {
|
||||
const m = opt.value.match(/^\d{4}-\d$/);
|
||||
if (m) return opt.value;
|
||||
}
|
||||
}
|
||||
// 4) 页面任意文本中形如 2026-1 / 2026-2027-1 的学期串
|
||||
const anyMatch = text.match(/(20\d{2}-\d)(?![\d-])/);
|
||||
if (anyMatch) return anyMatch[1];
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 从当前日期推算学年候选列表(登录后系统默认停在上学期, 假期导课表时要用新学期) */
|
||||
function buildSemesterCandidates(detected) {
|
||||
let baseYear = null;
|
||||
const dm = String(detected || "").match(/^(\d{4})-(\d)$/);
|
||||
if (dm) {
|
||||
baseYear = parseInt(dm[1], 10);
|
||||
} else {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = now.getMonth() + 1; // 1..12
|
||||
// 学年 Y 覆盖 当年9月 ~ 次年8月; 9月(含)前属于上学年
|
||||
baseYear = month >= 9 ? year : year - 1;
|
||||
}
|
||||
const out = [];
|
||||
for (let y = baseYear - 1; y <= baseYear + 1; y++) {
|
||||
out.push(y + "-1");
|
||||
out.push(y + "-2");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 弹出学期选择, 返回选中的学年学期; 取消返回 null */
|
||||
async function selectSemester(detected) {
|
||||
const candidates = buildSemesterCandidates(detected);
|
||||
let defaultIndex = 0;
|
||||
if (detected) {
|
||||
const idx = candidates.indexOf(detected);
|
||||
if (idx >= 0) defaultIndex = idx;
|
||||
else candidates.unshift(detected);
|
||||
}
|
||||
const selected = await window.shiguangBridgePromise.showSingleSelection(
|
||||
"选择学年学期",
|
||||
JSON.stringify(candidates),
|
||||
defaultIndex
|
||||
);
|
||||
if (selected === null || selected === undefined) return null;
|
||||
return candidates[parseInt(selected, 10)];
|
||||
}
|
||||
|
||||
async function fetchSchedule(academicYear) {
|
||||
const url = SCHEDULE_API +
|
||||
"?code=" + MENU_CODE +
|
||||
"&academicYear=" + encodeURIComponent(academicYear) +
|
||||
"&weekly=0&_t=" + Date.now();
|
||||
const resp = await fetch(url, {
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
headers: { "X-Requested-With": "XMLHttpRequest" }
|
||||
});
|
||||
if (!resp.ok) throw new Error("接口请求失败: HTTP " + resp.status);
|
||||
const json = await resp.json();
|
||||
if (json.code !== 200) {
|
||||
throw new Error("接口返回异常: code=" + json.code + (json.message ? " " + json.message : ""));
|
||||
}
|
||||
if (!Array.isArray(json.data)) {
|
||||
throw new Error("接口返回的 data 不是数组");
|
||||
}
|
||||
return json.data;
|
||||
}
|
||||
|
||||
/** 拉第 1 周周一作为开学日 */
|
||||
async function fetchSemesterStartDate(academicYear) {
|
||||
const url = CALENDAR_API +
|
||||
"?academicYear=" + encodeURIComponent(academicYear) +
|
||||
"&weekly=1&_t=" + Date.now();
|
||||
const resp = await fetch(url, {
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
headers: { "X-Requested-With": "XMLHttpRequest" }
|
||||
});
|
||||
if (!resp.ok) throw new Error("HTTP " + resp.status);
|
||||
const json = await resp.json();
|
||||
const start = json && json.data && json.data.startTime;
|
||||
if (json.code !== 200 || !start || !/^\d{4}-\d{2}-\d{2}$/.test(String(start))) {
|
||||
throw new Error("校历未返回有效 startTime");
|
||||
}
|
||||
return String(start);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析接口返回的字段串。
|
||||
* 接口每个星期字段(如 tuesday)的值是若干 "键:值" 对用 ";;" 连接的字符串,
|
||||
* 例如: "kcmc:课程名;;rkjs:教师;;skdd:地点;;zs:1-12周;;js:周二 2-4节;;..."。
|
||||
* 本函数按 ";;" 切分后提取各键值, 返回 { name, teacher, position, weeks, startSection, endSection }。
|
||||
*/
|
||||
function parseCourseField(fieldText) {
|
||||
const fields = {};
|
||||
String(fieldText).split(";;").forEach(function (part) {
|
||||
const idx = part.indexOf(":");
|
||||
if (idx <= 0) return;
|
||||
fields[part.slice(0, idx).trim()] = part.slice(idx + 1).trim();
|
||||
});
|
||||
|
||||
const name = fields.kcmc;
|
||||
if (!name) return null;
|
||||
|
||||
// 周次范围: 取 zs(如 "1-12周"); 个别学期 zs 缺失时回退到 sksj(如 "1-12每周(2-4节)")
|
||||
// 中同样的 "x-y周" 片段。
|
||||
const zsText = String(fields.zs || fields.sksj || "");
|
||||
const zsMatch = zsText.match(/(\d+)\s*-\s*(\d+)\s*每?周/) || zsText.match(/(\d+)\s*每?周/);
|
||||
if (!zsMatch) {
|
||||
console.warn("[SYSU] 无法解析周次:", zsText || "(空)", "字段:", fieldText);
|
||||
return null;
|
||||
}
|
||||
const weekStart = parseInt(zsMatch[1], 10);
|
||||
const weekEnd = zsMatch[2] ? parseInt(zsMatch[2], 10) : weekStart;
|
||||
const weeks = [];
|
||||
for (let w = weekStart; w <= weekEnd; w++) weeks.push(w);
|
||||
if (!weeks.length) {
|
||||
console.warn("[SYSU] 周次区间为空:", zsText);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 节次: js 形如 "周二 2-4节" 或 "周二 3节"; 星期由外层 JSON 键决定。
|
||||
const jsText = String(fields.js || "");
|
||||
const jsMatch = jsText.match(/(\d+)\s*-\s*(\d+)\s*节/) || jsText.match(/(\d+)\s*节/);
|
||||
if (!jsMatch) {
|
||||
console.warn("[SYSU] 无法解析节次:", jsText || "(空)", "字段:", fieldText);
|
||||
return null;
|
||||
}
|
||||
const startSection = parseInt(jsMatch[1], 10);
|
||||
const endSection = jsMatch[2] ? parseInt(jsMatch[2], 10) : startSection;
|
||||
if (!startSection || !endSection || endSection < startSection) {
|
||||
console.warn("[SYSU] 节次区间无效:", jsText);
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
name: name,
|
||||
teacher: fields.rkjs || "未知教师",
|
||||
position: fields.skdd || "未知地点",
|
||||
weeks: weeks,
|
||||
startSection: startSection,
|
||||
endSection: endSection
|
||||
};
|
||||
}
|
||||
|
||||
/** 遍历响应数组(每项 = 某周某节次的当日课程), 提取全部上课并去重 */
|
||||
function buildCoursesFromApi(data) {
|
||||
const meetings = [];
|
||||
let maxWeek = 0;
|
||||
|
||||
data.forEach(function (entry) {
|
||||
if (!entry) return;
|
||||
if (entry.weekly > maxWeek) maxWeek = entry.weekly;
|
||||
Object.keys(DAY_KEYS).forEach(function (dayKey) {
|
||||
const field = entry[dayKey];
|
||||
if (!field || typeof field !== "string" || field.length < 4) return;
|
||||
const course = parseCourseField(field);
|
||||
if (!course) return;
|
||||
course.weeks.forEach(function (w) {
|
||||
if (w > maxWeek) maxWeek = w;
|
||||
});
|
||||
meetings.push({
|
||||
name: course.name,
|
||||
teacher: course.teacher,
|
||||
position: course.position,
|
||||
day: DAY_KEYS[dayKey],
|
||||
startSection: course.startSection,
|
||||
endSection: course.endSection,
|
||||
weeks: course.weeks
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return { meetings: dedupe(meetings), maxWeek: maxWeek };
|
||||
}
|
||||
|
||||
function dedupe(meetings) {
|
||||
const seen = {};
|
||||
const out = [];
|
||||
meetings.forEach(function (m) {
|
||||
const key = [m.day, m.startSection, m.endSection, m.name, m.weeks.join(","), m.teacher, m.position].join("|");
|
||||
if (seen[key]) return;
|
||||
seen[key] = true;
|
||||
out.push(m);
|
||||
});
|
||||
return out.sort(function (a, b) {
|
||||
return a.day - b.day || a.startSection - b.startSection;
|
||||
});
|
||||
}
|
||||
|
||||
// ---------- 保存 ----------
|
||||
|
||||
async function saveToApp(courses, timeSlots, maxWeek, startDate) {
|
||||
const config = {
|
||||
semesterTotalWeeks: maxWeek > 0 ? maxWeek : 17,
|
||||
firstDayOfWeek: 1,
|
||||
defaultClassDuration: 45,
|
||||
defaultBreakDuration: 10
|
||||
};
|
||||
if (startDate) config.semesterStartDate = startDate;
|
||||
if (window.shiguangBridgePromise && window.shiguangBridgePromise.saveCourseConfig) {
|
||||
await window.shiguangBridgePromise.saveCourseConfig(JSON.stringify(config));
|
||||
}
|
||||
if (timeSlots.length && window.shiguangBridgePromise && window.shiguangBridgePromise.savePresetTimeSlots) {
|
||||
await window.shiguangBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
|
||||
}
|
||||
if (window.shiguangBridgePromise && window.shiguangBridgePromise.saveImportedCourses) {
|
||||
return await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(courses));
|
||||
}
|
||||
console.log("[SYSU] parsed courses:", JSON.stringify(courses, null, 2));
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------- 主流程 ----------
|
||||
|
||||
async function runImportFlow() {
|
||||
try {
|
||||
// 1. 确定学年学期: 自动探测 + 弹窗让用户确认/修改
|
||||
// (假期登录时系统默认是上学期, 必须允许用户选到目标学期)
|
||||
let academicYear = detectAcademicYear();
|
||||
if (window.shiguangBridgePromise && window.shiguangBridgePromise.showSingleSelection) {
|
||||
const picked = await selectSemester(academicYear);
|
||||
if (picked === null) return; // 用户取消
|
||||
academicYear = picked;
|
||||
} else if (!academicYear) {
|
||||
// 无 bridge 且页面也识别不到学期: 无法继续
|
||||
await alertUser("未识别到学年学期", "请先进入目标学期的「课表查询」页面再执行导入。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 接口直取(主路径)
|
||||
let courses = null, maxWeek = 0;
|
||||
try {
|
||||
toast("正在从教务接口获取 " + academicYear + " 课表...");
|
||||
const data = await fetchSchedule(academicYear);
|
||||
const result = buildCoursesFromApi(data);
|
||||
if (result.meetings.length) {
|
||||
courses = result.meetings;
|
||||
maxWeek = result.maxWeek;
|
||||
toast("接口获取成功, 一周共 " + courses.length + " 次上课");
|
||||
} else {
|
||||
console.warn("[SYSU] 接口返回空数据: academicYear=" + academicYear);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("[SYSU] 接口获取失败:", e);
|
||||
}
|
||||
|
||||
if (!courses || !courses.length) {
|
||||
await alertUser(
|
||||
"未解析到课程",
|
||||
"所选学期 " + academicYear + " 没有返回课程。\n\n可能原因:\n" +
|
||||
"1. 该学期课程尚未排课/选课(假期导入新学期时常见, 可等开学前选课后重试);\n" +
|
||||
"2. 学期代码不对(请重新执行导入, 在选择学期时核对);\n" +
|
||||
"3. 登录状态失效或接口异常(请重新进入课表页面登录后再试; 若仍失败, 请向适配维护者反馈)。"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 校历
|
||||
let startDate = null;
|
||||
try {
|
||||
startDate = await fetchSemesterStartDate(academicYear);
|
||||
toast("已获取开学日 " + startDate);
|
||||
} catch (e) {
|
||||
console.warn("[SYSU] 校历获取失败, 跳过开学日:", e);
|
||||
}
|
||||
|
||||
// 4. 保存
|
||||
const saved = await saveToApp(courses, TIME_SLOTS, maxWeek, startDate);
|
||||
if (!saved) { toast("课程保存失败, 请重试"); return; }
|
||||
|
||||
toast("导入成功: " + courses.length + " 个课程时段" +
|
||||
(startDate ? ", 开学日 " + startDate : "") +
|
||||
", 已同步作息时间");
|
||||
if (window.shiguangBridge && window.shiguangBridge.notifyTaskCompletion) {
|
||||
window.shiguangBridge.notifyTaskCompletion();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[SYSU] import failed:", error);
|
||||
await alertUser("导入失败", error && error.message ? error.message : String(error));
|
||||
}
|
||||
}
|
||||
|
||||
runImportFlow();
|
||||
})();
|
||||
Reference in New Issue
Block a user