feat(react-web-next): 新增.editorconfig和.prettierrc配置文件,添加public/.gitkeep占位文件
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import { Space, message, Avatar, Dropdown } from 'antd';
|
||||
import {
|
||||
LogoutOutlined,
|
||||
UserOutlined,
|
||||
SettingOutlined,
|
||||
DashboardOutlined,
|
||||
AppstoreOutlined,
|
||||
FileTextOutlined,
|
||||
TeamOutlined,
|
||||
ToolOutlined,
|
||||
BarChartOutlined,
|
||||
CloudServerOutlined,
|
||||
BugOutlined,
|
||||
SearchOutlined,
|
||||
MessageOutlined,
|
||||
FolderOutlined,
|
||||
SafetyOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { Link, useLocation, history } from 'umi';
|
||||
import ProLayout, { MenuDataItem } from '@ant-design/pro-layout';
|
||||
import { fetchMenuData, outLogin } from '@/services/login';
|
||||
import { useTheme } from '@/theme';
|
||||
import { ThemeSwitch } from '@/components/ui';
|
||||
|
||||
// 后端菜单节点(兼容 children 与 subMenus)
|
||||
type MenuNode = {
|
||||
id?: number | string;
|
||||
name?: string;
|
||||
url?: string;
|
||||
path?: string;
|
||||
css?: string;
|
||||
children?: MenuNode[];
|
||||
subMenus?: MenuNode[];
|
||||
};
|
||||
|
||||
const BasicLayout: React.FC = (props: any) => {
|
||||
const location = useLocation();
|
||||
const { mode, toggle } = useTheme();
|
||||
const [menus, setMenus] = useState<MenuNode[]>([]);
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
(async () => {
|
||||
try {
|
||||
const list = await fetchMenuData();
|
||||
if (mounted) {
|
||||
setMenus(Array.isArray(list) ? (list as MenuNode[]) : []);
|
||||
setLoaded(true);
|
||||
}
|
||||
} catch {
|
||||
if (mounted) setLoaded(true);
|
||||
}
|
||||
})();
|
||||
return () => { mounted = false; };
|
||||
}, []);
|
||||
|
||||
// 将后端菜单转换为 ProLayout 菜单(对齐 react-web 映射规则)
|
||||
const menuDataRender = useMemo(() => {
|
||||
const mapPath = (m: MenuNode): string | undefined => {
|
||||
const cand = (m.path || m.url || '').trim();
|
||||
if (!cand) return undefined;
|
||||
const lower = cand.toLowerCase();
|
||||
if (lower.startsWith('http')) return cand; // 外链
|
||||
if (lower.endsWith('.html')) return cand.startsWith('/') ? cand : `/${cand}`; // 仅 .html 可点击
|
||||
if (lower === '/welcome' || lower === 'welcome') return '/welcome';
|
||||
return undefined; // 其余作为分组,仅展开不跳转
|
||||
};
|
||||
|
||||
const getIcon = (p?: string) => {
|
||||
const s = (p || '').toLowerCase();
|
||||
if (s.includes('system')) return <SettingOutlined />;
|
||||
if (s.includes('order')) return <AppstoreOutlined />;
|
||||
if (s.includes('device')) return <CloudServerOutlined />;
|
||||
if (s.includes('log')) return <BugOutlined />;
|
||||
if (s.includes('search')) return <SearchOutlined />;
|
||||
if (s.includes('message')) return <MessageOutlined />;
|
||||
if (s.includes('file')) return <FolderOutlined />;
|
||||
if (s.includes('user')) return <TeamOutlined />;
|
||||
if (s.includes('welcome')) return <DashboardOutlined />;
|
||||
return <FileTextOutlined />;
|
||||
};
|
||||
|
||||
const normalize = (menu: MenuNode, index: number, parentKey = ''): MenuDataItem => {
|
||||
const stableKey =
|
||||
menu.id ||
|
||||
(menu.url && menu.url !== 'javascript:;' ? menu.url : '') ||
|
||||
(menu.path && menu.path !== 'javascript:;' ? menu.path : '') ||
|
||||
`${parentKey}-${menu.name}-${index}`;
|
||||
const keyStr = String(stableKey);
|
||||
|
||||
const path = mapPath(menu);
|
||||
const childrenList: MenuNode[] = (menu.children && menu.children.length ? menu.children : (menu.subMenus || [])) || [];
|
||||
|
||||
const item: MenuDataItem = {
|
||||
key: keyStr,
|
||||
name: menu.name || `菜单项${index + 1}`,
|
||||
path,
|
||||
icon: getIcon(path),
|
||||
children: childrenList.map((child: MenuNode, i: number) => normalize(child, i, keyStr)),
|
||||
};
|
||||
return item;
|
||||
};
|
||||
|
||||
// 首次未加载完毕前不渲染默认菜单,避免覆盖接口菜单
|
||||
if (!loaded) return [];
|
||||
const list = menus && menus.length ? menus : [{ name: '欢迎页', url: '/welcome', id: 'welcome-default' }];
|
||||
return list.map((m, i) => normalize(m, i));
|
||||
}, [menus, mode, loaded]);
|
||||
|
||||
// 右上角操作区
|
||||
const rightContentRender = () => (
|
||||
<Space size="middle">
|
||||
<ThemeSwitch mode={mode} onToggle={toggle} />
|
||||
<Dropdown
|
||||
menu={{
|
||||
items: [
|
||||
{ key: 'profile', icon: <UserOutlined />, label: '个人中心' },
|
||||
{ key: 'settings', icon: <SettingOutlined />, label: '系统设置' },
|
||||
{ type: 'divider' as any },
|
||||
{
|
||||
key: 'logout',
|
||||
icon: <LogoutOutlined />,
|
||||
label: '退出登录',
|
||||
onClick: async () => {
|
||||
try { await outLogin(); } catch {}
|
||||
try {
|
||||
sessionStorage.removeItem('access_token');
|
||||
sessionStorage.removeItem('current_user');
|
||||
} catch {}
|
||||
message.success('已退出登录');
|
||||
history.push('/user/login');
|
||||
},
|
||||
},
|
||||
],
|
||||
}}
|
||||
placement="bottomRight"
|
||||
>
|
||||
<div style={{ cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
<Avatar size="small" icon={<UserOutlined />} />
|
||||
<span style={{ color: 'var(--text-primary)' }}>管理员</span>
|
||||
</div>
|
||||
</Dropdown>
|
||||
</Space>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="page-enter">
|
||||
<ProLayout
|
||||
key="go-fly-pro-layout"
|
||||
title="Go-Fly Admin"
|
||||
logo={false}
|
||||
location={location}
|
||||
route={{ routes: menuDataRender }}
|
||||
navTheme={mode === 'dark' ? 'realDark' : 'light'}
|
||||
colorPrimary="var(--primary-color)"
|
||||
layout="mix"
|
||||
contentWidth="Fluid"
|
||||
fixedHeader
|
||||
fixSiderbar
|
||||
splitMenus={false}
|
||||
siderWidth={256}
|
||||
rightContentRender={rightContentRender}
|
||||
menuDataRender={() => menuDataRender}
|
||||
menuItemRender={(menuItemProps, defaultDom) => {
|
||||
if (menuItemProps.isUrl || !menuItemProps.path) return defaultDom;
|
||||
const path = String(menuItemProps.path);
|
||||
if (path === 'javascript:;' || path === '#' || path.startsWith('menu-item-')) {
|
||||
return <span className="menu-enter">{defaultDom}</span>;
|
||||
}
|
||||
return (
|
||||
<Link to={path} className="menu-enter">
|
||||
{defaultDom}
|
||||
</Link>
|
||||
);
|
||||
}}
|
||||
headerTitleRender={(logo, title) => (
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 12, fontWeight: 600 }}>
|
||||
<div
|
||||
style={{
|
||||
width: 32,
|
||||
height: 32,
|
||||
background: 'var(--tech-gradient)',
|
||||
borderRadius: '8px',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
color: 'var(--logo-initial-color, #0f5132)',
|
||||
}}
|
||||
>
|
||||
G
|
||||
</div>
|
||||
<span style={{ fontSize: 18, color: 'var(--text-primary)', fontWeight: 700 }}>{title}</span>
|
||||
</div>
|
||||
)}
|
||||
token={{
|
||||
sider: {
|
||||
// 左侧侧边栏背景使用更柔和的浅色,避免纯白突兀
|
||||
colorMenuBackground: 'var(--sider-bg)',
|
||||
colorBgMenuItemSelected: 'var(--primary-color)',
|
||||
colorTextMenuSelected: '#ffffff',
|
||||
colorTextMenu: 'var(--text-secondary)',
|
||||
colorBgMenuItemHover: 'var(--bg-glass)',
|
||||
},
|
||||
header: {
|
||||
// 顶部导航背景采用柔和渐变
|
||||
colorBgHeader: 'var(--header-bg)',
|
||||
colorHeaderTitle: 'var(--text-primary)',
|
||||
colorTextRightActionsItem: 'var(--text-secondary)',
|
||||
},
|
||||
}}
|
||||
>
|
||||
<div className="layout-content-wrapper" style={{ margin: 0, borderRadius: 0, minHeight: '100%' }}>
|
||||
{props.children}
|
||||
</div>
|
||||
</ProLayout>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default BasicLayout;
|
||||
Reference in New Issue
Block a user