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