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,145 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useEffect, useState, type ReactNode } from 'react';
4
+ import { Tooltip } from 'antd';
5
+ import { EyeOutlined, RiseOutlined } from '@ant-design/icons';
6
+ import {
7
+ ONLINE_WINDOW_MS,
8
+ VISIT_REFRESH_MS,
9
+ VISIT_STATS_EVENT,
10
+ formatVisitCount,
11
+ type VisitStats as VisitStatsData,
12
+ } from '@/lib/visits';
13
+ import { fs, MILITARY } from '@/lib/theme';
14
+
15
+ /**
16
+ * 顶栏访问统计:在线人数 / 今日访问 / 总访问量。
17
+ *
18
+ * 做成独立组件而不是塞进 AdminShell:
19
+ * 以后要看板、大屏、登录页复用同一组数字,直接 <VisitStats /> 即可,
20
+ * 取数口径和刷新策略(30 秒轮询 + 回到前台刷新 + 埋点成功后立即刷新)都在这里,
21
+ * 各页面不需要各写一遍,也不会出现"首页和顶栏数字对不上"。
22
+ *
23
+ * 统计是旁路信息:拉取失败保持上一次的值,绝不弹错、绝不阻塞页面。
24
+ */
25
+ export default function VisitStats() {
26
+ const [stats, setStats] = useState<VisitStatsData | null>(null);
27
+
28
+ const load = useCallback(async () => {
29
+ try {
30
+ const res = await fetch('/api/visits', { cache: 'no-store' });
31
+ const json = (await res.json()) as { code: number; data?: VisitStatsData };
32
+ if (json?.code === 0 && json.data) setStats(json.data);
33
+ } catch {
34
+ /* 静默:统计不是业务数据,拉不到就继续显示上一次的值 */
35
+ }
36
+ }, []);
37
+
38
+ useEffect(() => {
39
+ void load();
40
+
41
+ const refresh = () => {
42
+ if (document.visibilityState === 'visible') void load();
43
+ };
44
+ const timer = window.setInterval(refresh, VISIT_REFRESH_MS);
45
+ // 埋点写入成功 → 立即刷新,自己的访问不用等下一轮轮询才出现
46
+ window.addEventListener(VISIT_STATS_EVENT, refresh);
47
+ document.addEventListener('visibilitychange', refresh);
48
+ window.addEventListener('focus', refresh);
49
+ return () => {
50
+ window.clearInterval(timer);
51
+ window.removeEventListener(VISIT_STATS_EVENT, refresh);
52
+ document.removeEventListener('visibilitychange', refresh);
53
+ window.removeEventListener('focus', refresh);
54
+ };
55
+ }, [load]);
56
+
57
+ return (
58
+ <div
59
+ data-testid="visit-stats"
60
+ className="visit-stats"
61
+ aria-label="访问统计"
62
+ style={{
63
+ display: 'flex',
64
+ alignItems: 'center',
65
+ gap: 12,
66
+ height: 32,
67
+ padding: '0 12px',
68
+ borderRadius: 9,
69
+ background: MILITARY.paper,
70
+ border: `1px solid ${MILITARY.borderSoft}`,
71
+ whiteSpace: 'nowrap',
72
+ }}
73
+ >
74
+ <Metric
75
+ label="在线"
76
+ value={stats?.online}
77
+ tip={`最近 ${Math.round(ONLINE_WINDOW_MS / 60_000)} 分钟内有活动的人数(同一浏览器多标签只算 1 人)`}
78
+ marker={
79
+ <span
80
+ style={{
81
+ width: 6,
82
+ height: 6,
83
+ borderRadius: '50%',
84
+ background: MILITARY.success,
85
+ boxShadow: `0 0 0 3px ${MILITARY.success}22`,
86
+ }}
87
+ />
88
+ }
89
+ />
90
+ <Separator />
91
+ <Metric
92
+ label="今日"
93
+ value={stats?.today}
94
+ tip="今天 00:00 起的页面浏览量(PV)"
95
+ marker={<RiseOutlined style={{ fontSize: fs(11), color: MILITARY.warning }} />}
96
+ />
97
+ <Separator />
98
+ <Metric
99
+ label="总访问"
100
+ value={stats?.total}
101
+ tip="上线以来的累计页面浏览量(PV)"
102
+ marker={<EyeOutlined style={{ fontSize: fs(12), color: MILITARY.primary }} />}
103
+ />
104
+ </div>
105
+ );
106
+ }
107
+
108
+ function Separator() {
109
+ return <span style={{ width: 1, height: 14, background: MILITARY.border, flex: '0 0 auto' }} />;
110
+ }
111
+
112
+ function Metric({
113
+ label,
114
+ value,
115
+ tip,
116
+ marker,
117
+ }: {
118
+ label: string;
119
+ value: number | null | undefined;
120
+ tip: string;
121
+ marker: ReactNode;
122
+ }) {
123
+ return (
124
+ <Tooltip title={tip}>
125
+ <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, cursor: 'default' }}>
126
+ <span style={{ display: 'inline-flex', alignItems: 'center', flex: '0 0 auto' }}>{marker}</span>
127
+ <span style={{ fontSize: fs(12), color: MILITARY.textMuted }} className="visit-stat-label">
128
+ {label}
129
+ </span>
130
+ <span
131
+ style={{
132
+ minWidth: 16,
133
+ fontSize: fs(13),
134
+ fontWeight: 600,
135
+ color: MILITARY.text,
136
+ fontVariantNumeric: 'tabular-nums',
137
+ letterSpacing: 0,
138
+ }}
139
+ >
140
+ {formatVisitCount(value)}
141
+ </span>
142
+ </span>
143
+ </Tooltip>
144
+ );
145
+ }
@@ -0,0 +1,56 @@
1
+ import type { ReactNode } from 'react';
2
+ import {
3
+ ApartmentOutlined,
4
+ AppstoreOutlined,
5
+ AuditOutlined,
6
+ BellOutlined,
7
+ ClockCircleOutlined,
8
+ DashboardOutlined,
9
+ FileTextOutlined,
10
+ FileWordOutlined,
11
+ IdcardOutlined,
12
+ LineChartOutlined,
13
+ MenuOutlined,
14
+ MessageOutlined,
15
+ QuestionCircleOutlined,
16
+ RobotOutlined,
17
+ SafetyCertificateOutlined,
18
+ SettingOutlined,
19
+ TeamOutlined,
20
+ ThunderboltOutlined,
21
+ } from '@ant-design/icons';
22
+
23
+ /**
24
+ * 菜单 / 搜索共用的「字符串 → 图标」映射表。
25
+ *
26
+ * 为什么不用 `import * as Icons from '@ant-design/icons'`:
27
+ * 后者会把上千个图标全打进产物(几 MB),按需映射既小又可控。
28
+ *
29
+ * 新增菜单用到表外的图标时,在这里补一行即可 ——
30
+ * 侧边栏、面包屑、全局搜索三处都会自动跟上。
31
+ */
32
+ const ICONS: Record<string, ReactNode> = {
33
+ DashboardOutlined: <DashboardOutlined />,
34
+ SettingOutlined: <SettingOutlined />,
35
+ TeamOutlined: <TeamOutlined />,
36
+ SafetyCertificateOutlined: <SafetyCertificateOutlined />,
37
+ MenuOutlined: <MenuOutlined />,
38
+ FileTextOutlined: <FileTextOutlined />,
39
+ FileWordOutlined: <FileWordOutlined />,
40
+ AppstoreOutlined: <AppstoreOutlined />,
41
+ IdcardOutlined: <IdcardOutlined />,
42
+ ApartmentOutlined: <ApartmentOutlined />,
43
+ ThunderboltOutlined: <ThunderboltOutlined />,
44
+ LineChartOutlined: <LineChartOutlined />,
45
+ AuditOutlined: <AuditOutlined />,
46
+ RobotOutlined: <RobotOutlined />,
47
+ MessageOutlined: <MessageOutlined />,
48
+ ClockCircleOutlined: <ClockCircleOutlined />,
49
+ QuestionCircleOutlined: <QuestionCircleOutlined />,
50
+ BellOutlined: <BellOutlined />,
51
+ };
52
+
53
+ export function iconOf(name?: string | null): ReactNode {
54
+ if (!name) return undefined;
55
+ return ICONS[name] ?? <AppstoreOutlined />;
56
+ }
@@ -0,0 +1,351 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useMemo, useState } from 'react';
4
+ import { Button, DatePicker, Input, InputNumber, Select, Switch } from 'antd';
5
+ import { CheckCircleOutlined, EditOutlined, ExclamationCircleOutlined } from '@ant-design/icons';
6
+ import dayjs from 'dayjs';
7
+ import { fs, MILITARY } from '@/lib/theme';
8
+ import { displayValue, type FormCard, type FormFieldSpec } from '@/lib/aiFormTypes';
9
+ import { inferValidationType, validateField } from '@/lib/validation';
10
+
11
+ const { TextArea } = Input;
12
+
13
+ /**
14
+ * AI 输入表单卡 —— 模型要信息时,用户在对话里直接填,不用手打格式。
15
+ *
16
+ * ★ 为什么不做成聊天框里的"请提供开始日期、结束日期、事由":
17
+ * - 字段一多就漏(用户只回答了两个,模型又得追问一轮)
18
+ * - 日期写成"下周三"模型还要换算,换算就可能错
19
+ * - 枚举值(请假类型、紧急程度)用户不知道合法取值,只能猜
20
+ * 控件把这些不确定都消掉了:日期只能是日期,枚举只能是选项里的一个。
21
+ *
22
+ * ★ 控件类型完全由服务端下发的 kind 决定,组件里没有一行业务判断。
23
+ * 换业务(请销假 / 派车 / 报销)时这个文件不需要改。
24
+ *
25
+ * ★ 状态放在卡片上而不是组件内:
26
+ * 用户提交后要等 SSE 回来(可能生成确认卡,也可能再补一张表单),
27
+ * 状态得跟着消息走,否则用户往上翻历史会看到"提交按钮又出现了"。
28
+ */
29
+ export interface AiInputFormProps {
30
+ card: FormCard;
31
+ onSubmit: (values: Record<string, unknown>) => void;
32
+ onCancel: () => void;
33
+ }
34
+
35
+ function blank(v: unknown): boolean {
36
+ if (v === undefined || v === null) return true;
37
+ if (typeof v === 'string') return v.trim() === '';
38
+ return false;
39
+ }
40
+
41
+ function initialValues(card: FormCard): Record<string, unknown> {
42
+ const out: Record<string, unknown> = {};
43
+ for (const f of card.spec.fields) {
44
+ out[f.name] = f.value ?? undefined;
45
+ }
46
+ return out;
47
+ }
48
+
49
+ /** 服务端没给 validation 时,按字段名/标签兜底推断;数字、日期控件按控件类型兜底 */
50
+ function validationTypeOf(field: FormFieldSpec): string {
51
+ // 枚举/外键/布尔的值是选项或 id,不是用户手输的文本,不能套手机号/邮箱规则
52
+ if (field.kind === 'enum' || field.kind === 'fk' || field.kind === 'boolean') return 'text';
53
+ if (field.validation) return field.validation;
54
+ const inferred = inferValidationType(field.name, field.label, field.hint);
55
+ if (inferred) return inferred;
56
+ if (field.kind === 'number') return 'number';
57
+ if (field.kind === 'date') return 'date';
58
+ return 'text';
59
+ }
60
+
61
+ export default function AiInputForm({ card, onSubmit, onCancel }: AiInputFormProps) {
62
+ const { spec, status } = card;
63
+ const [values, setValues] = useState<Record<string, unknown>>(() => initialValues(card));
64
+ const [errors, setErrors] = useState<Record<string, string>>({});
65
+
66
+ const editable = status === 'pending' || status === 'failed';
67
+
68
+ const setValue = useCallback((name: string, v: unknown) => {
69
+ setValues((prev) => ({ ...prev, [name]: v }));
70
+ setErrors((prev) => {
71
+ if (!prev[name]) return prev;
72
+ const next = { ...prev };
73
+ delete next[name];
74
+ return next;
75
+ });
76
+ }, []);
77
+
78
+ const submit = useCallback(() => {
79
+ const errs: Record<string, string> = {};
80
+ const clean: Record<string, unknown> = { ...values };
81
+ for (const f of spec.fields) {
82
+ const result = validateField(validationTypeOf(f), values[f.name], {
83
+ label: f.label,
84
+ required: f.required,
85
+ });
86
+ if (!result.ok) {
87
+ errs[f.name] = result.message;
88
+ } else if (!blank(values[f.name])) {
89
+ // 文本类用规整后的值(身份证 X 大写、手机号去掉 +86/空格等);
90
+ // 数字/枚举/外键/布尔保持原类型,避免把 number 变成字符串传给工具
91
+ if (f.kind === 'number') {
92
+ clean[f.name] = Number(result.value);
93
+ } else if (f.kind === 'boolean' || f.kind === 'enum' || f.kind === 'fk') {
94
+ clean[f.name] = values[f.name];
95
+ } else {
96
+ clean[f.name] = result.value;
97
+ }
98
+ }
99
+ }
100
+ setErrors(errs);
101
+ if (Object.keys(errs).length) return;
102
+ onSubmit(clean);
103
+ }, [onSubmit, spec.fields, values]);
104
+
105
+ const filledCount = useMemo(
106
+ () => spec.fields.filter((f) => !blank(values[f.name])).length,
107
+ [spec.fields, values]
108
+ );
109
+
110
+ /* ---------------- 已提交:只读摘要 ---------------- */
111
+ if (status === 'done') {
112
+ return (
113
+ <div style={cardStyle(MILITARY.success)} data-testid="ai-input-form" data-form-state="done">
114
+ <Head icon={<CheckCircleOutlined />} color={MILITARY.success} title={`已提交:${spec.title}`} />
115
+ <div style={{ marginTop: 8, borderTop: `1px dashed ${MILITARY.border}`, paddingTop: 8 }}>
116
+ {spec.fields
117
+ .filter((f) => !blank(values[f.name]))
118
+ .map((f) => (
119
+ <div key={f.name} style={{ display: 'flex', gap: 8, fontSize: fs(12), lineHeight: '20px' }}>
120
+ <span style={{ flex: '0 0 76px', color: MILITARY.textMuted }}>{f.label}</span>
121
+ <span style={{ color: MILITARY.text, wordBreak: 'break-all' }}>
122
+ {displayValue(f, values[f.name])}
123
+ </span>
124
+ </div>
125
+ ))}
126
+ </div>
127
+ </div>
128
+ );
129
+ }
130
+
131
+ if (status === 'cancelled') {
132
+ return (
133
+ <div style={cardStyle(MILITARY.border)} data-testid="ai-input-form" data-form-state="cancelled">
134
+ <Head icon={<EditOutlined />} color={MILITARY.textMuted} title={spec.title} />
135
+ <div style={{ fontSize: fs(12), color: MILITARY.textMuted, marginTop: 6 }}>
136
+ 已取消,没有提交任何内容
137
+ </div>
138
+ </div>
139
+ );
140
+ }
141
+
142
+ /* ---------------- 待填写 / 提交中 / 失败重试 ---------------- */
143
+ return (
144
+ <div
145
+ style={cardStyle(status === 'failed' ? MILITARY.error : MILITARY.accent)}
146
+ data-testid="ai-input-form"
147
+ data-form-state={status}
148
+ data-form-tool={spec.tool ?? ''}
149
+ >
150
+ <Head
151
+ icon={<EditOutlined />}
152
+ color={status === 'failed' ? MILITARY.error : MILITARY.accent}
153
+ title={spec.title}
154
+ extra={`${filledCount}/${spec.fields.length}`}
155
+ />
156
+
157
+ {spec.description && (
158
+ <div style={{ fontSize: fs(12), color: MILITARY.textSecondary, margin: '4px 0 10px', lineHeight: '18px' }}>
159
+ {spec.description}
160
+ </div>
161
+ )}
162
+
163
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
164
+ {spec.fields.map((f) => (
165
+ <Field
166
+ key={f.name}
167
+ field={f}
168
+ value={values[f.name] !== undefined ? values[f.name] : f.value}
169
+ error={errors[f.name]}
170
+ disabled={!editable}
171
+ onChange={(v) => setValue(f.name, v)}
172
+ />
173
+ ))}
174
+ </div>
175
+
176
+ {status === 'failed' && card.error && (
177
+ <div style={{ marginTop: 8, fontSize: fs(11.5), color: MILITARY.error, display: 'flex', gap: 4 }}>
178
+ <ExclamationCircleOutlined style={{ marginTop: 2 }} />
179
+ <span>{card.error}</span>
180
+ </div>
181
+ )}
182
+
183
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginTop: 12, flexWrap: 'wrap' }}>
184
+ <Button
185
+ size="small"
186
+ type="primary"
187
+ loading={status === 'submitting'}
188
+ disabled={!editable}
189
+ onClick={submit}
190
+ data-testid="ai-form-submit"
191
+ >
192
+ 提交
193
+ </Button>
194
+ <Button
195
+ size="small"
196
+ disabled={status === 'submitting'}
197
+ onClick={onCancel}
198
+ data-testid="ai-form-cancel"
199
+ >
200
+ 取消
201
+ </Button>
202
+ <span style={{ fontSize: fs(11), color: MILITARY.textMuted }}>
203
+ {spec.mode === 'fill' ? '提交后生成确认卡,确认才写库' : '提交后我会继续处理'}
204
+ </span>
205
+ </div>
206
+ </div>
207
+ );
208
+ }
209
+
210
+ /* ------------------------------ 子组件 ------------------------------ */
211
+
212
+ function cardStyle(accent: string): React.CSSProperties {
213
+ return {
214
+ marginTop: 8,
215
+ border: `1px solid ${MILITARY.softStrong}`,
216
+ borderLeft: `3px solid ${accent}`,
217
+ borderRadius: 8,
218
+ background: MILITARY.card,
219
+ padding: '10px 12px',
220
+ };
221
+ }
222
+
223
+ function Head({
224
+ icon,
225
+ color,
226
+ title,
227
+ extra,
228
+ }: {
229
+ icon: React.ReactNode;
230
+ color: string;
231
+ title: string;
232
+ extra?: string;
233
+ }) {
234
+ return (
235
+ <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
236
+ <span style={{ color, fontSize: fs(13), display: 'inline-flex' }}>{icon}</span>
237
+ <span style={{ fontSize: fs(12.5), fontWeight: 500, color: MILITARY.text, minWidth: 0 }}>
238
+ {title}
239
+ </span>
240
+ <span style={{ flex: 1 }} />
241
+ {extra && <span style={{ fontSize: fs(11), color: MILITARY.textMuted }}>{extra}</span>}
242
+ </div>
243
+ );
244
+ }
245
+
246
+ function Field({
247
+ field,
248
+ value,
249
+ error,
250
+ disabled,
251
+ onChange,
252
+ }: {
253
+ field: FormFieldSpec;
254
+ value: unknown;
255
+ error?: string;
256
+ disabled: boolean;
257
+ onChange: (v: unknown) => void;
258
+ }) {
259
+ const common = {
260
+ size: 'small' as const,
261
+ disabled,
262
+ status: (error ? 'error' : undefined) as 'error' | undefined,
263
+ style: { width: '100%' },
264
+ };
265
+
266
+ let control: React.ReactNode;
267
+ switch (field.kind) {
268
+ case 'longtext':
269
+ control = (
270
+ <TextArea
271
+ {...common}
272
+ value={value === undefined || value === null ? '' : String(value)}
273
+ placeholder={field.placeholder}
274
+ autoSize={{ minRows: 2, maxRows: 4 }}
275
+ onChange={(e) => onChange(e.target.value)}
276
+ />
277
+ );
278
+ break;
279
+ case 'number':
280
+ control = (
281
+ <InputNumber
282
+ {...common}
283
+ value={value === undefined || value === null || value === '' ? null : Number(value)}
284
+ placeholder={field.placeholder}
285
+ onChange={(v) => onChange(v ?? '')}
286
+ />
287
+ );
288
+ break;
289
+ case 'boolean':
290
+ control = (
291
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8, height: 24 }}>
292
+ <Switch size="small" checked={Boolean(value)} disabled={disabled} onChange={onChange} />
293
+ <span style={{ fontSize: fs(12), color: MILITARY.textSecondary }}>
294
+ {value ? '是' : '否'}
295
+ </span>
296
+ </div>
297
+ );
298
+ break;
299
+ case 'date':
300
+ control = (
301
+ <DatePicker
302
+ {...common}
303
+ value={value ? dayjs(String(value)) : null}
304
+ placeholder={field.placeholder}
305
+ format="YYYY-MM-DD"
306
+ onChange={(d) => onChange(d ? d.format('YYYY-MM-DD') : '')}
307
+ />
308
+ );
309
+ break;
310
+ case 'enum':
311
+ case 'fk':
312
+ control = (
313
+ <Select
314
+ {...common}
315
+ value={value === undefined || value === null || value === '' ? undefined : (value as string | number)}
316
+ placeholder={field.placeholder}
317
+ allowClear
318
+ showSearch={field.kind === 'fk'}
319
+ optionFilterProp="label"
320
+ options={(field.options ?? []).map((o) => ({ label: o.label, value: o.value }))}
321
+ onChange={(v) => onChange(v ?? '')}
322
+ notFoundContent={field.kind === 'fk' ? '没有可选数据' : '没有选项'}
323
+ />
324
+ );
325
+ break;
326
+ default:
327
+ control = (
328
+ <Input
329
+ {...common}
330
+ value={value === undefined || value === null ? '' : String(value)}
331
+ placeholder={field.placeholder}
332
+ onChange={(e) => onChange(e.target.value)}
333
+ />
334
+ );
335
+ }
336
+
337
+ return (
338
+ <div data-testid={`ai-form-field-${field.name}`} data-field-kind={field.kind}>
339
+ <div style={{ display: 'flex', alignItems: 'center', gap: 4, marginBottom: 3 }}>
340
+ <span style={{ fontSize: fs(11.5), color: MILITARY.textSecondary }}>{field.label}</span>
341
+ {field.required && <span style={{ color: MILITARY.error, fontSize: fs(11) }}>*</span>}
342
+ </div>
343
+ {control}
344
+ {error ? (
345
+ <div style={{ fontSize: fs(11), color: MILITARY.error, marginTop: 2 }}>{error}</div>
346
+ ) : field.hint ? (
347
+ <div style={{ fontSize: fs(11), color: MILITARY.textMuted, marginTop: 2 }}>{field.hint}</div>
348
+ ) : null}
349
+ </div>
350
+ );
351
+ }