import React, { useState, useEffect, useCallback, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import { LogIn, User, LogOut, ChevronDown, ShieldCheck, ShieldAlert } from '../icons/commonIcons'; import { Lightbulb } from '../icons/commonIcons'; import { authService, UserInfo } from '../services/authService'; import { BrandingContext } from '../App'; const Navbar: React.FC = () => { const navigate = useNavigate(); const branding = React.useContext(BrandingContext); const [userInfo, setUserInfo] = useState(null); const [isAuthenticated, setIsAuthenticated] = useState(false); const [, forceUpdate] = useState({}); const [showDropdown, setShowDropdown] = useState(false); const dropdownRef = useRef(null); const checkAuthStatus = useCallback(() => { const authenticated = authService.isAuthenticated(); const user = authService.getUserInfo(); console.log('[Navbar] checkAuthStatus - authenticated:', authenticated, 'user:', user); setIsAuthenticated(authenticated); setUserInfo(authenticated ? user : null); forceUpdate({}); }, []); useEffect(() => { // 初始检查 checkAuthStatus(); // 订阅认证状态变化 const unsubscribe = authService.subscribe(() => { console.log('[Navbar] auth_state_change event received'); checkAuthStatus(); }); // 监听存储变化(其他标签页) const handleStorageChange = (e: StorageEvent) => { if (e.key === 'aigc_space_token' || e.key === 'aigc_space_user_info' || e.key === null) { checkAuthStatus(); } }; window.addEventListener('storage', handleStorageChange); return () => { unsubscribe(); window.removeEventListener('storage', handleStorageChange); }; }, [checkAuthStatus]); const handleLogin = () => { navigate('/login'); }; const handleProfile = () => { setShowDropdown(false); navigate('/profile'); }; const handleLogout = async () => { setShowDropdown(false); // 先本地清理登录状态 try { if (typeof window !== 'undefined') { localStorage.removeItem('aigc_space_token'); localStorage.removeItem('aigc_space_refresh_token'); localStorage.removeItem('aigc_space_user_info'); } } catch (e) { // ignore } if (typeof window === 'undefined') return; // 查询后端 SSO 配置,决定跳转目标 try { const cfg = await authService.getSsoConfig(); if (cfg?.sso_enabled) { // SSO 启用:跳 CAS 注销页 const casLogoutUrl = cfg.cas_login_url.replace(/\/login$/, '/logout'); try { if (window.top && window.top !== window) { window.top.location.replace(casLogoutUrl); } else { window.location.replace(casLogoutUrl); } } catch { window.location.href = casLogoutUrl; } } else { // SSO 未启用:直接跳普通登录页 window.location.replace('/'); } } catch { window.location.replace('/'); } }; // 点击外部关闭下拉菜单 useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setShowDropdown(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); return (
{/* 左侧:标题 */}
{branding.system_logo ? logo : }
{branding.system_name}
{/* 右侧:用户操作 */}
{isAuthenticated ? ( <> {/* 用户头像和昵称 - 下拉菜单 */}
setShowDropdown(!showDropdown)} className="flex items-center space-x-1 sm:space-x-2 cursor-pointer group hover:bg-gray-50 rounded-lg px-1 sm:px-2 py-1 transition-colors" >
{userInfo?.avatar ? ( 用户头像 ) : (
{userInfo?.nickname?.charAt(0)?.toUpperCase() || 'U'}
)}
{userInfo?.nickname || '用户'}
{/* 下拉菜单 */} {showDropdown && (
)}
) : ( /* 未登录状态 */
)}
); }; export default Navbar;