295 lines
13 KiB
HTML
295 lines
13 KiB
HTML
{% extends "base_tailwind.html" %}
|
|
|
|
{% block title %}抢课日志 - 教务抢课系统{% endblock %}
|
|
|
|
{% block content %}
|
|
<div>
|
|
<!-- 页面标题 -->
|
|
<div class="flex justify-between items-center mb-6 list-item-enter" style="animation-delay: 0ms;">
|
|
<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"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
|
|
抢课日志
|
|
</h2>
|
|
<div>
|
|
<button onclick="clearLogs()"
|
|
class="px-4 py-2 border border-red-300 dark:border-red-700 text-red-600 dark:text-red-400 rounded-full hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors flex items-center gap-2">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/></svg>
|
|
清空日志
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 筛选栏 -->
|
|
<div class="glass rounded-2xl p-6 mb-6 list-item-enter" style="animation-delay: 50ms;">
|
|
<div class="flex flex-col md:flex-row gap-4 items-center">
|
|
<div class="flex-1 grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
<div>
|
|
<select id="levelFilter" onchange="loadLogs()"
|
|
class="w-full px-4 py-2 rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-black focus:outline-none focus:ring-2 focus:ring-black dark:focus:ring-white">
|
|
<option value="">全部级别</option>
|
|
<option value="info">信息</option>
|
|
<option value="success">成功</option>
|
|
<option value="warning">警告</option>
|
|
<option value="error">错误</option>
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<select id="taskFilter" onchange="loadLogs()"
|
|
class="w-full px-4 py-2 rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-black focus:outline-none focus:ring-2 focus:ring-black dark:focus:ring-white">
|
|
<option value="">全部任务</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="flex items-center gap-4">
|
|
<button onclick="loadLogs()"
|
|
class="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-full hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors flex items-center gap-2">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/><path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16"/><path d="M16 21h5v-5"/></svg>
|
|
刷新
|
|
</button>
|
|
<label class="flex items-center gap-2 cursor-pointer">
|
|
<input type="checkbox" id="autoRefresh" checked onchange="setupAutoRefresh()"
|
|
class="w-4 h-4 accent-black dark:accent-white">
|
|
<span class="text-sm">自动刷新</span>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 日志列表 -->
|
|
<div class="glass rounded-2xl p-6 list-item-enter" style="animation-delay: 100ms;">
|
|
<div id="loadingIndicator" class="text-center py-12">
|
|
<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="emptyState" class="text-center py-12" style="display: none;">
|
|
<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"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
|
|
<p class="text-gray-500 dark:text-gray-400">暂无日志</p>
|
|
</div>
|
|
|
|
<div id="logsContainer" style="display: none;">
|
|
<div id="logsList" class="space-y-2 max-h-[600px] overflow-y-auto font-mono text-sm max-w-4xl mx-auto">
|
|
<!-- 日志内容 -->
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 分页 -->
|
|
<nav id="pagination" class="mt-6" style="display: none;">
|
|
<div class="flex justify-center items-center gap-2">
|
|
</div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
|
|
{% block extra_js %}
|
|
<script>
|
|
let currentPage = 1;
|
|
let autoRefreshInterval = null;
|
|
let lastLogId = 0;
|
|
|
|
// 加载日志
|
|
async function loadLogs(page = 1, incremental = false) {
|
|
if (!incremental) {
|
|
currentPage = page;
|
|
document.getElementById('loadingIndicator').style.display = 'block';
|
|
document.getElementById('emptyState').style.display = 'none';
|
|
document.getElementById('logsContainer').style.display = 'none';
|
|
document.getElementById('pagination').style.display = 'none';
|
|
}
|
|
|
|
const level = document.getElementById('levelFilter').value;
|
|
const taskId = document.getElementById('taskFilter').value;
|
|
|
|
try {
|
|
let url;
|
|
if (incremental && lastLogId > 0) {
|
|
url = `/api/logs/recent?after_id=${lastLogId}`;
|
|
if (taskId) url += `&task_id=${taskId}`;
|
|
} else {
|
|
url = `/api/logs/list?page=${page}&per_page=50`;
|
|
if (level) url += `&level=${level}`;
|
|
if (taskId) url += `&task_id=${taskId}`;
|
|
}
|
|
|
|
const data = await apiRequest(url);
|
|
|
|
if (!incremental) {
|
|
document.getElementById('loadingIndicator').style.display = 'none';
|
|
}
|
|
|
|
if (!data.success) {
|
|
if (!incremental) {
|
|
document.getElementById('emptyState').style.display = 'block';
|
|
}
|
|
return;
|
|
}
|
|
|
|
const logs = data.logs || [];
|
|
|
|
if (logs.length === 0 && !incremental) {
|
|
document.getElementById('emptyState').style.display = 'block';
|
|
return;
|
|
}
|
|
|
|
renderLogs(logs, incremental);
|
|
|
|
if (!incremental) {
|
|
document.getElementById('logsContainer').style.display = 'block';
|
|
if (data.pages > 1) {
|
|
renderPagination(data.total, data.pages, page);
|
|
document.getElementById('pagination').style.display = 'block';
|
|
}
|
|
}
|
|
|
|
} catch (error) {
|
|
if (!incremental) {
|
|
document.getElementById('loadingIndicator').style.display = 'none';
|
|
showToast('加载失败', 'error');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 渲染日志
|
|
function renderLogs(logs, append = false) {
|
|
const container = document.getElementById('logsList');
|
|
|
|
if (!append) {
|
|
container.innerHTML = '';
|
|
lastLogId = 0;
|
|
}
|
|
|
|
logs.forEach(log => {
|
|
if (log.id > lastLogId) {
|
|
lastLogId = log.id;
|
|
}
|
|
|
|
const levelColors = {
|
|
'info': 'text-blue-600 dark:text-blue-400',
|
|
'success': 'text-green-600 dark:text-green-400',
|
|
'warning': 'text-yellow-600 dark:text-yellow-400',
|
|
'error': 'text-red-600 dark:text-red-400'
|
|
};
|
|
|
|
const logClass = levelColors[log.level] || 'text-gray-600 dark:text-gray-400';
|
|
const taskInfo = log.task_id ? `<span class="text-gray-400">[任务#${log.task_id}]</span> ` : '';
|
|
|
|
const logHtml = `
|
|
<div class="p-3 rounded-lg bg-white/50 dark:bg-black/30 border border-gray-100 dark:border-gray-800 hover:bg-white dark:hover:bg-black transition-colors">
|
|
<span class="text-gray-400 text-xs">${formatTime(log.created_at)}</span>
|
|
<span class="${logClass} font-bold text-xs">[${log.level.toUpperCase()}]</span>
|
|
${taskInfo}<span class="text-gray-700 dark:text-gray-300">${escapeHtml(log.message)}</span>
|
|
</div>
|
|
`;
|
|
|
|
container.insertAdjacentHTML('beforeend', logHtml);
|
|
// 防 DOM 无限增长(长挂机内存泄漏)
|
|
while (container.children.length > 500) container.firstChild.remove();
|
|
});
|
|
|
|
// 滚动到底部(如果是追加模式)
|
|
if (append) {
|
|
container.scrollTop = container.scrollHeight;
|
|
}
|
|
}
|
|
|
|
// 渲染分页
|
|
function renderPagination(total, pages, current) {
|
|
const nav = document.getElementById('pagination').querySelector('div');
|
|
let html = '';
|
|
|
|
html += `
|
|
<button onclick="loadLogs(${current - 1})" ${current === 1 ? 'disabled' : ''}
|
|
class="px-3 py-1 rounded-lg border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 disabled:cursor-not-allowed">
|
|
上一页
|
|
</button>
|
|
`;
|
|
|
|
for (let i = 1; i <= pages; i++) {
|
|
if (i === 1 || i === pages || (i >= current - 2 && i <= current + 2)) {
|
|
html += `
|
|
<button onclick="loadLogs(${i})"
|
|
class="px-3 py-1 rounded-lg transition-colors ${i === current ? 'bg-black dark:bg-white text-white dark:text-black' : 'border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-800'}">
|
|
${i}
|
|
</button>
|
|
`;
|
|
} else if (i === current - 3 || i === current + 3) {
|
|
html += `<span class="px-2 text-gray-400">...</span>`;
|
|
}
|
|
}
|
|
|
|
html += `
|
|
<button onclick="loadLogs(${current + 1})" ${current === pages ? 'disabled' : ''}
|
|
class="px-3 py-1 rounded-lg border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 disabled:cursor-not-allowed">
|
|
下一页
|
|
</button>
|
|
`;
|
|
|
|
nav.innerHTML = html;
|
|
}
|
|
|
|
// 加载任务列表(用于筛选)
|
|
async function loadTasksForFilter() {
|
|
try {
|
|
const data = await apiRequest('/api/tasks/list?per_page=100');
|
|
if (data.success && data.tasks) {
|
|
const select = document.getElementById('taskFilter');
|
|
// 保留第一个选项
|
|
const firstOption = select.options[0];
|
|
select.innerHTML = '';
|
|
select.appendChild(firstOption);
|
|
|
|
data.tasks.forEach(task => {
|
|
const option = document.createElement('option');
|
|
option.value = task.id;
|
|
option.textContent = `#${task.id} ${task.course_name}`;
|
|
select.appendChild(option);
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('加载任务列表失败:', error);
|
|
}
|
|
}
|
|
|
|
// 清空日志
|
|
async function clearLogs() {
|
|
if (!confirm('确定要清空所有日志吗?此操作不可恢复。')) return;
|
|
|
|
// 这里可以添加清空日志的API调用
|
|
showToast('日志已清空', 'success');
|
|
loadLogs();
|
|
}
|
|
|
|
// HTML转义
|
|
function escapeHtml(text) {
|
|
if (!text) return '';
|
|
const div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
|
|
// 设置自动刷新
|
|
function setupAutoRefresh() {
|
|
const checkbox = document.getElementById('autoRefresh');
|
|
|
|
if (checkbox.checked) {
|
|
autoRefreshInterval = setInterval(() => {
|
|
loadLogs(currentPage, true);
|
|
}, 3000);
|
|
} else {
|
|
if (autoRefreshInterval) {
|
|
clearInterval(autoRefreshInterval);
|
|
autoRefreshInterval = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 页面加载
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
loadLogs();
|
|
loadTasksForFilter();
|
|
setupAutoRefresh();
|
|
});
|
|
</script>
|
|
{% endblock %}
|