import type { ReactNode } from 'react'; import { Navigate, useLocation, createBrowserRouter } from 'react-router-dom'; import MainLayout from '@/layouts/MainLayout'; import SessionSplash from '@/components/SessionSplash'; import CollectionPage from '@/pages/collection/CollectionPage'; import LoginPage from '@/pages/auth/LoginPage'; import OnlineProductsPage from '@/pages/online-products/OnlineProductsPage'; import PasswordSettingsPage from '@/pages/settings/PasswordSettingsPage'; import PricingPage from '@/pages/pricing/PricingPage'; import ProductEditPage from '@/pages/products/ProductEditPage'; import ProfileSettingsPage from '@/pages/settings/ProfileSettingsPage'; import RegisterPage from '@/pages/auth/RegisterPage'; import ShopsPage from '@/pages/shops/ShopsPage'; import WatermarksPage from '@/pages/watermarks/WatermarksPage'; import { PlatformProvider } from '@/services/platform'; import { useSession } from '@/services/session'; /** 主布局守卫:会话恢复中显示启动屏;未认证跳登录页并记住来源路径 */ function RequireSession({ children }: { children: ReactNode }) { const { status } = useSession(); const location = useLocation(); if (status === 'restoring') { return ; } if (status === 'anonymous') { return ; } return children; } /** 认证页互斥:已登录访问 /login、/register 时回到来源页或主布局 */ function RedirectIfAuthenticated({ children }: { children: ReactNode }) { const { status } = useSession(); const location = useLocation(); if (status === 'authenticated') { const from = (location.state as { from?: string } | null)?.from; return ; } return children; } export const router = createBrowserRouter([ { path: '/', element: ( {/* 平台上下文仅在登录后可用,随主布局挂载 */} ), children: [ { index: true, element: }, { path: 'online-products', element: }, { path: 'collection', element: }, { path: 'shops', element: }, { path: 'watermarks', element: }, { path: 'pricing', element: }, // 商品 AI 编辑页:从采集箱/在线商品进入,不在左侧菜单中出现 { path: 'products/:id', element: }, // 个人中心设置页:从页头用户菜单进入,不在左侧菜单中出现 { path: 'settings/profile', element: }, { path: 'settings/password', element: }, ], }, { path: 'login', element: ( ), }, { path: 'register', element: ( ), }, { path: '*', element: }, ]);