pref: 日志管理优化
This commit is contained in:
@@ -24,6 +24,7 @@ import EventScrollList from './DynamicNewsCard/EventScrollList';
|
|||||||
import DynamicNewsDetailPanel from './DynamicNewsDetail';
|
import DynamicNewsDetailPanel from './DynamicNewsDetail';
|
||||||
import UnifiedSearchBox from './UnifiedSearchBox';
|
import UnifiedSearchBox from './UnifiedSearchBox';
|
||||||
import { fetchDynamicNews, toggleEventFollow, selectEventFollowStatus } from '../../../store/slices/communityDataSlice';
|
import { fetchDynamicNews, toggleEventFollow, selectEventFollowStatus } from '../../../store/slices/communityDataSlice';
|
||||||
|
import { logger } from '../../../utils/logger';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页逻辑自定义 Hook
|
* 分页逻辑自定义 Hook
|
||||||
@@ -70,7 +71,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
const expectedCount = Math.min(pageSize, total - targetPageStartIndex);
|
const expectedCount = Math.min(pageSize, total - targetPageStartIndex);
|
||||||
const isTargetPageCached = validTargetData.length >= expectedCount;
|
const isTargetPageCached = validTargetData.length >= expectedCount;
|
||||||
|
|
||||||
console.log('[checkTargetPageCache] 目标页缓存检查', {
|
logger.debug('DynamicNewsCard', '目标页缓存检查', {
|
||||||
targetPage,
|
targetPage,
|
||||||
targetPageStartIndex,
|
targetPageStartIndex,
|
||||||
targetPageEndIndex,
|
targetPageEndIndex,
|
||||||
@@ -114,7 +115,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
preloadRange = [targetPage];
|
preloadRange = [targetPage];
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[calculatePreloadRange] 计算预加载范围', {
|
logger.debug('DynamicNewsCard', '计算预加载范围', {
|
||||||
targetPage,
|
targetPage,
|
||||||
fromPage,
|
fromPage,
|
||||||
isSequentialNavigation,
|
isSequentialNavigation,
|
||||||
@@ -136,7 +137,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
|
|
||||||
// 如果该页超出数组范围,说明未缓存
|
// 如果该页超出数组范围,说明未缓存
|
||||||
if (pageEndIndex > allCachedEvents.length) {
|
if (pageEndIndex > allCachedEvents.length) {
|
||||||
console.log(`[findMissingPages] 页面${page}超出数组范围`, {
|
logger.debug('DynamicNewsCard', `页面${page}超出数组范围`, {
|
||||||
pageStartIndex,
|
pageStartIndex,
|
||||||
pageEndIndex,
|
pageEndIndex,
|
||||||
allCachedEventsLength: allCachedEvents.length
|
allCachedEventsLength: allCachedEvents.length
|
||||||
@@ -150,7 +151,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
const expectedCount = Math.min(pageSize, total - pageStartIndex);
|
const expectedCount = Math.min(pageSize, total - pageStartIndex);
|
||||||
const hasNullOrIncomplete = validData.length < expectedCount;
|
const hasNullOrIncomplete = validData.length < expectedCount;
|
||||||
|
|
||||||
console.log(`[findMissingPages] 页面${page}检查`, {
|
logger.debug('DynamicNewsCard', `页面${page}数据检查`, {
|
||||||
pageStartIndex,
|
pageStartIndex,
|
||||||
pageEndIndex,
|
pageEndIndex,
|
||||||
pageDataLength: pageData.length,
|
pageDataLength: pageData.length,
|
||||||
@@ -162,7 +163,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
return hasNullOrIncomplete;
|
return hasNullOrIncomplete;
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('[findMissingPages] 缺失页面检测完成', {
|
logger.debug('DynamicNewsCard', '缺失页面检测完成', {
|
||||||
preloadRange,
|
preloadRange,
|
||||||
missingPages,
|
missingPages,
|
||||||
missingPagesCount: missingPages.length
|
missingPagesCount: missingPages.length
|
||||||
@@ -185,7 +186,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`[loadPages] 开始加载`, {
|
logger.debug('DynamicNewsCard', '开始加载页面数据', {
|
||||||
missingPages,
|
missingPages,
|
||||||
targetPage,
|
targetPage,
|
||||||
silentMode,
|
silentMode,
|
||||||
@@ -194,7 +195,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
|
|
||||||
// 拆分为单页请求,避免 per_page 动态值导致后端返回空数据
|
// 拆分为单页请求,避免 per_page 动态值导致后端返回空数据
|
||||||
for (const page of missingPages) {
|
for (const page of missingPages) {
|
||||||
console.log(`[loadPages] 开始加载第 ${page} 页`);
|
logger.debug('DynamicNewsCard', `开始加载第 ${page} 页`);
|
||||||
|
|
||||||
await dispatch(fetchDynamicNews({
|
await dispatch(fetchDynamicNews({
|
||||||
page: page,
|
page: page,
|
||||||
@@ -203,17 +204,21 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
clearCache: false
|
clearCache: false
|
||||||
})).unwrap();
|
})).unwrap();
|
||||||
|
|
||||||
console.log(`[loadPages] 第 ${page} 页加载完成`);
|
logger.debug('DynamicNewsCard', `第 ${page} 页加载完成`);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[loadPages] 所有页面加载完成', {
|
logger.debug('DynamicNewsCard', '所有页面加载完成', {
|
||||||
missingPages,
|
missingPages,
|
||||||
silentMode
|
silentMode
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[loadPages] 加载失败', error);
|
logger.error('DynamicNewsCard', 'loadPages', error, {
|
||||||
|
targetPage,
|
||||||
|
silentMode,
|
||||||
|
missingPages
|
||||||
|
});
|
||||||
|
|
||||||
if (!silentMode) {
|
if (!silentMode) {
|
||||||
// 非静默模式下显示错误提示
|
// 非静默模式下显示错误提示
|
||||||
@@ -239,7 +244,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
// 翻页处理(智能预加载)- 使用子函数重构
|
// 翻页处理(智能预加载)- 使用子函数重构
|
||||||
const handlePageChange = useCallback(async (newPage) => {
|
const handlePageChange = useCallback(async (newPage) => {
|
||||||
// 🔍 诊断日志 - 记录翻页开始状态
|
// 🔍 诊断日志 - 记录翻页开始状态
|
||||||
console.log('[handlePageChange] 开始翻页', {
|
logger.debug('DynamicNewsCard', '开始翻页', {
|
||||||
currentPage,
|
currentPage,
|
||||||
newPage,
|
newPage,
|
||||||
pageSize,
|
pageSize,
|
||||||
@@ -262,7 +267,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
// 步骤4: 根据情况加载数据
|
// 步骤4: 根据情况加载数据
|
||||||
if (isTargetPageCached && missingPages.length > 0 && hasMore) {
|
if (isTargetPageCached && missingPages.length > 0 && hasMore) {
|
||||||
// 场景A: 目标页已缓存,立即切换,后台静默预加载其他页
|
// 场景A: 目标页已缓存,立即切换,后台静默预加载其他页
|
||||||
console.log('[handlePageChange] 目标页已缓存,立即切换 + 后台预加载', {
|
logger.debug('DynamicNewsCard', '目标页已缓存,立即切换 + 后台预加载', {
|
||||||
currentPage,
|
currentPage,
|
||||||
newPage,
|
newPage,
|
||||||
缺失页面: missingPages
|
缺失页面: missingPages
|
||||||
@@ -272,7 +277,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
await loadPages(missingPages, newPage, true); // 静默模式
|
await loadPages(missingPages, newPage, true); // 静默模式
|
||||||
} else if (missingPages.length > 0 && hasMore) {
|
} else if (missingPages.length > 0 && hasMore) {
|
||||||
// 场景B: 目标页未缓存,显示 loading 并等待加载完成
|
// 场景B: 目标页未缓存,显示 loading 并等待加载完成
|
||||||
console.log('[handlePageChange] 目标页未缓存,显示 loading', {
|
logger.debug('DynamicNewsCard', '目标页未缓存,显示 loading', {
|
||||||
currentPage,
|
currentPage,
|
||||||
newPage,
|
newPage,
|
||||||
缺失页面: missingPages
|
缺失页面: missingPages
|
||||||
@@ -284,7 +289,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
}
|
}
|
||||||
} else if (missingPages.length === 0) {
|
} else if (missingPages.length === 0) {
|
||||||
// 场景C: 所有页面均已缓存,直接切换
|
// 场景C: 所有页面均已缓存,直接切换
|
||||||
console.log('[handlePageChange] 无需加载,直接切换', {
|
logger.debug('DynamicNewsCard', '无需加载,直接切换', {
|
||||||
currentPage,
|
currentPage,
|
||||||
newPage,
|
newPage,
|
||||||
reason: '所有页面均已缓存'
|
reason: '所有页面均已缓存'
|
||||||
@@ -293,7 +298,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
|
|||||||
setCurrentPage(newPage);
|
setCurrentPage(newPage);
|
||||||
} else {
|
} else {
|
||||||
// 场景D: 意外分支(有缺失页面但 hasMore=false)
|
// 场景D: 意外分支(有缺失页面但 hasMore=false)
|
||||||
console.warn('[handlePageChange] 意外分支:有缺失页面但无法加载', {
|
logger.warn('DynamicNewsCard', '意外分支:有缺失页面但无法加载', {
|
||||||
missingPages,
|
missingPages,
|
||||||
hasMore,
|
hasMore,
|
||||||
currentPage,
|
currentPage,
|
||||||
|
|||||||
Reference in New Issue
Block a user