// 论文分析 JavaScript
// 将API密钥存储在会话存储中
let apiKey = sessionStorage.getItem('deepseekApiKey');
// DOM元素
const apiKeyInput = document.getElementById('apiKey');
const saveApiKeyBtn = document.getElementById('saveApiKey');
const fileInput = document.getElementById('paperFile');
const fileInfo = document.getElementById('fileInfo');
const analyzeBtn = document.getElementById('analyzePaper');
const resultsSection = document.querySelector('.results-section');
const tabButtons = document.querySelectorAll('.tab-btn');
const tabPanes = document.querySelectorAll('.tab-pane');
const exportBtn = document.getElementById('exportResults');
// 如果存在API密钥则初始化
if (apiKey) {
apiKeyInput.value = apiKey;
}
// 保存API密钥并测试连接
saveApiKeyBtn.addEventListener('click', async () => {
const newApiKey = apiKeyInput.value.trim();
if (!newApiKey) {
showNotification('请输入有效的API密钥', 'error');
return;
}
try {
showLoading('正在测试API连接...');
const response = await fetch('/paper-analysis/api/test-deepseek', {
method: 'POST',
headers: {
'X-API-Key': newApiKey,
'Content-Type': 'application/json'
}
});
const data = await response.json();
if (response.ok && data.success) {
sessionStorage.setItem('deepseekApiKey', newApiKey);
apiKey = newApiKey;
showNotification('API连接成功', 'success');
} else {
showNotification(`API错误: ${data.error || '未知错误'}`, 'error');
console.error('API错误详情:', data);
}
} catch (error) {
showNotification('测试API连接时出错: ' + error.message, 'error');
console.error('API测试错误:', error);
} finally {
hideLoading();
}
});
// 文件上传处理
fileInput.addEventListener('change', handleFileSelect);
document.querySelector('.file-upload-container').addEventListener('dragover', handleDragOver);
document.querySelector('.file-upload-container').addEventListener('drop', handleFileDrop);
function handleFileSelect(event) {
const file = event.target.files[0];
if (file) {
updateFileInfo(file);
}
}
function handleDragOver(event) {
event.preventDefault();
event.stopPropagation();
event.currentTarget.classList.add('drag-over');
}
function handleFileDrop(event) {
event.preventDefault();
event.stopPropagation();
event.currentTarget.classList.remove('drag-over');
const file = event.dataTransfer.files[0];
if (file) {
fileInput.files = event.dataTransfer.files;
updateFileInfo(file);
}
}
function updateFileInfo(file) {
const sizeInMB = (file.size / (1024 * 1024)).toFixed(2);
fileInfo.innerHTML = `
文件: ${file.name}
大小: ${sizeInMB} MB
类型: ${file.type || '未知'}
`;
}
// 标签页导航
tabButtons.forEach(button => {
button.addEventListener('click', () => {
const tabName = button.getAttribute('data-tab');
// 更新激活状态
tabButtons.forEach(btn => btn.classList.remove('active'));
tabPanes.forEach(pane => pane.classList.remove('active'));
button.classList.add('active');
document.getElementById(`${tabName}Tab`).classList.add('active');
});
});
// 论文分析
analyzeBtn.addEventListener('click', async () => {
if (!apiKey) {
showNotification('请先配置您的Deepseek API密钥', 'error');
return;
}
if (!fileInput.files[0]) {
showNotification('请选择要分析的文件', 'error');
return;
}
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('extract_keywords', document.getElementById('extractKeywords').checked);
formData.append('generate_summary', document.getElementById('generateSummary').checked);
formData.append('find_related', document.getElementById('findRelatedWorks').checked);
try {
showLoading('正在分析论文...');
const response = await fetch('/paper-analysis/api/analyze-paper', {
method: 'POST',
headers: {
'X-API-Key': apiKey
},
body: formData
});
const responseData = await response.json();
if (!response.ok) {
// 显示详细的错误信息
const errorMessage = responseData.error || `HTTP错误! 状态: ${response.status}`;
throw new Error(errorMessage);
}
displayResults(responseData);
hideLoading();
resultsSection.style.display = 'block';
showNotification('分析完成成功', 'success');
} catch (error) {
hideLoading();
console.error('分析错误:', error);
showNotification('分析论文时出错: ' + error.message, 'error');
}
});
// 显示结果
function displayResults(results) {
// 显示关键词
const keywordsContainer = document.querySelector('.keywords-container');
if (results.keywords) {
keywordsContainer.innerHTML = results.keywords.map(keyword =>
`