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.
- package/LICENSE +21 -0
- package/README.md +35 -2
- package/lib/sync.js +121 -0
- package/package.json +14 -4
- package/resources/.cache/ref.b64 +1 -0
- package/resources/README.md +97 -0
- package/resources/_.env +6 -0
- package/resources/_.env.example +25 -0
- package/resources/_.gitignore +18 -0
- package/resources/_.lintstagedrc.json +9 -0
- package/resources/_.npmrc +5 -0
- package/resources/_.prettierrc +7 -0
- package/resources/commitlint.config.mjs +1 -0
- package/resources/content/docs/README.md +63 -0
- package/resources/content/docs/faq.md +107 -0
- package/resources/content/docs/features/_meta.json +5 -0
- package/resources/content/docs/features/ai.md +71 -0
- package/resources/content/docs/features/appearance.md +47 -0
- package/resources/content/docs/features/feedback.md +79 -0
- package/resources/content/docs/features/files.md +74 -0
- package/resources/content/docs/features/graph.md +66 -0
- package/resources/content/docs/features/inbox.md +61 -0
- package/resources/content/docs/guide/_meta.json +5 -0
- package/resources/content/docs/guide/account.md +76 -0
- package/resources/content/docs/guide/data.md +87 -0
- package/resources/content/docs/guide/navigation.md +81 -0
- package/resources/content/docs/guide/permissions.md +67 -0
- package/resources/content/docs/guide/system.md +92 -0
- package/resources/content/docs/quick-start.md +70 -0
- package/resources/content/docs/shortcuts.md +69 -0
- package/resources/eslint.config.mjs +17 -0
- package/resources/next-env.d.ts +6 -0
- package/resources/next.config.ts +13 -0
- package/resources/postcss.config.mjs +7 -0
- package/resources/prisma/org.config.ts +98 -0
- package/resources/prisma/schema.prisma +336 -0
- package/resources/prisma/seed.ts +721 -0
- package/resources/prisma/seedSafe.ts +347 -0
- package/resources/prisma.config.ts +14 -0
- package/resources/scripts/check-config.mjs +773 -0
- package/resources/scripts/lib/schema-graph.mjs +410 -0
- package/resources/scripts/patch-next-trace.mjs +86 -0
- package/resources/scripts/suggest-relations.mjs +105 -0
- package/resources/src/app/(admin)/ai/page.tsx +469 -0
- package/resources/src/app/(admin)/dashboard/page.tsx +260 -0
- package/resources/src/app/(admin)/demo/employees/page.tsx +508 -0
- package/resources/src/app/(admin)/demo/files/page.tsx +451 -0
- package/resources/src/app/(admin)/demo/loading/page.tsx +513 -0
- package/resources/src/app/(admin)/error.tsx +96 -0
- package/resources/src/app/(admin)/feedback/page.tsx +573 -0
- package/resources/src/app/(admin)/graph/page.tsx +1224 -0
- package/resources/src/app/(admin)/inbox/page.tsx +31 -0
- package/resources/src/app/(admin)/layout.tsx +58 -0
- package/resources/src/app/(admin)/loading.tsx +31 -0
- package/resources/src/app/(admin)/system/depts/page.tsx +415 -0
- package/resources/src/app/(admin)/system/logs/page.tsx +215 -0
- package/resources/src/app/(admin)/system/menus/page.tsx +403 -0
- package/resources/src/app/(admin)/system/roles/page.tsx +433 -0
- package/resources/src/app/(admin)/system/users/page.tsx +620 -0
- package/resources/src/app/api/account-recovery/route.ts +132 -0
- package/resources/src/app/api/ai/act/route.ts +70 -0
- package/resources/src/app/api/ai/ask/route.ts +82 -0
- package/resources/src/app/api/ai/chat/route.ts +404 -0
- package/resources/src/app/api/ai/report/route.ts +164 -0
- package/resources/src/app/api/auth/[...nextauth]/route.ts +3 -0
- package/resources/src/app/api/auth/change-password/route.ts +57 -0
- package/resources/src/app/api/auth/forgot/route.ts +98 -0
- package/resources/src/app/api/auth/register/route.ts +95 -0
- package/resources/src/app/api/auth/register-config/route.ts +51 -0
- package/resources/src/app/api/badges/route.ts +39 -0
- package/resources/src/app/api/depts/[id]/route.ts +145 -0
- package/resources/src/app/api/depts/route.ts +126 -0
- package/resources/src/app/api/docs/export/route.ts +87 -0
- package/resources/src/app/api/docs/parse/route.ts +57 -0
- package/resources/src/app/api/docs/search/route.ts +22 -0
- package/resources/src/app/api/employees/[id]/route.ts +147 -0
- package/resources/src/app/api/employees/export/route.ts +115 -0
- package/resources/src/app/api/employees/import/route.ts +205 -0
- package/resources/src/app/api/employees/route.ts +149 -0
- package/resources/src/app/api/export/route.ts +254 -0
- package/resources/src/app/api/favorites/route.ts +56 -0
- package/resources/src/app/api/feedback/ai/route.ts +31 -0
- package/resources/src/app/api/feedback/approve/route.ts +67 -0
- package/resources/src/app/api/feedback/report/route.ts +213 -0
- package/resources/src/app/api/feedback/route.ts +246 -0
- package/resources/src/app/api/files/[name]/route.ts +82 -0
- package/resources/src/app/api/graph/focus/route.ts +75 -0
- package/resources/src/app/api/graph/node/move/route.ts +48 -0
- package/resources/src/app/api/graph/node/route.ts +274 -0
- package/resources/src/app/api/graph/route.ts +272 -0
- package/resources/src/app/api/health/route.ts +11 -0
- package/resources/src/app/api/inbox/read/route.ts +36 -0
- package/resources/src/app/api/inbox/summary/route.ts +15 -0
- package/resources/src/app/api/logs/route.ts +82 -0
- package/resources/src/app/api/menus/[id]/route.ts +132 -0
- package/resources/src/app/api/menus/mine/route.ts +23 -0
- package/resources/src/app/api/menus/route.ts +89 -0
- package/resources/src/app/api/roles/[id]/menus/route.ts +73 -0
- package/resources/src/app/api/roles/[id]/route.ts +96 -0
- package/resources/src/app/api/roles/route.ts +114 -0
- package/resources/src/app/api/search/route.ts +22 -0
- package/resources/src/app/api/stats/route.ts +92 -0
- package/resources/src/app/api/upload/route.ts +38 -0
- package/resources/src/app/api/users/[id]/password/route.ts +55 -0
- package/resources/src/app/api/users/[id]/route.ts +145 -0
- package/resources/src/app/api/users/route.ts +153 -0
- package/resources/src/app/api/visits/route.ts +48 -0
- package/resources/src/app/change-password/ChangePasswordForm.tsx +174 -0
- package/resources/src/app/change-password/page.tsx +19 -0
- package/resources/src/app/docs/[[...slug]]/page.tsx +56 -0
- package/resources/src/app/docs/layout.tsx +34 -0
- package/resources/src/app/docs/loading.tsx +19 -0
- package/resources/src/app/docs/not-found.tsx +25 -0
- package/resources/src/app/forgot/page.tsx +392 -0
- package/resources/src/app/global-error.tsx +75 -0
- package/resources/src/app/globals.css +235 -0
- package/resources/src/app/layout.tsx +44 -0
- package/resources/src/app/login/page.tsx +267 -0
- package/resources/src/app/page.tsx +14 -0
- package/resources/src/app/register/page.tsx +237 -0
- package/resources/src/auth.ts +215 -0
- package/resources/src/components/AiPanel.tsx +970 -0
- package/resources/src/components/AuthContext.tsx +82 -0
- package/resources/src/components/DataTable.tsx +459 -0
- package/resources/src/components/EChart.tsx +140 -0
- package/resources/src/components/FeedbackAiModal.tsx +476 -0
- package/resources/src/components/FeedbackReportModal.tsx +479 -0
- package/resources/src/components/Markdown.tsx +532 -0
- package/resources/src/components/OntologyGraph.tsx +1196 -0
- package/resources/src/components/Providers.tsx +99 -0
- package/resources/src/components/VisitTracker.tsx +89 -0
- package/resources/src/components/admin/AdminShell.tsx +749 -0
- package/resources/src/components/admin/CountBadge.tsx +67 -0
- package/resources/src/components/admin/GlobalSearch.tsx +509 -0
- package/resources/src/components/admin/InboxBell.tsx +78 -0
- package/resources/src/components/admin/VisitStats.tsx +145 -0
- package/resources/src/components/admin/menu-icons.tsx +56 -0
- package/resources/src/components/ai/AiInputForm.tsx +351 -0
- package/resources/src/components/appearance/AppearanceDrawer.tsx +268 -0
- package/resources/src/components/appearance/AppearanceProvider.tsx +160 -0
- package/resources/src/components/docs/DocArticle.tsx +106 -0
- package/resources/src/components/docs/DocToc.tsx +100 -0
- package/resources/src/components/docs/DocsChrome.tsx +259 -0
- package/resources/src/components/docs/DocsHome.tsx +209 -0
- package/resources/src/components/docs/DocsSearch.tsx +365 -0
- package/resources/src/components/docs/docs.css +1458 -0
- package/resources/src/components/feedback/AsyncButton.tsx +108 -0
- package/resources/src/components/feedback/LoadingOverlay.tsx +125 -0
- package/resources/src/components/feedback/PressableCard.tsx +92 -0
- package/resources/src/components/feedback/README.md +97 -0
- package/resources/src/components/feedback/RouteProgress.tsx +108 -0
- package/resources/src/components/feedback/Skeleton.tsx +249 -0
- package/resources/src/components/feedback/StatCard.tsx +70 -0
- package/resources/src/components/feedback/TaskHint.tsx +153 -0
- package/resources/src/components/feedback/index.ts +93 -0
- package/resources/src/components/feedback/motion.css +204 -0
- package/resources/src/components/feedback/motion.ts +63 -0
- package/resources/src/components/feedback/useCountUp.ts +110 -0
- package/resources/src/components/feedback/usePending.ts +209 -0
- package/resources/src/components/feedback/useRowFlash.ts +40 -0
- package/resources/src/components/inbox/InboxView.tsx +409 -0
- package/resources/src/components/ui/button.tsx +35 -0
- package/resources/src/components/ui/card.tsx +37 -0
- package/resources/src/components/ui/input.tsx +19 -0
- package/resources/src/lib/ai.ts +310 -0
- package/resources/src/lib/aiContext.ts +357 -0
- package/resources/src/lib/aiForm.test.ts +191 -0
- package/resources/src/lib/aiForm.ts +532 -0
- package/resources/src/lib/aiFormTypes.ts +83 -0
- package/resources/src/lib/aiToken.ts +72 -0
- package/resources/src/lib/aiTools.ts +823 -0
- package/resources/src/lib/api.ts +160 -0
- package/resources/src/lib/appInfo.ts +128 -0
- package/resources/src/lib/appearance.ts +643 -0
- package/resources/src/lib/badge-count.ts +146 -0
- package/resources/src/lib/badges.test.ts +58 -0
- package/resources/src/lib/badges.ts +79 -0
- package/resources/src/lib/brand-mark.test.ts +87 -0
- package/resources/src/lib/brand-mark.ts +45 -0
- package/resources/src/lib/docs-shared.test.ts +148 -0
- package/resources/src/lib/docs-shared.ts +422 -0
- package/resources/src/lib/docs.ts +381 -0
- package/resources/src/lib/err-text.ts +21 -0
- package/resources/src/lib/excel.ts +152 -0
- package/resources/src/lib/favorite-path.ts +60 -0
- package/resources/src/lib/favorites.ts +59 -0
- package/resources/src/lib/features.ts +69 -0
- package/resources/src/lib/feedback-access.ts +17 -0
- package/resources/src/lib/feedbackAi.ts +1621 -0
- package/resources/src/lib/formRules.ts +76 -0
- package/resources/src/lib/format.ts +52 -0
- package/resources/src/lib/graph-cascade.ts +304 -0
- package/resources/src/lib/graph-focus.ts +301 -0
- package/resources/src/lib/graph-link.ts +17 -0
- package/resources/src/lib/graph-node-write.ts +150 -0
- package/resources/src/lib/graph-schema.ts +475 -0
- package/resources/src/lib/graph-scope.test.ts +46 -0
- package/resources/src/lib/graph-scope.ts +28 -0
- package/resources/src/lib/http.ts +157 -0
- package/resources/src/lib/inbox-types.ts +57 -0
- package/resources/src/lib/inbox.ts +55 -0
- package/resources/src/lib/menu-service.ts +204 -0
- package/resources/src/lib/notifications.ts +113 -0
- package/resources/src/lib/office.ts +531 -0
- package/resources/src/lib/ontology.ts +277 -0
- package/resources/src/lib/openaiCompat.ts +257 -0
- package/resources/src/lib/pending.ts +98 -0
- package/resources/src/lib/prisma.ts +30 -0
- package/resources/src/lib/rbac.test.ts +106 -0
- package/resources/src/lib/rbac.ts +185 -0
- package/resources/src/lib/search-data.ts +158 -0
- package/resources/src/lib/security-questions.ts +20 -0
- package/resources/src/lib/skin-theme.ts +155 -0
- package/resources/src/lib/system-config.ts +55 -0
- package/resources/src/lib/theme.ts +96 -0
- package/resources/src/lib/todos/feedback.ts +86 -0
- package/resources/src/lib/todos/index.ts +73 -0
- package/resources/src/lib/todos/types.ts +33 -0
- package/resources/src/lib/upload.ts +106 -0
- package/resources/src/lib/utils.test.ts +9 -0
- package/resources/src/lib/utils.ts +6 -0
- package/resources/src/lib/validation.test.ts +297 -0
- package/resources/src/lib/validation.ts +658 -0
- package/resources/src/lib/visit-stats.ts +109 -0
- package/resources/src/lib/visits.ts +111 -0
- package/resources/src/lib/zodField.ts +44 -0
- package/resources/tsconfig.json +41 -0
- package/resources/vitest.config.ts +16 -0
- package/resources/vitest.setup.ts +1 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { APP_NAME } from '@/lib/appInfo';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import {
|
|
7
|
+
App,
|
|
8
|
+
Alert,
|
|
9
|
+
Button,
|
|
10
|
+
Card,
|
|
11
|
+
Col,
|
|
12
|
+
Descriptions,
|
|
13
|
+
Empty,
|
|
14
|
+
Row,
|
|
15
|
+
Segmented,
|
|
16
|
+
Space,
|
|
17
|
+
Spin,
|
|
18
|
+
Tag,
|
|
19
|
+
Typography,
|
|
20
|
+
Upload,
|
|
21
|
+
} from 'antd';
|
|
22
|
+
import {
|
|
23
|
+
FileExcelOutlined,
|
|
24
|
+
FilePdfOutlined,
|
|
25
|
+
FileTextOutlined,
|
|
26
|
+
FileWordOutlined,
|
|
27
|
+
InboxOutlined,
|
|
28
|
+
ReloadOutlined,
|
|
29
|
+
} from '@ant-design/icons';
|
|
30
|
+
import { fs, MILITARY } from '@/lib/theme';
|
|
31
|
+
import { formatElapsed, useElapsed } from '@/components/feedback';
|
|
32
|
+
|
|
33
|
+
const { Dragger } = Upload;
|
|
34
|
+
const { Paragraph, Text, Title } = Typography;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 文档处理演示页。
|
|
38
|
+
*
|
|
39
|
+
* 这是整个模板里技术含量最高的一个功能,所以界面上要把「过程」也讲清楚:
|
|
40
|
+
* - 顶部一句话说明支持哪些格式、哪些格式需要 LibreOffice
|
|
41
|
+
* - 解析结果分左右两栏:左边纯文本(机器可读的形态)、右边 HTML(还原排版)
|
|
42
|
+
* - 底部反向演示「生成」:把解析出来的内容再变成 Word / PDF 下载走
|
|
43
|
+
* 一进一出闭环,评审一眼就能看懂这个模块解决了什么问题。
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
type ParseResult = {
|
|
47
|
+
filename: string;
|
|
48
|
+
kind: 'word' | 'excel' | 'pdf' | 'text';
|
|
49
|
+
text: string;
|
|
50
|
+
html: string | null;
|
|
51
|
+
sheets: string[] | null;
|
|
52
|
+
meta: Record<string, unknown>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
type Section = {
|
|
56
|
+
heading?: string;
|
|
57
|
+
level?: number;
|
|
58
|
+
paragraphs?: string[];
|
|
59
|
+
table?: { headers: string[]; rows: string[][] };
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const KIND_META: Record<string, { text: string; color: string }> = {
|
|
63
|
+
word: { text: 'Word 文档', color: 'blue' },
|
|
64
|
+
excel: { text: 'Excel 表格', color: 'green' },
|
|
65
|
+
pdf: { text: 'PDF', color: 'red' },
|
|
66
|
+
text: { text: '纯文本', color: 'default' },
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** 没上传文件时的示例内容,保证「生成」按钮任何时候都有东西可导 */
|
|
70
|
+
const SAMPLE_SECTIONS: Section[] = [
|
|
71
|
+
{
|
|
72
|
+
heading: '一、模板说明',
|
|
73
|
+
level: 1,
|
|
74
|
+
paragraphs: [
|
|
75
|
+
`本文件由${APP_NAME}自动生成,用于演示服务端文档导出能力。`,
|
|
76
|
+
'Word 走 docx 库直接生成 OOXML;PDF 中文走 HTML + LibreOffice 转换,纯英文走 pdf-lib。',
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
heading: '二、能力清单',
|
|
81
|
+
level: 1,
|
|
82
|
+
table: {
|
|
83
|
+
headers: ['能力', '依赖', '说明'],
|
|
84
|
+
rows: [
|
|
85
|
+
['解析 Word', 'mammoth', '输出纯文本 + HTML 预览'],
|
|
86
|
+
['解析 Excel', 'exceljs', '按工作表输出内容'],
|
|
87
|
+
['解析 PDF', 'pdf-lib (+ pdf-parse)', '元信息必出,正文可选'],
|
|
88
|
+
['生成 Word', 'docx', '标题层级 / 段落 / 表格'],
|
|
89
|
+
['生成 PDF', 'LibreOffice 或 pdf-lib', '中文需 LibreOffice'],
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
async function errMsg(e: unknown): Promise<string> {
|
|
96
|
+
if (axios.isAxiosError(e)) {
|
|
97
|
+
const d = e.response?.data;
|
|
98
|
+
// 错误响应是 blob 时(导出接口),要先把 Blob 读成文本
|
|
99
|
+
if (d instanceof Blob) {
|
|
100
|
+
try {
|
|
101
|
+
const j = JSON.parse(await d.text()) as { message?: string };
|
|
102
|
+
return j.message || '请求失败';
|
|
103
|
+
} catch {
|
|
104
|
+
return '请求失败';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return (d as { message?: string } | undefined)?.message || e.message;
|
|
108
|
+
}
|
|
109
|
+
return e instanceof Error ? e.message : String(e);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function downloadBlob(data: Blob, filename: string) {
|
|
113
|
+
const url = window.URL.createObjectURL(data);
|
|
114
|
+
const a = document.createElement('a');
|
|
115
|
+
a.href = url;
|
|
116
|
+
a.download = filename;
|
|
117
|
+
document.body.appendChild(a);
|
|
118
|
+
a.click();
|
|
119
|
+
a.remove();
|
|
120
|
+
window.URL.revokeObjectURL(url);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default function FilesDemoPage() {
|
|
124
|
+
const { message } = App.useApp();
|
|
125
|
+
const [parsing, setParsing] = useState(false);
|
|
126
|
+
const [result, setResult] = useState<ParseResult | null>(null);
|
|
127
|
+
const [exporting, setExporting] = useState<'' | 'docx' | 'pdf'>('');
|
|
128
|
+
const [view, setView] = useState<'text' | 'html'>('text');
|
|
129
|
+
/** 上传百分比(0~99)。到 99 就封顶 —— 之后是服务端解析,不再有百分比可报 */
|
|
130
|
+
const [uploadPct, setUploadPct] = useState(0);
|
|
131
|
+
const parseElapsed = useElapsed(parsing);
|
|
132
|
+
|
|
133
|
+
const parseFile = async (file: File) => {
|
|
134
|
+
const fd = new FormData();
|
|
135
|
+
fd.append('file', file);
|
|
136
|
+
setParsing(true);
|
|
137
|
+
setUploadPct(0);
|
|
138
|
+
try {
|
|
139
|
+
const { data } = await axios.post('/api/docs/parse', fd, {
|
|
140
|
+
// ★ 上传大文件时唯一的真实进度来源。
|
|
141
|
+
// 没有它,30MB 的文件在局域网也要几秒,用户只看到转圈会怀疑没传上去。
|
|
142
|
+
onUploadProgress: (evt) => {
|
|
143
|
+
if (evt.total) {
|
|
144
|
+
setUploadPct(Math.min(99, Math.round((evt.loaded / evt.total) * 100)));
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
if (!data || data.code !== 0) throw new Error(data?.message || '解析失败');
|
|
149
|
+
setResult(data.data as ParseResult);
|
|
150
|
+
setView('text');
|
|
151
|
+
message.success('解析完成');
|
|
152
|
+
} catch (e) {
|
|
153
|
+
message.error(await errMsg(e));
|
|
154
|
+
} finally {
|
|
155
|
+
setParsing(false);
|
|
156
|
+
setUploadPct(0);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** 有解析结果就把结果做成文档,没有就用示例内容 —— 按钮永远可用 */
|
|
161
|
+
const buildSections = (): Section[] => {
|
|
162
|
+
if (!result) return SAMPLE_SECTIONS;
|
|
163
|
+
|
|
164
|
+
const lines = result.text.split(/\r?\n/).filter((l) => l.trim()).slice(0, 60);
|
|
165
|
+
const metaRows = Object.entries(result.meta).map(([k, v]) => [
|
|
166
|
+
k,
|
|
167
|
+
typeof v === 'string' ? v : JSON.stringify(v),
|
|
168
|
+
]);
|
|
169
|
+
|
|
170
|
+
return [
|
|
171
|
+
{
|
|
172
|
+
heading: `来源文件:${result.filename}`,
|
|
173
|
+
level: 1,
|
|
174
|
+
paragraphs: lines.length ? lines : ['(文件内容为空)'],
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
heading: '解析元信息',
|
|
178
|
+
level: 1,
|
|
179
|
+
table: { headers: ['字段', '值'], rows: metaRows },
|
|
180
|
+
},
|
|
181
|
+
];
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const exportDoc = async (type: 'docx' | 'pdf') => {
|
|
185
|
+
setExporting(type);
|
|
186
|
+
try {
|
|
187
|
+
const res = await axios.post(
|
|
188
|
+
'/api/docs/export',
|
|
189
|
+
{
|
|
190
|
+
type,
|
|
191
|
+
title: result ? `文档解析报告 - ${result.filename}` : `${APP_NAME} - 文档导出示例`,
|
|
192
|
+
sections: buildSections(),
|
|
193
|
+
},
|
|
194
|
+
{ responseType: 'blob' }
|
|
195
|
+
);
|
|
196
|
+
const ct = String(res.headers['content-type'] || '');
|
|
197
|
+
if (ct.includes('application/json')) {
|
|
198
|
+
const j = JSON.parse(await (res.data as Blob).text()) as { message?: string };
|
|
199
|
+
throw new Error(j.message || '生成失败');
|
|
200
|
+
}
|
|
201
|
+
downloadBlob(res.data as Blob, type === 'docx' ? '导出文档.docx' : '导出文档.pdf');
|
|
202
|
+
message.success(type === 'docx' ? 'Word 文档已生成' : 'PDF 已生成');
|
|
203
|
+
} catch (e) {
|
|
204
|
+
message.error(`生成失败:${await errMsg(e)}`);
|
|
205
|
+
} finally {
|
|
206
|
+
setExporting('');
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
return (
|
|
211
|
+
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
|
|
212
|
+
<Alert
|
|
213
|
+
type="info"
|
|
214
|
+
showIcon
|
|
215
|
+
title="文档解析与生成"
|
|
216
|
+
description={
|
|
217
|
+
<span>
|
|
218
|
+
支持 <b>.docx / .xlsx / .pdf / .txt / .md / .csv</b> 直接解析;
|
|
219
|
+
<b>.doc / .xls</b> 属于老式二进制格式,需要本机安装 LibreOffice 才能转换。
|
|
220
|
+
生成中文 PDF 同样依赖 LibreOffice,纯英文可用内置字体直接生成。
|
|
221
|
+
</span>
|
|
222
|
+
}
|
|
223
|
+
/>
|
|
224
|
+
|
|
225
|
+
<Card variant="borderless" style={{ boxShadow: MILITARY.shadowSm }}>
|
|
226
|
+
<Dragger
|
|
227
|
+
accept=".doc,.docx,.xls,.xlsx,.pdf,.txt,.md,.csv"
|
|
228
|
+
multiple={false}
|
|
229
|
+
showUploadList={false}
|
|
230
|
+
disabled={parsing}
|
|
231
|
+
beforeUpload={(file) => {
|
|
232
|
+
void parseFile(file as unknown as File);
|
|
233
|
+
return false; // 交给自定义逻辑处理,不走 antd 的自动上传
|
|
234
|
+
}}
|
|
235
|
+
>
|
|
236
|
+
<p className="ant-upload-drag-icon">
|
|
237
|
+
<InboxOutlined />
|
|
238
|
+
</p>
|
|
239
|
+
<p className="ant-upload-text">点击或拖拽文件到此处上传解析</p>
|
|
240
|
+
<p className="ant-upload-hint">
|
|
241
|
+
单个文件不超过 30MB。文档只在内存里解析,不会落盘保留。
|
|
242
|
+
</p>
|
|
243
|
+
</Dragger>
|
|
244
|
+
</Card>
|
|
245
|
+
|
|
246
|
+
{/*
|
|
247
|
+
两段式等待,是"耗时操作"最该讲清楚的一种:
|
|
248
|
+
第一段有真实百分比(浏览器知道上传了多少字节)
|
|
249
|
+
第二段没有百分比(服务端在解析,可能是 LibreOffice 在起进程)
|
|
250
|
+
所以第一段给确定进度条,第二段换成"不确定进度条 + 已等待秒数"。
|
|
251
|
+
明明不知道还剩多久,就不要假装知道 —— 这比一个假的 99% 更让人信服。
|
|
252
|
+
*/}
|
|
253
|
+
{parsing ? (
|
|
254
|
+
<Card variant="borderless" style={{ boxShadow: MILITARY.shadowSm }}>
|
|
255
|
+
<div
|
|
256
|
+
style={{
|
|
257
|
+
display: 'flex',
|
|
258
|
+
flexDirection: 'column',
|
|
259
|
+
alignItems: 'center',
|
|
260
|
+
gap: 10,
|
|
261
|
+
padding: '36px 16px',
|
|
262
|
+
}}
|
|
263
|
+
>
|
|
264
|
+
<Spin size="large" />
|
|
265
|
+
<div style={{ fontSize: fs(13.5), color: MILITARY.textSecondary }}>
|
|
266
|
+
{uploadPct > 0 && uploadPct < 99
|
|
267
|
+
? `正在上传文件… ${uploadPct}%`
|
|
268
|
+
: '上传完成,正在解析文档…'}
|
|
269
|
+
</div>
|
|
270
|
+
<div style={{ fontSize: fs(12), color: MILITARY.textMuted, textAlign: 'center' }}>
|
|
271
|
+
{uploadPct > 0 && uploadPct < 99
|
|
272
|
+
? '大文件传输中,请勿关闭页面'
|
|
273
|
+
: `已等待 ${formatElapsed(parseElapsed)} · 老式 .doc / .xls 需调用 LibreOffice,会慢一些`}
|
|
274
|
+
</div>
|
|
275
|
+
|
|
276
|
+
<div
|
|
277
|
+
style={{
|
|
278
|
+
width: 260,
|
|
279
|
+
height: 4,
|
|
280
|
+
borderRadius: 999,
|
|
281
|
+
background: MILITARY.soft,
|
|
282
|
+
overflow: 'hidden',
|
|
283
|
+
marginTop: 2,
|
|
284
|
+
}}
|
|
285
|
+
>
|
|
286
|
+
{uploadPct > 0 && uploadPct < 99 ? (
|
|
287
|
+
<div
|
|
288
|
+
style={{
|
|
289
|
+
width: `${uploadPct}%`,
|
|
290
|
+
height: '100%',
|
|
291
|
+
background: `linear-gradient(90deg, ${MILITARY.primary}, ${MILITARY.accent})`,
|
|
292
|
+
transition: 'width .25s ease',
|
|
293
|
+
}}
|
|
294
|
+
/>
|
|
295
|
+
) : (
|
|
296
|
+
<div
|
|
297
|
+
className="wb-indeterminate-bar"
|
|
298
|
+
style={{
|
|
299
|
+
width: '38%',
|
|
300
|
+
height: '100%',
|
|
301
|
+
background: `linear-gradient(90deg, ${MILITARY.primaryHover}, ${MILITARY.accent})`,
|
|
302
|
+
}}
|
|
303
|
+
/>
|
|
304
|
+
)}
|
|
305
|
+
</div>
|
|
306
|
+
</div>
|
|
307
|
+
</Card>
|
|
308
|
+
) : null}
|
|
309
|
+
|
|
310
|
+
{result && !parsing ? (
|
|
311
|
+
<Card
|
|
312
|
+
variant="borderless"
|
|
313
|
+
style={{ boxShadow: MILITARY.shadowSm }}
|
|
314
|
+
title={
|
|
315
|
+
<Space size={8}>
|
|
316
|
+
<span>{result.filename}</span>
|
|
317
|
+
<Tag color={KIND_META[result.kind]?.color ?? 'default'}>
|
|
318
|
+
{KIND_META[result.kind]?.text ?? result.kind}
|
|
319
|
+
</Tag>
|
|
320
|
+
{result.sheets?.length ? (
|
|
321
|
+
<Tag icon={<FileExcelOutlined />}>{result.sheets.join(' / ')}</Tag>
|
|
322
|
+
) : null}
|
|
323
|
+
</Space>
|
|
324
|
+
}
|
|
325
|
+
extra={
|
|
326
|
+
<Segmented
|
|
327
|
+
value={view}
|
|
328
|
+
onChange={(v) => setView(v as 'text' | 'html')}
|
|
329
|
+
options={[
|
|
330
|
+
{ label: '纯文本', value: 'text' },
|
|
331
|
+
{ label: 'HTML 预览', value: 'html' },
|
|
332
|
+
]}
|
|
333
|
+
/>
|
|
334
|
+
}
|
|
335
|
+
>
|
|
336
|
+
<Row gutter={16}>
|
|
337
|
+
{/* 左:纯文本,大屏才并排;小屏下 Segmented 决定看哪一栏 */}
|
|
338
|
+
<Col xs={24} lg={12} className={view === 'text' ? '' : 'hidden lg:block'}>
|
|
339
|
+
<Title level={5} style={{ marginTop: 0 }}>
|
|
340
|
+
<FileTextOutlined /> 提取的纯文本
|
|
341
|
+
</Title>
|
|
342
|
+
<pre
|
|
343
|
+
style={{
|
|
344
|
+
margin: 0,
|
|
345
|
+
padding: 12,
|
|
346
|
+
// 用皮肤变量而不是写死的浅灰:炫酷皮肤是深底,
|
|
347
|
+
// 写死 #f6f8fa 会让正文变成"浅底浅字",整块读不出来
|
|
348
|
+
background: MILITARY.soft,
|
|
349
|
+
color: MILITARY.text,
|
|
350
|
+
borderRadius: 6,
|
|
351
|
+
height: 420,
|
|
352
|
+
overflow: 'auto',
|
|
353
|
+
whiteSpace: 'pre-wrap',
|
|
354
|
+
wordBreak: 'break-word',
|
|
355
|
+
fontSize: fs(13),
|
|
356
|
+
lineHeight: 1.8,
|
|
357
|
+
}}
|
|
358
|
+
>
|
|
359
|
+
{result.text || '(空)'}
|
|
360
|
+
</pre>
|
|
361
|
+
</Col>
|
|
362
|
+
|
|
363
|
+
<Col xs={24} lg={12} className={view === 'html' ? '' : 'hidden lg:block'}>
|
|
364
|
+
<Title level={5} style={{ marginTop: 0 }}>
|
|
365
|
+
<FileWordOutlined /> 富文本预览
|
|
366
|
+
</Title>
|
|
367
|
+
<div
|
|
368
|
+
style={{
|
|
369
|
+
padding: 12,
|
|
370
|
+
border: `1px solid ${MILITARY.borderSoft}`,
|
|
371
|
+
borderRadius: 6,
|
|
372
|
+
height: 420,
|
|
373
|
+
overflow: 'auto',
|
|
374
|
+
background: MILITARY.card,
|
|
375
|
+
}}
|
|
376
|
+
>
|
|
377
|
+
{result.html ? (
|
|
378
|
+
// 内容来自服务端 mammoth / 转义处理,这里直接渲染以还原原始排版
|
|
379
|
+
<div dangerouslySetInnerHTML={{ __html: result.html }} />
|
|
380
|
+
) : (
|
|
381
|
+
<Empty
|
|
382
|
+
image={Empty.PRESENTED_IMAGE_SIMPLE}
|
|
383
|
+
description="该格式没有富文本形态(Excel / PDF 只提供结构化文本)"
|
|
384
|
+
/>
|
|
385
|
+
)}
|
|
386
|
+
</div>
|
|
387
|
+
</Col>
|
|
388
|
+
</Row>
|
|
389
|
+
|
|
390
|
+
<Descriptions
|
|
391
|
+
title="解析元信息"
|
|
392
|
+
size="small"
|
|
393
|
+
bordered
|
|
394
|
+
column={{ xs: 1, sm: 2, lg: 3 }}
|
|
395
|
+
style={{ marginTop: 20 }}
|
|
396
|
+
>
|
|
397
|
+
{Object.entries(result.meta).map(([k, v]) => (
|
|
398
|
+
<Descriptions.Item key={k} label={k}>
|
|
399
|
+
{typeof v === 'string' ? v : JSON.stringify(v)}
|
|
400
|
+
</Descriptions.Item>
|
|
401
|
+
))}
|
|
402
|
+
</Descriptions>
|
|
403
|
+
</Card>
|
|
404
|
+
) : null}
|
|
405
|
+
|
|
406
|
+
<Card
|
|
407
|
+
variant="borderless"
|
|
408
|
+
style={{ boxShadow: MILITARY.shadowSm }}
|
|
409
|
+
title="反向生成:把内容导出为文档"
|
|
410
|
+
>
|
|
411
|
+
<Paragraph type="secondary" style={{ marginTop: 0 }}>
|
|
412
|
+
没有上传文件时,会导出一份内置的示例文档;上传解析后,导出的就是这份解析报告,
|
|
413
|
+
包含正文与元信息表格。可以借此验证「读进来 → 写出去」的完整链路。
|
|
414
|
+
</Paragraph>
|
|
415
|
+
<Space wrap>
|
|
416
|
+
<Button
|
|
417
|
+
type="primary"
|
|
418
|
+
icon={<FileWordOutlined />}
|
|
419
|
+
loading={exporting === 'docx'}
|
|
420
|
+
onClick={() => exportDoc('docx')}
|
|
421
|
+
>
|
|
422
|
+
生成 Word 文档
|
|
423
|
+
</Button>
|
|
424
|
+
<Button
|
|
425
|
+
icon={<FilePdfOutlined />}
|
|
426
|
+
loading={exporting === 'pdf'}
|
|
427
|
+
onClick={() => exportDoc('pdf')}
|
|
428
|
+
>
|
|
429
|
+
生成 PDF
|
|
430
|
+
</Button>
|
|
431
|
+
{result ? (
|
|
432
|
+
<Button
|
|
433
|
+
icon={<ReloadOutlined />}
|
|
434
|
+
onClick={() => {
|
|
435
|
+
setResult(null);
|
|
436
|
+
setView('text');
|
|
437
|
+
}}
|
|
438
|
+
>
|
|
439
|
+
清空当前结果
|
|
440
|
+
</Button>
|
|
441
|
+
) : null}
|
|
442
|
+
</Space>
|
|
443
|
+
<div style={{ marginTop: 12 }}>
|
|
444
|
+
<Text type="secondary" style={{ fontSize: fs(12) }}>
|
|
445
|
+
提示:中文 PDF 依赖 LibreOffice。若环境未安装,会退回生成一份英文占位 PDF,并在正文里说明原因。
|
|
446
|
+
</Text>
|
|
447
|
+
</div>
|
|
448
|
+
</Card>
|
|
449
|
+
</div>
|
|
450
|
+
);
|
|
451
|
+
}
|