import { Navigate, useLocation } from 'react-router-dom'; import { getStoreProfile, hasShopWxSession } from '../lib/api'; import { useStoreSession } from '../contexts/StoreSessionContext'; import { peekShopReturnTo, rememberShopReturnPath } from '../lib/shop-redeem-return'; const PUBLIC_PATHS = new Set(['/login', '/legal/user-agreement', '/legal/privacy-policy']); const SELECT_STORE_PATH = '/select-store'; export default function AuthGate({ children }: { children: React.ReactNode }) { const { ready, authenticated, needsSelectStore } = useStoreSession(); const location = useLocation(); if (!ready) { return (

加载中…

); } if (authenticated && location.pathname === '/login') { const next = needsSelectStore ? SELECT_STORE_PATH : (peekShopReturnTo() || '/'); return ; } if (authenticated && needsSelectStore && location.pathname !== SELECT_STORE_PATH) { rememberShopReturnPath(location); return ; } if (!authenticated && !PUBLIC_PATHS.has(location.pathname)) { rememberShopReturnPath(location); const profile = getStoreProfile(); if (profile && hasShopWxSession() && location.pathname !== '/login') { return ; } return ; } return <>{children}; }