From 854d6caaa74bdfdf142b4f2939f836816690eac1 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:27:01 +0800 Subject: [PATCH] Update feed.js --- js/feed.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/js/feed.js b/js/feed.js index 7407952..71a72b2 100644 --- a/js/feed.js +++ b/js/feed.js @@ -3,16 +3,31 @@ // ============================================================ let currentSort = 'latest'; +let currentPage = 1; +let totalPages = 1; +const PAGE_SIZE = 20; function renderFeed() { const container = document.getElementById('postList'); container.innerHTML = '
加载中...
'; - API.getPosts(currentSort).then(posts => { - if (!posts || posts.length === 0) { + API.getPosts(currentSort, currentPage, PAGE_SIZE).then(result => { + const posts = result.data || []; + const pagination = result.pagination || { total: 0, totalPages: 1, page: 1 }; + totalPages = pagination.totalPages || 1; + currentPage = pagination.page || 1; + + if (posts.length === 0 && currentPage === 1) { container.innerHTML = '
✨ 还没有帖子,快来发布第一条吧!
'; return; } + + if (posts.length === 0 && currentPage > 1) { + currentPage = 1; + renderFeed(); + return; + } + let html = ''; posts.forEach(p => { const username = p.username || '匿名用户'; @@ -58,6 +73,40 @@ function renderFeed() { }); container.innerHTML = html; + // 分页控件 + if (totalPages > 1) { + let paginationHtml = ` +
+ + + 第 ${currentPage} / ${totalPages} 页(共 ${pagination.total} 帖) + + +
+ `; + container.innerHTML += paginationHtml; + + container.querySelectorAll('.page-btn:not([disabled])').forEach(btn => { + btn.addEventListener('click', function() { + const page = parseInt(this.dataset.page); + if (page >= 1 && page <= totalPages) { + currentPage = page; + const url = new URL(window.location); + url.searchParams.set('postpage', page); + window.history.pushState({}, '', url); + renderFeed(); + } + }); + }); + } + + // 事件绑定 container.querySelectorAll('.post-card').forEach(card => { card.addEventListener('click', function(e) { if (e.target.closest('button')) return; @@ -121,6 +170,10 @@ document.querySelectorAll('.tab').forEach(tab => { document.querySelectorAll('.tab').forEach(t => t.classList.remove('active')); this.classList.add('active'); currentSort = this.dataset.sort; + currentPage = 1; + const url = new URL(window.location); + url.searchParams.delete('postpage'); + window.history.pushState({}, '', url); if (currentPage === 'feed') { renderFeed(); }