index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import * as React from 'react';
  2. import { observer } from 'mobx-react';
  3. import { useParams } from 'react-router-dom';
  4. import { Form, Input, Button, message } from 'antd';
  5. import { apis } from '@/apis';
  6. import router from '@/router';
  7. import { ArrowLeftOutlined } from '@ant-design/icons';
  8. const { TextArea } = Input;
  9. const FormItem = Form.Item;
  10. interface TakaiSliceDetailRequest {
  11. knowledgeId: string,
  12. sliceId: string,
  13. sliceText: string,
  14. }
  15. const SliceDetail: React.FC = () => {
  16. const [form] = Form.useForm();
  17. const params = useParams();
  18. const [listLoading, setListLoading] = React.useState(false);
  19. const appApi = {
  20. fetchList: async () => {
  21. setListLoading(true);
  22. try {
  23. if (!params.sliceId || !params.knowledgeId) {
  24. throw new Error('参数错误');
  25. }
  26. const res = await apis.fetchTakaiSliceDetail(params.sliceId, params.knowledgeId);
  27. const info = res.data.data;
  28. form.setFieldsValue({
  29. slice_id: info.slice_id,
  30. slice_text: info.slice_text,
  31. document_id: info.document_id,
  32. })
  33. console.log(res, 'info');
  34. } catch (error) {
  35. console.error(error);
  36. } finally {
  37. setListLoading(false);
  38. }
  39. }
  40. };
  41. const init = async () => {
  42. if (params.sliceId) {
  43. await appApi.fetchList();
  44. }
  45. }
  46. React.useEffect(() => {
  47. init();
  48. }, [])
  49. return (
  50. <div>
  51. <div className='questionAnswerList'>
  52. <div style={{ overflow: 'hidden' }}>
  53. <Form
  54. style={{ paddingTop: '20px', height: '100%' }}
  55. form={form}
  56. layout='vertical'
  57. >
  58. <FormItem
  59. name="slice_text"
  60. rules={[{ required: true, message: '切片内容不能为空' }]}>
  61. <TextArea
  62. style={{
  63. width: '50%',
  64. height: '200px',
  65. overflow: 'auto',
  66. resize: 'both',
  67. boxSizing: 'border-box',
  68. }}
  69. placeholder=""
  70. autoSize={{ minRows: 20, maxRows: 5000 }}
  71. />
  72. </FormItem>
  73. <Button type='primary' icon={<ArrowLeftOutlined />} onClick={() => {
  74. router.navigate(-1);
  75. }}>返回</Button>
  76. <Button
  77. style={{ marginLeft: '10px' }}
  78. type='primary'
  79. onClick={() => {
  80. form.validateFields().then(async (values) => {
  81. // 验证参数是否存在
  82. if (!params.knowledgeId || !params.sliceId) {
  83. message.error('知识库ID或切片ID无效');
  84. return;
  85. }
  86. const data = values;
  87. const info = {
  88. knowledgeId: params.knowledgeId,
  89. sliceId: params.sliceId === null ? '' : params.sliceId,
  90. sliceText: values.slice_text,
  91. documentId: params.documentId,
  92. };
  93. console.log(params.sliceId, 'params.sliceId');
  94. let res = null;
  95. if (params.sliceId && params.sliceId !== 'null') {
  96. console.log('11111111');
  97. // 编辑应用
  98. res = await apis.modifyTakaiSliceInfo(info);
  99. } else {
  100. console.log('2222222');
  101. res = await apis.addTakaiSlice(info);
  102. }
  103. if (res.code === 200 && res.data === 1) {
  104. if(params.sliceId && params.sliceId !== 'null'){
  105. message.success('修改成功');
  106. }else{
  107. message.success('新增成功');
  108. }
  109. }else{
  110. if(params.sliceId && params.sliceId !== 'null'){
  111. message.error('修改失败');
  112. }else{
  113. message.error('新增失败');
  114. }
  115. }
  116. const pageInfo = {
  117. knowledge_id: params.knowledgeId,
  118. document_id: params.documentId,
  119. text: '',
  120. pageNum: 1,
  121. pageSize: 10,
  122. }
  123. // 刷新列表
  124. await router.navigate({ pathname: '/deepseek/knowledgeLib/slice/' + params.documentId + '/' + params.knowledgeId + '/' + params.embeddingId });
  125. }).catch((error) => {
  126. console.error(error);
  127. });
  128. }}
  129. >保存</Button>
  130. </Form>
  131. </div>
  132. </div>
  133. </div>
  134. )
  135. };
  136. export default observer(SliceDetail);