node-resource-utils 0.0.0-stage → 1.1.7

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,620 @@
1
+ 'use client';
2
+
3
+ import { useEffect, useState } from 'react';
4
+ import { useQuery } from '@tanstack/react-query';
5
+ import axios from 'axios';
6
+ import {
7
+ Alert,
8
+ App,
9
+ Button,
10
+ Card,
11
+ Col,
12
+ Form,
13
+ Input,
14
+ Modal,
15
+ Popconfirm,
16
+ Row,
17
+ Select,
18
+ Space,
19
+ Switch,
20
+ Tag,
21
+ TreeSelect,
22
+ } from 'antd';
23
+ import type { ColumnsType } from 'antd/es/table';
24
+ import { ApartmentOutlined, KeyOutlined, UserAddOutlined } from '@ant-design/icons';
25
+ import DataTable from '@/components/DataTable';
26
+ import { fs, MILITARY } from '@/lib/theme';
27
+ import { useAuth } from '@/components/AuthContext';
28
+ import { useRowFlash } from '@/components/feedback';
29
+ import { fmtDateTime } from '@/lib/format';
30
+ import { graphHref } from '@/lib/graph-link';
31
+ import { SECURITY_QUESTIONS } from '@/lib/security-questions';
32
+ import { formRules } from '@/lib/formRules';
33
+
34
+ type RoleBrief = { id: string; code: string; name: string };
35
+
36
+ type UserRow = {
37
+ id: string;
38
+ username: string;
39
+ name: string;
40
+ email: string | null;
41
+ phone: string | null;
42
+ deptId: string | null;
43
+ dept: { id: string; name: string } | null;
44
+ status: number;
45
+ isSuper: number;
46
+ securityQuestion: string | null;
47
+ mustChangePassword: number;
48
+ lastLogin: string | null;
49
+ roles: RoleBrief[];
50
+ };
51
+
52
+ type DeptNode = { id: string; name: string; parentId: string | null; children?: DeptNode[] };
53
+ type TreeNode = { value: string; title: string; children?: TreeNode[] };
54
+
55
+ type FormValues = {
56
+ username: string;
57
+ password?: string;
58
+ name: string;
59
+ email?: string;
60
+ phone?: string;
61
+ deptId?: string;
62
+ roleIds?: string[];
63
+ status: number;
64
+ remark?: string;
65
+ securityQuestion?: string;
66
+ securityAnswer?: string;
67
+ };
68
+
69
+ /** 统一的 axios 包装:把「HTTP 200 但业务失败」也当异常抛出 */
70
+ async function request<T>(
71
+ method: 'get' | 'post' | 'put' | 'delete',
72
+ url: string,
73
+ body?: unknown,
74
+ params?: unknown
75
+ ): Promise<T> {
76
+ const { data } = await axios.request({ method, url, data: body, params });
77
+ if (!data || data.code !== 0) throw new Error(data?.message || '请求失败');
78
+ return data.data as T;
79
+ }
80
+
81
+ function errMsg(e: unknown): string {
82
+ if (axios.isAxiosError(e)) {
83
+ const d = e.response?.data as { message?: string } | undefined;
84
+ return d?.message || e.message;
85
+ }
86
+ return e instanceof Error ? e.message : String(e);
87
+ }
88
+
89
+ function toTreeData(nodes: DeptNode[]): TreeNode[] {
90
+ return nodes.map((n) => ({
91
+ value: n.id,
92
+ title: n.name,
93
+ children: n.children?.length ? toTreeData(n.children) : undefined,
94
+ }));
95
+ }
96
+
97
+ /** 部门下拉用扁平结构(带缩进),TreeSelect 用树结构 */
98
+ function flattenDepts(nodes: DeptNode[], depth = 0): { label: string; value: string }[] {
99
+ const out: { label: string; value: string }[] = [];
100
+ for (const n of nodes) {
101
+ out.push({ label: `${' '.repeat(depth)}${n.name}`, value: n.id });
102
+ if (n.children?.length) out.push(...flattenDepts(n.children, depth + 1));
103
+ }
104
+ return out;
105
+ }
106
+
107
+ export default function UsersPage() {
108
+ const { message } = App.useApp();
109
+ const { user, hasPerm } = useAuth();
110
+
111
+ const [refreshKey, setRefreshKey] = useState(0);
112
+ const [open, setOpen] = useState(false);
113
+ const [editing, setEditing] = useState<UserRow | null>(null);
114
+ const [submitting, setSubmitting] = useState(false);
115
+ const [form] = Form.useForm<FormValues>();
116
+
117
+ /**
118
+ * 注册开关(仅超级管理员可见可改)。
119
+ * 与接口侧约定一致:PUT /api/auth/register-config 只认 isSuper,
120
+ * 这里的 isSuper 判断只是「别给非超管看一个拨了没反应的开关」。
121
+ */
122
+ const isSuper = Boolean(user?.isSuper);
123
+ const [registerEnabled, setRegisterEnabled] = useState(false);
124
+ const [togglingRegister, setTogglingRegister] = useState(false);
125
+
126
+ useEffect(() => {
127
+ if (!isSuper) return;
128
+ let alive = true;
129
+ axios
130
+ .get('/api/auth/register-config')
131
+ .then(({ data }) => {
132
+ if (alive && data?.code === 0) setRegisterEnabled(Boolean(data.data?.enabled));
133
+ })
134
+ .catch(() => {});
135
+ return () => {
136
+ alive = false;
137
+ };
138
+ }, [isSuper]);
139
+
140
+ const toggleRegister = async (checked: boolean) => {
141
+ setTogglingRegister(true);
142
+ try {
143
+ const { data } = await axios.put('/api/auth/register-config', { enabled: checked });
144
+ if (data?.code !== 0) throw new Error(data?.message || '切换失败');
145
+ setRegisterEnabled(checked);
146
+ message.success(checked ? '注册功能已开放,登录页将出现注册入口' : '注册功能已关闭');
147
+ } catch (e) {
148
+ // 受控 Switch:不更新 state,antd 会自动把拨片弹回原位
149
+ message.error(errMsg(e));
150
+ } finally {
151
+ setTogglingRegister(false);
152
+ }
153
+ };
154
+
155
+ /**
156
+ * 保存 / 切换状态后,让目标行闪一下。
157
+ * 只有 toast 的时候,用户看到"保存成功"却不知道改的是哪一行 ——
158
+ * 尤其是宽表横向滚动过、或者刚翻过页的情况下。
159
+ */
160
+ const { highlightRow, flashRow } = useRowFlash();
161
+
162
+ const [resetTarget, setResetTarget] = useState<UserRow | null>(null);
163
+ const [resetSubmitting, setResetSubmitting] = useState(false);
164
+ const [resetForm] = Form.useForm<{ password: string; confirm: string }>();
165
+
166
+ const deptQuery = useQuery({
167
+ queryKey: ['depts'],
168
+ queryFn: () => request<DeptNode[]>('get', '/api/depts'),
169
+ staleTime: 5 * 60 * 1000,
170
+ });
171
+
172
+ const roleQuery = useQuery({
173
+ queryKey: ['roles-all'],
174
+ queryFn: () => request<{ list: RoleBrief[] }>('get', '/api/roles', undefined, { page: 1, pageSize: 200 }),
175
+ staleTime: 5 * 60 * 1000,
176
+ });
177
+
178
+ const deptTree = deptQuery.data ?? [];
179
+ const roleOptions = (roleQuery.data?.list ?? []).map((r) => ({ label: r.name, value: r.id }));
180
+
181
+ const refresh = () => setRefreshKey((k) => k + 1);
182
+
183
+ const openCreate = () => {
184
+ setEditing(null);
185
+ form.resetFields();
186
+ form.setFieldsValue({ status: 1, roleIds: [] });
187
+ setOpen(true);
188
+ };
189
+
190
+ const openEdit = (row: UserRow) => {
191
+ setEditing(row);
192
+ form.resetFields();
193
+ form.setFieldsValue({
194
+ username: row.username,
195
+ name: row.name,
196
+ email: row.email ?? undefined,
197
+ phone: row.phone ?? undefined,
198
+ deptId: row.deptId ?? undefined,
199
+ roleIds: row.roles.map((r) => r.id),
200
+ status: row.status,
201
+ });
202
+ setOpen(true);
203
+ };
204
+
205
+ const submit = async () => {
206
+ let values: FormValues;
207
+ try {
208
+ values = await form.validateFields();
209
+ } catch {
210
+ return; // 校验未通过,antd 已在字段上给出红字提示
211
+ }
212
+
213
+ setSubmitting(true);
214
+ try {
215
+ if (editing) {
216
+ const body: Partial<FormValues> = { ...values };
217
+ delete body.username; // 用户名不允许改,避免破坏审计线索
218
+ delete body.password;
219
+ // 密保只在新建时设置;编辑态不提交这两个字段,避免误清空已有密保
220
+ delete body.securityQuestion;
221
+ delete body.securityAnswer;
222
+ await request('put', `/api/users/${editing.id}`, body);
223
+ message.success('保存成功');
224
+ flashRow(editing.id);
225
+ } else {
226
+ await request('post', '/api/users', values);
227
+ message.success('新增成功');
228
+ }
229
+ setOpen(false);
230
+ refresh();
231
+ } catch (e) {
232
+ message.error(errMsg(e));
233
+ } finally {
234
+ setSubmitting(false);
235
+ }
236
+ };
237
+
238
+ const toggleStatus = async (row: UserRow, checked: boolean) => {
239
+ try {
240
+ await request('put', `/api/users/${row.id}`, { status: checked ? 1 : 0 });
241
+ message.success(checked ? '已启用' : '已停用');
242
+ flashRow(row.id);
243
+ refresh();
244
+ } catch (e) {
245
+ message.error(errMsg(e));
246
+ }
247
+ };
248
+
249
+ const remove = async (row: UserRow) => {
250
+ try {
251
+ await request('delete', `/api/users/${row.id}`);
252
+ message.success('删除成功');
253
+ refresh();
254
+ } catch (e) {
255
+ message.error(errMsg(e));
256
+ }
257
+ };
258
+
259
+ const submitReset = async () => {
260
+ let values: { password: string; confirm: string };
261
+ try {
262
+ values = await resetForm.validateFields();
263
+ } catch {
264
+ return;
265
+ }
266
+ if (!resetTarget) return;
267
+ setResetSubmitting(true);
268
+ try {
269
+ await request('put', `/api/users/${resetTarget.id}/password`, { password: values.password });
270
+ message.success('密码已重置,该用户下次登录需先修改密码');
271
+ setResetTarget(null);
272
+ resetForm.resetFields();
273
+ } catch (e) {
274
+ message.error(errMsg(e));
275
+ } finally {
276
+ setResetSubmitting(false);
277
+ }
278
+ };
279
+
280
+ const columns: ColumnsType<UserRow> = [
281
+ {
282
+ title: '用户名',
283
+ dataIndex: 'username',
284
+ width: 140,
285
+ render: (v: string, row) => (
286
+ <Space size={6} wrap>
287
+ <span>{v}</span>
288
+ {row.isSuper === 1 ? <Tag color="gold">超管</Tag> : null}
289
+ {row.mustChangePassword === 1 ? <Tag color="warning">待改密</Tag> : null}
290
+ {!row.securityQuestion ? <Tag>未设密保</Tag> : null}
291
+ </Space>
292
+ ),
293
+ },
294
+ { title: '姓名', dataIndex: 'name', width: 120 },
295
+ { title: '部门', dataIndex: ['dept', 'name'], width: 140, render: (_v, row) => row.dept?.name ?? '-' },
296
+ {
297
+ title: '角色',
298
+ dataIndex: 'roles',
299
+ width: 200,
300
+ render: (_v, row) =>
301
+ row.roles.length ? (
302
+ <Space size={4} wrap>
303
+ {row.roles.map((r) => (
304
+ <Tag key={r.id} style={{ background: MILITARY.soft, color: MILITARY.primary, borderColor: MILITARY.softStrong }}>
305
+ {r.name}
306
+ </Tag>
307
+ ))}
308
+ </Space>
309
+ ) : (
310
+ '-'
311
+ ),
312
+ },
313
+ { title: '手机号', dataIndex: 'phone', width: 140, render: (v: string | null) => v || '-' },
314
+ { title: '邮箱', dataIndex: 'email', width: 200, ellipsis: true, render: (v: string | null) => v || '-' },
315
+ {
316
+ title: '状态',
317
+ dataIndex: 'status',
318
+ width: 100,
319
+ render: (v: number, row) => (
320
+ <Switch
321
+ checked={v === 1}
322
+ checkedChildren="启用"
323
+ unCheckedChildren="停用"
324
+ disabled={!hasPerm('system:user:edit')}
325
+ onChange={(checked) => toggleStatus(row, checked)}
326
+ />
327
+ ),
328
+ },
329
+ {
330
+ title: '最后登录',
331
+ dataIndex: 'lastLogin',
332
+ width: 180,
333
+ render: (v: string | null) => fmtDateTime(v),
334
+ },
335
+ {
336
+ // ★ 多实体关联:一条账号 = 部门 + 若干角色,
337
+ // 点「关联」跳图谱后会高亮这几个节点之间的连线(而不是只定位一个节点)。
338
+ // 放在固定的操作列里,宽表横向滚动时也始终可见。
339
+ title: '操作',
340
+ key: 'action',
341
+ width: 270,
342
+ fixed: 'right',
343
+ render: (_v, row) => (
344
+ <Space size={4}>
345
+ <Button type="link" size="small" icon={<ApartmentOutlined />} href={graphHref('User', row.id)}>
346
+ 关联
347
+ </Button>
348
+ {hasPerm('system:user:edit') ? (
349
+ <Button type="link" size="small" onClick={() => openEdit(row)}>
350
+ 编辑
351
+ </Button>
352
+ ) : null}
353
+ {hasPerm('system:user:reset') ? (
354
+ <Button
355
+ type="link"
356
+ size="small"
357
+ icon={<KeyOutlined />}
358
+ onClick={() => {
359
+ resetForm.resetFields();
360
+ setResetTarget(row);
361
+ }}
362
+ >
363
+ 重置密码
364
+ </Button>
365
+ ) : null}
366
+ {hasPerm('system:user:del') ? (
367
+ <Popconfirm
368
+ title="确认删除该用户?"
369
+ description="删除后该账号将无法登录,历史操作日志仍会保留。"
370
+ okText="删除"
371
+ cancelText="取消"
372
+ okButtonProps={{ danger: true }}
373
+ onConfirm={() => remove(row)}
374
+ >
375
+ <Button type="link" size="small" danger>
376
+ 删除
377
+ </Button>
378
+ </Popconfirm>
379
+ ) : null}
380
+ </Space>
381
+ ),
382
+ },
383
+ ];
384
+
385
+ return (
386
+ <>
387
+ {/* 注册开关:只给超管看。非超管即便猜到接口也改不了(PUT 侧校验 isSuper) */}
388
+ {isSuper ? (
389
+ <Card size="small" style={{ marginBottom: 12 }}>
390
+ <div style={{ display: 'flex', alignItems: 'center', gap: 12, flexWrap: 'wrap' }}>
391
+ <UserAddOutlined style={{ fontSize: fs(18), color: MILITARY.primary }} />
392
+ <div style={{ flex: '1 1 240px' }}>
393
+ <div style={{ fontWeight: 600, fontSize: fs(13.5), color: MILITARY.text }}>注册设置</div>
394
+ <div style={{ fontSize: fs(12), color: MILITARY.textMuted, marginTop: 2, lineHeight: 1.6 }}>
395
+ 开启后,登录页会显示「注册账号」入口,访客可自助开通账号。新注册账号默认没有角色与部门,
396
+ 需手动分配后才能使用功能;同一 IP 10 分钟内最多注册 5 个账号。
397
+ </div>
398
+ </div>
399
+ <Switch
400
+ checked={registerEnabled}
401
+ loading={togglingRegister}
402
+ checkedChildren="开放注册"
403
+ unCheckedChildren="关闭注册"
404
+ onChange={toggleRegister}
405
+ />
406
+ </div>
407
+ </Card>
408
+ ) : null}
409
+
410
+ <DataTable<UserRow>
411
+ api="/api/users"
412
+ rowKey="id"
413
+ columns={columns}
414
+ refreshKey={refreshKey}
415
+ highlightRow={highlightRow}
416
+ scrollX={1400}
417
+ selectable={hasPerm('system:user:del')}
418
+ onBatchDelete={async (keys) => {
419
+ try {
420
+ await Promise.all(keys.map((k) => request('delete', `/api/users/${String(k)}`)));
421
+ message.success('批量删除完成');
422
+ refresh();
423
+ } catch (e) {
424
+ message.error(errMsg(e));
425
+ }
426
+ }}
427
+ searchFields={[
428
+ {
429
+ name: 'status',
430
+ label: '状态',
431
+ type: 'select',
432
+ options: [
433
+ { label: '启用', value: 1 },
434
+ { label: '停用', value: 0 },
435
+ ],
436
+ },
437
+ { name: 'deptId', label: '部门', type: 'select', options: flattenDepts(deptTree) },
438
+ ]}
439
+ canAdd={hasPerm('system:user:add')}
440
+ onAdd={openCreate}
441
+ addText="新增用户"
442
+ />
443
+
444
+ <Modal
445
+ title={editing ? '编辑用户' : '新增用户'}
446
+ open={open}
447
+ onCancel={() => setOpen(false)}
448
+ onOk={submit}
449
+ confirmLoading={submitting}
450
+ destroyOnHidden
451
+ mask={{ closable: false }}
452
+ width={560}
453
+ >
454
+ <Form form={form} layout="vertical" style={{ marginTop: 8 }}>
455
+ <Form.Item
456
+ name="username"
457
+ label="用户名"
458
+ rules={formRules('username', { label: '用户名', required: true })}
459
+ >
460
+ <Input maxLength={32} placeholder="登录账号,创建后不可修改" disabled={Boolean(editing)} />
461
+ </Form.Item>
462
+
463
+ {!editing ? (
464
+ <Form.Item
465
+ name="password"
466
+ label="初始密码"
467
+ rules={formRules('password', { label: '初始密码', required: true })}
468
+ >
469
+ <Input.Password maxLength={64} placeholder="6-64 位,不能含空格" autoComplete="new-password" />
470
+ </Form.Item>
471
+ ) : null}
472
+
473
+ {!editing ? (
474
+ <>
475
+ <Alert
476
+ type="info"
477
+ showIcon
478
+ style={{ marginBottom: 16 }}
479
+ title="建议创建账号时设置密保问题"
480
+ description="用户忘记密码时可凭密保问题自助找回;未设置密保的账号只能提交人工申诉,由超管核实后重置。"
481
+ />
482
+ <Row gutter={16}>
483
+ <Col span={12}>
484
+ <Form.Item
485
+ name="securityQuestion"
486
+ label="密保问题"
487
+ dependencies={['securityAnswer']}
488
+ rules={[
489
+ ({ getFieldValue }) => ({
490
+ validator: (_r, value) =>
491
+ !getFieldValue('securityAnswer') || value
492
+ ? Promise.resolve()
493
+ : Promise.reject(new Error('请选择密保问题')),
494
+ }),
495
+ ]}
496
+ >
497
+ <Select
498
+ allowClear
499
+ placeholder="选填(推荐设置)"
500
+ options={SECURITY_QUESTIONS.map((q) => ({ label: q, value: q }))}
501
+ />
502
+ </Form.Item>
503
+ </Col>
504
+ <Col span={12}>
505
+ <Form.Item
506
+ name="securityAnswer"
507
+ label="密保答案"
508
+ dependencies={['securityQuestion']}
509
+ rules={[
510
+ ({ getFieldValue }) => ({
511
+ validator: (_r, value) =>
512
+ !getFieldValue('securityQuestion') || value
513
+ ? Promise.resolve()
514
+ : Promise.reject(new Error('请填写密保答案')),
515
+ }),
516
+ ]}
517
+ >
518
+ <Input.Password placeholder="选填,加密保存" autoComplete="new-password" />
519
+ </Form.Item>
520
+ </Col>
521
+ </Row>
522
+ </>
523
+ ) : null}
524
+
525
+ <Form.Item
526
+ name="name"
527
+ label="姓名"
528
+ rules={formRules('name', { label: '姓名', required: true })}
529
+ >
530
+ <Input maxLength={32} placeholder="真实姓名,2-32 个汉字" />
531
+ </Form.Item>
532
+
533
+ <Form.Item name="deptId" label="所属部门">
534
+ <TreeSelect
535
+ allowClear
536
+ treeDefaultExpandAll
537
+ placeholder="请选择部门"
538
+ treeData={toTreeData(deptTree)}
539
+ />
540
+ </Form.Item>
541
+
542
+ <Form.Item name="roleIds" label="角色">
543
+ <Select
544
+ mode="multiple"
545
+ allowClear
546
+ placeholder="可多选"
547
+ options={roleOptions}
548
+ loading={roleQuery.isLoading}
549
+ />
550
+ </Form.Item>
551
+
552
+ <Row gutter={16}>
553
+ <Col span={12}>
554
+ <Form.Item name="phone" label="手机号" rules={formRules('phone', { label: '手机号' })}>
555
+ <Input maxLength={20} placeholder="选填,11 位手机号" />
556
+ </Form.Item>
557
+ </Col>
558
+ <Col span={12}>
559
+ <Form.Item name="email" label="邮箱" rules={formRules('email', { label: '邮箱' })}>
560
+ <Input maxLength={254} placeholder="选填,如 name@example.com" />
561
+ </Form.Item>
562
+ </Col>
563
+ </Row>
564
+
565
+ <Form.Item name="status" label="状态" initialValue={1}>
566
+ <Select
567
+ options={[
568
+ { label: '启用', value: 1 },
569
+ { label: '停用', value: 0 },
570
+ ]}
571
+ />
572
+ </Form.Item>
573
+ </Form>
574
+ </Modal>
575
+
576
+ <Modal
577
+ title={`重置密码${resetTarget ? ` — ${resetTarget.name}` : ''}`}
578
+ open={Boolean(resetTarget)}
579
+ onCancel={() => setResetTarget(null)}
580
+ onOk={submitReset}
581
+ confirmLoading={resetSubmitting}
582
+ destroyOnHidden
583
+ width={420}
584
+ >
585
+ <Alert
586
+ type="warning"
587
+ showIcon
588
+ style={{ marginBottom: 16 }}
589
+ title="重置后该用户首次登录必须修改密码"
590
+ description="请通过安全渠道把临时密码告知本人;用户登录后会被强制跳转到修改密码页。"
591
+ />
592
+ <Form form={resetForm} layout="vertical" style={{ marginTop: 8 }}>
593
+ <Form.Item
594
+ name="password"
595
+ label="新密码"
596
+ rules={formRules('password', { label: '新密码', required: true })}
597
+ >
598
+ <Input.Password maxLength={64} placeholder="6-64 位,不能含空格" autoComplete="new-password" />
599
+ </Form.Item>
600
+ <Form.Item
601
+ name="confirm"
602
+ label="确认新密码"
603
+ dependencies={['password']}
604
+ rules={[
605
+ { required: true, message: '请再次输入新密码' },
606
+ ({ getFieldValue }) => ({
607
+ validator: (_r, value) =>
608
+ !value || getFieldValue('password') === value
609
+ ? Promise.resolve()
610
+ : Promise.reject(new Error('两次输入的密码不一致')),
611
+ }),
612
+ ]}
613
+ >
614
+ <Input.Password maxLength={64} placeholder="再次输入" autoComplete="new-password" />
615
+ </Form.Item>
616
+ </Form>
617
+ </Modal>
618
+ </>
619
+ );
620
+ }