From 9e52b1c816f915867b508f3f0b1258932e116d62 Mon Sep 17 00:00:00 2001 From: moux1024 <403053463@qq.com> Date: Tue, 30 Sep 2025 14:33:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20localstorage=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/common/CallbackModal.tsx | 2 +- components/layout/H5TopBar.tsx | 44 ++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/components/common/CallbackModal.tsx b/components/common/CallbackModal.tsx index 206b8b8..994e71c 100644 --- a/components/common/CallbackModal.tsx +++ b/components/common/CallbackModal.tsx @@ -167,7 +167,7 @@ export default function CallbackModal({ return } - const userId = JSON.parse(localStorage.getItem('currentUser') || '{}').id + const userId = typeof window !== 'undefined' ? JSON.parse(localStorage.getItem('currentUser') || '{}').id : 0 try { if (paymentType === 'subscription') { diff --git a/components/layout/H5TopBar.tsx b/components/layout/H5TopBar.tsx index 6c6cfdf..b92adc8 100644 --- a/components/layout/H5TopBar.tsx +++ b/components/layout/H5TopBar.tsx @@ -7,7 +7,8 @@ import { Drawer } from 'antd'; import { fetchTabsByCode, HomeTabItem } from '@/api/serversetting'; import { getSigninStatus } from '@/api/signin'; import { getCurrentUser, isAuthenticated, logoutUser } from '@/lib/auth'; -import { getUserSubscriptionInfo, createPortalSession, redirectToPortal } from '@/lib/stripe'; +import { getUserSubscriptionInfo } from '@/lib/stripe'; +import { trackEvent } from '@/utils/analytics'; import { GradientText } from '@/components/ui/gradient-text'; import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'; import SigninBox from './signin-box'; @@ -184,13 +185,42 @@ export default function H5TopBar({ onSelectHomeTab }: H5TopBarProps) { if (!user?.id) return; setIsManagingSubscription(true); try { - const response = await createPortalSession({ - user_id: String(user.id), - return_url: window.location.origin + '/dashboard', - }); - if (response.successful && response.data?.portal_url) { - redirectToPortal(response.data.portal_url); + // 获取用户当前订阅信息 + const response = await getUserSubscriptionInfo(String(user.id)); + if (!response?.successful || !response?.data) { + throw new Error('Failed to get subscription info'); } + + const currentPlan = response.data.plan_name; + const billingType = 'month'; // 默认使用月付,用户可以在pricing页面切换 + + // 跟踪订阅管理按钮点击事件 + trackEvent('subscription_manage_click', { + event_category: 'subscription', + event_label: 'manage_subscription', + custom_parameters: { + current_plan: currentPlan, + billing_type: billingType, + }, + }); + + // 复用pricing页面的跳转方案:构建pay-redirect URL + const url = `/pay-redirect?type=subscription&plan=${encodeURIComponent(currentPlan)}&billing=${encodeURIComponent(billingType)}`; + const win = window.open(url, '_blank'); + + // 通知当前窗口等待支付(显示loading模态框) + window.postMessage({ + type: 'waiting-payment', + paymentType: 'subscription', + }, '*'); + + if (!win) { + throw new Error('Unable to open redirect window, please check popup settings'); + } + } catch (error) { + console.error('Failed to manage subscription:', error); + // 如果出错,回退到pricing页面 + router.push('/pricing'); } finally { setIsManagingSubscription(false); }