// 研创·智核 - 主应用JavaScript class InnoCoreApp { constructor() { this.api = new API(); this.router = new Router(); this.state = new StateManager(); this.init(); } init() { this.setupEventListeners(); this.setupRouter(); this.checkAuth(); } setupEventListeners() { // 全局事件监听 document.addEventListener('click', (e) => { if (e.target.matches('[data-action]')) { this.handleAction(e.target.dataset.action, e.target); } }); // 表单提交 document.addEventListener('submit', (e) => { if (e.target.matches('.ajax-form')) { e.preventDefault(); this.handleFormSubmit(e.target); } }); // 模态框关闭 document.addEventListener('click', (e) => { if (e.target.matches('.modal') || e.target.matches('.modal-close')) { this.closeModal(e.target.closest('.modal')); } }); } setupRouter() { // 路由配置 this.router.addRoute('/', () => this.showDashboard()); this.router.addRoute('/papers', () => this.showPapers()); this.router.addRoute('/papers/new', () => this.showNewPaper()); this.router.addRoute('/papers/:id', (params) => this.showPaperDetail(params.id)); this.router.addRoute('/tasks', () => this.showTasks()); this.router.addRoute('/tasks/:id', (params) => this.showTaskDetail(params.id)); this.router.addRoute('/analysis', () => this.showAnalysis()); this.router.addRoute('/writing', () => this.showWriting()); this.router.addRoute('/profile', () => this.showProfile()); } async checkAuth() { const token = localStorage.getItem('token'); if (token) { try { const user = await this.api.get('/auth/me'); this.state.setUser(user); this.updateUI(); } catch (error) { localStorage.removeItem('token'); this.showLogin(); } } else { this.showLogin(); } } updateUI() { const user = this.state.getUser(); if (user) { document.querySelector('.user-name').textContent = user.username; document.querySelector('.user-email').textContent = user.email; } } // 页面显示方法 async showDashboard() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('dashboard'); // 加载统计数据 const stats = await this.api.get('/dashboard/stats'); this.renderDashboardStats(stats); // 加载最近任务 const tasks = await this.api.get('/tasks?limit=5'); this.renderRecentTasks(tasks); } async showPapers() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('papers'); // 加载论文列表 const papers = await this.api.get('/papers'); this.renderPapersList(papers); } async showNewPaper() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('new-paper'); // 初始化表单 this.initPaperForm(); } async showPaperDetail(paperId) { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('paper-detail'); // 加载论文详情 const paper = await this.api.get(`/papers/${paperId}`); this.renderPaperDetail(paper); } async showTasks() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('tasks'); // 加载任务列表 const tasks = await this.api.get('/tasks'); this.renderTasksList(tasks); } async showTaskDetail(taskId) { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('task-detail'); // 加载任务详情 const task = await this.api.get(`/tasks/${taskId}`); this.renderTaskDetail(task); // 如果任务正在运行,开始轮询状态 if (task.status === 'running') { this.startTaskPolling(taskId); } } async showAnalysis() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('analysis'); // 加载分析列表 const analyses = await this.api.get('/analysis'); this.renderAnalysisList(analyses); } async showWriting() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('writing'); // 加载写作列表 const writings = await this.api.get('/writing'); this.renderWritingList(writings); } async showProfile() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('profile'); // 加载用户信息 const user = await this.api.get('/auth/me'); this.renderProfile(user); } async showLogin() { const content = document.getElementById('content'); content.innerHTML = await this.loadTemplate('login'); // 初始化登录表单 this.initLoginForm(); } // 事件处理方法 async handleAction(action, element) { switch (action) { case 'logout': await this.logout(); break; case 'delete-paper': await this.deletePaper(element.dataset.id); break; case 'delete-task': await this.deleteTask(element.dataset.id); break; case 'cancel-task': await this.cancelTask(element.dataset.id); break; case 'retry-task': await this.retryTask(element.dataset.id); break; case 'export-paper': await this.exportPaper(element.dataset.id, element.dataset.format); break; case 'show-modal': this.showModal(element.dataset.target); break; case 'close-modal': this.closeModal(element.closest('.modal')); break; } } async handleFormSubmit(form) { const formData = new FormData(form); const data = Object.fromEntries(formData.entries()); try { this.showLoading(form); const endpoint = form.dataset.endpoint; const method = form.dataset.method || 'POST'; let result; if (method === 'POST') { result = await this.api.post(endpoint, data); } else if (method === 'PUT') { result = await this.api.put(endpoint, data); } this.showSuccess('操作成功!'); // 根据结果跳转 if (form.dataset.redirect) { this.router.navigate(form.dataset.redirect); } else if (result.id) { this.router.navigate(`/${form.dataset.resource}/${result.id}`); } } catch (error) { this.showError(error.message); } finally { this.hideLoading(form); } } // API调用方法 async createLiteratureSearchTask(query, options = {}) { const data = { title: `文献搜索: ${query}`, task_type: 'literature_search', parameters: { query, max_papers: options.maxPapers || 20, year_range: options.yearRange, venues: options.venues || [] } }; const task = await this.api.post('/tasks', data); this.router.navigate(`/tasks/${task.id}`); return task; } async createAnalysisTask(paperIds, analysisType) { const data = { title: `论文分析: ${analysisType}`, task_type: 'analysis', parameters: { paper_ids: paperIds, analysis_type: analysisType } }; const task = await this.api.post('/tasks', data); this.router.navigate(`/tasks/${task.id}`); return task; } async createWritingTask(paperIds, writingType, outline) { const data = { title: `学术写作: ${writingType}`, task_type: 'writing', parameters: { paper_ids: paperIds, writing_type: writingType, outline: outline } }; const task = await this.api.post('/tasks', data); this.router.navigate(`/tasks/${task.id}`); return task; } // 渲染方法 renderDashboardStats(stats) { const container = document.getElementById('stats-container'); container.innerHTML = `
论文总数
任务总数
分析报告
写作文档
${paper.abstract || '暂无摘要'}
${task.description || '暂无描述'}