diff --git a/src/hooks/useSubscription.js b/src/hooks/useSubscription.js index 2afe95fe..e49270ac 100644 --- a/src/hooks/useSubscription.js +++ b/src/hooks/useSubscription.js @@ -12,6 +12,7 @@ import { resetToFree, selectSubscriptionInfo, selectSubscriptionLoading, + selectSubscriptionLoaded, selectSubscriptionError, selectSubscriptionModalOpen } from '../store/slices/subscriptionSlice'; @@ -66,14 +67,15 @@ export const useSubscription = () => { // Redux 状态 const subscriptionInfo = useSelector(selectSubscriptionInfo); const loading = useSelector(selectSubscriptionLoading); + const loaded = useSelector(selectSubscriptionLoaded); const error = useSelector(selectSubscriptionError); const isSubscriptionModalOpen = useSelector(selectSubscriptionModalOpen); // 自动加载订阅信息(带防重复逻辑) useEffect(() => { if (isAuthenticated && user) { - // 只在没有数据且未在加载时才请求,避免多个组件重复调用 - if (!subscriptionInfo.type && !loading) { + // 只在未加载且未在加载中时才请求,避免多个组件重复调用 + if (!loaded && !loading) { dispatch(fetchSubscriptionInfo()); logger.debug('useSubscription', '加载订阅信息', { userId: user.id }); } @@ -82,7 +84,7 @@ export const useSubscription = () => { dispatch(resetToFree()); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [isAuthenticated, user?.id, dispatch]); + }, [isAuthenticated, user?.id, dispatch, loaded, loading]); // 获取订阅级别数值 const getSubscriptionLevel = (type = null) => { diff --git a/src/store/slices/subscriptionSlice.js b/src/store/slices/subscriptionSlice.js index 3f2ad364..1f90eee7 100644 --- a/src/store/slices/subscriptionSlice.js +++ b/src/store/slices/subscriptionSlice.js @@ -71,6 +71,7 @@ const subscriptionSlice = createSlice({ }, // 加载状态 loading: false, + loaded: false, // 是否已加载过(用于防止重复请求) error: null, // 订阅 Modal 状态 isModalOpen: false, @@ -104,8 +105,8 @@ const subscriptionSlice = createSlice({ end_date: null }; state.loading = false; + state.loaded = false; // 重置已加载标记,下次登录时重新获取 state.error = null; - logger.debug('subscriptionSlice', '重置订阅信息为免费版'); }, }, extraReducers: (builder) => { @@ -118,6 +119,7 @@ const subscriptionSlice = createSlice({ // fetchSubscriptionInfo - fulfilled .addCase(fetchSubscriptionInfo.fulfilled, (state, action) => { state.loading = false; + state.loaded = true; // 标记已加载 state.info = action.payload; state.error = null; }) @@ -136,6 +138,7 @@ export const { openModal, closeModal, resetToFree } = subscriptionSlice.actions; // 导出 selectors export const selectSubscriptionInfo = (state) => state.subscription.info; export const selectSubscriptionLoading = (state) => state.subscription.loading; +export const selectSubscriptionLoaded = (state) => state.subscription.loaded; export const selectSubscriptionError = (state) => state.subscription.error; export const selectSubscriptionModalOpen = (state) => state.subscription.isModalOpen;