/** * 书籍目录页面 - 主要逻辑 */ class BookCatalogApp { constructor() { this.books = []; this.filteredBooks = []; this.statistics = null; this.init(); } async init() { try { await this.loadStatistics(); await this.loadBooks(); this.setupEventListeners(); } catch (error) { console.error('应用初始化失败:', error); this.showToast('应用初始化失败,请刷新页面重试', 'error'); } } setupEventListeners() { // 搜索功能 const searchInput = document.getElementById('searchInput'); const searchBtn = document.getElementById('searchBtn'); searchBtn.addEventListener('click', () => this.performSearch()); searchInput.addEventListener('input', () => this.performSearch()); searchInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') this.performSearch(); }); } async loadStatistics() { try { const response = await fetch('/api/v2/statistics'); if (!response.ok) { throw new Error('获取统计信息失败'); } const result = await response.json(); if (!result.success) { throw new Error(result.error || '获取统计信息失败'); } this.statistics = result.statistics; this.renderStatistics(); } catch (error) { console.error('加载统计信息失败:', error); // 统计信息加载失败不影响主要功能,只记录错误 } } renderStatistics() { if (!this.statistics) return; document.getElementById('totalBooks').textContent = this.statistics.total_books || 0; document.getElementById('totalPages').textContent = this.statistics.total_pages || 0; document.getElementById('totalPieces').textContent = this.statistics.total_pieces || 0; document.getElementById('totalCatalogs').textContent = this.statistics.total_catalogs || 0; document.getElementById('statistics').style.display = 'grid'; } async loadBooks() { const loading = document.getElementById('loading'); const booksGrid = document.getElementById('booksGrid'); const emptyState = document.getElementById('emptyState'); loading.style.display = 'block'; booksGrid.style.display = 'none'; emptyState.style.display = 'none'; try { const response = await fetch('/api/v2/books'); if (!response.ok) { throw new Error('获取书籍列表失败'); } const result = await response.json(); if (!result.success) { throw new Error(result.error || '获取书籍列表失败'); } this.books = result.books || []; this.filteredBooks = [...this.books]; console.log(`加载了 ${this.books.length} 本书籍`); if (this.books.length === 0) { loading.style.display = 'none'; emptyState.style.display = 'block'; } else { this.renderBooks(); } } catch (error) { console.error('加载书籍列表失败:', error); loading.style.display = 'none'; emptyState.style.display = 'block'; this.showToast('加载书籍列表失败: ' + error.message, 'error'); } } renderBooks() { const loading = document.getElementById('loading'); const booksGrid = document.getElementById('booksGrid'); const emptyState = document.getElementById('emptyState'); loading.style.display = 'none'; if (this.filteredBooks.length === 0) { booksGrid.style.display = 'none'; emptyState.innerHTML = `