BasicLayout.tsx
6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { Layout, Menu, Avatar, Dropdown, Badge, Tooltip, Input, Button } from 'antd';
import { Suspense } from 'react';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import {
DashboardOutlined,
ShopOutlined,
UploadOutlined,
SafetyOutlined,
SearchOutlined,
LogoutOutlined,
UserOutlined,
TeamOutlined,
AuditOutlined,
BellOutlined,
MenuFoldOutlined,
MenuUnfoldOutlined,
QuestionCircleOutlined,
} from '@ant-design/icons';
import { useAccess } from '@/access';
import { useAppStore } from '@/store/useAppStore';
import { brandGradient } from '@/theme/themeConfig';
import { Logo } from '@/components/Logo';
import { Skeleton } from '@uj/ui';
import menuJson from '@/config/menu.json';
import type { ResourceKey } from '@/access';
import { roleLabels } from '@/access';
import type { MenuProps } from 'antd';
const { Header, Sider, Content } = Layout;
/* ---------- 菜单数据结构(与 src/config/menu.json 对应,后续由后台下发替换) ---------- */
interface MenuItemDef {
path: string;
label: string;
icon: string;
resource: ResourceKey;
}
interface MenuGroupDef {
group: string;
children: MenuItemDef[];
}
// 图标注册表:JSON 只存图标名,渲染时查表(新增图标在这里登记即可)
const iconMap: Record<string, React.ReactNode> = {
DashboardOutlined: <DashboardOutlined />,
ShopOutlined: <ShopOutlined />,
UploadOutlined: <UploadOutlined />,
SafetyOutlined: <SafetyOutlined />,
TeamOutlined: <TeamOutlined />,
AuditOutlined: <AuditOutlined />,
UserOutlined: <UserOutlined />,
};
const allGroups = (() => {
const raw = (menuJson as { menus?: unknown }).menus;
if (!Array.isArray(raw)) {
throw new Error('[menu] menu.json 的 menus 必须是数组');
}
return raw as MenuGroupDef[];
})();
export default function BasicLayout() {
const navigate = useNavigate();
const location = useLocation();
const { can, role } = useAccess();
const { collapsed, toggleCollapsed, currentUser, logout } = useAppStore();
// JSON -> antd Menu items(按权限过滤)
const items: MenuProps['items'] = allGroups.map((g) => ({
type: 'group' as const,
label: g.group,
children: g.children
.filter((m) => can(m.resource))
.map((m) => ({
key: m.path,
icon: iconMap[m.icon],
label: m.label,
})),
}));
const flat = allGroups.flatMap((g) => g.children);
const selectedKey =
flat.find((m) => location.pathname.startsWith(m.path))?.path ?? '/store/listing';
const current = flat.find((m) => m.path === selectedKey);
const currentGroup = allGroups.find((g) => g.children.some((m) => m.path === selectedKey));
const onLogout = () => {
logout();
navigate('/login');
};
return (
<Layout style={{ height: '100vh', overflow: 'hidden' }}>
<Sider
collapsed={collapsed}
trigger={null}
width={232}
collapsedWidth={68}
className="uj-sider"
style={{ background: brandGradient }}
>
{/* 品牌区 */}
<div className={`uj-sider-brand${collapsed ? ' is-collapsed' : ''}`}>
<Logo size={34} />
{!collapsed && (
<div className="uj-sider-brand-text">
<span className="t">优加门店上架</span>
<span className="s">Store Launch Platform</span>
</div>
)}
</div>
{/* 菜单区 */}
<div className="uj-sider-menu">
<Menu
theme="dark"
mode="inline"
className="uj-menu"
selectedKeys={[selectedKey]}
items={items}
onClick={({ key }) => navigate(key)}
/>
</div>
</Sider>
<Layout style={{ minWidth: 0 }}>
<Header className="uj-header">
{/* 左:折叠 + 面包屑式页题 */}
<div className="uj-header-left">
<Button
type="text"
aria-label="切换菜单"
icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
onClick={toggleCollapsed}
className="uj-header-fold"
/>
<span style={{ fontSize: 16, fontWeight: 600, color: '#1D2533' }}>
{current?.label ?? '门店列表'}
</span>
{currentGroup && (
<>
<span className="uj-header-crumb-sep">/</span>
<span style={{ fontSize: 13, color: '#8A94A6' }}>{currentGroup.group}</span>
</>
)}
</div>
{/* 中:全局搜索 */}
<div className="uj-header-search">
<Input
prefix={<SearchOutlined style={{ color: '#8A94A6' }} />}
placeholder="搜索门店 / 任务…"
variant="borderless"
allowClear
/>
<span className="uj-header-search-kbd">⌘K</span>
</div>
{/* 右:通知 / 帮助 / 用户 */}
<div className="uj-header-right">
<Badge dot>
<button type="button" className="uj-header-icon-btn" aria-label="通知">
<BellOutlined />
</button>
</Badge>
<Tooltip title="帮助中心">
<button type="button" className="uj-header-icon-btn" aria-label="帮助">
<QuestionCircleOutlined />
</button>
</Tooltip>
<span className="uj-header-divider" />
<Dropdown
placement="bottomRight"
menu={{
items: [
{ key: 'profile', icon: <UserOutlined />, label: '个人资料' },
{ type: 'divider' },
{
key: 'logout',
icon: <LogoutOutlined />,
label: '退出登录',
onClick: onLogout,
},
],
}}
>
<button type="button" className="uj-header-user">
<Avatar
size={32}
style={{ background: 'linear-gradient(135deg,#2B5CE6,#5B8DEF)' }}
icon={<UserOutlined />}
/>
<div className="uj-header-user-text">
<span className="n">{currentUser.name}</span>
<span className="r">{roleLabels[role]}</span>
</div>
</button>
</Dropdown>
</div>
</Header>
<Content
style={{
flex: 1,
minHeight: 0,
overflow: 'auto',
background: '#F4F6FA',
padding: 24,
display: 'flex',
flexDirection: 'column',
}}
>
<Suspense fallback={<Skeleton variant="default" />}>
<Outlet />
</Suspense>
</Content>
</Layout>
</Layout>
);
}