index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import * as React from 'react';
  2. import { useParams, Link } from 'react-router-dom';
  3. import { observer } from 'mobx-react';
  4. import store from './store';
  5. import './style.less';
  6. import { Button, Table, TableColumnsType, Modal, TablePaginationConfig, Upload, UploadProps, message, Spin } from 'antd';
  7. import { EditOutlined, DeleteOutlined, InboxOutlined, PlusOutlined, FileOutlined, ArrowLeftOutlined } from '@ant-design/icons';
  8. import InfoModal from './components/InfoModal';
  9. import InfoModalSetting from './components/InfoModalSetting';
  10. import router from '@/router';
  11. import { Record, RecordKonwledge } from './types';
  12. import dayjs from 'dayjs';
  13. import { set } from 'mobx';
  14. import axios from 'axios';
  15. const { Dragger } = Upload;
  16. const KnowledgeLibInfo: React.FC = () => {
  17. const {
  18. state,
  19. init,
  20. onClickModify,
  21. onClickDelete,
  22. onChangePagination,
  23. onClickDocumentDetail,
  24. infoModalOnClickConfirm,
  25. infoModalOnClickCancel,
  26. infoModalSettingOnClickConfirm,
  27. infoModalSettingOnClickCancel,
  28. onClickSettings,
  29. reset
  30. } = store;
  31. const {
  32. knowledge_id,
  33. listLoading,
  34. page,
  35. list,
  36. infoModalOpen,
  37. infoModalId,
  38. infoModalSettingOpen,
  39. infoModalSettingId,
  40. knowledgeDetail,
  41. } = state;
  42. const [uploadLoading, setUploadLoading] = React.useState(false);
  43. const params = useParams();
  44. const [fileList, setFileList] = React.useState<any[]>([]);
  45. const [uploading, setUploading] = React.useState(false);
  46. const props: UploadProps = {
  47. name: 'files',
  48. multiple: true,
  49. action: '/api/deepseek/api/uploadDocument/' + params.knowledgeId,
  50. beforeUpload(file, fileList) {
  51. setUploadLoading(true);
  52. // const allowedExtensions = ['md', 'txt', 'pdf', 'jpg', 'png', 'jpeg', 'docx', 'xlsx', 'pptx', 'eml', 'csv', 'tar', 'gz', 'bz2', 'zip', 'rar', 'jar'];
  53. const allowedExtensions = ['txt', 'pdf', 'jpg', 'png', 'jpeg', 'doc', 'docx', 'ppt', 'pptx'];
  54. // 检查文件类型
  55. for (const file of fileList) {
  56. const fileExt = file.name.split('.').pop()?.toLowerCase();
  57. if (!fileExt || !allowedExtensions.includes(fileExt)) {
  58. message.error(`不支持 ${fileExt} 格式的文件上传`);
  59. setUploadLoading(false);
  60. return Upload.LIST_IGNORE;
  61. }
  62. }
  63. // 检查文件大小
  64. let totalSize = 0;
  65. for (const file of fileList) {
  66. const fileExt = file.name.split('.').pop()?.toLowerCase();
  67. const fileSizeMB = file.size / 1024 / 1024;
  68. if (fileSizeMB > 30) {
  69. message.error('单个文件不能大于30M');
  70. setUploadLoading(false);
  71. return Upload.LIST_IGNORE;
  72. }
  73. if (['jpg', 'png', 'jpeg'].includes(fileExt!) && fileSizeMB > 5) {
  74. message.error('单张图片不能大于5M');
  75. setUploadLoading(false);
  76. return Upload.LIST_IGNORE;
  77. }
  78. totalSize += fileSizeMB;
  79. }
  80. if (totalSize > 125) {
  81. message.error('文件总大小超过125M');
  82. setUploadLoading(false);
  83. return Upload.LIST_IGNORE;
  84. }
  85. },
  86. onChange(info) {
  87. const { status } = info.file;
  88. if (status !== 'uploading') {
  89. console.log(status, 'status--uploading');
  90. }
  91. if (status === 'done') {
  92. console.log(status, 'status--done');
  93. console.info(info.file.response, 'info.file.response.data');
  94. if (info.file.response.code === 200 && info.file.response.data === 1) {
  95. message.success(`${info.file.name} file uploaded successfully.`);
  96. init(params.knowledgeId);
  97. }
  98. setUploadLoading(false);
  99. } else if (status === 'error') {
  100. console.log(status, 'status--error');
  101. message.error(`${info.file.name} file upload failed.`);
  102. setUploadLoading(false);
  103. }
  104. },
  105. onDrop(e) {
  106. console.log('Dropped files', e.dataTransfer.files);
  107. },
  108. };
  109. const handleUpload = async () => {
  110. if (fileList.length === 0) return;
  111. setUploading(true);
  112. const formData = new FormData();
  113. // 添加所有文件
  114. fileList.forEach(file => {
  115. if (file.originFileObj) {
  116. formData.append('files', file.originFileObj);
  117. }
  118. });
  119. try {
  120. const res = await axios.post('/api/deepseek/api/uploadDocument/' + params.knowledgeId, formData, {
  121. headers: { 'Content-Type': 'multipart/form-data' }
  122. });
  123. message.success(`${fileList.length}个文件上传成功`);
  124. setFileList([]);
  125. } catch (err) {
  126. message.error('上传失败');
  127. } finally {
  128. setUploading(false);
  129. }
  130. };
  131. React.useEffect(() => {
  132. init(params.knowledgeId);
  133. return () => reset();
  134. }, []);
  135. const columns: TableColumnsType<Record> = [
  136. {
  137. title: '序号',
  138. dataIndex: 'index',
  139. width: 80,
  140. render: (text, record, index) => {
  141. return index + 1;
  142. }
  143. },
  144. {
  145. title: '文件名',
  146. dataIndex: 'name',
  147. width: 300,
  148. render: (text, record) => {
  149. return (
  150. `${text}`
  151. )
  152. }
  153. },
  154. {
  155. title: '文件大小',
  156. dataIndex: 'length',
  157. width: 100,
  158. render: (text) => {
  159. if (text) {
  160. const size = (text / 1024 / 1024).toFixed(2);
  161. return `${size} M`;
  162. }else{
  163. return '--'
  164. }
  165. }
  166. },
  167. {
  168. title: '字符数量',
  169. dataIndex: 'wordNum',
  170. width: 100,
  171. render: (text) => {
  172. if (text) {
  173. return `${text}`;
  174. }else{
  175. return '--'
  176. }
  177. }
  178. },
  179. {
  180. title: '分段',
  181. dataIndex: 'sliceTotal',
  182. width: 100,
  183. render: (text) => {
  184. if (text) {
  185. return `${text}`;
  186. } else {
  187. return '--';
  188. }
  189. }
  190. },
  191. {
  192. title: '上传时间',
  193. dataIndex: 'createTime',
  194. width: 180,
  195. render: (text) => {
  196. if (text) {
  197. return dayjs(text).format('YYYY-MM-DD HH:mm:ss');
  198. } else {
  199. return '--';
  200. }
  201. }
  202. },
  203. {
  204. title: '更新时间',
  205. dataIndex: 'updateTime',
  206. width: 180,
  207. render: (text) => {
  208. if (text) {
  209. return dayjs(text).format('YYYY-MM-DD HH:mm:ss');
  210. } else {
  211. return '--';
  212. }
  213. }
  214. },
  215. {
  216. title: '操作',
  217. dataIndex: 'operation',
  218. width: 180,
  219. fixed: 'right',
  220. render: (text, record) => {
  221. return (
  222. <>
  223. <a style={{ marginRight: 16 }}
  224. onClick={() => {
  225. router.navigate({ pathname: '/deepseek/knowledgeLib/slice/' + record.documentId + '/' + params.knowledgeId + '/' + state.knowledgeDetail.embeddingId });
  226. }}>
  227. 切片
  228. </a>
  229. <a
  230. style={{ marginRight: 16 }}
  231. onClick={() => {
  232. onClickSettings(record.documentId);
  233. }}
  234. >
  235. 配置
  236. </a>
  237. <a
  238. style={{ marginRight: 16 }}
  239. onClick={() => {
  240. onClickModify(record.documentId);
  241. }}>
  242. <EditOutlined />
  243. </a>
  244. <a
  245. className='text-error'
  246. onClick={() => {
  247. Modal.confirm({
  248. title: '删除',
  249. content: `确定删除知识文件:${record.name}吗?`,
  250. okType: 'danger',
  251. onOk: async () => {
  252. await onClickDelete(record.documentId);
  253. }
  254. });
  255. }}
  256. >
  257. <DeleteOutlined />
  258. </a>
  259. </>
  260. )
  261. }
  262. }
  263. ];
  264. const paginationConfig: TablePaginationConfig = {
  265. // 显示数据总量
  266. showTotal: (total: number) => {
  267. return `共 ${total} 条`;
  268. },
  269. // 展示分页条数切换
  270. showSizeChanger: true,
  271. // 指定每页显示条数
  272. pageSizeOptions: ['10', '20', '50', '100'],
  273. // 快速跳转至某页
  274. showQuickJumper: true,
  275. current: page.page,
  276. pageSize: page.size,
  277. total: page.total,
  278. onChange: async (page, pageSize) => {
  279. await onChangePagination(page, pageSize);
  280. },
  281. };
  282. return (
  283. <div className='knowledgeLibInfo'>
  284. <Spin spinning={uploadLoading || listLoading}>
  285. <div className='knowledgeLibList-operation'>
  286. {
  287. page.total === 0 &&
  288. <>
  289. <div>
  290. <Button type='primary' icon={<ArrowLeftOutlined />} onClick={() => {
  291. router.navigate(-1);
  292. }}>返回</Button>
  293. </div>
  294. <div style={{ marginTop: 20, width: '100%', height: '200px' }}>
  295. <Dragger {...props}>
  296. <p className="ant-upload-drag-icon">
  297. <InboxOutlined />
  298. </p>
  299. <p >
  300. 点击上传,或拖放文件到此处
  301. </p >
  302. <p className="ant-upload-hint">
  303. 支持文件格式txt, pdf, jpg, png, jpeg, doc, docx, ppt, pptx,
  304. 单个文档小于30M,单张图片小于5M,文件总
  305. 大小不得超过125M.
  306. </p>
  307. </Dragger>
  308. </div>
  309. </>
  310. }
  311. </div>
  312. {
  313. page.total > 0 &&
  314. <>
  315. <div style={{ display: 'flex', justifyContent: 'space-between' }}>
  316. <div>
  317. <Button type='primary' icon={<ArrowLeftOutlined />} onClick={() => {
  318. router.navigate(-1);
  319. }}>返回</Button>
  320. </div>
  321. <div>
  322. <Upload {...props}>
  323. <Button type='primary' icon={<PlusOutlined />}>上传知识文件</Button>
  324. </Upload>
  325. </div>
  326. </div>
  327. <Table
  328. scroll={{ x: 'max-content' }}
  329. rowKey={(record) => record.documentId}
  330. loading={listLoading}
  331. columns={columns}
  332. dataSource={list}
  333. pagination={paginationConfig}
  334. />
  335. {
  336. infoModalOpen &&
  337. <InfoModal
  338. id={infoModalId}
  339. open={infoModalOpen}
  340. onClickConfirm={infoModalOnClickConfirm}
  341. onClickCancel={infoModalOnClickCancel}
  342. />
  343. }
  344. {
  345. infoModalSettingOpen &&
  346. <InfoModalSetting
  347. id={infoModalSettingId}
  348. open={infoModalSettingOpen}
  349. onClickConfirm={infoModalSettingOnClickConfirm}
  350. onClickCancel={infoModalSettingOnClickCancel}
  351. />
  352. }
  353. </>
  354. }
  355. </Spin>
  356. </div>
  357. );
  358. };
  359. export default observer(KnowledgeLibInfo);