feat: 新增帖子搜索功能
支持按标题与正文搜索帖子,入口在首页排序标签与发布按钮之间。 实现方式选择 ILIKE 子串匹配而非 PostgreSQL 全文检索:后者依赖分词, 中文分词需额外安装 zhparser 等扩展,多数部署环境没有;ILIKE 无需任何 扩展即可正确匹配中文子串,英文也天然大小写不敏感。 后端(GET /api/posts/search) - 关键词长度限制 100 字,超长或为空返回 400 - 转义 % 与 _:它们是 LIKE 的通配符,若不过滤,搜索「%」会匹配全部 帖子 - 复用 parsePagination,响应结构与 /api/posts 一致 - 路由必须声明在 /api/posts/:id 之前,否则 search 会被当作 id 匹配 - 新增迁移 005:创建 pg_trgm 扩展与 trigram 索引。 实测索引效果分两种情况——英文关键词(postgresql)走 Bitmap Index Scan,0.2ms,比全表扫描快约 47 倍;中文因 pg_trgm 难以切出有效 trigram,仍走全表扫描,3000 条数据约 7~9ms。即索引对英文有效、 对中文无效,考虑到许多论坛中英混排,予以保留。 创建扩展需要权限,部分托管数据库不开放,故失败时仅提示,不影响 搜索功能本身。 前端 - 首页新增搜索框,含清除按钮;输入防抖 350ms 自动搜索,回车立即搜索, Esc 清空 - 关键词写入 URL(?q=),刷新与分享可保持搜索状态;初始化时先于 renderFeed 设定关键词,避免首屏先按普通列表加载一次 - 搜索时输入框下方显示细进度条,而非整块骨架屏——搜索通常很快, 画骨架反而闪得厉害 - 无结果时给出明确提示与改进建议 - 移动端搜索框独占一行,避免与标签、按钮挤压 - 补充 6 条 i18n 文案;空结果提示中的关键词单独放在 <strong> 内, 使固定文案能作为独立文本节点被词典匹配 实测(Puppeteer + 接口直测): - 中文「论坛」「分词」、英文 resend/RESEND 均正确命中 - 空关键词与超长关键词正确返回 400 - 搜索 % 与 _ 返回 0 条,确认通配符已转义 - 输入「分词」后列表由 5 条筛为 1 条,URL 同步为 ?q=分词 - 清除按钮恢复全部列表并移除 URL 参数 - 通过 ?q=论坛 直接访问时,输入框自动填入且结果为 2 条 完整测试套件 25/25 通过。
This commit is contained in:
1 parent
9844988cf2
commit
fdf90f4b1d
9 files changed
+397
-3
No files matched your search
+106
-3
@@ -6,6 +6,9 @@ let currentSort = 'latest';
|
||||
let totalPages = 1;
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
// 当前搜索关键词。为空表示处于普通列表模式。
|
||||
let currentSearch = '';
|
||||
|
||||
// 翻页后滚回列表顶部。分页控件在列表末尾,不回到顶部的话用户看到的
|
||||
// 仍是新一页的底部。
|
||||
// 尊重系统的「减少动画」设置:开启时直接跳转,不做平滑滚动。
|
||||
@@ -20,13 +23,20 @@ function scrollFeedToTop() {
|
||||
|
||||
function renderFeed() {
|
||||
const container = document.getElementById('postList');
|
||||
const keyword = currentSearch;
|
||||
|
||||
// 骨架屏只在首次加载时显示。翻页时列表已有内容,再画一遍骨架会
|
||||
// 造成明显的闪动,反而比直接留白更糟。
|
||||
const isFirstLoad = container.dataset.loaded !== '1';
|
||||
container.innerHTML = isFirstLoad
|
||||
? Skeleton.postList(3)
|
||||
: '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
||||
API.getPosts(currentSort, currentPageNum, PAGE_SIZE).then(result => {
|
||||
|
||||
const request = keyword
|
||||
? API.searchPosts(keyword, currentPageNum, PAGE_SIZE)
|
||||
: API.getPosts(currentSort, currentPageNum, PAGE_SIZE);
|
||||
|
||||
request.then(result => {
|
||||
container.dataset.loaded = '1';
|
||||
const posts = result.data || [];
|
||||
const pagination = result.pagination || { total: 0, totalPages: 1, page: 1 };
|
||||
@@ -34,8 +44,14 @@ function renderFeed() {
|
||||
currentPageNum = pagination.page || 1;
|
||||
|
||||
if (posts.length === 0 && currentPageNum === 1) {
|
||||
container.innerHTML =
|
||||
'<div style="text-align:center;color:#94a3b8;padding:60px 0;">✨ 还没有帖子,快来发布第一条吧!</div>';
|
||||
// 空结果提示。关键词单独放在 <strong> 里,让固定文案能作为独立
|
||||
// 文本节点被 i18n 匹配到——整句含动态关键词时匹配不上词典。
|
||||
container.innerHTML = keyword
|
||||
? '<div class="search-empty">'
|
||||
+ '<span>没有找到相关帖子</span><br>'
|
||||
+ '<strong>「' + escapeHTML(keyword) + '」</strong><br>'
|
||||
+ '<span style="font-size:13px;">试试其他关键词,或检查是否有错别字</span></div>'
|
||||
: '<div style="text-align:center;color:#94a3b8;padding:60px 0;">✨ 还没有帖子,快来发布第一条吧!</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -250,3 +266,90 @@ document.querySelectorAll('.tab').forEach(tab => {
|
||||
renderFeed();
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================
|
||||
// 🔍 搜索
|
||||
// ============================================================
|
||||
// 输入时防抖 350ms 自动搜索,回车立即搜索。
|
||||
// 关键词写入 URL(?q=),刷新与分享可保持搜索状态。
|
||||
|
||||
(function initSearch() {
|
||||
const input = document.getElementById('postSearchInput');
|
||||
const clearBtn = document.getElementById('postSearchClear');
|
||||
const wrap = input ? input.closest('.feed-search') : null;
|
||||
if (!input) return;
|
||||
|
||||
let debounceTimer = null;
|
||||
|
||||
function syncClearButton() {
|
||||
if (clearBtn) clearBtn.style.display = input.value.trim() ? 'flex' : 'none';
|
||||
}
|
||||
|
||||
function runSearch(keyword, { updateUrl = true } = {}) {
|
||||
const next = (keyword || '').trim();
|
||||
// 与当前状态相同则不必重新请求
|
||||
if (next === currentSearch && currentPageNum === 1) return;
|
||||
|
||||
currentSearch = next;
|
||||
currentPageNum = 1;
|
||||
|
||||
if (updateUrl) {
|
||||
const url = new URL(window.location);
|
||||
if (next) url.searchParams.set('q', next);
|
||||
else url.searchParams.delete('q');
|
||||
url.searchParams.delete('postpage');
|
||||
window.history.pushState({}, '', url);
|
||||
}
|
||||
|
||||
if (wrap) wrap.classList.add('is-searching');
|
||||
renderFeed();
|
||||
// renderFeed 内部是异步的,这里在下一轮事件循环后移除进度条即可
|
||||
// (列表本身会先显示「加载中」,不会长时间空白)
|
||||
window.setTimeout(() => {
|
||||
if (wrap) wrap.classList.remove('is-searching');
|
||||
}, 600);
|
||||
}
|
||||
|
||||
input.addEventListener('input', () => {
|
||||
syncClearButton();
|
||||
if (debounceTimer) clearTimeout(debounceTimer);
|
||||
debounceTimer = setTimeout(() => runSearch(input.value), 350);
|
||||
});
|
||||
|
||||
// 回车立即搜索,跳过防抖
|
||||
input.addEventListener('keydown', e => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
if (debounceTimer) clearTimeout(debounceTimer);
|
||||
runSearch(input.value);
|
||||
} else if (e.key === 'Escape') {
|
||||
e.preventDefault();
|
||||
input.value = '';
|
||||
syncClearButton();
|
||||
if (debounceTimer) clearTimeout(debounceTimer);
|
||||
runSearch('');
|
||||
}
|
||||
});
|
||||
|
||||
if (clearBtn) {
|
||||
clearBtn.addEventListener('click', () => {
|
||||
input.value = '';
|
||||
syncClearButton();
|
||||
if (debounceTimer) clearTimeout(debounceTimer);
|
||||
runSearch('');
|
||||
input.focus();
|
||||
});
|
||||
}
|
||||
|
||||
// 暴露给外部(初始化时按 URL 恢复搜索状态)
|
||||
window.__feedSearch = {
|
||||
setKeyword(keyword) {
|
||||
input.value = keyword || '';
|
||||
currentSearch = (keyword || '').trim();
|
||||
syncClearButton();
|
||||
},
|
||||
run: runSearch,
|
||||
};
|
||||
|
||||
syncClearButton();
|
||||
})();
|
||||
Reference in new issue
Block a user