import { AuditOutlined, DashboardOutlined, FileSearchOutlined, LogoutOutlined, MenuFoldOutlined, MenuUnfoldOutlined, OrderedListOutlined, SettingOutlined, ShopOutlined, TeamOutlined, UnorderedListOutlined, UserOutlined, } from '@ant-design/icons' import { App, Button, Dropdown, Layout, Menu, Space, Typography } from 'antd' import type { MenuProps } from 'antd' import { useMemo, useState } from 'react' import { Outlet, useLocation, useNavigate } from 'react-router' import { logoutAdmin } from '@/services/admin' import { clearAdminSession, getAdminRole, getAdminTokenExpiresAt, getAdminUsername, } from '@/utils/admin-auth' import { formatAdminDateTime } from '@/utils/admin-time' const SIDEBAR_COLLAPSED_KEY = 'react-admin-sidebar-collapsed' export default function AdminLayout() { const navigate = useNavigate() const location = useLocation() const { message } = App.useApp() const [collapsed, setCollapsed] = useState( () => window.localStorage.getItem(SIDEBAR_COLLAPSED_KEY) === '1', ) const role = getAdminRole() const username = getAdminUsername() || 'admin' const expiresAt = getAdminTokenExpiresAt() const menuItems = useMemo(() => { const operationItems: MenuProps['items'] = [ { key: '/admin/dashboard', icon: , label: '概览' }, { key: '/admin/orders', icon: , label: '订单' }, { key: '/admin/tasks', icon: , label: '任务' }, { key: '/admin/cloudtentacles-records', icon: , label: '发货记录' }, ] const items: MenuProps['items'] = [ { key: 'group-operation', type: 'group', label: '运营', children: operationItems, }, ] if (role === 'admin') { items.push( { key: 'group-config', type: 'group', label: '配置', children: [ { key: '/admin/users', icon: , label: '后台用户' }, { key: '/admin/platform-shops', icon: , label: '平台配置' }, { key: '/admin/platform-fulfillment', icon: , label: '履约配置' }, ], }, { key: 'group-system', type: 'group', label: '系统', children: [{ key: '/admin/audit-logs', icon: , label: '审计日志' }], }, ) } return items }, [role]) async function submitLogout() { try { await logoutAdmin() } catch { // 后端退出是无状态的,本地清理优先。 } finally { clearAdminSession() message.success('已退出后台') navigate('/admin/login', { replace: true }) } } function toggleCollapsed() { const next = !collapsed setCollapsed(next) window.localStorage.setItem(SIDEBAR_COLLAPSED_KEY, next ? '1' : '0') } const selectedKey = resolveSelectedKey(location.pathname) return (
{collapsed ? 'OS' : '运营后台'} {!collapsed ? 订单与交付管理 : null}
{!collapsed ? (
{username} {roleLabel(role)} {expiresAt ? ( {formatAdminDateTime(expiresAt)} 到期 ) : null}
) : null} navigate(String(key))} />
) } function resolveSelectedKey(pathname: string) { if (pathname.startsWith('/admin/orders')) return '/admin/orders' if (pathname.startsWith('/admin/tasks')) return '/admin/tasks' if (pathname === '/admin/platform-shops') return '/admin/platform-shops' return pathname } function roleLabel(role: string) { if (role === 'admin') return '管理员' if (role === 'operator') return '普通运营' return '客服' }