'use client' import { useState, useEffect } from 'react' import { usePathname, useRouter } from 'next/navigation' import { Button } from '@/components/ui/button' import LoginPage from '@/components/admin/LoginPage' import { getUserInfo, clearAuthInfo, isAuthenticated, type AuthUser } from '@/lib/auth' import Link from 'next/link' const navItems = [ { key: 'endpoints', label: 'API端点', href: '/admin' }, { key: 'rules', label: 'URL替换规则', href: '/admin/rules' }, { key: 'home', label: '首页配置', href: '/admin/home' }, ] export default function AdminLayout({ children, }: { children: React.ReactNode }) { const [user, setUser] = useState(null) const [loading, setLoading] = useState(true) const pathname = usePathname() const router = useRouter() useEffect(() => { checkAuth() }, []) const checkAuth = async () => { if (!isAuthenticated()) { setLoading(false) return } const savedUser = getUserInfo() if (savedUser) { setUser(savedUser) setLoading(false) return } // 如果没有用户信息,清除认证状态 clearAuthInfo() setLoading(false) } const handleLoginSuccess = (userInfo: AuthUser) => { setUser(userInfo) setLoading(false) } const handleLogout = () => { clearAuthInfo() setUser(null) router.push('/admin') } if (loading) { return (
) } if (!user) { return } return (
{/* Header */}

随机API管理后台

{/* Navigation */}
欢迎, {user.name}
{/* Main Content */}
{children}
) }