let proId = decryptCBC(getUrlParam('proId')); let type = decryptCBC(getUrlParam('type')); let title = decryptCBC(getUrlParam('title')); let proName = decryptCBC(getUrlParam('proName')); $('#title').html(proName +"-"+ title); document.getElementById('downloadBtn').addEventListener('click', function() { const btn = this; btn.disabled = true; document.getElementById('progressContainer').style.display = 'block'; // 创建任务ID const taskId = 'task_' + Date.now(); // 使用EventSource接收服务器推送的进度更新 const eventSource = new EventSource(`/imgTool/api/download/progress?taskId=${taskId}`); eventSource.onmessage = function(event) { const data = JSON.parse(event.data); if (data.type === 'progress') { // 更新进度条 document.getElementById('progress').style.width = data.progress + '%'; document.getElementById('statusText').textContent = `正在压缩: ${data.progress}% (已处理 ${data.processed} / ${data.total} 文件)`; } else if (data.type === 'complete') { // 完成处理 document.getElementById('progress').style.width = '100%'; document.getElementById('statusText').textContent = '压缩完成!'; // 显示下载通知 const notification = document.getElementById('downloadNotification'); const downloadLink = document.getElementById('downloadLink'); downloadLink.onclick = function(e) { e.preventDefault(); window.location.href = data.downloadUrl; notification.style.display = 'none'; window.close(); }; notification.style.display = 'block'; // 2小时后自动隐藏通知 setTimeout(() => { notification.style.display = 'none'; }, 1000 * 60 * 60 * 2); // 关闭EventSource连接 eventSource.close(); } else if (data.type === 'error') { // 错误处理 document.getElementById('statusText').textContent = '错误: ' + data.message; btn.disabled = false; eventSource.close(); } }; eventSource.onerror = function() { document.getElementById('statusText').textContent = '连接出错,请重试'; btn.disabled = false; eventSource.close(); }; // 启动下载任务 fetch('/imgTool/api/download/start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ taskId: taskId, proId: proId, type: type, }) }).catch(error => { document.getElementById('statusText').textContent = '启动任务失败'; btn.disabled = false; eventSource.close(); }); });