修复 localstorage安全操作

This commit is contained in:
moux1024 2025-09-30 14:33:59 +08:00
parent 4b33619c20
commit 9e52b1c816
2 changed files with 38 additions and 8 deletions

View File

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

View File

@ -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);
}