mirror of
https://github.com/KingHWJ/WeReadBoard.git
synced 2026-11-03 18:24:58 +08:00
feat: add book detail, notes detail, recommendation page and MySQL schema
新增功能: - 书籍详情弹窗:点击书架书籍展示详细信息(书名、作者、出版社、ISBN、字数、评分、简介、阅读进度),数据实时从微信读书 API 获取并入库 - 笔记详情弹窗:点击有笔记的书展示划线和想法/点评,按章节分组展示,数据实时获取并入库 - 推荐页面:个性化推荐(为你推荐)+ 关键词搜索书籍,数据从微信读书 API 获取并入库 - 修复 Flask 中文 URL 参数编码问题 后端新增 API: - GET /api/book/detail?book_id=xxx — 书籍详情 - GET /api/book/notes?book_id=xxx — 笔记详情(划线+想法) - GET /api/recommend?maxIdx=0 — 个性化推荐 - GET /api/search?keyword=xxx — 搜索书籍 数据库新增表: - book_details, book_progress, book_highlights, book_chapters - book_reviews, recommended_books, search_results MySQL 版本建表语句 schema_mysql.sql: - 18 张表的完整 DDL,含索引、字符集 utf8mb4、字段注释 - 适用于后续从 SQLite 迁移至 MySQL
This commit is contained in:
1 parent
1e9c537b86
commit
ebfa3a1852
6 files changed
+1660
-4
No files matched your search
+248
-4
@@ -24,6 +24,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
loadTopBooks();
|
||||
loadShelf(1);
|
||||
loadNotebooks();
|
||||
loadRecommend(0);
|
||||
initNavScroll();
|
||||
});
|
||||
|
||||
@@ -525,7 +526,7 @@ async function loadNotebooks() {
|
||||
.map((nb) => {
|
||||
const progress = nb.reading_progress || 0;
|
||||
return `
|
||||
<div class="notebook-card">
|
||||
<div class="notebook-card" onclick="openNotes('${nb.book_id}', '${nb.title.replace(/'/g, "\\'")}', '${(nb.author || '').replace(/'/g, "\\'")}', '${nb.cover}')" style="cursor:pointer">
|
||||
<img class="notebook-cover" src="${nb.cover}" alt="${nb.title}"
|
||||
onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2260%22 height=%2280%22%3E%3Crect fill=%22%23e8e0d4%22 width=%2260%22 height=%2280%22/%3E%3C/svg%3E'">
|
||||
<div class="notebook-info">
|
||||
@@ -548,9 +549,252 @@ async function loadNotebooks() {
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 打开书籍 =====
|
||||
// ===== 打开书籍详情弹窗 =====
|
||||
function openBook(bookId) {
|
||||
if (bookId) {
|
||||
window.open(`weread://reading?bId=${bookId}`, "_blank");
|
||||
if (!bookId) return;
|
||||
showModal("bookDetailModal");
|
||||
const body = document.getElementById("bookDetailBody");
|
||||
body.innerHTML = `<div class="loading"><div class="loading-spinner"></div></div>`;
|
||||
|
||||
fetchAPI(`/api/book/detail?book_id=${bookId}`)
|
||||
.then((data) => {
|
||||
const p = data.progress || {};
|
||||
const progress = p.progress || 0;
|
||||
const rating = data.new_rating > 0 ? (data.new_rating / 10).toFixed(1) : null;
|
||||
const tags = [];
|
||||
if (data.category) tags.push(data.category);
|
||||
if (rating) tags.push(`${rating}分`);
|
||||
if (data.new_rating_count) tags.push(`${data.new_rating_count}人评分`);
|
||||
|
||||
body.innerHTML = `
|
||||
<div class="detail-header">
|
||||
<img class="detail-cover" src="${data.cover || ''}" alt="${data.title}"
|
||||
onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%22120%22 height=%22160%22%3E%3Crect fill=%22%23e8e0d4%22 width=%22120%22 height=%22160%22/%3E%3C/svg%3E'">
|
||||
<div class="detail-meta">
|
||||
<div class="detail-title">${data.title}</div>
|
||||
<div class="detail-author">${data.author || ''}${data.translator ? ' / 译: ' + data.translator : ''}</div>
|
||||
<div class="detail-tags">${tags.map(t => `<span class="detail-tag">${t}</span>`).join('')}</div>
|
||||
${progress.is_start_reading ? `
|
||||
<div class="detail-progress-info">
|
||||
<div style="font-size:13px;color:var(--text-secondary)">
|
||||
阅读进度: <strong>${progress.progress}%</strong>
|
||||
${progress.record_reading_time_str ? ' · 累计 ' + progress.record_reading_time_str : ''}
|
||||
${progress.finish_time ? ' · 读完于 ' + progress.finish_time : ''}
|
||||
</div>
|
||||
<div class="detail-progress-bar">
|
||||
<div class="detail-progress-fill" style="width:${progress}%"></div>
|
||||
</div>
|
||||
</div>` : '<div style="font-size:13px;color:var(--text-light);margin-top:8px">尚未开始阅读</div>'}
|
||||
</div>
|
||||
</div>
|
||||
${data.intro ? `<div class="detail-intro">${data.intro}</div>` : ''}
|
||||
<div class="detail-info-grid">
|
||||
${data.publisher ? `<div class="detail-info-item"><strong>出版社:</strong> ${data.publisher}</div>` : ''}
|
||||
${data.publish_time ? `<div class="detail-info-item"><strong>出版时间:</strong> ${data.publish_time}</div>` : ''}
|
||||
${data.isbn ? `<div class="detail-info-item"><strong>ISBN:</strong> ${data.isbn}</div>` : ''}
|
||||
${data.word_count ? `<div class="detail-info-item"><strong>字数:</strong> ${(data.word_count / 10000).toFixed(1)}万字</div>` : ''}
|
||||
</div>
|
||||
<div class="detail-actions">
|
||||
<button class="btn-primary" onclick="window.open('weread://reading?bId=${bookId}','_blank')">在微信读书中打开</button>
|
||||
</div>
|
||||
`;
|
||||
})
|
||||
.catch((e) => {
|
||||
body.innerHTML = `<div class="empty-state">加载失败: ${e.message}</div>`;
|
||||
});
|
||||
}
|
||||
|
||||
// ===== 打开笔记详情弹窗 =====
|
||||
function openNotes(bookId, title, author, cover) {
|
||||
if (!bookId) return;
|
||||
showModal("notesDetailModal");
|
||||
const body = document.getElementById("notesDetailBody");
|
||||
body.innerHTML = `
|
||||
<div class="notes-header">
|
||||
<img class="notes-cover" src="${cover || ''}" alt="${title}"
|
||||
onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2260%22 height=%2280%22%3E%3Crect fill=%22%23e8e0d4%22 width=%2260%22 height=%2280%22/%3E%3C/svg%3E'">
|
||||
<div>
|
||||
<div class="notes-title">${title}</div>
|
||||
<div class="notes-author">${author || ''}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="loading"><div class="loading-spinner"></div></div>
|
||||
`;
|
||||
|
||||
fetchAPI(`/api/book/notes?book_id=${bookId}`)
|
||||
.then((data) => {
|
||||
let html = `
|
||||
<div class="notes-header">
|
||||
<img class="notes-cover" src="${cover || ''}" alt="${title}"
|
||||
onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2260%22 height=%2280%22%3E%3Crect fill=%22%23e8e0d4%22 width=%2260%22 height=%2280%22/%3E%3C/svg%3E'">
|
||||
<div>
|
||||
<div class="notes-title">${title}</div>
|
||||
<div class="notes-author">${author || ''}</div>
|
||||
<div class="notes-counts">
|
||||
<span>划线 <strong>${data.highlight_count}</strong></span>
|
||||
<span>想法/点评 <strong>${data.review_count}</strong></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// 划线区
|
||||
if (data.highlight_count > 0) {
|
||||
html += `<div class="notes-section-title">📌 划线 (${data.highlight_count})</div>`;
|
||||
const grouped = data.grouped_highlights;
|
||||
for (const [chTitle, items] of Object.entries(grouped)) {
|
||||
html += `<div class="chapter-group">`;
|
||||
html += `<div class="chapter-title">${chTitle}</div>`;
|
||||
items.forEach((h) => {
|
||||
html += `
|
||||
<div class="highlight-item">
|
||||
${escapeHtml(h.mark_text)}
|
||||
<div class="highlight-date">${h.create_time || ''}</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
html += `</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
// 想法/点评区
|
||||
if (data.review_count > 0) {
|
||||
html += `<div class="notes-section-title">💭 想法/点评 (${data.review_count})</div>`;
|
||||
data.reviews.forEach((r) => {
|
||||
const stars = r.star > 0 ? `<span class="review-star">${"★".repeat(r.star)}${"☆".repeat(5 - r.star)}</span>` : "";
|
||||
html += `
|
||||
<div class="review-item">
|
||||
${escapeHtml(r.content)}
|
||||
<div class="review-meta">
|
||||
${stars}
|
||||
${r.chapter_name ? ' · ' + r.chapter_name : ''}
|
||||
${r.is_finish ? ' · 书评' : ''}
|
||||
· ${r.create_time || ''}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
}
|
||||
|
||||
if (data.highlight_count === 0 && data.review_count === 0) {
|
||||
html += `<div class="empty-state">暂无笔记数据</div>`;
|
||||
}
|
||||
|
||||
body.innerHTML = html;
|
||||
})
|
||||
.catch((e) => {
|
||||
body.innerHTML = `<div class="empty-state">加载失败: ${e.message}</div>`;
|
||||
});
|
||||
}
|
||||
|
||||
// ===== 弹窗控制 =====
|
||||
function showModal(id) {
|
||||
document.getElementById(id).classList.add("active");
|
||||
document.body.style.overflow = "hidden";
|
||||
}
|
||||
|
||||
function closeModal(id) {
|
||||
document.getElementById(id).classList.remove("active");
|
||||
document.body.style.overflow = "";
|
||||
}
|
||||
|
||||
// ESC 关闭弹窗
|
||||
document.addEventListener("keydown", (e) => {
|
||||
if (e.key === "Escape") {
|
||||
closeModal("bookDetailModal");
|
||||
closeModal("notesDetailModal");
|
||||
}
|
||||
});
|
||||
|
||||
// ===== HTML 转义 =====
|
||||
function escapeHtml(text) {
|
||||
if (!text) return "";
|
||||
const div = document.createElement("div");
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
// ===== 推荐发现 =====
|
||||
let recommendMaxIdx = 0;
|
||||
|
||||
function switchDiscoverTab(tab) {
|
||||
const recPanel = document.getElementById("recommendPanel");
|
||||
const searchPanel = document.getElementById("searchPanel");
|
||||
const tabRec = document.getElementById("tabRecommend");
|
||||
const tabSearch = document.getElementById("tabSearch");
|
||||
|
||||
if (tab === "recommend") {
|
||||
recPanel.style.display = "";
|
||||
searchPanel.style.display = "none";
|
||||
tabRec.classList.add("active");
|
||||
tabSearch.classList.remove("active");
|
||||
} else {
|
||||
recPanel.style.display = "none";
|
||||
searchPanel.style.display = "";
|
||||
tabRec.classList.remove("active");
|
||||
tabSearch.classList.add("active");
|
||||
}
|
||||
}
|
||||
|
||||
async function loadRecommend(maxIdx) {
|
||||
const grid = document.getElementById("recommendGrid");
|
||||
grid.innerHTML = `<div class="loading"><div class="loading-spinner"></div></div>`;
|
||||
|
||||
try {
|
||||
const data = await fetchAPI(`/api/recommend?maxIdx=${maxIdx}`);
|
||||
recommendMaxIdx = data.last_search_idx || 0;
|
||||
renderDiscoverGrid(grid, data.books, true);
|
||||
} catch (e) {
|
||||
grid.innerHTML = `<div class="empty-state">加载推荐失败: ${e.message}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMoreRecommend() {
|
||||
loadRecommend(recommendMaxIdx);
|
||||
}
|
||||
|
||||
async function doSearch() {
|
||||
const keyword = document.getElementById("searchInput").value.trim();
|
||||
if (!keyword) return;
|
||||
|
||||
const grid = document.getElementById("searchGrid");
|
||||
grid.innerHTML = `<div class="loading"><div class="loading-spinner"></div></div>`;
|
||||
|
||||
try {
|
||||
const data = await fetchAPI(`/api/search?keyword=${encodeURIComponent(keyword)}`);
|
||||
if (data.books.length === 0) {
|
||||
grid.innerHTML = `<div class="empty-state">未找到相关书籍</div>`;
|
||||
return;
|
||||
}
|
||||
renderDiscoverGrid(grid, data.books, false);
|
||||
} catch (e) {
|
||||
grid.innerHTML = `<div class="empty-state">搜索失败: ${e.message}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function renderDiscoverGrid(grid, books, showReason) {
|
||||
grid.innerHTML = books
|
||||
.map((book) => {
|
||||
const rating = book.new_rating > 0 ? (book.new_rating / 10).toFixed(1) : null;
|
||||
const ratingLabel = book.rating_title ? `<span class="discover-rating-label">${book.rating_title}</span>` : "";
|
||||
const ratingHtml = rating ? `<span class="discover-rating">${rating}</span>` : "";
|
||||
const readingHtml = book.reading_count ? `<span class="discover-reading-count">${book.reading_count}人在读</span>` : "";
|
||||
const reasonHtml = (showReason && book.reason) ? `<div class="discover-reason">${book.reason}</div>` : "";
|
||||
const categoryHtml = book.category ? `<div class="discover-category">${book.category}</div>` : "";
|
||||
|
||||
return `
|
||||
<div class="discover-card" onclick="openBook('${book.book_id}')">
|
||||
<img class="discover-cover" src="${book.cover || ''}" alt="${book.title}"
|
||||
onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%22200%22 height=%22180%22%3E%3Crect fill=%22%23e8e0d4%22 width=%22200%22 height=%22180%22/%3E%3Ctext x=%22100%22 y=%2290%22 text-anchor=%22middle%22 fill=%22%239b8e80%22 font-size=%2214%22%3E无封面%3C/text%3E%3C/svg%3E'">
|
||||
<div class="discover-info">
|
||||
<div class="discover-title">${book.title}</div>
|
||||
<div class="discover-author">${book.author || ''}</div>
|
||||
<div class="discover-meta">${ratingHtml}${ratingLabel}${readingHtml}</div>
|
||||
${categoryHtml}
|
||||
${reasonHtml}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
})
|
||||
.join("");
|
||||
}
|
||||
Reference in new issue
Block a user