feat: 之前的防重复逻辑 !subscriptionInfo.type 永远为 false(因为初始值是 free),导致订阅 API 从不被调用

This commit is contained in:
zdl
2025-11-26 11:49:12 +08:00
parent 7b4c4be7bf
commit dabedc1c0b
2 changed files with 9 additions and 4 deletions

View File

@@ -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) => {