// 当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 = `