index.tsx
12.3 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 工作台 = 运营驾驶舱首页。
// 真实数据只来自后端分页接口的 total(待我审批 / 我的提交 / 在架门店),按当前账号权限过滤;
// 不展示任何伪造的 KPI / 趋势 / 分布。所有入口同样按权限过滤后展示。
import { useNavigate } from 'react-router-dom';
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import {
AuditOutlined,
ShopOutlined,
FileDoneOutlined,
UploadOutlined,
TeamOutlined,
UserOutlined,
SafetyOutlined,
ArrowRightOutlined,
CheckCircleOutlined,
DownloadOutlined,
} from '@ant-design/icons';
import type { KeyboardEvent, ReactNode } from 'react';
import { http } from '@/api/core';
import { UButton } from '@uj/ui';
import { useAppStore } from '@/store/useAppStore';
import { useAccess, type ResourceKey } from '@/access';
/** 后端分页信封只取 total,不引入子应用 DTO 类型(主壳不依赖子应用类型)。 */
interface CountVO {
total: number;
}
/** 按时段给问候语 */
function greeting(): string {
const h = new Date().getHours();
if (h < 6) return '凌晨好';
if (h < 12) return '上午好';
if (h < 14) return '中午好';
if (h < 18) return '下午好';
return '晚上好';
}
function todayText(): string {
return new Date().toLocaleDateString('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
});
}
interface QuickLink {
path: string;
label: string;
desc: string;
icon: ReactNode;
resource: ResourceKey;
}
interface QuickGroup {
title: string;
items: QuickLink[];
}
// 快捷入口按业务域分组;展示与否由当前账号权限决定(can)。
const QUICK_GROUPS: QuickGroup[] = [
{
title: '门店上架',
items: [
{
path: '/store/import',
label: '导入门店信息',
desc: '下载模板、上传并校验,确认后提交审批',
icon: <UploadOutlined />,
resource: 'store:import',
},
{
path: '/store/approval',
label: '门店审批',
desc: '审核待上架门店,通过或打回',
icon: <AuditOutlined />,
resource: 'store:approve',
},
{
path: '/store/listing',
label: '门店列表',
desc: '查看、筛选与维护已上架门店',
icon: <ShopOutlined />,
resource: 'store:list',
},
],
},
{
title: '系统设置',
items: [
{
path: '/system/role',
label: '角色管理',
desc: '配置角色权限与菜单可见范围',
icon: <TeamOutlined />,
resource: 'system:role',
},
{
path: '/system/user',
label: '账号管理',
desc: '维护系统账号、重置密码与启停',
icon: <UserOutlined />,
resource: 'system:user',
},
],
},
];
type StatTint = 'info' | 'success' | 'neutral';
interface StatDef {
key: 'pending' | 'mine' | 'listing';
label: string;
icon: ReactNode;
path: string;
resource: ResourceKey;
url: string;
tint: StatTint;
}
// 三张真实计数卡片:拉对应分页接口 total。权限门控,无权限不渲染。
const STATS: StatDef[] = [
{
key: 'pending',
label: '待我审批',
icon: <AuditOutlined />,
path: '/store/approval',
resource: 'store:approve',
url: '/store/approval/pending',
tint: 'info',
},
{
key: 'mine',
label: '我的提交',
icon: <FileDoneOutlined />,
path: '/store/approval?tab=mine',
resource: 'store:approve',
url: '/store/approval/mine',
tint: 'neutral',
},
{
key: 'listing',
label: '在架门店',
icon: <ShopOutlined />,
path: '/store/listing',
resource: 'store:list',
url: '/store/listing/page',
tint: 'success',
},
];
function useCount(url: string, enabled: boolean): UseQueryResult<CountVO> {
// 注意:http 在 http.ts 末尾被 `as unknown as AxiosInstance` 强制成 axios 形状,
// 故 TS 视 http.get<T> 返回 Promise<AxiosResponse<T>>;但运行时 api-core 已解包信封、直接返回内层 data。
// 这里按「运行时返回内层 CountVO」取数,转型封住类型(与 ApiClient.call 的 .then(res => res.data) 同源)。
return useQuery<CountVO>({
queryKey: ['dashboard', url],
queryFn: () =>
http.get<CountVO>(url, { params: { current: 1, size: 1 } }) as unknown as Promise<CountVO>,
enabled,
staleTime: 30_000,
});
}
/** 可点击卡片:div 承载 role=button,避开 button 嵌套;Enter / Space 触发跳转。 */
function cardActivator(navigate: (p: string) => void, path: string) {
return {
role: 'button',
tabIndex: 0,
onClick: () => navigate(path),
onKeyDown: (e: KeyboardEvent<HTMLDivElement>) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
navigate(path);
}
},
};
}
// 上架流程指引:如实描述四步,不链接到具体计数,仅作新用户引导。
const ONBOARD_STEPS: { title: string; desc: string; icon: ReactNode }[] = [
{ title: '下载模板', desc: '获取标准上架模板(V1.0.1,21 列)', icon: <DownloadOutlined /> },
{ title: '上传校验', desc: '上传文件,系统校验格式与业务规则', icon: <UploadOutlined /> },
{ title: '确认提交', desc: '核对预览,提交进入审批流程', icon: <FileDoneOutlined /> },
{ title: '审批上架', desc: '商务 / 财务依次审批,全部通过生效', icon: <CheckCircleOutlined /> },
];
export default function Dashboard() {
const navigate = useNavigate();
const { currentUser } = useAppStore();
const { can, role, roleCodes } = useAccess();
// 三个计数查询固定顺序调用(常量数组,hook 顺序稳定);enabled 跟随权限。
const pending = useCount('/store/approval/pending', can('store:approve'));
const mine = useCount('/store/approval/mine', can('store:approve'));
const listing = useCount('/store/listing/page', can('store:list'));
const statQuery: Record<StatDef['key'], UseQueryResult<CountVO>> = { pending, mine, listing };
const roleText = roleCodes.length ? roleCodes.join(' / ') : role;
const visibleStats = STATS.filter((s) => can(s.resource));
const visibleGroups = QUICK_GROUPS.map((g) => ({
...g,
items: g.items.filter((i) => can(i.resource)),
})).filter((g) => g.items.length > 0);
return (
<div className="uj-deck">
{/* 报头:问候 + 身份,左缘品牌光楔点睛 */}
<section className="uj-deck-hero" aria-label="欢迎">
<div className="uj-deck-hero-main">
<div className="uj-deck-brand">
<span className="uj-deck-brand-mark" aria-hidden />
优加门店上架平台
</div>
<h1 className="uj-deck-greeting">
{greeting()},{currentUser.name || '欢迎回来'}
</h1>
<p className="uj-deck-sub">{todayText()} · 欢迎回到优加门店上架系统</p>
</div>
<div className="uj-deck-meta">
<div className="uj-deck-chip">
<span className="uj-deck-chip-label">账号</span>
<span className="uj-deck-chip-value">{currentUser.account || '-'}</span>
</div>
<div className="uj-deck-chip">
<span className="uj-deck-chip-label">角色</span>
<span className="uj-deck-chip-value">{roleText}</span>
</div>
</div>
</section>
{/* 运营概览:真实计数 */}
{visibleStats.length > 0 && (
<section className="uj-deck-stats" aria-label="运营概览">
{visibleStats.map((s) => {
const q = statQuery[s.key];
return (
<div
key={s.key}
className={`uj-deck-stat uj-deck-stat--${s.tint}`}
{...cardActivator(navigate, s.path)}
>
<div className="uj-deck-stat-top">
<span className="uj-deck-stat-ico" aria-hidden>
{s.icon}
</span>
<span className="uj-deck-stat-go">
查看 <ArrowRightOutlined />
</span>
</div>
<div className="uj-deck-stat-label">{s.label}</div>
{q.isError ? (
<>
<div className="uj-deck-stat-value is-error">—</div>
<button
type="button"
className="uj-deck-stat-retry"
onClick={(e) => {
e.stopPropagation();
void q.refetch();
}}
>
加载失败,点击重试
</button>
</>
) : (
<div className={`uj-deck-stat-value${q.isPending ? ' is-loading' : ''}`}>
{q.isPending ? '' : (q.data?.total ?? 0)}
</div>
)}
</div>
);
})}
</section>
)}
{/* 主区:快捷开始 + 右栏 */}
<div className="uj-deck-main">
<section className="uj-deck-launch" aria-label="快捷开始">
{visibleGroups.map((g) => (
<div key={g.title}>
<div className="uj-deck-group-title">{g.title}</div>
<div className="uj-deck-quick-grid">
{g.items.map((l) => (
<div
key={l.path}
className="uj-deck-quick"
{...cardActivator(navigate, l.path)}
>
<span className="uj-deck-quick-ico" aria-hidden>
{l.icon}
</span>
<div className="uj-deck-quick-label">{l.label}</div>
<div className="uj-deck-quick-desc">{l.desc}</div>
<div className="uj-deck-quick-go">
进入 <ArrowRightOutlined />
</div>
</div>
))}
</div>
</div>
))}
{visibleGroups.length === 0 && (
<div className="uj-deck-sec">
<div className="uj-deck-sec-desc">暂无可用功能,请联系管理员分配权限。</div>
</div>
)}
</section>
<aside className="uj-deck-rail" aria-label="账户与指引">
{/* 账号与安全 */}
<div className="uj-deck-sec uj-deck-sec--security">
<div className="uj-deck-sec-head">
<span className="uj-deck-sec-icon" aria-hidden>
<SafetyOutlined />
</span>
<div>
<div className="uj-deck-sec-title">账号与安全</div>
<div className="uj-deck-sec-desc">定期更新密码;如使用初始密码登录,请尽快修改。</div>
</div>
</div>
<div className="uj-deck-sec-body" style={{ textAlign: 'right' }}>
<UButton onClick={() => navigate('/profile')}>前往修改</UButton>
</div>
</div>
{/* 上架流程指引 */}
<div className="uj-deck-sec">
<div className="uj-deck-sec-head">
<span className="uj-deck-sec-icon" aria-hidden style={{ background: 'var(--uj-primary-soft, #eef3ff)', color: 'var(--uj-primary, #2b5ce6)' }}>
<UploadOutlined />
</span>
<div>
<div className="uj-deck-sec-title">上架流程指引</div>
<div className="uj-deck-sec-desc">四步完成一家门店上架</div>
</div>
</div>
<div className="uj-deck-sec-body">
<ol className="uj-deck-steps">
{ONBOARD_STEPS.map((step, idx) => (
<li key={step.title} className="uj-deck-step">
<span className="uj-deck-step-rail">
<span className="uj-deck-step-node" aria-hidden>
{idx + 1}
</span>
</span>
<span>
<span className="uj-deck-step-title">{step.title}</span>
<span className="uj-deck-step-desc">{step.desc}</span>
</span>
</li>
))}
</ol>
</div>
</div>
</aside>
</div>
</div>
);
}