diff --git a/src/store/slices/communityDataSlice.js b/src/store/slices/communityDataSlice.js index 536cd18d..04461618 100644 --- a/src/store/slices/communityDataSlice.js +++ b/src/store/slices/communityDataSlice.js @@ -164,6 +164,11 @@ export const fetchHotEvents = createAsyncThunk( * @param {number} params.per_page - 每页数量 * @param {boolean} params.clearCache - 是否清空缓存(默认 false) * @param {boolean} params.prependMode - 是否追加到头部(用于定时刷新,默认 false) + * @param {string} params.sort - 排序方式(new/hot) + * @param {string} params.importance - 重要性筛选(all/1/2/3/4/5) + * @param {string} params.q - 搜索关键词 + * @param {string} params.date_range - 时间范围 + * @param {string} params.industry_code - 行业代码 */ export const fetchDynamicNews = createAsyncThunk( 'communityData/fetchDynamicNews', @@ -172,20 +177,34 @@ export const fetchDynamicNews = createAsyncThunk( per_page = 5, pageSize = 5, // 每页实际显示的数据量(用于计算索引) clearCache = false, - prependMode = false + prependMode = false, + sort = 'new', + importance, + q, + date_range, + industry_code } = {}, { rejectWithValue }) => { try { + // 构建筛选参数 + const filters = {}; + if (sort) filters.sort = sort; + if (importance && importance !== 'all') filters.importance = importance; + if (q) filters.q = q; + if (date_range) filters.date_range = date_range; + if (industry_code) filters.industry_code = industry_code; + logger.debug('CommunityData', '开始获取动态新闻', { page, per_page, clearCache, - prependMode + prependMode, + filters }); const response = await eventService.getEvents({ page, per_page, - sort: 'new' + ...filters }); if (response.success && response.data?.events) { diff --git a/src/views/Community/components/DynamicNewsCard.js b/src/views/Community/components/DynamicNewsCard.js index e7cc4d0b..0f30e277 100644 --- a/src/views/Community/components/DynamicNewsCard.js +++ b/src/views/Community/components/DynamicNewsCard.js @@ -131,11 +131,36 @@ const DynamicNewsCard = forwardRef(({ page: PAGINATION_CONFIG.INITIAL_PAGE, per_page: PAGINATION_CONFIG.CAROUSEL_PAGE_SIZE, pageSize: PAGINATION_CONFIG.CAROUSEL_PAGE_SIZE, // 传递 pageSize 确保索引计算一致 - clearCache: true + clearCache: true, + ...filters // 应用初始筛选条件 })); } }, [dispatch, allCachedEvents.length]); + // 监听筛选条件变化 - 清空缓存并重新请求数据 + useEffect(() => { + // 跳过初始加载(由上面的 useEffect 处理) + if (!hasInitialized.current) return; + + console.log('%c🔍 [筛选] 筛选条件改变,重新请求数据', 'color: #8B5CF6; font-weight: bold;', filters); + + // 筛选条件改变时,清空缓存并从第1页开始加载 + dispatch(fetchDynamicNews({ + page: PAGINATION_CONFIG.INITIAL_PAGE, + per_page: pageSize, + pageSize: pageSize, + clearCache: true, // 清空缓存 + ...filters // 应用新的筛选条件 + })); + }, [ + filters.sort, + filters.importance, + filters.q, + filters.date_range, + filters.industry_code, + dispatch + ]); // 只监听筛选参数的变化,不监听 page + // 自动选中逻辑 - 只在首次加载时自动选中第一个事件,翻页时不自动选中 useEffect(() => { if (currentPageEvents.length > 0) {