feat: 接入真实数据

This commit is contained in:
zdl
2025-11-03 10:06:48 +08:00
parent 7ae4bc418f
commit befa68cc51
2 changed files with 76 additions and 71 deletions

View File

@@ -156,6 +156,38 @@ export const fetchHotEvents = createAsyncThunk(
}
);
/**
* 获取动态新闻(无缓存,每次都发起请求)
* 用于 DynamicNewsCard 组件,需要保持实时性
*/
export const fetchDynamicNews = createAsyncThunk(
'communityData/fetchDynamicNews',
async (_, { rejectWithValue }) => {
try {
logger.debug('CommunityData', '开始获取动态新闻');
const response = await eventService.getEvents({
page: 1,
per_page: 5,
sort: 'new'
});
if (response.success && response.data?.events) {
logger.info('CommunityData', '动态新闻加载成功', {
count: response.data.events.length,
total: response.data.pagination?.total || 0
});
return response.data.events;
}
logger.warn('CommunityData', '动态新闻返回数据为空', response);
return [];
} catch (error) {
logger.error('CommunityData', '获取动态新闻失败', error);
return rejectWithValue(error.message || '获取动态新闻失败');
}
}
);
// ==================== Slice 定义 ====================
const communityDataSlice = createSlice({
@@ -164,29 +196,34 @@ const communityDataSlice = createSlice({
// 数据
popularKeywords: [],
hotEvents: [],
dynamicNews: [], // 动态新闻(无缓存)
// 加载状态
loading: {
popularKeywords: false,
hotEvents: false
hotEvents: false,
dynamicNews: false
},
// 错误信息
error: {
popularKeywords: null,
hotEvents: null
hotEvents: null,
dynamicNews: null
},
// 最后更新时间
lastUpdated: {
popularKeywords: null,
hotEvents: null
hotEvents: null,
dynamicNews: null
}
},
reducers: {
/**
* 清除所有缓存Redux + localStorage
* 注意dynamicNews 不使用 localStorage 缓存
*/
clearCache: (state) => {
// 清除 localStorage
@@ -195,15 +232,17 @@ const communityDataSlice = createSlice({
// 清除 Redux 状态
state.popularKeywords = [];
state.hotEvents = [];
state.dynamicNews = []; // 动态新闻也清除
state.lastUpdated.popularKeywords = null;
state.lastUpdated.hotEvents = null;
state.lastUpdated.dynamicNews = null;
logger.info('CommunityData', '所有缓存已清除');
},
/**
* 清除指定类型的缓存
* @param {string} payload - 缓存类型 ('popularKeywords' | 'hotEvents')
* @param {string} payload - 缓存类型 ('popularKeywords' | 'hotEvents' | 'dynamicNews')
*/
clearSpecificCache: (state, action) => {
const type = action.payload;
@@ -218,6 +257,11 @@ const communityDataSlice = createSlice({
state.hotEvents = [];
state.lastUpdated.hotEvents = null;
logger.info('CommunityData', '热点事件缓存已清除');
} else if (type === 'dynamicNews') {
// dynamicNews 不使用 localStorage只清除 Redux state
state.dynamicNews = [];
state.lastUpdated.dynamicNews = null;
logger.info('CommunityData', '动态新闻数据已清除');
}
},
@@ -235,6 +279,7 @@ const communityDataSlice = createSlice({
// 使用工厂函数创建 reducers消除重复代码
createDataReducers(builder, fetchPopularKeywords, 'popularKeywords');
createDataReducers(builder, fetchHotEvents, 'hotEvents');
createDataReducers(builder, fetchDynamicNews, 'dynamicNews');
}
});
@@ -245,6 +290,7 @@ export const { clearCache, clearSpecificCache, preloadData } = communityDataSlic
// 基础选择器Selectors
export const selectPopularKeywords = (state) => state.communityData.popularKeywords;
export const selectHotEvents = (state) => state.communityData.hotEvents;
export const selectDynamicNews = (state) => state.communityData.dynamicNews;
export const selectLoading = (state) => state.communityData.loading;
export const selectError = (state) => state.communityData.error;
export const selectLastUpdated = (state) => state.communityData.lastUpdated;
@@ -264,6 +310,13 @@ export const selectHotEventsWithLoading = (state) => ({
lastUpdated: state.communityData.lastUpdated.hotEvents
});
export const selectDynamicNewsWithLoading = (state) => ({
data: state.communityData.dynamicNews,
loading: state.communityData.loading.dynamicNews,
error: state.communityData.error.dynamicNews,
lastUpdated: state.communityData.lastUpdated.dynamicNews
});
// 工具函数:检查数据是否需要刷新(超过指定时间)
export const shouldRefresh = (lastUpdated, thresholdMinutes = 30) => {
if (!lastUpdated) return true;