From dabedc1c0b178451ff87c5eefcdd3e4f0acffb73 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Wed, 26 Nov 2025 11:49:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B9=8B=E5=89=8D=E7=9A=84=E9=98=B2?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E9=80=BB=E8=BE=91=20!subscriptionInfo.type?= =?UTF-8?q?=20=E6=B0=B8=E8=BF=9C=E4=B8=BA=20false=EF=BC=88=E5=9B=A0?= =?UTF-8?q?=E4=B8=BA=E5=88=9D=E5=A7=8B=E5=80=BC=E6=98=AF=20free=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E5=AF=BC=E8=87=B4=E8=AE=A2=E9=98=85=20API=20=E4=BB=8E?= =?UTF-8?q?=E4=B8=8D=E8=A2=AB=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useSubscription.js | 8 +++++--- src/store/slices/subscriptionSlice.js | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) 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;