feat: 新增自动下载 API 和设置页面 UI

This commit is contained in:
2026-04-29 16:18:31 +08:00
parent 89b01bb522
commit 75bdc94cfe
5 changed files with 808 additions and 0 deletions
+103
View File
@@ -395,6 +395,109 @@ function closeUploadModal() {
elements.uploadModal.classList.remove('active');
}
// --- Auto Download ---
let autoDownloadPollTimer = null;
function openAutoDownloadModal() {
const modal = document.getElementById('autoDownloadModal');
// 默认日期为昨天
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const dateStr = yesterday.toISOString().split('T')[0];
document.getElementById('autoStartDate').value = dateStr;
document.getElementById('autoEndDate').value = dateStr;
document.getElementById('autoDownloadStatus').style.display = 'none';
document.getElementById('autoDownloadBtn').disabled = false;
modal.classList.add('active');
}
function closeAutoDownloadModal() {
const modal = document.getElementById('autoDownloadModal');
modal.classList.remove('active');
if (autoDownloadPollTimer) {
clearInterval(autoDownloadPollTimer);
autoDownloadPollTimer = null;
}
}
function startAutoDownload() {
const startDate = document.getElementById('autoStartDate').value;
const endDate = document.getElementById('autoEndDate').value;
if (!startDate) {
alert('请选择开始日期');
return;
}
const btn = document.getElementById('autoDownloadBtn');
btn.disabled = true;
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 下载中...';
const statusDiv = document.getElementById('autoDownloadStatus');
const statusText = document.getElementById('autoDownloadStatusText');
statusDiv.style.display = 'flex';
statusText.textContent = '正在启动下载任务...';
fetch('/api/auto-download', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ start_date: startDate, end_date: endDate || startDate })
})
.then(res => res.json())
.then(data => {
if (data.success) {
statusText.textContent = data.message;
// 开始轮询状态
pollAutoDownloadStatus();
} else {
statusText.textContent = data.error || '启动失败';
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-download"></i> 重试';
}
})
.catch(err => {
statusText.textContent = '请求失败: ' + err.message;
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-download"></i> 重试';
});
}
function pollAutoDownloadStatus() {
if (autoDownloadPollTimer) clearInterval(autoDownloadPollTimer);
autoDownloadPollTimer = setInterval(() => {
fetch('/api/auto-download/status')
.then(res => res.json())
.then(data => {
if (data.success) {
const status = data.status;
const statusText = document.getElementById('autoDownloadStatusText');
const btn = document.getElementById('autoDownloadBtn');
statusText.textContent = status.message;
if (!status.running) {
clearInterval(autoDownloadPollTimer);
autoDownloadPollTimer = null;
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-download"></i> 开始下载';
if (status.last_file) {
// 下载成功,刷新文件列表
setTimeout(() => {
closeAutoDownloadModal();
loadFileList();
}, 1500);
}
}
}
})
.catch(() => { });
}, 2000);
}
function showLoading(show, text) {
elements.loadingOverlay.style.display = show ? 'flex' : 'none';
if (text) elements.loadingText.textContent = text;