node-resource-utils 0.0.0-stage → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -2
  3. package/lib/sync.js +121 -0
  4. package/package.json +14 -4
  5. package/resources/.cache/ref.b64 +1 -0
  6. package/resources/README.md +97 -0
  7. package/resources/_.env +6 -0
  8. package/resources/_.env.example +25 -0
  9. package/resources/_.gitignore +18 -0
  10. package/resources/_.lintstagedrc.json +9 -0
  11. package/resources/_.npmrc +5 -0
  12. package/resources/_.prettierrc +7 -0
  13. package/resources/commitlint.config.mjs +1 -0
  14. package/resources/content/docs/README.md +63 -0
  15. package/resources/content/docs/faq.md +107 -0
  16. package/resources/content/docs/features/_meta.json +5 -0
  17. package/resources/content/docs/features/ai.md +71 -0
  18. package/resources/content/docs/features/appearance.md +47 -0
  19. package/resources/content/docs/features/feedback.md +79 -0
  20. package/resources/content/docs/features/files.md +74 -0
  21. package/resources/content/docs/features/graph.md +66 -0
  22. package/resources/content/docs/features/inbox.md +61 -0
  23. package/resources/content/docs/guide/_meta.json +5 -0
  24. package/resources/content/docs/guide/account.md +76 -0
  25. package/resources/content/docs/guide/data.md +87 -0
  26. package/resources/content/docs/guide/navigation.md +81 -0
  27. package/resources/content/docs/guide/permissions.md +67 -0
  28. package/resources/content/docs/guide/system.md +92 -0
  29. package/resources/content/docs/quick-start.md +70 -0
  30. package/resources/content/docs/shortcuts.md +69 -0
  31. package/resources/eslint.config.mjs +17 -0
  32. package/resources/next-env.d.ts +6 -0
  33. package/resources/next.config.ts +13 -0
  34. package/resources/postcss.config.mjs +7 -0
  35. package/resources/prisma/org.config.ts +98 -0
  36. package/resources/prisma/schema.prisma +336 -0
  37. package/resources/prisma/seed.ts +721 -0
  38. package/resources/prisma/seedSafe.ts +347 -0
  39. package/resources/prisma.config.ts +14 -0
  40. package/resources/scripts/check-config.mjs +773 -0
  41. package/resources/scripts/lib/schema-graph.mjs +410 -0
  42. package/resources/scripts/patch-next-trace.mjs +86 -0
  43. package/resources/scripts/suggest-relations.mjs +105 -0
  44. package/resources/src/app/(admin)/ai/page.tsx +469 -0
  45. package/resources/src/app/(admin)/dashboard/page.tsx +260 -0
  46. package/resources/src/app/(admin)/demo/employees/page.tsx +508 -0
  47. package/resources/src/app/(admin)/demo/files/page.tsx +451 -0
  48. package/resources/src/app/(admin)/demo/loading/page.tsx +513 -0
  49. package/resources/src/app/(admin)/error.tsx +96 -0
  50. package/resources/src/app/(admin)/feedback/page.tsx +573 -0
  51. package/resources/src/app/(admin)/graph/page.tsx +1224 -0
  52. package/resources/src/app/(admin)/inbox/page.tsx +31 -0
  53. package/resources/src/app/(admin)/layout.tsx +58 -0
  54. package/resources/src/app/(admin)/loading.tsx +31 -0
  55. package/resources/src/app/(admin)/system/depts/page.tsx +415 -0
  56. package/resources/src/app/(admin)/system/logs/page.tsx +215 -0
  57. package/resources/src/app/(admin)/system/menus/page.tsx +403 -0
  58. package/resources/src/app/(admin)/system/roles/page.tsx +433 -0
  59. package/resources/src/app/(admin)/system/users/page.tsx +620 -0
  60. package/resources/src/app/api/account-recovery/route.ts +132 -0
  61. package/resources/src/app/api/ai/act/route.ts +70 -0
  62. package/resources/src/app/api/ai/ask/route.ts +82 -0
  63. package/resources/src/app/api/ai/chat/route.ts +404 -0
  64. package/resources/src/app/api/ai/report/route.ts +164 -0
  65. package/resources/src/app/api/auth/[...nextauth]/route.ts +3 -0
  66. package/resources/src/app/api/auth/change-password/route.ts +57 -0
  67. package/resources/src/app/api/auth/forgot/route.ts +98 -0
  68. package/resources/src/app/api/auth/register/route.ts +95 -0
  69. package/resources/src/app/api/auth/register-config/route.ts +51 -0
  70. package/resources/src/app/api/badges/route.ts +39 -0
  71. package/resources/src/app/api/depts/[id]/route.ts +145 -0
  72. package/resources/src/app/api/depts/route.ts +126 -0
  73. package/resources/src/app/api/docs/export/route.ts +87 -0
  74. package/resources/src/app/api/docs/parse/route.ts +57 -0
  75. package/resources/src/app/api/docs/search/route.ts +22 -0
  76. package/resources/src/app/api/employees/[id]/route.ts +147 -0
  77. package/resources/src/app/api/employees/export/route.ts +115 -0
  78. package/resources/src/app/api/employees/import/route.ts +205 -0
  79. package/resources/src/app/api/employees/route.ts +149 -0
  80. package/resources/src/app/api/export/route.ts +254 -0
  81. package/resources/src/app/api/favorites/route.ts +56 -0
  82. package/resources/src/app/api/feedback/ai/route.ts +31 -0
  83. package/resources/src/app/api/feedback/approve/route.ts +67 -0
  84. package/resources/src/app/api/feedback/report/route.ts +213 -0
  85. package/resources/src/app/api/feedback/route.ts +246 -0
  86. package/resources/src/app/api/files/[name]/route.ts +82 -0
  87. package/resources/src/app/api/graph/focus/route.ts +75 -0
  88. package/resources/src/app/api/graph/node/move/route.ts +48 -0
  89. package/resources/src/app/api/graph/node/route.ts +274 -0
  90. package/resources/src/app/api/graph/route.ts +272 -0
  91. package/resources/src/app/api/health/route.ts +11 -0
  92. package/resources/src/app/api/inbox/read/route.ts +36 -0
  93. package/resources/src/app/api/inbox/summary/route.ts +15 -0
  94. package/resources/src/app/api/logs/route.ts +82 -0
  95. package/resources/src/app/api/menus/[id]/route.ts +132 -0
  96. package/resources/src/app/api/menus/mine/route.ts +23 -0
  97. package/resources/src/app/api/menus/route.ts +89 -0
  98. package/resources/src/app/api/roles/[id]/menus/route.ts +73 -0
  99. package/resources/src/app/api/roles/[id]/route.ts +96 -0
  100. package/resources/src/app/api/roles/route.ts +114 -0
  101. package/resources/src/app/api/search/route.ts +22 -0
  102. package/resources/src/app/api/stats/route.ts +92 -0
  103. package/resources/src/app/api/upload/route.ts +38 -0
  104. package/resources/src/app/api/users/[id]/password/route.ts +55 -0
  105. package/resources/src/app/api/users/[id]/route.ts +145 -0
  106. package/resources/src/app/api/users/route.ts +153 -0
  107. package/resources/src/app/api/visits/route.ts +48 -0
  108. package/resources/src/app/change-password/ChangePasswordForm.tsx +174 -0
  109. package/resources/src/app/change-password/page.tsx +19 -0
  110. package/resources/src/app/docs/[[...slug]]/page.tsx +56 -0
  111. package/resources/src/app/docs/layout.tsx +34 -0
  112. package/resources/src/app/docs/loading.tsx +19 -0
  113. package/resources/src/app/docs/not-found.tsx +25 -0
  114. package/resources/src/app/forgot/page.tsx +392 -0
  115. package/resources/src/app/global-error.tsx +75 -0
  116. package/resources/src/app/globals.css +235 -0
  117. package/resources/src/app/layout.tsx +44 -0
  118. package/resources/src/app/login/page.tsx +267 -0
  119. package/resources/src/app/page.tsx +14 -0
  120. package/resources/src/app/register/page.tsx +237 -0
  121. package/resources/src/auth.ts +215 -0
  122. package/resources/src/components/AiPanel.tsx +970 -0
  123. package/resources/src/components/AuthContext.tsx +82 -0
  124. package/resources/src/components/DataTable.tsx +459 -0
  125. package/resources/src/components/EChart.tsx +140 -0
  126. package/resources/src/components/FeedbackAiModal.tsx +476 -0
  127. package/resources/src/components/FeedbackReportModal.tsx +479 -0
  128. package/resources/src/components/Markdown.tsx +532 -0
  129. package/resources/src/components/OntologyGraph.tsx +1196 -0
  130. package/resources/src/components/Providers.tsx +99 -0
  131. package/resources/src/components/VisitTracker.tsx +89 -0
  132. package/resources/src/components/admin/AdminShell.tsx +749 -0
  133. package/resources/src/components/admin/CountBadge.tsx +67 -0
  134. package/resources/src/components/admin/GlobalSearch.tsx +509 -0
  135. package/resources/src/components/admin/InboxBell.tsx +78 -0
  136. package/resources/src/components/admin/VisitStats.tsx +145 -0
  137. package/resources/src/components/admin/menu-icons.tsx +56 -0
  138. package/resources/src/components/ai/AiInputForm.tsx +351 -0
  139. package/resources/src/components/appearance/AppearanceDrawer.tsx +268 -0
  140. package/resources/src/components/appearance/AppearanceProvider.tsx +160 -0
  141. package/resources/src/components/docs/DocArticle.tsx +106 -0
  142. package/resources/src/components/docs/DocToc.tsx +100 -0
  143. package/resources/src/components/docs/DocsChrome.tsx +259 -0
  144. package/resources/src/components/docs/DocsHome.tsx +209 -0
  145. package/resources/src/components/docs/DocsSearch.tsx +365 -0
  146. package/resources/src/components/docs/docs.css +1458 -0
  147. package/resources/src/components/feedback/AsyncButton.tsx +108 -0
  148. package/resources/src/components/feedback/LoadingOverlay.tsx +125 -0
  149. package/resources/src/components/feedback/PressableCard.tsx +92 -0
  150. package/resources/src/components/feedback/README.md +97 -0
  151. package/resources/src/components/feedback/RouteProgress.tsx +108 -0
  152. package/resources/src/components/feedback/Skeleton.tsx +249 -0
  153. package/resources/src/components/feedback/StatCard.tsx +70 -0
  154. package/resources/src/components/feedback/TaskHint.tsx +153 -0
  155. package/resources/src/components/feedback/index.ts +93 -0
  156. package/resources/src/components/feedback/motion.css +204 -0
  157. package/resources/src/components/feedback/motion.ts +63 -0
  158. package/resources/src/components/feedback/useCountUp.ts +110 -0
  159. package/resources/src/components/feedback/usePending.ts +209 -0
  160. package/resources/src/components/feedback/useRowFlash.ts +40 -0
  161. package/resources/src/components/inbox/InboxView.tsx +409 -0
  162. package/resources/src/components/ui/button.tsx +35 -0
  163. package/resources/src/components/ui/card.tsx +37 -0
  164. package/resources/src/components/ui/input.tsx +19 -0
  165. package/resources/src/lib/ai.ts +310 -0
  166. package/resources/src/lib/aiContext.ts +357 -0
  167. package/resources/src/lib/aiForm.test.ts +191 -0
  168. package/resources/src/lib/aiForm.ts +532 -0
  169. package/resources/src/lib/aiFormTypes.ts +83 -0
  170. package/resources/src/lib/aiToken.ts +72 -0
  171. package/resources/src/lib/aiTools.ts +823 -0
  172. package/resources/src/lib/api.ts +160 -0
  173. package/resources/src/lib/appInfo.ts +128 -0
  174. package/resources/src/lib/appearance.ts +643 -0
  175. package/resources/src/lib/badge-count.ts +146 -0
  176. package/resources/src/lib/badges.test.ts +58 -0
  177. package/resources/src/lib/badges.ts +79 -0
  178. package/resources/src/lib/brand-mark.test.ts +87 -0
  179. package/resources/src/lib/brand-mark.ts +45 -0
  180. package/resources/src/lib/docs-shared.test.ts +148 -0
  181. package/resources/src/lib/docs-shared.ts +422 -0
  182. package/resources/src/lib/docs.ts +381 -0
  183. package/resources/src/lib/err-text.ts +21 -0
  184. package/resources/src/lib/excel.ts +152 -0
  185. package/resources/src/lib/favorite-path.ts +60 -0
  186. package/resources/src/lib/favorites.ts +59 -0
  187. package/resources/src/lib/features.ts +69 -0
  188. package/resources/src/lib/feedback-access.ts +17 -0
  189. package/resources/src/lib/feedbackAi.ts +1621 -0
  190. package/resources/src/lib/formRules.ts +76 -0
  191. package/resources/src/lib/format.ts +52 -0
  192. package/resources/src/lib/graph-cascade.ts +304 -0
  193. package/resources/src/lib/graph-focus.ts +301 -0
  194. package/resources/src/lib/graph-link.ts +17 -0
  195. package/resources/src/lib/graph-node-write.ts +150 -0
  196. package/resources/src/lib/graph-schema.ts +475 -0
  197. package/resources/src/lib/graph-scope.test.ts +46 -0
  198. package/resources/src/lib/graph-scope.ts +28 -0
  199. package/resources/src/lib/http.ts +157 -0
  200. package/resources/src/lib/inbox-types.ts +57 -0
  201. package/resources/src/lib/inbox.ts +55 -0
  202. package/resources/src/lib/menu-service.ts +204 -0
  203. package/resources/src/lib/notifications.ts +113 -0
  204. package/resources/src/lib/office.ts +531 -0
  205. package/resources/src/lib/ontology.ts +277 -0
  206. package/resources/src/lib/openaiCompat.ts +257 -0
  207. package/resources/src/lib/pending.ts +98 -0
  208. package/resources/src/lib/prisma.ts +30 -0
  209. package/resources/src/lib/rbac.test.ts +106 -0
  210. package/resources/src/lib/rbac.ts +185 -0
  211. package/resources/src/lib/search-data.ts +158 -0
  212. package/resources/src/lib/security-questions.ts +20 -0
  213. package/resources/src/lib/skin-theme.ts +155 -0
  214. package/resources/src/lib/system-config.ts +55 -0
  215. package/resources/src/lib/theme.ts +96 -0
  216. package/resources/src/lib/todos/feedback.ts +86 -0
  217. package/resources/src/lib/todos/index.ts +73 -0
  218. package/resources/src/lib/todos/types.ts +33 -0
  219. package/resources/src/lib/upload.ts +106 -0
  220. package/resources/src/lib/utils.test.ts +9 -0
  221. package/resources/src/lib/utils.ts +6 -0
  222. package/resources/src/lib/validation.test.ts +297 -0
  223. package/resources/src/lib/validation.ts +658 -0
  224. package/resources/src/lib/visit-stats.ts +109 -0
  225. package/resources/src/lib/visits.ts +111 -0
  226. package/resources/src/lib/zodField.ts +44 -0
  227. package/resources/tsconfig.json +41 -0
  228. package/resources/vitest.config.ts +16 -0
  229. package/resources/vitest.setup.ts +1 -0
@@ -0,0 +1,215 @@
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import axios from 'axios';
5
+ import { App, Button, Descriptions, Drawer, Popconfirm, Tag, Typography } from 'antd';
6
+ import type { ColumnsType } from 'antd/es/table';
7
+ import { ClearOutlined, EyeOutlined } from '@ant-design/icons';
8
+ import DataTable from '@/components/DataTable';
9
+ import { useAuth } from '@/components/AuthContext';
10
+ import { fmtDateTime } from '@/lib/format';
11
+ import { fs, MILITARY } from '@/lib/theme';
12
+
13
+ type LogRow = {
14
+ id: string;
15
+ userId: string | null;
16
+ username: string | null;
17
+ module: string;
18
+ action: string;
19
+ method: string | null;
20
+ path: string | null;
21
+ targetId: string | null;
22
+ detail: string | null;
23
+ ip: string | null;
24
+ userAgent: string | null;
25
+ status: number;
26
+ durationMs: number | null;
27
+ createdAt: string;
28
+ };
29
+
30
+ const MODULE_OPTIONS = [
31
+ '用户管理',
32
+ '角色管理',
33
+ '菜单管理',
34
+ '操作日志',
35
+ '员工管理',
36
+ '数据导出',
37
+ '文档处理',
38
+ '文件管理',
39
+ ].map((m) => ({ label: m, value: m }));
40
+
41
+ const ACTION_OPTIONS = ['新增', '修改', '删除', '导出', '导入', '分配权限', '重置密码', '清空', '解析', '生成导出', '上传'].map(
42
+ (a) => ({ label: a, value: a })
43
+ );
44
+
45
+ async function request<T>(method: 'get' | 'post' | 'put' | 'delete', url: string, body?: unknown): Promise<T> {
46
+ const { data } = await axios.request({ method, url, data: body });
47
+ if (!data || data.code !== 0) throw new Error(data?.message || '请求失败');
48
+ return data.data as T;
49
+ }
50
+
51
+ function errMsg(e: unknown): string {
52
+ if (axios.isAxiosError(e)) {
53
+ const d = e.response?.data as { message?: string } | undefined;
54
+ return d?.message || e.message;
55
+ }
56
+ return e instanceof Error ? e.message : String(e);
57
+ }
58
+
59
+ /** detail 存的是 JSON 字符串;解析失败就原样展示,不要因为一条脏日志把抽屉搞崩 */
60
+ function prettyDetail(detail: string | null): string {
61
+ if (!detail) return '(无详情)';
62
+ try {
63
+ return JSON.stringify(JSON.parse(detail), null, 2);
64
+ } catch {
65
+ return detail;
66
+ }
67
+ }
68
+
69
+ export default function LogsPage() {
70
+ const { message, modal } = App.useApp();
71
+ const { hasPerm } = useAuth();
72
+
73
+ const [refreshKey, setRefreshKey] = useState(0);
74
+ const [detail, setDetail] = useState<LogRow | null>(null);
75
+
76
+ const clearAll = async () => {
77
+ try {
78
+ const res = await request<{ deleted: number }>('delete', '/api/logs');
79
+ message.success(`已清空 ${res.deleted} 条日志`);
80
+ setRefreshKey((k) => k + 1);
81
+ } catch (e) {
82
+ message.error(errMsg(e));
83
+ }
84
+ };
85
+
86
+ const columns: ColumnsType<LogRow> = [
87
+ { title: '时间', dataIndex: 'createdAt', width: 180, render: (v: string) => fmtDateTime(v) },
88
+ { title: '操作人', dataIndex: 'username', width: 120, render: (v: string | null) => v || '-' },
89
+ { title: '模块', dataIndex: 'module', width: 120, render: (v: string) => <Tag style={{ background: MILITARY.soft, color: MILITARY.primary, borderColor: MILITARY.softStrong }}>{v}</Tag> },
90
+ { title: '动作', dataIndex: 'action', width: 110 },
91
+ { title: '方法', dataIndex: 'method', width: 90, render: (v: string | null) => v || '-' },
92
+ { title: '路径', dataIndex: 'path', ellipsis: true, render: (v: string | null) => v || '-' },
93
+ { title: 'IP', dataIndex: 'ip', width: 140, render: (v: string | null) => v || '-' },
94
+ {
95
+ title: '结果',
96
+ dataIndex: 'status',
97
+ width: 90,
98
+ render: (v: number) => (v === 1 ? <Tag color="success">成功</Tag> : <Tag color="error">失败</Tag>),
99
+ },
100
+ {
101
+ title: '耗时',
102
+ dataIndex: 'durationMs',
103
+ width: 100,
104
+ render: (v: number | null) => (v === null || v === undefined ? '-' : `${v} ms`),
105
+ },
106
+ {
107
+ title: '操作',
108
+ key: 'action',
109
+ width: 100,
110
+ fixed: 'right',
111
+ render: (_v, row) => (
112
+ <Button type="link" size="small" icon={<EyeOutlined />} onClick={() => setDetail(row)}>
113
+ 详情
114
+ </Button>
115
+ ),
116
+ },
117
+ ];
118
+
119
+ return (
120
+ <>
121
+ <DataTable<LogRow>
122
+ api="/api/logs"
123
+ rowKey="id"
124
+ columns={columns}
125
+ refreshKey={refreshKey}
126
+ scrollX={1400}
127
+ exportable={{ filename: '操作日志.xlsx', module: 'logs' }}
128
+ searchFields={[
129
+ { name: 'logModule', label: '模块', type: 'select', options: MODULE_OPTIONS },
130
+ { name: 'action', label: '动作', type: 'select', options: ACTION_OPTIONS },
131
+ { name: 'dateRange', label: '时间范围', type: 'dateRange' },
132
+ ]}
133
+ toolbar={
134
+ hasPerm('system:log:del') ? (
135
+ <Popconfirm
136
+ title="确认清空全部日志?"
137
+ description="该操作不可恢复。"
138
+ okText="清空"
139
+ cancelText="取消"
140
+ okButtonProps={{ danger: true }}
141
+ onConfirm={clearAll}
142
+ >
143
+ <Button danger icon={<ClearOutlined />}>
144
+ 清空日志
145
+ </Button>
146
+ </Popconfirm>
147
+ ) : null
148
+ }
149
+ />
150
+
151
+ <Drawer
152
+ title="日志详情"
153
+ size={560}
154
+ open={Boolean(detail)}
155
+ onClose={() => setDetail(null)}
156
+ extra={
157
+ <Button
158
+ onClick={() => {
159
+ modal.info({
160
+ title: '原始 User-Agent',
161
+ width: 560,
162
+ content: <div style={{ wordBreak: 'break-all' }}>{detail?.userAgent || '(无)'}</div>,
163
+ });
164
+ }}
165
+ >
166
+ 查看 UA
167
+ </Button>
168
+ }
169
+ >
170
+ {detail ? (
171
+ <>
172
+ <Descriptions column={1} size="small" bordered>
173
+ <Descriptions.Item label="时间">{fmtDateTime(detail.createdAt)}</Descriptions.Item>
174
+ <Descriptions.Item label="操作人">{detail.username || '-'}</Descriptions.Item>
175
+ <Descriptions.Item label="模块 / 动作">
176
+ {detail.module} / {detail.action}
177
+ </Descriptions.Item>
178
+ <Descriptions.Item label="请求">
179
+ {detail.method || '-'} {detail.path || ''}
180
+ </Descriptions.Item>
181
+ <Descriptions.Item label="目标 ID">{detail.targetId || '-'}</Descriptions.Item>
182
+ <Descriptions.Item label="IP">{detail.ip || '-'}</Descriptions.Item>
183
+ <Descriptions.Item label="结果">
184
+ {detail.status === 1 ? '成功' : '失败'}
185
+ {detail.durationMs !== null && detail.durationMs !== undefined
186
+ ? `(${detail.durationMs} ms)`
187
+ : ''}
188
+ </Descriptions.Item>
189
+ </Descriptions>
190
+
191
+ <Typography.Title level={5} style={{ marginTop: 20 }}>
192
+ 变更内容
193
+ </Typography.Title>
194
+ <pre
195
+ style={{
196
+ margin: 0,
197
+ padding: 12,
198
+ // 跟随皮肤:深色皮肤下写死浅灰底会让 JSON 明细变成浅底浅字
199
+ background: MILITARY.soft,
200
+ color: MILITARY.text,
201
+ borderRadius: 6,
202
+ maxHeight: 380,
203
+ overflow: 'auto',
204
+ fontSize: fs(12),
205
+ lineHeight: 1.7,
206
+ }}
207
+ >
208
+ {prettyDetail(detail.detail)}
209
+ </pre>
210
+ </>
211
+ ) : null}
212
+ </Drawer>
213
+ </>
214
+ );
215
+ }
@@ -0,0 +1,403 @@
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import { useQuery } from '@tanstack/react-query';
5
+ import axios from 'axios';
6
+ import {
7
+ App,
8
+ Button,
9
+ Col,
10
+ Form,
11
+ Input,
12
+ InputNumber,
13
+ Modal,
14
+ Popconfirm,
15
+ Row,
16
+ Select,
17
+ Space,
18
+ Table,
19
+ Tag,
20
+ TreeSelect,
21
+ } from 'antd';
22
+ import type { ColumnsType } from 'antd/es/table';
23
+ import { PlusOutlined, ReloadOutlined } from '@ant-design/icons';
24
+ import { useAuth } from '@/components/AuthContext';
25
+ import { BADGE_META, BADGE_SOURCES } from '@/lib/badges';
26
+
27
+ type MenuRow = {
28
+ id: string;
29
+ parentId: string | null;
30
+ name: string;
31
+ path: string | null;
32
+ icon: string | null;
33
+ keywords: string | null;
34
+ badgeKey: string | null;
35
+ type: string;
36
+ permCode: string | null;
37
+ sort: number;
38
+ hidden: number;
39
+ status: number;
40
+ children?: MenuRow[];
41
+ };
42
+
43
+ type TreeSelectNode = { value: string; title: string; children?: TreeSelectNode[] };
44
+
45
+ type FormValues = {
46
+ name: string;
47
+ parentId?: string;
48
+ path?: string;
49
+ icon?: string;
50
+ keywords?: string;
51
+ badgeKey?: string;
52
+ type: 'dir' | 'menu' | 'button';
53
+ permCode?: string;
54
+ sort: number;
55
+ hidden: number;
56
+ status: number;
57
+ };
58
+
59
+ const TYPE_META: Record<string, { text: string; color: string }> = {
60
+ dir: { text: '目录', color: 'blue' },
61
+ menu: { text: '菜单', color: 'green' },
62
+ button: { text: '按钮', color: 'orange' },
63
+ };
64
+
65
+ async function request<T>(
66
+ method: 'get' | 'post' | 'put' | 'delete',
67
+ url: string,
68
+ body?: unknown
69
+ ): Promise<T> {
70
+ const { data } = await axios.request({ method, url, data: body });
71
+ if (!data || data.code !== 0) throw new Error(data?.message || '请求失败');
72
+ return data.data as T;
73
+ }
74
+
75
+ function errMsg(e: unknown): string {
76
+ if (axios.isAxiosError(e)) {
77
+ const d = e.response?.data as { message?: string } | undefined;
78
+ return d?.message || e.message;
79
+ }
80
+ return e instanceof Error ? e.message : String(e);
81
+ }
82
+
83
+ /** antd 会把 children: [] 也当成有子节点渲染出展开箭头,需要拍掉空数组 */
84
+ function normalize(nodes: MenuRow[]): MenuRow[] {
85
+ return nodes.map((n) => {
86
+ const children = n.children?.length ? normalize(n.children) : undefined;
87
+ return { ...n, children };
88
+ });
89
+ }
90
+
91
+ /** 排除自身及其子孙,避免把父级选成自己造成环 */
92
+ function toParentOptions(nodes: MenuRow[], excludeId?: string): TreeSelectNode[] {
93
+ return nodes
94
+ .filter((n) => n.id !== excludeId && n.type !== 'button')
95
+ .map((n) => ({
96
+ value: n.id,
97
+ title: n.name,
98
+ children: n.children?.length ? toParentOptions(n.children, excludeId) : undefined,
99
+ }));
100
+ }
101
+
102
+ export default function MenusPage() {
103
+ const { message } = App.useApp();
104
+ const { hasPerm } = useAuth();
105
+ const canEdit = hasPerm('system:menu:edit');
106
+
107
+ const [open, setOpen] = useState(false);
108
+ const [editing, setEditing] = useState<MenuRow | null>(null);
109
+ const [submitting, setSubmitting] = useState(false);
110
+ const [form] = Form.useForm<FormValues>();
111
+ // 只是为了让「徽标」那一项在选中后立刻显示这条计数源的说明
112
+ const watchedBadgeKey = Form.useWatch('badgeKey', form);
113
+
114
+ const query = useQuery({
115
+ queryKey: ['menus'],
116
+ queryFn: () => request<MenuRow[]>('get', '/api/menus'),
117
+ });
118
+
119
+ const tree = normalize(query.data ?? []);
120
+
121
+ const openCreate = (parent?: MenuRow) => {
122
+ setEditing(null);
123
+ form.resetFields();
124
+ form.setFieldsValue({
125
+ parentId: parent?.id,
126
+ type: parent ? 'menu' : 'dir',
127
+ sort: 0,
128
+ hidden: 0,
129
+ status: 1,
130
+ });
131
+ setOpen(true);
132
+ };
133
+
134
+ const openEdit = (row: MenuRow) => {
135
+ setEditing(row);
136
+ form.setFieldsValue({
137
+ name: row.name,
138
+ parentId: row.parentId ?? undefined,
139
+ path: row.path ?? undefined,
140
+ icon: row.icon ?? undefined,
141
+ keywords: row.keywords ?? undefined,
142
+ badgeKey: row.badgeKey ?? undefined,
143
+ type: (row.type as FormValues['type']) ?? 'menu',
144
+ permCode: row.permCode ?? undefined,
145
+ sort: row.sort,
146
+ hidden: row.hidden,
147
+ status: row.status,
148
+ });
149
+ setOpen(true);
150
+ };
151
+
152
+ const submit = async () => {
153
+ let values: FormValues;
154
+ try {
155
+ values = await form.validateFields();
156
+ } catch {
157
+ return;
158
+ }
159
+ setSubmitting(true);
160
+ try {
161
+ const body = { ...values, parentId: values.parentId || null };
162
+ if (editing) {
163
+ await request('put', `/api/menus/${editing.id}`, body);
164
+ message.success('保存成功');
165
+ } else {
166
+ await request('post', '/api/menus', body);
167
+ message.success('新增成功');
168
+ }
169
+ setOpen(false);
170
+ query.refetch();
171
+ } catch (e) {
172
+ message.error(errMsg(e));
173
+ } finally {
174
+ setSubmitting(false);
175
+ }
176
+ };
177
+
178
+ const remove = async (row: MenuRow) => {
179
+ try {
180
+ await request('delete', `/api/menus/${row.id}`);
181
+ message.success('删除成功');
182
+ query.refetch();
183
+ } catch (e) {
184
+ message.error(errMsg(e));
185
+ }
186
+ };
187
+
188
+ const columns: ColumnsType<MenuRow> = [
189
+ { title: '菜单名称', dataIndex: 'name', width: 220 },
190
+ {
191
+ title: '类型',
192
+ dataIndex: 'type',
193
+ width: 90,
194
+ render: (v: string) => <Tag color={TYPE_META[v]?.color ?? 'default'}>{TYPE_META[v]?.text ?? v}</Tag>,
195
+ },
196
+ { title: '路由地址', dataIndex: 'path', width: 190, render: (v: string | null) => v || '-' },
197
+ {
198
+ title: '搜索别名',
199
+ dataIndex: 'keywords',
200
+ width: 150,
201
+ render: (v: string | null) => v || <span style={{ color: '#bbb' }}>-</span>,
202
+ },
203
+ {
204
+ title: '侧边栏徽标',
205
+ dataIndex: 'badgeKey',
206
+ width: 150,
207
+ render: (v: string | null) =>
208
+ v ? (
209
+ <Tag color="red">{BADGE_META[v]?.label ?? v}</Tag>
210
+ ) : (
211
+ <span style={{ color: '#bbb' }}>-</span>
212
+ ),
213
+ },
214
+ {
215
+ title: '权限标识',
216
+ dataIndex: 'permCode',
217
+ width: 200,
218
+ render: (v: string | null) => (v ? <Tag>{v}</Tag> : '-'),
219
+ },
220
+ { title: '图标', dataIndex: 'icon', width: 170, render: (v: string | null) => v || '-' },
221
+ { title: '排序', dataIndex: 'sort', width: 70 },
222
+ {
223
+ title: '显示',
224
+ dataIndex: 'hidden',
225
+ width: 80,
226
+ render: (v: number) => (v === 1 ? <Tag>隐藏</Tag> : <Tag color="success">显示</Tag>),
227
+ },
228
+ {
229
+ title: '状态',
230
+ dataIndex: 'status',
231
+ width: 80,
232
+ render: (v: number) => (v === 1 ? <Tag color="success">启用</Tag> : <Tag color="default">停用</Tag>),
233
+ },
234
+ {
235
+ title: '操作',
236
+ key: 'action',
237
+ width: 200,
238
+ render: (_v, row) =>
239
+ canEdit ? (
240
+ <Space size={4}>
241
+ {row.type !== 'button' ? (
242
+ <Button type="link" size="small" icon={<PlusOutlined />} onClick={() => openCreate(row)}>
243
+ 子菜单
244
+ </Button>
245
+ ) : null}
246
+ <Button type="link" size="small" onClick={() => openEdit(row)}>
247
+ 编辑
248
+ </Button>
249
+ <Popconfirm
250
+ title="确认删除该菜单?"
251
+ description="存在子菜单时无法删除。"
252
+ okText="删除"
253
+ cancelText="取消"
254
+ okButtonProps={{ danger: true }}
255
+ onConfirm={() => remove(row)}
256
+ >
257
+ <Button type="link" size="small" danger>
258
+ 删除
259
+ </Button>
260
+ </Popconfirm>
261
+ </Space>
262
+ ) : (
263
+ '-'
264
+ ),
265
+ },
266
+ ];
267
+
268
+ return (
269
+ <div>
270
+ <div style={{ display: 'flex', justifyContent: 'flex-end', gap: 8, marginBottom: 12 }}>
271
+ <Button icon={<ReloadOutlined />} onClick={() => query.refetch()}>
272
+ 刷新
273
+ </Button>
274
+ {canEdit ? (
275
+ <Button type="primary" icon={<PlusOutlined />} onClick={() => openCreate()}>
276
+ 新增菜单
277
+ </Button>
278
+ ) : null}
279
+ </div>
280
+
281
+ <Table<MenuRow>
282
+ rowKey="id"
283
+ columns={columns}
284
+ dataSource={tree}
285
+ loading={query.isLoading}
286
+ pagination={false}
287
+ size="middle"
288
+ defaultExpandAllRows
289
+ scroll={{ x: 1340 }}
290
+ />
291
+
292
+ <Modal
293
+ title={editing ? '编辑菜单' : '新增菜单'}
294
+ open={open}
295
+ onCancel={() => setOpen(false)}
296
+ onOk={submit}
297
+ confirmLoading={submitting}
298
+ destroyOnHidden
299
+ mask={{ closable: false }}
300
+ width={560}
301
+ >
302
+ <Form form={form} layout="vertical" style={{ marginTop: 8 }}>
303
+ <Form.Item name="parentId" label="上级菜单">
304
+ <TreeSelect
305
+ allowClear
306
+ treeDefaultExpandAll
307
+ placeholder="不选表示顶级菜单"
308
+ treeData={toParentOptions(tree, editing?.id)}
309
+ />
310
+ </Form.Item>
311
+
312
+ <Form.Item name="type" label="类型" rules={[{ required: true, message: '请选择类型' }]}>
313
+ <Select
314
+ options={[
315
+ { label: '目录', value: 'dir' },
316
+ { label: '菜单', value: 'menu' },
317
+ { label: '按钮', value: 'button' },
318
+ ]}
319
+ />
320
+ </Form.Item>
321
+
322
+ <Form.Item name="name" label="菜单名称" rules={[{ required: true, message: '请输入菜单名称' }]}>
323
+ <Input placeholder="显示在侧边栏的文字" />
324
+ </Form.Item>
325
+
326
+ <Row gutter={16}>
327
+ <Col span={12}>
328
+ <Form.Item name="path" label="路由地址">
329
+ <Input placeholder="如 /system/users" />
330
+ </Form.Item>
331
+ </Col>
332
+ <Col span={12}>
333
+ <Form.Item name="icon" label="图标名">
334
+ <Input placeholder="如 TeamOutlined" />
335
+ </Form.Item>
336
+ </Col>
337
+ </Row>
338
+
339
+ <Form.Item name="permCode" label="权限标识" tooltip="唯一;按钮级鉴权用它判断显隐">
340
+ <Input placeholder="如 system:user:add" />
341
+ </Form.Item>
342
+
343
+ <Form.Item
344
+ name="keywords"
345
+ label="搜索别名"
346
+ tooltip="全局搜索(Ctrl K)的命中词。用户口头怎么叫这个功能,就写什么,空格或逗号分隔"
347
+ >
348
+ <Input placeholder="如:员工 花名册 人员" />
349
+ </Form.Item>
350
+
351
+ <Form.Item
352
+ name="badgeKey"
353
+ label="侧边栏徽标"
354
+ tooltip="选了计数源,这个菜单就会在侧边栏显示红色数字"
355
+ extra={
356
+ watchedBadgeKey
357
+ ? BADGE_META[watchedBadgeKey]?.hint
358
+ : '留空表示不显示徽标。数字由对应模块实时计算,不同角色看到的可能不同'
359
+ }
360
+ >
361
+ <Select
362
+ allowClear
363
+ placeholder="不显示徽标"
364
+ options={BADGE_SOURCES.map((src) => ({
365
+ label: src.label,
366
+ value: src.key,
367
+ title: src.hint,
368
+ }))}
369
+ />
370
+ </Form.Item>
371
+
372
+ <Row gutter={16}>
373
+ <Col span={8}>
374
+ <Form.Item name="sort" label="排序">
375
+ <InputNumber min={0} max={9999} style={{ width: '100%' }} />
376
+ </Form.Item>
377
+ </Col>
378
+ <Col span={8}>
379
+ <Form.Item name="hidden" label="侧边栏">
380
+ <Select
381
+ options={[
382
+ { label: '显示', value: 0 },
383
+ { label: '隐藏', value: 1 },
384
+ ]}
385
+ />
386
+ </Form.Item>
387
+ </Col>
388
+ <Col span={8}>
389
+ <Form.Item name="status" label="状态">
390
+ <Select
391
+ options={[
392
+ { label: '启用', value: 1 },
393
+ { label: '停用', value: 0 },
394
+ ]}
395
+ />
396
+ </Form.Item>
397
+ </Col>
398
+ </Row>
399
+ </Form>
400
+ </Modal>
401
+ </div>
402
+ );
403
+ }