import React, { useEffect } from 'react'; import { useNavigate, useLocation, } from 'react-router'; import { useAuth } from '../hooks/AuthProvider'; export const ProtectedRoute = ({ children }: { children: React.ReactNode }) => { const { isAuthenticated, isLoading } = useAuth(); const navigate = useNavigate(); const location = useLocation(); useEffect(() => { // 只有在加载完成且未认证时才重定向 if (!isLoading && !isAuthenticated) { // 保存当前路径到localStorage,用于登录后跳转回来源页 const returnUrl = location.pathname + location.search; localStorage.setItem('mobile_return_url', returnUrl); navigate('/mobile/login', { replace: true }); } }, [isAuthenticated, isLoading, navigate, location]); // 显示加载状态,直到认证检查完成 if (isLoading) { return (