From d695f8ff7bc972675b8244ca362c67bc0e20fdc1 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Mon, 20 Oct 2025 13:58:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=99=BB=E9=99=86=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/settings.local.json | 3 +- src/components/Navbars/HomeNavbar.js | 4 +- .../Subscription/SubscriptionContent.js | 87 ++++++------------- src/hooks/useSubscription.js | 3 +- src/views/Dashboard/Center.js | 4 +- src/views/Home/HomePage.backup.js | 2 +- src/views/Home/HomePage.js | 2 +- src/views/TradingSimulation/index.js | 2 +- 8 files changed, 37 insertions(+), 70 deletions(-) diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 1faf39fd..07fef7db 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -11,7 +11,8 @@ "Bash(rm /Users/qiye/Desktop/jzqy/vf_react/src/views/Dashboard/Automotive.js)", "Bash(rm /Users/qiye/Desktop/jzqy/vf_react/src/views/Dashboard/CRM.js)", "Bash(rm /Users/qiye/Desktop/jzqy/vf_react/src/views/Dashboard/SmartHome.js)", - "Bash(rm /Users/qiye/Desktop/jzqy/vf_react/src/views/Dashboard/Landing.js)" + "Bash(rm /Users/qiye/Desktop/jzqy/vf_react/src/views/Dashboard/Landing.js)", + "mcp__ide__getDiagnostics" ], "deny": [], "ask": [] diff --git a/src/components/Navbars/HomeNavbar.js b/src/components/Navbars/HomeNavbar.js index 284a18fa..6d5d0c07 100644 --- a/src/components/Navbars/HomeNavbar.js +++ b/src/components/Navbars/HomeNavbar.js @@ -603,7 +603,7 @@ export default function HomeNavbar() { error: error.message }); } - }, [isAuthenticated, user]); // 移除 getApiBase 依赖,因为它现在在组件外部 + }, [isAuthenticated, user?.id]); // 只依赖 user.id,避免 user 对象变化导致无限循环 // 监听用户变化,重置检查标志(用户切换或退出登录时) React.useEffect(() => { @@ -622,7 +622,7 @@ export default function HomeNavbar() { const timer = setTimeout(checkProfileCompleteness, 1000); return () => clearTimeout(timer); } - }, [isAuthenticated, user, checkProfileCompleteness]); + }, [isAuthenticated, user?.id, checkProfileCompleteness]); // 只依赖 user.id,避免无限循环 // 加载订阅信息 React.useEffect(() => { diff --git a/src/components/Subscription/SubscriptionContent.js b/src/components/Subscription/SubscriptionContent.js index 341a5d98..94fef6b9 100644 --- a/src/components/Subscription/SubscriptionContent.js +++ b/src/components/Subscription/SubscriptionContent.js @@ -24,12 +24,12 @@ import { } from '@chakra-ui/react'; import React, { useState, useEffect } from 'react'; import { logger } from '../../utils/logger'; +import { useAuth } from '../../contexts/AuthContext'; // Icons import { FaWeixin, FaGem, - FaStar, FaCheck, FaQrcode, FaClock, @@ -38,6 +38,9 @@ import { } from 'react-icons/fa'; export default function SubscriptionContent() { + // Auth context + const { user } = useAuth(); + // Chakra color mode const textColor = useColorModeValue('gray.700', 'white'); const borderColor = useColorModeValue('gray.200', 'gray.600'); @@ -50,7 +53,6 @@ export default function SubscriptionContent() { // State const [subscriptionPlans, setSubscriptionPlans] = useState([]); - const [currentUser, setCurrentUser] = useState(null); const [selectedPlan, setSelectedPlan] = useState(null); const [selectedCycle, setSelectedCycle] = useState('monthly'); const [paymentOrder, setPaymentOrder] = useState(null); @@ -63,7 +65,7 @@ export default function SubscriptionContent() { // 加载订阅套餐数据 useEffect(() => { fetchSubscriptionPlans(); - fetchCurrentUser(); + // 不再需要 fetchCurrentUser(),直接使用 AuthContext 的 user }, []); // 倒计时更新 @@ -124,30 +126,8 @@ export default function SubscriptionContent() { } }; - const fetchCurrentUser = async () => { - try { - const response = await fetch('/api/auth/session', { - credentials: 'include' - }); - if (response.ok) { - const data = await response.json(); - logger.debug('SubscriptionContent', '用户数据获取成功', { data }); - if (data.success) { - setCurrentUser(data.user); - logger.debug('SubscriptionContent', '用户信息已更新', { - userId: data.user?.id, - subscriptionType: data.user?.subscription_type, - subscriptionStatus: data.user?.subscription_status - }); - } - } - } catch (error) { - logger.error('SubscriptionContent', 'fetchCurrentUser', error); - } - }; - const handleSubscribe = (plan) => { - if (!currentUser) { + if (!user) { toast({ title: '请先登录', status: 'warning', @@ -287,25 +267,9 @@ export default function SubscriptionContent() { } }; - const handleRefreshUserStatus = async () => { - try { - await fetchCurrentUser(); - toast({ - title: '用户状态已刷新', - description: '订阅信息已更新', - status: 'success', - duration: 3000, - isClosable: true, - }); - } catch (error) { - toast({ - title: '刷新失败', - description: '请稍后重试', - status: 'error', - duration: 3000, - isClosable: true, - }); - } + const handleRefreshUserStatus = () => { + // 刷新页面以重新加载用户数据 + window.location.reload(); }; const handleForceUpdatePayment = async () => { @@ -459,7 +423,7 @@ export default function SubscriptionContent() { return ( {/* 当前订阅状态 */} - {currentUser && ( + {user && ( - {currentUser.subscription_type === 'free' ? '基础版' : - currentUser.subscription_type === 'pro' ? 'Pro 专业版' : 'Max 旗舰版'} + 1SubscriptionContent {user.subscription_type} + {user.subscription_type === 'free' ? '基础版' : + user.subscription_type === 'pro' ? 'Pro 专业版' : 'Max 旗舰版'} - {currentUser.subscription_status === 'active' ? '已激活' : '未激活'} + {user.subscription_status === 'active' ? '已激活' : '未激活'} - {currentUser.subscription_end_date && ( + {user.subscription_end_date && ( - 到期时间: {new Date(currentUser.subscription_end_date).toLocaleDateString('zh-CN')} + 到期时间: {new Date(user.subscription_end_date).toLocaleDateString('zh-CN')} )} - {currentUser.subscription_status === 'active' && currentUser.subscription_type !== 'free' && ( + {user.subscription_status === 'active' && user.subscription_type !== 'free' && ( )} @@ -665,16 +630,16 @@ export default function SubscriptionContent() { variant="solid" onClick={() => handleSubscribe(plan)} isDisabled={ - currentUser?.subscription_type === plan.name && - currentUser?.subscription_status === 'active' + user?.subscription_type === plan.name && + user?.subscription_status === 'active' } _hover={{ transform: 'scale(1.02)', }} transition="all 0.2s" > - {currentUser?.subscription_type === plan.name && - currentUser?.subscription_status === 'active' + {user?.subscription_type === plan.name && + user?.subscription_status === 'active' ? '✓ 已订阅' : `选择 ${plan.display_name}` } diff --git a/src/hooks/useSubscription.js b/src/hooks/useSubscription.js index 57e7e7da..9d75aa92 100644 --- a/src/hooks/useSubscription.js +++ b/src/hooks/useSubscription.js @@ -106,7 +106,8 @@ export const useSubscription = () => { useEffect(() => { fetchSubscriptionInfo(); - }, [isAuthenticated, user]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [isAuthenticated, user?.id]); // 只依赖 user.id,避免 user 对象变化导致无限循环 // 获取订阅级别数值 const getSubscriptionLevel = (type = null) => { diff --git a/src/views/Dashboard/Center.js b/src/views/Dashboard/Center.js index 503461ab..ed4deef1 100644 --- a/src/views/Dashboard/Center.js +++ b/src/views/Dashboard/Center.js @@ -112,7 +112,7 @@ export default function CenterDashboard() { } finally { setLoading(false); } - }, [user]); + }, [user?.id]); // 只依赖 user.id,避免无限循环 // 加载实时行情 const loadRealtimeQuotes = useCallback(async () => { @@ -198,7 +198,7 @@ export default function CenterDashboard() { }; document.addEventListener('visibilitychange', onVis); return () => document.removeEventListener('visibilitychange', onVis); - }, [user, location.pathname, loadData]); + }, [user?.id, location.pathname, loadData]); // 只依赖 user.id,避免无限循环 // 定时刷新实时行情(每分钟一次) useEffect(() => { diff --git a/src/views/Home/HomePage.backup.js b/src/views/Home/HomePage.backup.js index f65f6606..5ca88f42 100644 --- a/src/views/Home/HomePage.backup.js +++ b/src/views/Home/HomePage.backup.js @@ -37,7 +37,7 @@ export default function HomePage() { nickname: user.nickname } : null }); - }, [user, isAuthenticated, isLoading]); + }, [user?.id, isAuthenticated, isLoading]); // 只依赖 user.id,避免无限循环 // 统计数据动画 const [stats, setStats] = useState({ diff --git a/src/views/Home/HomePage.js b/src/views/Home/HomePage.js index 35901322..dbb8e462 100755 --- a/src/views/Home/HomePage.js +++ b/src/views/Home/HomePage.js @@ -35,7 +35,7 @@ export default function HomePage() { isAuthenticated, hasUser: !!user }); - }, [user, isAuthenticated]); + }, [user?.id, isAuthenticated]); // 只依赖 user.id,避免无限循环 // 核心功能配置 - 5个主要功能 const coreFeatures = [ diff --git a/src/views/TradingSimulation/index.js b/src/views/TradingSimulation/index.js index dfce7e60..e8d145c0 100644 --- a/src/views/TradingSimulation/index.js +++ b/src/views/TradingSimulation/index.js @@ -92,7 +92,7 @@ export default function TradingSimulation() { userId: user?.id, userName: user?.name }); - }, [isAuthenticated, user]); + }, [isAuthenticated, user?.id]); // 只依赖 user.id,避免无限循环 // 获取资产历史数据的 useEffect useEffect(() => {