Spaces:
Sleeping
Sleeping
File size: 8,042 Bytes
d629cd4 426f2a4 d629cd4 426f2a4 d629cd4 426f2a4 d629cd4 426f2a4 d629cd4 426f2a4 d629cd4 426f2a4 d629cd4 f45e448 426f2a4 d629cd4 f45e448 d629cd4 426f2a4 d629cd4 426f2a4 d629cd4 426f2a4 d629cd4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | "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 (
<div className="min-h-screen flex items-center justify-center bg-gray-50">
<div className="flex flex-col items-center gap-3">
<div className="w-8 h-8 border-4 border-blue-600 border-t-transparent rounded-full animate-spin"></div>
<p className="text-gray-500 font-medium">应用加载中...</p>
</div>
</div>
);
}
const siteName = uiConfig?.siteName || '极简AI';
const footerText = uiConfig?.footerText || `© ${new Date().getFullYear()} 极简AI. 保留所有权利。`;
const navLinks = uiConfig?.navLinks || [];
return (
<>
<header className="bg-white border-b sticky top-0 z-50">
<div className="max-w-7xl mx-auto px-4 h-16 flex items-center justify-between">
<Link href="/" className="text-xl font-bold text-blue-600">
{uiConfig?.logo ? (
// eslint-disable-next-line @next/next/no-img-element
<img src={uiConfig.logo} alt={siteName} className="h-8 object-contain" />
) : (
siteName
)}
</Link>
<nav className="flex items-center gap-6">
<Link href="/" className="text-gray-600 hover:text-blue-600 font-medium">首页</Link>
{navLinks.map((link, idx) => (
<Link
key={idx}
href={`/?category=${link.value}`}
className="text-gray-600 hover:text-blue-600 font-medium"
>
{link.label}
</Link>
))}
{user ? (
<>
{user.role !== 'admin' && (
<>
<Link href="/user/stars" className="text-gray-600 hover:text-blue-600 font-medium">我的收藏</Link>
<Link href="/user/courses" className="text-gray-600 hover:text-blue-600 font-medium">我的学习</Link>
</>
)}
{user.role === 'admin' && (
<Link href="/admin" className="text-gray-600 hover:text-blue-600 font-medium">管理后台</Link>
)}
<div className="relative border-l pl-6 ml-2">
<div
className="w-10 h-10 rounded-full flex items-center justify-center text-white font-bold cursor-pointer hover:opacity-90 transition-opacity select-none relative"
style={{ backgroundColor: getAvatarColor(user.nickname) }}
onClick={() => setDropdownOpen(!dropdownOpen)}
>
{user.nickname.charAt(0).toUpperCase()}
{user.isVip && (
<div className="absolute -bottom-1 -right-1 bg-yellow-400 text-xs text-white px-1.5 rounded-full border-2 border-white shadow-sm font-bold scale-75">
VIP
</div>
)}
</div>
{dropdownOpen && (
<>
<div
className="fixed inset-0 z-40"
onClick={() => setDropdownOpen(false)}
></div>
<div className="absolute right-0 mt-3 w-48 bg-white rounded-xl shadow-lg border border-gray-100 py-2 z-50 overflow-hidden">
<div className="px-4 py-2 border-b border-gray-50 mb-1">
<div className="text-sm font-medium text-gray-900 truncate">{user.nickname}</div>
<div className="text-xs text-gray-500 truncate">{user.email}</div>
</div>
{!user.isVip && (
<button
onClick={() => {
setDropdownOpen(false);
handleUpgradeVip();
}}
className="w-full text-left px-4 py-2.5 text-sm text-yellow-600 hover:bg-yellow-50 font-medium flex items-center transition-colors"
>
<span className="mr-2">👑</span>
注册会员 (¥{uiConfig?.memberFee ?? 99})
</button>
)}
<button
onClick={() => {
setDropdownOpen(false);
handleLogout();
}}
className="w-full text-left px-4 py-2.5 text-sm text-gray-600 hover:bg-gray-50 transition-colors"
>
退出登录
</button>
</div>
</>
)}
</div>
</>
) : (
<Link href="/login" className="px-5 py-2.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors font-medium text-sm">
登录 / 注册
</Link>
)}
</nav>
</div>
</header>
<main className="flex-1 max-w-7xl mx-auto w-full px-4 py-8">
{children}
</main>
<footer className="bg-white border-t py-8 mt-auto">
<div className="max-w-7xl mx-auto px-4 text-center text-gray-500 text-sm">
{footerText}
</div>
</footer>
</>
);
}
|