This commit is contained in:
2026-06-26 16:21:26 +08:00
parent 30e263e7d7
commit 14dae263bb
8 changed files with 247 additions and 258 deletions
+89 -110
View File
@@ -138,10 +138,8 @@ const AppLayout: React.FC = () => {
const [menuItems, setMenuItems] = useState<MenuConfig[]>([]);
const [rechargeOptions, setRechargeOptions] = useState<any[]>([]);
const [collapsedGroups, setCollapsedGroups] = useState<Record<string, boolean>>({});
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
const [mobileDrawerParent, setMobileDrawerParent] = useState<MenuConfig | null>(null);
const [mobileDrawerChildren, setMobileDrawerChildren] = useState<MenuConfig[]>([]);
const [unreadCount, setUnreadCount] = useState(0);
const [mobileExpandedParent, setMobileExpandedParent] = useState<MenuConfig | null>(null);
const [siteName, setSiteName] = useState(() => {
const cached = localStorage.getItem('siteInfo');
const name = cached ? JSON.parse(cached).siteName || '' : '';
@@ -286,6 +284,24 @@ const AppLayout: React.FC = () => {
const selectedKey = location.pathname.startsWith('/records') ? '/records' : location.pathname;
const sidebarW = SIDEBAR_W;
// Compute menu hierarchy (used by both desktop sidebar and mobile nav)
const childMap: Record<string, MenuConfig[]> = {};
menuItems.forEach(m => {
const pid = m.parent_id ?? m.parentId;
if (pid) {
if (!childMap[pid]) childMap[pid] = [];
childMap[pid].push(m);
}
});
const topLevelItems = menuItems.filter(m => !(m.parent_id ?? m.parentId));
topLevelItems.sort((a, b) => {
const orderA = typeof a.sortOrder === 'number' ? a.sortOrder : Infinity;
const orderB = typeof b.sortOrder === 'number' ? b.sortOrder : Infinity;
return orderA - orderB;
});
// For mobile nav: show top-level items that either have a path or have children
const topLevelForNav = topLevelItems.filter(m => m.path || (childMap[m.id] && childMap[m.id].length > 0));
const userMenuItems = [
{ key: 'profile', icon: <UserOutlined />, label: `账号: ${user?.username}`, disabled: true },
{ key: 'credits', icon: <WalletOutlined style={{ color: '#c9a96e' }} />, label: `积分: ${user?.credits ?? 0}`, disabled: true },
@@ -309,35 +325,6 @@ const AppLayout: React.FC = () => {
else if (key === 'orderRecords') { navigate('/user-center?tab=orders'); }
};
// Mobile two-level menu: open drawer with children
const handleMobileNavClick = (item: MenuConfig) => {
const children = menuItems.filter(m => (m.parent_id ?? m.parentId) === item.id);
if (children.length > 0) {
setMobileDrawerParent(item);
setMobileDrawerChildren(children.sort((a, b) => {
const orderA = typeof a.sortOrder === 'number' ? a.sortOrder : Infinity;
const orderB = typeof b.sortOrder === 'number' ? b.sortOrder : Infinity;
return orderA - orderB;
}));
setMobileDrawerOpen(true);
} else if (item.path) {
navigate(item.path);
}
};
const handleMobileChildClick = (item: MenuConfig) => {
if (item.path) {
setMobileDrawerOpen(false);
setMobileDrawerParent(null);
navigate(item.path);
}
};
const closeMobileDrawer = () => {
setMobileDrawerOpen(false);
setMobileDrawerParent(null);
};
const handleChangePwd = async () => {
try {
await pwdForm.validateFields();
@@ -493,23 +480,7 @@ const AppLayout: React.FC = () => {
{/* Menu */}
<div style={{ flex: 1, padding: '8px 8px', overflow: 'auto' }}>
{(() => {
const childMap: Record<string, MenuConfig[]> = {};
menuItems.forEach(m => {
const pid = m.parent_id ?? m.parentId;
if (pid) {
if (!childMap[pid]) childMap[pid] = [];
childMap[pid].push(m);
}
});
const topLevelItems = menuItems.filter(m => !(m.parent_id ?? m.parentId));
topLevelItems.sort((a, b) => {
const orderA = typeof a.sortOrder === 'number' ? a.sortOrder : Infinity;
const orderB = typeof b.sortOrder === 'number' ? b.sortOrder : Infinity;
return orderA - orderB;
});
// childMap, topLevelItems are computed at component level above
const items: React.ReactNode[] = [];
const renderMenuItem = (item: MenuConfig, depth: number = 0) => {
@@ -742,69 +713,77 @@ const AppLayout: React.FC = () => {
</div>
</div>
{/* Mobile Bottom Nav */}
<div className="mobile-bottom-nav">
{menuItems.filter((item) => {
if ((item.menu_type ?? item.menuType) === 'group') return false;
// Only show top-level items (no parent)
if (item.parent_id || item.parentId) return false;
return true;
}).sort((a, b) => {
const orderA = typeof a.sortOrder === 'number' ? a.sortOrder : Infinity;
const orderB = typeof b.sortOrder === 'number' ? b.sortOrder : Infinity;
return orderA - orderB;
}).map((item) => {
const isActive = item.path === selectedKey || menuItems.some(m =>
(m.parent_id ?? m.parentId) === item.id && m.path === selectedKey
);
return (
<div key={item.id}
className={`nav-item ${isActive ? 'active' : ''}`}
onClick={() => handleMobileNavClick(item)}>
<span className="nav-icon">{iconMap[item.icon] || <HomeOutlined />}</span>
<span>{item.label}</span>
{/* Mobile Bottom Nav - Two Level */}
<div className="mobile-bottom-nav-wrapper">
{/* Sub-menu panel (level-2) */}
{mobileExpandedParent && (
<div className="mobile-submenu-panel">
<div className="mobile-submenu-header" onClick={() => setMobileExpandedParent(null)}>
<LeftOutlined style={{ fontSize: 16, color: '#6366f1' }} />
<span className="mobile-submenu-title">{mobileExpandedParent.label}</span>
</div>
);
})}
<div className="nav-item" onClick={handleMobileRecharge}>
<span className="nav-icon"><PlusCircleOutlined /></span>
<span></span>
</div>
<div className="nav-item" onClick={handleLogout}>
<span className="nav-icon"><LogoutOutlined /></span>
<span>退</span>
</div>
</div>
<div className="mobile-submenu-list">
{(childMap[mobileExpandedParent.id] || []).sort((a, b) => {
const orderA = typeof a.sortOrder === 'number' ? a.sortOrder : Infinity;
const orderB = typeof b.sortOrder === 'number' ? b.sortOrder : Infinity;
return orderA - orderB;
}).map(child => (
<div
key={child.id}
className={`mobile-submenu-item ${child.path === selectedKey ? 'active' : ''}`}
onClick={() => {
if (child.path) {
navigate(child.path);
setMobileExpandedParent(null);
}
}}
>
<span className="mobile-submenu-icon">{iconMap[child.icon] || <RightOutlined />}</span>
<span>{child.label}</span>
</div>
))}
</div>
</div>
)}
{/* Mobile Menu Drawer (two-level) */}
<div className={`mobile-menu-overlay ${mobileDrawerOpen ? 'open' : ''}`} onClick={closeMobileDrawer} />
<div className={`mobile-menu-drawer ${mobileDrawerOpen ? 'open' : ''}`}>
<div className="drawer-handle"><span /></div>
<div className="drawer-header">
{mobileDrawerParent && (
<div className="drawer-back" onClick={() => { setMobileDrawerParent(null); }}>
<LeftOutlined />
</div>
)}
<div className="drawer-title">{mobileDrawerParent?.label || '菜单'}</div>
<div style={{ width: 50 }} />
</div>
<div className="drawer-body">
{mobileDrawerChildren.map(item => (
<div
key={item.id}
className="drawer-item"
onClick={() => handleMobileChildClick(item)}
>
<span className="drawer-icon">{iconMap[item.icon] || <RightOutlined />}</span>
<span>{item.label}</span>
</div>
))}
{mobileDrawerChildren.length === 0 && (
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '24px 0' }}>
</div>
)}
{/* Bottom nav (level-1) */}
<div className="mobile-bottom-nav">
{topLevelForNav.map((item) => {
const hasChildren = childMap[item.id] && childMap[item.id].length > 0;
const isActive = mobileExpandedParent?.id === item.id
|| (!mobileExpandedParent && item.path === selectedKey)
|| (!mobileExpandedParent && hasChildren && (childMap[item.id] || []).some(c => c.path === selectedKey));
return (
<div
key={item.id}
className={`nav-item ${isActive ? 'active' : ''}`}
onClick={() => {
if (hasChildren) {
setMobileExpandedParent(prev => prev?.id === item.id ? null : item);
} else if (item.path) {
navigate(item.path);
}
}}
>
<span className="nav-icon">{iconMap[item.icon] || <HomeOutlined />}</span>
<span>{item.label}</span>
{hasChildren && (
<span className="nav-expand-dot" style={{
width: 4, height: 4, borderRadius: '50%',
background: isActive ? '#6366f1' : '#cbd5e1',
}} />
)}
</div>
);
})}
<div className="nav-item" onClick={handleMobileRecharge}>
<span className="nav-icon"><PlusCircleOutlined /></span>
<span></span>
</div>
<div className="nav-item" onClick={handleLogout}>
<span className="nav-icon"><LogoutOutlined /></span>
<span>退</span>
</div>
</div>
</div>