// 当DOM加载完成后执行 document.addEventListener('DOMContentLoaded', function() { let currentPage = 0; let isLoading = false; let hasMore = true; // 初始化加载第一页数据 loadAnalyses(); // 无限滚动加载 let scrollTimeout; window.addEventListener('scroll', function() { // 防抖处理,避免频繁触发 clearTimeout(scrollTimeout); scrollTimeout = setTimeout(function() { if (isLoading || !hasMore) return; const scrollPosition = window.pageYOffset; const windowSize = window.innerHeight; const bodyHeight = document.body.offsetHeight; // 当滚动到距离底部200px时加载更多 if (bodyHeight - (scrollPosition + windowSize) < 200) { loadAnalyses(); } }, 100); }); // 返回顶部按钮功能 const backToTopBtn = document.getElementById('backToTop'); window.addEventListener('scroll', function() { if (window.pageYOffset > 300) { backToTopBtn.style.display = 'block'; } else { backToTopBtn.style.display = 'none'; } }); backToTopBtn.addEventListener('click', function() { window.scrollTo({ top: 0, behavior: 'smooth' }); }); // 加载分析数据 function loadAnalyses() { if (isLoading || !hasMore) return; isLoading = true; showLoading(); const xhr = new XMLHttpRequest(); const url = CONTEXT_PATH + '/AnalysisTimelineServlet?action=loadMore&page=' + currentPage + '&pageSize=20'; console.log('请求URL:', url); xhr.open('GET', url, true); xhr.timeout = 10000; // 10秒超时 xhr.onload = function() { isLoading = false; hideLoading(); if (xhr.status === 200) { try { const response = JSON.parse(xhr.responseText); if (!response.success) { console.error('服务器返回错误:', response.error); showError('服务器错误: ' + response.error); return; } if (response.data && response.data.length > 0) { renderAnalyses(response.data); currentPage++; // 根据hasMore字段判断是否还有更多数据 if (!response.hasMore) { hasMore = false; showNoMore(); } } else { hasMore = false; showNoMore(); } } catch (e) { console.error('解析响应数据时出错:', e); showError('解析数据时出错: ' + e.message); } } else { console.error('请求失败,状态码:', xhr.status); showError('请求失败,状态码: ' + xhr.status); } }; xhr.onerror = function() { isLoading = false; hideLoading(); console.error('网络请求失败'); showError('网络请求失败,请检查网络连接'); }; xhr.ontimeout = function() { isLoading = false; hideLoading(); console.error('请求超时'); showError('请求超时,请检查网络连接'); }; xhr.send(); } // 渲染分析数据到时间轴 function renderAnalyses(analyses) { const timeline = document.getElementById('timeline'); analyses.forEach((analysis, index) => { const timelineItem = createTimelineItem(analysis, currentPage * 20 + index); timeline.appendChild(timelineItem); }); } // 创建单个时间轴项目 function createTimelineItem(analysis, index) { const item = document.createElement('div'); item.className = 'timeline-item'; // 格式化日期 const publishDate = formatDate(new Date(analysis.publishTime)); const analysisDate = analysis.analysisTime ? formatDate(new Date(analysis.analysisTime)) : '未知时间'; // 处理分析内容,将**加粗**文本转换为HTML let analysisHtml = analysis.analysis || '暂无分析内容'; analysisHtml = analysisHtml.replace(/\*\*(.*?)\*\*/g, '$1'); analysisHtml = analysisHtml.replace(/\n/g, '
'); // 处理内容文本,添加展开/收起功能 const contentText = analysis.content || '无详细内容'; const isContentLong = contentText.length > 150; const displayContent = isContentLong ? contentText.substring(0, 150) + '...' : contentText; item.innerHTML = `
${publishDate}

${analysis.title}

消息ID: ${analysis.telegraphId} 分析时间: ${analysisDate}
${displayContent}
${isContentLong ? `
展开全文
` : ''}
DeepSeek智能分析
${analysisHtml}
`; // 添加延迟动画效果 item.style.animationDelay = `${index * 0.1}s`; return item; } // 格式化日期为YYYY-MM-DD HH:MM格式 function formatDate(date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${year}-${month}-${day} ${hours}:${minutes}`; } // 显示加载指示器 function showLoading() { const loading = document.getElementById('loading'); const noMore = document.getElementById('noMore'); loading.style.display = 'block'; noMore.style.display = 'none'; } // 隐藏加载指示器 function hideLoading() { document.getElementById('loading').style.display = 'none'; } // 显示无更多内容 function showNoMore() { const loading = document.getElementById('loading'); const noMore = document.getElementById('noMore'); loading.style.display = 'none'; noMore.style.display = 'block'; } // 显示错误信息 function showError(message) { // 简单的错误提示 const errorDiv = document.createElement('div'); errorDiv.style.cssText = ` position: fixed; top: 20px; right: 20px; background: #e74c3c; color: white; padding: 15px 20px; border-radius: 5px; box-shadow: 0 4px 12px rgba(0,0,0,0.15); z-index: 1000; max-width: 300px; font-family: inherit; `; errorDiv.innerHTML = ` 错误: ${message} `; document.body.appendChild(errorDiv); // 5秒后自动移除错误提示 setTimeout(() => { if (errorDiv.parentElement) { errorDiv.remove(); } }, 5000); } // 转义HTML特殊字符 function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } // 全局函数,用于切换内容展开/收起 window.toggleContent = function(id, fullContent) { const contentElement = document.getElementById('content-' + id); const readMoreElement = contentElement.nextElementSibling; if (contentElement.classList.contains('expanded')) { // 收起 const shortContent = fullContent.substring(0, 150) + '...'; contentElement.innerHTML = shortContent; contentElement.classList.remove('expanded'); if (readMoreElement && readMoreElement.classList.contains('read-more')) { readMoreElement.textContent = '展开全文'; } } else { // 展开 contentElement.innerHTML = fullContent; contentElement.classList.add('expanded'); if (readMoreElement && readMoreElement.classList.contains('read-more')) { readMoreElement.textContent = '收起'; } } }; });