From 25a6ff164b337f94be1f114a66daa2b4f79fe0ad Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Wed, 5 Nov 2025 19:28:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BF=BB=E9=A1=B5bugfix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DynamicNewsCard/hooks/usePagination.js | 42 +++++-------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/src/views/Community/components/DynamicNewsCard/hooks/usePagination.js b/src/views/Community/components/DynamicNewsCard/hooks/usePagination.js index 8b916995..98d9f51c 100644 --- a/src/views/Community/components/DynamicNewsCard/hooks/usePagination.js +++ b/src/views/Community/components/DynamicNewsCard/hooks/usePagination.js @@ -193,22 +193,7 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t cachedCount }); - // 特殊处理:返回第一页 - 清空缓存重新加载 - if (newPage === 1) { - logger.debug('DynamicNewsCard', '返回第一页,清空缓存重新加载'); - setCurrentPage(1); - dispatch(fetchDynamicNews({ - mode: mode, // 传递 mode 参数 - per_page: pageSize, - pageSize: pageSize, - clearCache: true, // 清空缓存 - ...filters, // 先展开筛选条件 - page: 1, // 然后覆盖 page 参数 - })); - return; - } - - // 检查目标页缓存状态 + // 检查目标页缓存状态(统一处理,包括第一页) const { isTargetPageCached, targetPageInfo } = checkTargetPageCache(newPage); console.log(`%c🟡 [缓存检查] 目标页${newPage}缓存状态`, 'color: #EAB308; font-weight: bold;'); @@ -275,18 +260,23 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t // 加载下一页(用于无限滚动) const loadNextPage = useCallback(async () => { - if (currentPage >= totalPages || loadingPage !== null) { + // 修复:使用 hasMore 判断而不是 currentPage >= totalPages + // 原因:去重后 cachedCount 可能小于 total,但 currentPage 已达到 totalPages + if (!hasMore || loadingPage !== null) { logger.debug('DynamicNewsCard', '无法加载下一页', { currentPage, totalPages, + hasMore, + cachedCount, + total, loadingPage, - reason: currentPage >= totalPages ? '已是最后一页' : '正在加载中' + reason: !hasMore ? '已加载全部数据 (cachedCount >= total)' : '正在加载中' }); return Promise.resolve(false); // 没有更多数据或正在加载 } const nextPage = currentPage + 1; - logger.debug('DynamicNewsCard', '懒加载:加载下一页', { currentPage, nextPage }); + logger.debug('DynamicNewsCard', '懒加载:加载下一页', { currentPage, nextPage, hasMore, cachedCount, total }); try { await handlePageChange(nextPage); @@ -295,7 +285,7 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t logger.error('DynamicNewsCard', '懒加载失败', error, { nextPage }); return false; } - }, [currentPage, totalPages, loadingPage, handlePageChange]); + }, [currentPage, totalPages, hasMore, cachedCount, total, loadingPage, handlePageChange]); // 加载上一页(用于双向无限滚动) const loadPrevPage = useCallback(async () => { @@ -326,17 +316,7 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t setMode(newMode); setCurrentPage(PAGINATION_CONFIG.INITIAL_PAGE); - - const newPageSize = (() => { - switch (newMode) { - case DISPLAY_MODES.FOUR_ROW: - return PAGINATION_CONFIG.FOUR_ROW_PAGE_SIZE; - case DISPLAY_MODES.VERTICAL: - return PAGINATION_CONFIG.VERTICAL_PAGE_SIZE; - default: - return PAGINATION_CONFIG.VERTICAL_PAGE_SIZE; - } - })(); + // pageSize 会根据 mode 自动重新计算(第35-44行) }, [mode]); return {