"use client"; import Link from "next/link"; import { useAuthStore, useConfigStore } from "@/lib/store"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { api } from "@/lib/api"; export default function ClientLayout({ children }: { children: React.ReactNode }) { const { user, logout } = useAuthStore(); const { uiConfig, setUiConfig } = useConfigStore(); const router = useRouter(); const [isInitializing, setIsInitializing] = useState(true); const [dropdownOpen, setDropdownOpen] = useState(false); const { setAuth, token } = useAuthStore(); useEffect(() => { let isMounted = true; const initApp = async () => { try { const configPromise = api.get('/api/config/ui').catch(e => { console.error('Failed to fetch UI config:', e); return null; }); const authPromise = (async () => { const storedToken = localStorage.getItem('token'); if (storedToken && !user) { try { const res = await api.get('/api/auth/profile'); if (res.success && isMounted) { setAuth(res.data, storedToken); } } catch (err) { console.error('Failed to restore auth:', err); localStorage.removeItem('token'); } } })(); const [configRes] = await Promise.all([configPromise, authPromise]); if (isMounted && configRes && configRes.success && configRes.data) { setUiConfig(configRes.data); } } finally { if (isMounted) { setIsInitializing(false); } } }; if (!uiConfig?.siteName || !user) { initApp(); } else { setIsInitializing(false); } return () => { isMounted = false; }; }, [setUiConfig, setAuth, uiConfig?.siteName, user]); const handleLogout = () => { logout(); router.push('/'); }; const handleUpgradeVip = async () => { try { const res = await api.post('/api/orders/create', { isVip: true }); if (res.success) { router.push(`/payment/${res.data.orderId}`); } } catch (err) { console.error(err); alert('创建会员订单失败,请稍后重试'); } }; const getAvatarColor = (name: string) => { let hash = 0; for (let i = 0; i < name.length; i++) { hash = name.charCodeAt(i) + ((hash << 5) - hash); } const hue = Math.abs(hash % 360); return `hsl(${hue}, 70%, 50%)`; }; if (isInitializing) { return (

应用加载中...

); } const siteName = uiConfig?.siteName || '极简AI'; const footerText = uiConfig?.footerText || `© ${new Date().getFullYear()} 极简AI. 保留所有权利。`; const navLinks = uiConfig?.navLinks || []; return ( <>
{uiConfig?.logo ? ( // eslint-disable-next-line @next/next/no-img-element {siteName} ) : ( siteName )}
{children}
); }