From cf71ee111b00c522862605ee689e669fd0e3100b Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Fri, 24 Oct 2025 17:10:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useSubscription.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/hooks/useSubscription.js b/src/hooks/useSubscription.js index 9d75aa92..328578f7 100644 --- a/src/hooks/useSubscription.js +++ b/src/hooks/useSubscription.js @@ -1,5 +1,5 @@ // src/hooks/useSubscription.js -import { useState, useEffect } from 'react'; +import { useState, useEffect, useRef } from 'react'; import { useAuth } from '../contexts/AuthContext'; import { logger } from '../utils/logger'; @@ -104,10 +104,32 @@ export const useSubscription = () => { } }; + // ⚡ 提取 userId 为独立变量,避免 user 对象引用变化导致无限循环 + const userId = user?.id; + const prevUserIdRef = useRef(userId); + const prevIsAuthenticatedRef = useRef(isAuthenticated); + useEffect(() => { - fetchSubscriptionInfo(); + // ⚡ 只在 userId 或 isAuthenticated 真正变化时才请求 + const userIdChanged = prevUserIdRef.current !== userId; + const authChanged = prevIsAuthenticatedRef.current !== isAuthenticated; + + if (userIdChanged || authChanged) { + logger.debug('useSubscription', 'fetchSubscriptionInfo 触发', { + userIdChanged, + authChanged, + prevUserId: prevUserIdRef.current, + currentUserId: userId, + prevAuth: prevIsAuthenticatedRef.current, + currentAuth: isAuthenticated + }); + + prevUserIdRef.current = userId; + prevIsAuthenticatedRef.current = isAuthenticated; + fetchSubscriptionInfo(); + } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [isAuthenticated, user?.id]); // 只依赖 user.id,避免 user 对象变化导致无限循环 + }, [isAuthenticated, userId]); // 使用 userId 原始值,而不是 user?.id 表达式 // 获取订阅级别数值 const getSubscriptionLevel = (type = null) => {