Files
jwxt/app/templates/timetable.html
T

1070 lines
52 KiB
HTML

{% extends "base_tailwind.html" %}
{% block title %}课表查询 - {{ brand_name }}{% endblock %}
{% block extra_css %}
<style>
/* 课程格子入场动画 */
@keyframes cellFadeIn {
from { opacity: 0; transform: translateY(10px) scale(0.93); }
to { opacity: 1; transform: translateY(0) scale(1); }
}
.course-cell {
animation: cellFadeIn 0.38s cubic-bezier(0.22, 1, 0.36, 1) both;
}
/* 切周时整体淡出 */
@keyframes gridFadeOut {
from { opacity: 1; transform: translateY(0); }
to { opacity: 0; transform: translateY(-6px); }
}
#timetableGrid.switching {
animation: gridFadeOut 0.18s ease both;
pointer-events: none;
}
/* 弹窗容器:始终存在,用 opacity + pointer-events 控制可见性 */
#courseModal {
opacity: 0;
pointer-events: none;
transition: opacity 0.24s ease;
}
#courseModal.modal-open {
opacity: 1;
pointer-events: auto;
}
/* 遮罩 */
#courseModal .modal-backdrop {
opacity: 0;
transition: opacity 0.24s ease;
}
#courseModal.modal-open .modal-backdrop {
opacity: 1;
}
/* 面板 */
#courseModal .modal-panel {
opacity: 0;
transform: scale(0.94) translateY(14px);
transition: opacity 0.24s ease, transform 0.26s cubic-bezier(0.22, 1, 0.36, 1);
}
#courseModal.modal-open .modal-panel {
opacity: 1;
transform: scale(1) translateY(0);
}
@media (prefers-reduced-motion: reduce) {
#courseModal .modal-panel { transition: none !important; }
}
</style>
{% endblock %}
{% block content %}
<div>
<!-- 页面标题 -->
<div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-6 list-item-enter" style="animation-delay: 0ms;">
<div>
<h2 class="text-2xl font-bold flex items-center gap-2">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="3" y="4" width="18" height="18" rx="2" ry="2" />
<line x1="16" y1="2" x2="16" y2="6" />
<line x1="8" y1="2" x2="8" y2="6" />
<line x1="3" y1="10" x2="21" y2="10" />
</svg>
课表查询
</h2>
</div>
<div class="flex flex-wrap gap-3 items-center">
<button id="prevWeek"
class="px-3 py-2 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-black/30 text-sm font-medium hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2">
<polyline points="15 18 9 12 15 6"></polyline>
</svg>
</button>
<select id="weekSelect"
class="px-4 py-2 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-black/30 text-sm font-medium focus:outline-none focus:ring-2 focus:ring-black dark:focus:ring-white">
</select>
<button id="nextWeek"
class="px-3 py-2 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-black/30 text-sm font-medium hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2">
<polyline points="9 18 15 12 9 6"></polyline>
</svg>
</button>
</div>
</div>
<!-- 学期选择和当前时间信息 -->
<div class="glass rounded-2xl p-4 mb-4 list-item-enter" style="animation-delay: 50ms;">
<div class="flex flex-col md:flex-row md:items-center justify-between gap-4">
<div class="flex items-center gap-4 text-sm">
<span class="text-gray-500 dark:text-gray-400">学年学期:</span>
<select id="semesterSelect" onchange="onSemesterChange()"
class="px-3 py-1.5 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-black/30 text-sm font-medium focus:outline-none focus:ring-2 focus:ring-black dark:focus:ring-white min-w-[160px]">
<option value="">加载中...</option>
</select>
</div>
<div class="flex items-center gap-4 text-sm">
<span class="text-gray-500 dark:text-gray-400">当前时间:</span>
<span id="currentTime" class="font-semibold">--:--</span>
</div>
</div>
</div>
<!-- 加载提示 -->
<div id="loadingIndicator" class="text-center py-12 hidden list-item-enter" style="animation-delay: 100ms;">
<div
class="w-10 h-10 border-2 border-black dark:border-white border-t-transparent rounded-full animate-spin mx-auto mb-4">
</div>
<p class="text-gray-500 dark:text-gray-400">正在加载课表...</p>
</div>
<!-- 错误提示 -->
<div id="errorMessage" class="glass rounded-2xl p-6 mb-4 text-center hidden list-item-enter" style="animation-delay: 100ms;">
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
class="mx-auto mb-4 text-red-500">
<circle cx="12" cy="12" r="10" />
<line x1="15" y1="9" x2="9" y2="15" />
<line x1="9" y1="9" x2="15" y2="15" />
</svg>
<p id="errorText" class="text-gray-600 dark:text-gray-400"></p>
</div>
<!-- 课表展示区域 - Grid 布局 -->
<div id="timetableContainer" class="hidden list-item-enter" style="animation-delay: 150ms;">
<div class="glass rounded-2xl p-2 sm:p-4 overflow-hidden">
<!-- 表头 -->
<div class="grid grid-cols-8 gap-1 mb-2 min-w-[320px]" id="timetableHeader">
<div
class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-1 sm:py-2 bg-gray-100/50 dark:bg-gray-800/50 rounded-lg">
节次</div>
<div class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-0.5 sm:py-1" id="headerDay1"><span>周一</span><span class="block text-[9px] sm:text-[10px] font-normal text-gray-400 dark:text-gray-500 header-date"></span></div>
<div class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-0.5 sm:py-1" id="headerDay2"><span>周二</span><span class="block text-[9px] sm:text-[10px] font-normal text-gray-400 dark:text-gray-500 header-date"></span></div>
<div class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-0.5 sm:py-1" id="headerDay3"><span>周三</span><span class="block text-[9px] sm:text-[10px] font-normal text-gray-400 dark:text-gray-500 header-date"></span></div>
<div class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-0.5 sm:py-1" id="headerDay4"><span>周四</span><span class="block text-[9px] sm:text-[10px] font-normal text-gray-400 dark:text-gray-500 header-date"></span></div>
<div class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-0.5 sm:py-1" id="headerDay5"><span>周五</span><span class="block text-[9px] sm:text-[10px] font-normal text-gray-400 dark:text-gray-500 header-date"></span></div>
<div class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-0.5 sm:py-1" id="headerDay6"><span>周六</span><span class="block text-[9px] sm:text-[10px] font-normal text-gray-400 dark:text-gray-500 header-date"></span></div>
<div class="text-center text-[10px] sm:text-xs font-bold text-gray-700 dark:text-gray-300 py-0.5 sm:py-1" id="headerDay7"><span>周日</span><span class="block text-[9px] sm:text-[10px] font-normal text-gray-400 dark:text-gray-500 header-date"></span></div>
</div>
<!-- 课表主体 - Grid 布局 -->
<div class="overflow-x-auto">
<div class="grid grid-cols-8 gap-1 min-w-[320px]" id="timetableGrid"
style="grid-template-rows: repeat(11, minmax(60px, auto));">
<!-- 由 JS 动态生成 -->
</div>
</div>
</div>
</div>
<!-- 空状态 -->
<div id="emptyState" class="glass rounded-2xl p-12 text-center hidden">
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
class="mx-auto mb-4 text-gray-400">
<rect x="3" y="4" width="18" height="18" rx="2" ry="2" />
<line x1="16" y1="2" x2="16" y2="6" />
<line x1="8" y1="2" x2="8" y2="6" />
<line x1="3" y1="10" x2="21" y2="10" />
<path d="M9 16h6" />
</svg>
<p class="text-gray-500 dark:text-gray-400 mb-4">暂无课程信息</p>
</div>
<!-- 课程详情弹窗 -->
<div id="courseModal" class="fixed inset-0 z-50 flex items-center justify-center p-4" aria-labelledby="modal-title" role="dialog" aria-modal="true">
<!-- 背景遮罩 -->
<div class="modal-backdrop fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeCourseModal()"></div>
<!-- 弹窗面板:固定宽高,内容区滚动 -->
<div class="modal-panel relative glass rounded-2xl shadow-xl w-full max-w-md h-[480px] flex flex-col overflow-hidden">
<!-- 顶部标题区 -->
<div class="flex items-center gap-3 p-6 pb-4 flex-shrink-0">
<div id="modalCourseIcon" class="w-12 h-12 rounded-xl flex items-center justify-center text-2xl flex-shrink-0"></div>
<div class="min-w-0 flex-1">
<h3 id="modalCourseName" class="text-xl font-bold text-gray-900 dark:text-white leading-tight truncate"></h3>
<p id="modalCourseCode" class="text-sm text-gray-500 dark:text-gray-400 truncate"></p>
</div>
<button onclick="closeCourseModal()" class="flex-shrink-0 p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-full transition-colors">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
</div>
<!-- 分割线 -->
<div class="border-t border-gray-200 dark:border-gray-700 mx-6 flex-shrink-0"></div>
<!-- 可滚动内容区 -->
<div class="flex-1 overflow-y-auto p-6 pt-4 space-y-4">
<!-- 授课教师 -->
<div class="flex items-start gap-3">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0">
<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
<circle cx="12" cy="7" r="4"></circle>
</svg>
<div>
<p class="text-xs text-gray-500 dark:text-gray-400">授课教师</p>
<p id="modalTeacher" class="text-sm font-medium text-gray-900 dark:text-white"></p>
</div>
</div>
<!-- 上课地点 -->
<div class="flex items-start gap-3">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0">
<path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path>
<circle cx="12" cy="10" r="3"></circle>
</svg>
<div>
<p class="text-xs text-gray-500 dark:text-gray-400">上课地点</p>
<p id="modalLocation" class="text-sm font-medium text-gray-900 dark:text-white"></p>
</div>
</div>
<!-- 上课时间 -->
<div class="flex items-start gap-3">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0">
<circle cx="12" cy="12" r="10"></circle>
<polyline points="12 6 12 12 16 14"></polyline>
</svg>
<div>
<p class="text-xs text-gray-500 dark:text-gray-400">上课时间</p>
<p id="modalTime" class="text-sm font-medium text-gray-900 dark:text-white"></p>
</div>
</div>
<!-- 周次范围 -->
<div class="flex items-start gap-3">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0">
<rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect>
<line x1="16" y1="2" x2="16" y2="6"></line>
<line x1="8" y1="2" x2="8" y2="6"></line>
<line x1="3" y1="10" x2="21" y2="10"></line>
</svg>
<div>
<p class="text-xs text-gray-500 dark:text-gray-400">周次范围</p>
<p id="modalWeekRange" class="text-sm font-medium text-gray-900 dark:text-white"></p>
</div>
</div>
<!-- 节数范围 -->
<div class="flex items-start gap-3">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0">
<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
<circle cx="9" cy="7" r="4"></circle>
<path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
<path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
</svg>
<div>
<p class="text-xs text-gray-500 dark:text-gray-400">节数范围</p>
<p id="modalPeriodRange" class="text-sm font-medium text-gray-900 dark:text-white"></p>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block extra_js %}
<script>
// ─── 课程图标 SVG 映射 ────────────────────────────────────────────────────
// 每个图标均为 20×20 stroke 风格,与弹窗内其他 SVG 保持一致
const COURSE_ICONS = {
// 数学类
math: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M4 19V5a2 2 0 0 1 2-2h13.4a.6.6 0 0 1 .6.6v13.114"/>
<path d="M8 23h12a2 2 0 0 0 2-2v-1H8"/>
<path d="M4 21a2 2 0 1 0 4 0V5"/>
<line x1="9" y1="9" x2="15" y2="9"/><line x1="12" y1="6" x2="12" y2="12"/>
<line x1="9" y1="15" x2="15" y2="15"/>
</svg>`,
// 编程/计算机类(代码符号)
code: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="16 18 22 12 16 6"/>
<polyline points="8 6 2 12 8 18"/>
</svg>`,
// 数据库
database: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<ellipse cx="12" cy="5" rx="9" ry="3"/>
<path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"/>
<path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"/>
</svg>`,
// 物理
physics: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="3"/>
<path d="M12 2C6.48 2 2 6.68 2 12c0 1.34.28 2.62.78 3.78"/>
<path d="M22 12c0-5.52-4.48-10-10-10"/>
<ellipse cx="12" cy="12" rx="10" ry="4" transform="rotate(45 12 12)"/>
<ellipse cx="12" cy="12" rx="10" ry="4" transform="rotate(-45 12 12)"/>
</svg>`,
// 化学/实验
chemistry: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M9 2v6l-2.06 3.27A9.06 9.06 0 0 0 6 15a6 6 0 0 0 12 0c0-1.5-.5-2.9-1.42-4L15 8V2"/>
<line x1="9" y1="2" x2="15" y2="2"/>
<line x1="11" y1="14" x2="11" y2="14"/>
<circle cx="11" cy="14" r="1" fill="currentColor"/>
</svg>`,
// 英语/外语(对话气泡)
language: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
<line x1="9" y1="10" x2="15" y2="10"/>
<line x1="12" y1="7" x2="12" y2="13"/>
</svg>`,
// 体育(活动人形)
sport: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="4" r="2"/>
<path d="M15 8h-6l-2 8h10z"/>
<path d="M9 16l-1.5 5"/>
<path d="M15 16l1.5 5"/>
</svg>`,
// 思政/政治/马克思(书本+星)
politics: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/>
<path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/>
<polygon points="12 7 13.2 10.5 17 10.5 14 12.7 15.2 16.2 12 14 8.8 16.2 10 12.7 7 10.5 10.8 10.5"/>
</svg>`,
// 经济/管理/统计(趋势图)
economics: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="18" y1="20" x2="18" y2="10"/>
<line x1="12" y1="20" x2="12" y2="4"/>
<line x1="6" y1="20" x2="6" y2="14"/>
<line x1="2" y1="20" x2="22" y2="20"/>
</svg>`,
// 网络/通信
network: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="2" y="2" width="20" height="8" rx="2" ry="2"/>
<rect x="2" y="14" width="20" height="8" rx="2" ry="2"/>
<line x1="6" y1="6" x2="6.01" y2="6"/>
<line x1="6" y1="18" x2="6.01" y2="18"/>
</svg>`,
// 机械/工程
engineering: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="3"/>
<path d="M19.07 4.93l-1.41 1.41"/>
<path d="M12 2v2"/>
<path d="M4.93 4.93l1.41 1.41"/>
<path d="M2 12h2"/>
<path d="M4.93 19.07l1.41-1.41"/>
<path d="M12 20v2"/>
<path d="M19.07 19.07l-1.41-1.41"/>
<path d="M20 12h2"/>
</svg>`,
// 艺术/音乐/美术
art: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="13.5" cy="6.5" r="0.5" fill="currentColor"/>
<circle cx="17.5" cy="10.5" r="0.5" fill="currentColor"/>
<circle cx="8.5" cy="7.5" r="0.5" fill="currentColor"/>
<circle cx="6.5" cy="12.5" r="0.5" fill="currentColor"/>
<path d="M12 2C6.5 2 2 6.5 2 12s4.5 10 10 10c.926 0 1.648-.746 1.648-1.688 0-.437-.18-.835-.437-1.125-.29-.289-.438-.652-.438-1.125a1.64 1.64 0 0 1 1.668-1.668h1.996c3.051 0 5.555-2.503 5.555-5.554C21.965 6.012 17.461 2 12 2z"/>
</svg>`,
// 法律
law: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 22V8"/>
<path d="M5 12H2a10 10 0 0 0 20 0h-3"/>
<path d="M8 6l4-4 4 4"/>
<path d="M17.5 9.5l-2-4"/>
<path d="M6.5 9.5l2-4"/>
</svg>`,
// 心理/哲学
psychology: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/>
<circle cx="12" cy="12" r="10"/>
<line x1="12" y1="17" x2="12.01" y2="17"/>
</svg>`,
// 默认(翻开的书)
default: `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/>
<path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/>
</svg>`
};
/**
* 根据课程名称匹配最合适的 SVG 图标
* @param {string} name 课程名称
* @returns {string} SVG HTML 字符串
*/
function getCourseIcon(name) {
const n = (name || '');
// 判断辅助:包含任意一个关键词即返回 true
function has(...keys) { return keys.some(k => n.includes(k)); }
// ① 思政/政治(优先级最高,防止被其他规则截胡)
if (has('马克思', '毛泽东', '习近平', '思政', '中国特色', '党史', '形势与政策', '中国近代史', '军事理论', '劳动教育')) return COURSE_ICONS.politics;
// ② 数据库(含"数据库"的课程)
if (has('数据库', 'Database', 'SQL', 'MySQL', 'Oracle', 'Redis')) return COURSE_ICONS.database;
// ③ 网络/通信
if (has('网络', '通信', 'Internet', 'TCP', '协议', '路由', '交换', '物联网', '信息安全')) return COURSE_ICONS.network;
// ④ 数学类
if (has('数学', '微积分', '线性代数', '概率', '统计学', '离散数学', '运筹学')) return COURSE_ICONS.math;
// ⑤ 编程/计算机(注意:不用"程序"以免误伤"面向对象程序设计"→仍归 code,但"数据结构"已在此)
if (has('C语言', 'C++', 'Java', 'Python', '编程', '程序设计', '算法', '数据结构',
'操作系统', '计算机', '软件工程', '软件', 'Web', '前端', '后端',
'人工智能', '机器学习', '深度学习', '网页', '面向对象', '汇编')) return COURSE_ICONS.code;
// ⑥ 物理
if (has('物理', '力学', '热学', '光学', '电磁')) return COURSE_ICONS.physics;
// ⑦ 化学
if (has('化学', '有机', '无机', '生化', '分析化学')) return COURSE_ICONS.chemistry;
// ⑧ 英语/外语
if (has('英语', '外语', '日语', '韩语', '法语', '德语', '语言学', '口语', '写作', '阅读')) return COURSE_ICONS.language;
// ⑨ 体育
if (has('体育', '田径', '篮球', '足球', '游泳', '羽毛球', '乒乓球', '健身', '武术', '瑜伽', '排球')) return COURSE_ICONS.sport;
// ⑩ 经济/管理
if (has('经济', '管理', '会计', '财务', '审计', '市场营销', '人力资源', '项目管理', '运营', '商业')) return COURSE_ICONS.economics;
// ⑪ 机械/工程/电子
if (has('机械', '工程', '电路', '模拟电子', '数字电子', '自动控制', '嵌入式', '单片机', '传感器')) return COURSE_ICONS.engineering;
// ⑫ 艺术/设计
if (has('艺术', '美术', '设计', '音乐', '摄影', '舞蹈', '影视', '动画')) return COURSE_ICONS.art;
// ⑬ 法律
if (has('法律', '法学', '民法', '刑法', '宪法', '经济法', '知识产权')) return COURSE_ICONS.law;
// ⑭ 心理/哲学
if (has('心理', '哲学', '逻辑', '伦理')) return COURSE_ICONS.psychology;
// ⑮ 环境/生态
if (has('环境', '生态', '可持续', '生物', '地理')) return COURSE_ICONS.chemistry;
return COURSE_ICONS.default;
}
// ────────────────────────────────────────────────────────────────────────────
// 全局变量
let periodTimes = {}; // 节次时间 {节次: {start, end}}
let timetableData = null;
let currentWeek = 1;
// 课程颜色配置(统一使用半透明深色背景+浅色文字)
const COURSE_COLORS = [
{ bg: 'bg-blue-900/50', border: 'border-blue-700/70', text: 'text-blue-100' },
{ bg: 'bg-emerald-900/50', border: 'border-emerald-700/70', text: 'text-emerald-100' },
{ bg: 'bg-violet-900/50', border: 'border-violet-700/70', text: 'text-violet-100' },
{ bg: 'bg-amber-900/50', border: 'border-amber-700/70', text: 'text-amber-100' },
{ bg: 'bg-rose-900/50', border: 'border-rose-700/70', text: 'text-rose-100' },
{ bg: 'bg-cyan-900/50', border: 'border-cyan-700/70', text: 'text-cyan-100' },
{ bg: 'bg-lime-900/50', border: 'border-lime-700/70', text: 'text-lime-100' },
{ bg: 'bg-fuchsia-900/50', border: 'border-fuchsia-700/70', text: 'text-fuchsia-100' }
];
// 课程名称到颜色索引的映射(确保相同课程颜色相同)
const courseColorMap = new Map();
// 根据课程名称获取颜色配置
function getCourseColor(courseName) {
if (!courseColorMap.has(courseName)) {
// 使用课程名称的哈希值确保颜色分配稳定
let hash = 0;
for (let i = 0; i < courseName.length; i++) {
hash = courseName.charCodeAt(i) + ((hash << 5) - hash);
}
const colorIndex = Math.abs(hash) % COURSE_COLORS.length;
courseColorMap.set(courseName, colorIndex);
}
return COURSE_COLORS[courseColorMap.get(courseName)];
}
// 检测当前是否为深色模式
function isDarkMode() {
return document.documentElement.classList.contains('dark') ||
window.matchMedia('(prefers-color-scheme: dark)').matches;
}
// API 请求工具函数
async function apiRequest(url, options = {}) {
const headers = {
'Content-Type': 'application/json',
...options.headers
};
const response = await fetch(url, {
credentials: 'same-origin',
...options,
headers
});
// 检查 HTTP 状态码
if (!response.ok) {
const errorText = await response.text();
console.error(`API 请求失败 [${response.status}]:`, errorText);
throw new Error(`服务器错误: ${response.status}`);
}
// 尝试解析 JSON
const text = await response.text();
try {
return JSON.parse(text);
} catch (e) {
console.error('JSON 解析失败,响应内容:', text);
throw new Error('响应数据格式错误');
}
}
// 加载学期信息(只拿周次和当前周,不请求课表)
async function loadSemesterInfo() {
try {
const result = await apiRequest('/api/timetable/semester');
if (result.success && result.data) {
const { xnm, xqm, weeks, current_week, xnm_options, xqm_options } = result.data;
// 填充学年学期下拉框(使用教务系统返回的完整学年列表)
const semSelect = document.getElementById('semesterSelect');
if (xnm_options && xnm_options.length > 0 && xqm_options && xqm_options.length > 0) {
const xqmLabels = {};
xqm_options.forEach(o => { xqmLabels[o.value] = o.label; });
const options = [];
xnm_options.forEach(xo => {
xqm_options.forEach(xqo => {
const label = `${xo.label} 第${xqo.label}学期`;
const val = `${xo.value}|${xqo.value}`;
options.push({
val,
label,
selected: xo.value === xnm && xqo.value === xqm
});
});
});
semSelect.innerHTML = options.map(o =>
`<option value="${o.val}" ${o.selected ? 'selected' : ''}>${o.label}</option>`
).join('');
}
// 填充周次选择
const weekSelect = document.getElementById('weekSelect');
if (weeks && weeks.length > 0) {
weekSelect.innerHTML = weeks.map((week, index) => {
const dateRange = week.zcrq
.replace(/^\d+\(/, '')
.replace(/\)$/, '')
.replace(/至/, '-')
.replace(/(\d{4})-(\d{2})-(\d{2})-/, '$1.$2.$3-')
.replace(/(\d{4})-(\d{2})-(\d{2})/, '$1.$2.$3')
.replace(/^20(\d{2})\./, '$1.')
.replace(/-20(\d{2})\./, '-$1.');
return `<option value="${index + 1}">第${week.zs}周(${dateRange})</option>`;
}).join('');
weekSelect.value = current_week || 1;
currentWeek = current_week || 1;
}
}
} catch (error) {
console.error('加载学期信息失败:', error);
}
}
// 学年学期切换
let _semesterChanging = false;
let _weekChanging = false; // 阻止程序设值时触发 change 事件
async function onSemesterChange() {
if (_semesterChanging) return;
_semesterChanging = true;
_weekChanging = true;
try {
const val = document.getElementById('semesterSelect').value;
if (!val) return;
const [xnm, xqm] = val.split('|');
// 重新加载该学期信息
const result = await apiRequest(`/api/timetable/semester?xnm=${xnm}&xqm=${xqm}`);
if (result.success && result.data) {
const { weeks, current_week } = result.data;
const weekSelect = document.getElementById('weekSelect');
if (weeks && weeks.length > 0) {
weekSelect.innerHTML = weeks.map((week, index) => {
const dateRange = week.zcrq
.replace(/^\d+\(/, '')
.replace(/\)$/, '')
.replace(/至/, '-')
.replace(/(\d{4})-(\d{2})-(\d{2})-/, '$1.$2.$3-')
.replace(/(\d{4})-(\d{2})-(\d{2})/, '$1.$2.$3')
.replace(/^20(\d{2})\./, '$1.')
.replace(/-20(\d{2})\./, '-$1.');
return `<option value="${index + 1}">第${week.zs}周(${dateRange})</option>`;
}).join('');
weekSelect.value = current_week || 1;
currentWeek = current_week || 1;
}
// 携带学年参数重新加载课表
await loadTimetable({ xnm, xqm });
}
} catch (error) {
console.error('切换学期失败:', error);
} finally {
_semesterChanging = false;
_weekChanging = false;
}
}
// 加载节次时间
async function loadPeriodTimes() {
try {
const result = await apiRequest('/api/timetable/jc_times');
if (result.success) {
periodTimes = result.data;
}
} catch (error) {
console.error('加载节次时间失败:', error);
// 使用默认时间作为备选
periodTimes = {
1: { start: '08:30', end: '09:15' },
2: { start: '09:20', end: '10:05' },
3: { start: '10:25', end: '11:10' },
4: { start: '11:15', end: '12:00' },
5: { start: '14:00', end: '14:45' },
6: { start: '14:55', end: '15:40' },
7: { start: '16:00', end: '16:45' },
8: { start: '16:55', end: '17:40' },
9: { start: '19:00', end: '19:45' },
10: { start: '19:55', end: '20:40' },
11: { start: '20:45', end: '21:30' }
};
}
}
// 解析周次描述
function parseWeekRange(zcd) {
// 格式: "1-16周(单)" 或 "1-16周" 或 "3周" 或 "1-2周,4-16周"(多段)
if (!zcd) {
return { start: 1, end: 20, isOdd: false, isEven: false };
}
const isOdd = zcd.includes('(单)');
const isEven = zcd.includes('(双)');
// 提取所有周次段 "1-2周,4-16周" -> ["1-2", "4-16"]
const clean = zcd.replace(/\([^)]*\)/g, '');
const segments = clean.split(',');
let minWeek = 20, maxWeek = 0;
for (const seg of segments) {
const match = seg.match(/(\d+)(?:-(\d+))?/);
if (match) {
const start = parseInt(match[1]);
const end = match[2] ? parseInt(match[2]) : start;
minWeek = Math.min(minWeek, start);
maxWeek = Math.max(maxWeek, end);
}
}
if (minWeek > maxWeek) {
return { start: 1, end: 20, isOdd, isEven };
}
return { start: minWeek, end: maxWeek, isOdd, isEven };
}
// 判断课程是否在指定周次(支持多段格式)
function isCourseInWeek(course, week) {
const zcd = course.zcd;
if (!zcd) return true;
const isOdd = zcd.includes('(单)');
const isEven = zcd.includes('(双)');
// 去掉括号后分割多段
const clean = zcd.replace(/\([^)]*\)/g, '');
const segments = clean.split(',');
let inRange = false;
for (const seg of segments) {
const match = seg.match(/(\d+)(?:-(\d+))?/);
if (match) {
const start = parseInt(match[1]);
const end = match[2] ? parseInt(match[2]) : start;
if (start <= week && week <= end) {
inRange = true;
break;
}
}
}
if (!inRange) return false;
if (isOdd && week % 2 === 0) return false;
if (isEven && week % 2 === 1) return false;
return true;
}
// 解析节数范围
function parsePeriodRange(jcor) {
if (!jcor || typeof jcor !== 'string') {
console.warn('Invalid jcor value:', jcor);
return { start: 1, end: 1 }; // 默认返回第1节
}
const parts = jcor.split(/[-,\s]+/).filter(p => p.trim());
if (parts.length >= 2) {
const start = parseInt(parts[0]);
const end = parseInt(parts[parts.length - 1]);
if (!isNaN(start) && !isNaN(end) && start > 0 && end > 0) {
return { start, end };
}
}
const period = parseInt(jcor);
if (!isNaN(period) && period > 0) {
return { start: period, end: period };
}
console.warn('Failed to parse jcor:', jcor);
return { start: 1, end: 1 }; // 默认返回第1节
}
// 打开课程详情弹窗
function openCourseModal(courseData) {
const modal = document.getElementById('courseModal');
const color = getCourseColor(courseData.kcmc);
// 设置课程信息
document.getElementById('modalCourseName').textContent = courseData.kcmc;
document.getElementById('modalCourseCode').textContent = courseData.kch || '';
document.getElementById('modalTeacher').textContent = courseData.xm || '无';
document.getElementById('modalLocation').textContent = courseData.cdmc || '无';
document.getElementById('modalWeekRange').textContent = courseData.zcd || '无';
document.getElementById('modalPeriodRange').textContent = courseData.jcor || '无';
// 设置上课时间
const periodRange = parsePeriodRange(courseData.jcor);
const startPeriod = periodRange.start;
const endPeriod = periodRange.end;
const timeInfo = periodTimes[startPeriod];
const endTimeInfo = periodTimes[endPeriod];
if (timeInfo && endTimeInfo) {
document.getElementById('modalTime').textContent = `${timeInfo.start} - ${endTimeInfo.end}`;
} else {
document.getElementById('modalTime').textContent = `${startPeriod}-${endPeriod}节`;
}
// 设置课程图标(SVG,按课程名称匹配)
const iconEl = document.getElementById('modalCourseIcon');
iconEl.className = `w-12 h-12 rounded-xl flex items-center justify-center flex-shrink-0 ${color.bg} ${color.text}`;
iconEl.innerHTML = getCourseIcon(courseData.kcmc);
// 显示弹窗
modal.classList.add('modal-open');
// 锁定滚动:同时锁定 html 和 body
document.documentElement.style.overflow = 'hidden';
document.body.style.overflow = 'hidden';
}
// 关闭课程详情弹窗
function closeCourseModal() {
const modal = document.getElementById('courseModal');
modal.classList.remove('modal-open');
// 恢复滚动
document.documentElement.style.overflow = '';
document.body.style.overflow = '';
}
// 更新表头日期(从 rqazcList 读取)
function updateHeaderDates(data) {
const rqazcList = data.rqazcList || [];
const today = new Date();
const todayStr = today.toISOString().slice(0, 10); // "2026-04-06"
rqazcList.forEach(item => {
const dayNum = parseInt(item.xqj); // 1-7
const headerEl = document.getElementById('headerDay' + dayNum);
if (!headerEl) return;
// 格式: "4.6"
const parts = (item.rq || '').split('-');
const dateLabel = parts.length === 3 ? `${parseInt(parts[1])}.${parseInt(parts[2])}` : '';
const dateSpan = headerEl.querySelector('.header-date');
if (dateSpan) dateSpan.textContent = dateLabel;
// 高亮今天
if (item.rq === todayStr) {
headerEl.classList.add('bg-black/5', 'dark:bg-white/10', 'rounded-lg');
headerEl.querySelector('span:first-child').classList.add('text-black', 'dark:text-white');
} else {
headerEl.classList.remove('bg-black/5', 'dark:bg-white/10', 'rounded-lg');
headerEl.querySelector('span:first-child').classList.remove('text-black', 'dark:text-white');
}
});
}
// 渲染课表 - Grid 布局版本
function renderTimetable(data) {
const container = document.getElementById('timetableContainer');
const timetableGrid = document.getElementById('timetableGrid');
const emptyState = document.getElementById('emptyState');
const courses = data.kbList || [];
if (courses.length === 0) {
container.classList.add('hidden');
emptyState.classList.remove('hidden');
return;
}
container.classList.remove('hidden');
emptyState.classList.add('hidden');
timetableData = data;
// 更新表头日期
updateHeaderDates(data);
// 创建课表网格:11节 x 7天
const grid = Array(11).fill(null).map(() => Array(7).fill(null));
// 填充课程数据
courses.forEach(course => {
const dayIndex = parseInt(course.xqj) - 1; // 0-6
if (isNaN(dayIndex) || dayIndex < 0 || dayIndex > 6) {
console.warn('Invalid xqj for course:', course.kcmc, course.xqj);
return;
}
const periodRange = parsePeriodRange(course.jcor);
// 检查是否在当前周
if (!isCourseInWeek(course, currentWeek)) {
return;
}
// 标记该课程占用的格子
for (let p = periodRange.start; p <= periodRange.end; p++) {
if (p >= 1 && p <= 11) {
const periodIdx = p - 1;
if (!grid[periodIdx][dayIndex]) {
grid[periodIdx][dayIndex] = {
course: course,
isStart: p === periodRange.start,
rowSpan: periodRange.end - periodRange.start + 1
};
}
}
}
});
// 渲染 Grid
let html = '';
// 按列优先分配延迟:day列 * 55ms + period行 * 18ms,最多 400ms
for (let period = 1; period <= 11; period++) {
const periodIdx = period - 1;
const timeInfo = periodTimes[period] || { start: '', end: '' };
// 节次列(第1列)
html += `
<div class="bg-white dark:bg-gray-900 rounded-lg p-1 sm:p-2 flex flex-col justify-center items-center border border-gray-200 dark:border-gray-700" style="grid-column: 1; grid-row: ${period};">
<div class="text-sm sm:text-lg font-bold text-gray-800 dark:text-gray-200">${period}</div>
<div class="text-[8px] sm:text-[10px] text-gray-500 dark:text-gray-400">${timeInfo.start}</div>
<div class="text-[8px] sm:text-[10px] text-gray-500 dark:text-gray-400">${timeInfo.end}</div>
</div>
`;
// 7天的课程
for (let day = 0; day < 7; day++) {
const cell = grid[periodIdx][day];
if (cell && cell.isStart) {
// 按列优先错开:列间距 55ms,行间距 18ms
const delay = Math.min(day * 55 + periodIdx * 18, 400);
const color = getCourseColor(cell.course.kcmc);
html += `
<div class="course-cell rounded-lg overflow-hidden ${color.bg} ${color.text} border-l-4 ${color.border} hover:shadow-lg hover:scale-[1.03] hover:brightness-110 transition-all duration-200 cursor-pointer flex flex-col justify-center p-1 sm:p-2"
style="grid-column: ${day + 2}; grid-row: ${period} / span ${cell.rowSpan}; animation-delay: ${delay}ms;"
data-course-key="${cell.course.kcmc}-${day}"
onclick="openCourseModal(${JSON.stringify(cell.course).replace(/"/g, '&quot;')})">
<div class="font-bold text-[10px] sm:text-xs leading-tight mb-0.5 sm:mb-1">${cell.course.kcmc}</div>
<div class="text-[8px] sm:text-[10px] opacity-90">${cell.course.cdmc}</div>
${cell.course.xm ? `<div class="text-[8px] sm:text-[10px] opacity-75 mt-0.5">${cell.course.xm}</div>` : ''}
</div>
`;
} else if (!cell) {
// 空格子
html += `
<div class="bg-gray-50/30 dark:bg-gray-800/20 rounded-lg border border-gray-200 dark:border-gray-700/50" style="grid-column: ${day + 2}; grid-row: ${period};"></div>
`;
}
// 如果是跨行课程的后续格子,不渲染(由 grid-row span 自动填充)
}
}
timetableGrid.innerHTML = html;
}
// 更新当前时间显示
function updateCurrentTime() {
const now = new Date();
const timeStr = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
document.getElementById('currentTime').textContent = timeStr;
}
// 加载课表
async function loadTimetable({ silent = false, xnm: overrideXnm, xqm: overrideXqm } = {}) {
const weekSelect = document.getElementById('weekSelect');
if (!weekSelect) {
console.error('weekSelect 元素不存在');
return;
}
const week = weekSelect.value;
// 获取当前选中的学年学期
const semVal = document.getElementById('semesterSelect').value;
let xnmParam = overrideXnm || '';
let xqmParam = overrideXqm || '';
if ((!xnmParam || !xqmParam) && semVal) {
const parts = semVal.split('|');
xnmParam = parts[0];
xqmParam = parts[1];
}
// 切周时:先让当前 grid 淡出,再加载新内容
const timetableGrid = document.getElementById('timetableGrid');
const hasCells = timetableGrid && timetableGrid.querySelector('.course-cell');
if (!silent && hasCells) {
timetableGrid.classList.add('switching');
await new Promise(r => setTimeout(r, 190));
timetableGrid.classList.remove('switching');
}
if (!silent) {
showLoading(true);
hideAllMessages();
}
try {
const params = new URLSearchParams({ kblx: '1', week });
if (xnmParam) params.set('xnm', xnmParam);
if (xqmParam) params.set('xqm', xqmParam);
const result = await apiRequest(`/api/timetable?${params}`);
if (result.success) {
renderTimetable(result.data);
} else {
showError(result.message || '加载课表失败');
}
} catch (error) {
console.error('加载课表失败:', error);
showError(`网络错误: ${error.message}`);
} finally {
if (!silent) {
showLoading(false);
}
}
}
// 显示/隐藏加载状态
function showLoading(show) {
const el = document.getElementById('loadingIndicator');
el.style.display = show ? 'block' : 'none';
}
// 显示错误消息
function showError(message) {
const el = document.getElementById('errorMessage');
const text = document.getElementById('errorText');
// 检查是否是课表错误,添加友好提示
if (message.includes('xskbcxMobile_cxXsKb') && message.includes('HTML 错误页面')) {
message += '\n\n提示:请尝试登录教务系统(https://jwxt.sdmu.edu.cn/jwglxt)检查账号状态,如需要更换密码请在教务系统中操作,然后运行:python app/update_session.py -u <学号>';
}
text.textContent = message;
el.classList.remove('hidden');
}
// 隐藏所有消息
function hideAllMessages() {
document.getElementById('errorMessage').classList.add('hidden');
document.getElementById('emptyState').classList.add('hidden');
document.getElementById('timetableContainer').classList.add('hidden');
}
// 页面加载时初始化
document.addEventListener('DOMContentLoaded', function () {
// 将弹窗移到 body 直接子级,确保 fixed 定位基于视口居中
const modal = document.getElementById('courseModal');
if (modal) {
document.body.appendChild(modal);
}
// 并行发起学期信息 + 节次时间请求
// 两者完成后直接加载当前周课表(weekSelect.value 已在 loadSemesterInfo 中就位)
showLoading(true);
hideAllMessages();
const semesterPromise = loadSemesterInfo();
const periodTimesPromise = loadPeriodTimes();
Promise.allSettled([semesterPromise, periodTimesPromise]).then(results => {
// 学期信息加载完后直接拉课表(weekSelect.value 和 currentWeek 已在 loadSemesterInfo 中就位)
showLoading(false);
if (results[0].status === 'fulfilled') {
loadTimetable();
}
});
// 周次切换
document.getElementById('weekSelect').addEventListener('change', function() {
if (_weekChanging) { _weekChanging = false; return; }
currentWeek = parseInt(this.value);
loadTimetable();
});
// 上一周
document.getElementById('prevWeek').addEventListener('click', function () {
const weekSelect = document.getElementById('weekSelect');
const currentValue = parseInt(weekSelect.value);
if (currentValue > 1) {
weekSelect.value = currentValue - 1;
currentWeek = currentValue - 1;
loadTimetable();
}
});
// 下一周
document.getElementById('nextWeek').addEventListener('click', function () {
const weekSelect = document.getElementById('weekSelect');
const maxValue = weekSelect.options.length;
const currentValue = parseInt(weekSelect.value);
if (currentValue < maxValue) {
weekSelect.value = currentValue + 1;
currentWeek = currentValue + 1;
loadTimetable();
}
});
// 每分钟更新当前时间
setInterval(updateCurrentTime, 60000);
updateCurrentTime();
// ESC 键关闭弹窗
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') {
closeCourseModal();
}
});
});
</script>
{% endblock %}