'use client'; /** * 用户菜单组件 * - 已登录:显示用户名、VIP 徽章、退出按钮 * - 未登录:显示登录/注册按钮 */ import { useState, useEffect, useRef } from 'react'; import { User, LogOut, Crown, LogIn, Smartphone, Shield } from 'lucide-react'; import { useAuthStore } from '@/store/authStore'; import AuthModal from './AuthModal'; import PaymentModal from './PaymentModal'; import UserInfoModal from './UserInfoModal'; import AdminPanel from './AdminPanel'; interface UserMenuProps { compact?: boolean; // 紧凑模式(游戏进行中) } export default function UserMenu({ compact = false }: UserMenuProps) { const { username, isVip, vipExpireAt, isInitialized, logout, initialize } = useAuthStore(); const [showAuthModal, setShowAuthModal] = useState(false); const [showPayment, setShowPayment] = useState(false); const [showUserInfo, setShowUserInfo] = useState(false); const [showAdminPanel, setShowAdminPanel] = useState(false); const [showDropdown, setShowDropdown] = useState(false); const dropdownRef = useRef(null); // 初始化认证状态 useEffect(() => { if (!isInitialized) { initialize(); } }, [isInitialized, initialize]); // 点击外部关闭下拉 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); }, []); const handleLogout = async () => { setShowDropdown(false); await logout(); }; // 格式化 VIP 到期时间 const formatExpireDate = (dateStr: string | null) => { if (!dateStr) return ''; const date = new Date(dateStr); const now = new Date(); const diffDays = Math.ceil((date.getTime() - now.getTime()) / (1000 * 60 * 60 * 24)); if (diffDays <= 0) return '已过期'; if (diffDays <= 7) return `${diffDays}天后到期`; return dateStr.split(' ')[0]; // 只显示日期部分 }; if (!isInitialized) return null; // 未登录状态 if (!username) { return ( <> {showAuthModal && setShowAuthModal(false)} />} ); } // 已登录状态 return ( <>
{/* 下拉菜单 */} {showDropdown && (
{/* 用户信息 */}
{ setShowDropdown(false); setShowUserInfo(true); }} >
{username}
{isVip ? (
VIP 会员 {formatExpireDate(vipExpireAt)}
) : (
普通用户
)}
{/* 操作 */}
{!isVip && ( )} {/* 超级管理员额外功能 */} {username === '583079759' && ( )}
)}
{showAuthModal && setShowAuthModal(false)} />} setShowPayment(false)} /> setShowUserInfo(false)} username={username || ''} isVip={isVip} vipExpireAt={vipExpireAt} onRenewVip={() => { setShowUserInfo(false); setShowPayment(true); }} /> setShowAdminPanel(false)} /> ); }