upload.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* eslint-disable react-hooks/rules-of-hooks */
  2. import { metadataBatch } from '@/client/api';
  3. import { IFlowNodeParameter } from '@/types/flow';
  4. import { convertKeysToCamelCase } from '@/utils/flow';
  5. import { UploadOutlined } from '@ant-design/icons';
  6. import type { UploadFile, UploadProps } from 'antd';
  7. import { Button, Upload, message } from 'antd';
  8. import { useEffect, useRef, useState } from 'react';
  9. import { useTranslation } from 'react-i18next';
  10. type Props = {
  11. formValuesChange: any;
  12. data: IFlowNodeParameter;
  13. onChange?: (value: any) => void;
  14. };
  15. export const renderUpload = (params: Props) => {
  16. const { t } = useTranslation();
  17. const urlList = useRef<string[]>([]);
  18. const { data, formValuesChange } = params;
  19. const [fileList, setFileList] = useState<UploadFile[]>([]);
  20. // 获取上传文件元数据
  21. useEffect(() => {
  22. if (data.value) {
  23. let uris: string[] = [];
  24. typeof data.value === 'string' ? uris.push(data.value) : (uris = data.value);
  25. const parameter: any = {
  26. uris,
  27. };
  28. metadataBatch(parameter)
  29. .then(res => {
  30. const urlList: UploadFile[] = [];
  31. for (let index = 0; index < res.data.data.length; index++) {
  32. const element = res.data.data[index];
  33. urlList.push({
  34. uid: element.file_id,
  35. name: element.file_name,
  36. status: 'done',
  37. url: element.uri,
  38. });
  39. }
  40. setFileList(urlList);
  41. })
  42. .catch(error => {
  43. console.log(error);
  44. });
  45. }
  46. }, []);
  47. const attr = convertKeysToCamelCase(data.ui?.attr || {});
  48. const [uploading, setUploading] = useState(false);
  49. const [uploadType, setUploadType] = useState('');
  50. const getUploadSuccessUrl = (url: string) => {
  51. if (urlList.current.length === data.ui.attr.max_count) {
  52. urlList.current.pop();
  53. }
  54. urlList.current.push(url);
  55. if (data.ui.attr.max_count === 1) {
  56. formValuesChange({ [data.name]: urlList.current.toString() });
  57. } else {
  58. formValuesChange({ [data.name]: urlList.current });
  59. }
  60. };
  61. const handleFileRemove = (file: any) => {
  62. const index = urlList.current.indexOf(file.response.data[0].uri);
  63. if (index !== -1) {
  64. urlList.current.splice(index, 1);
  65. }
  66. setUploading(false);
  67. if (data.ui.attr.max_count === 1) {
  68. formValuesChange({ [data.name]: urlList.current.toString() });
  69. } else {
  70. formValuesChange({ [data.name]: urlList.current });
  71. }
  72. };
  73. const props: UploadProps = {
  74. name: 'files',
  75. action: process.env.API_BASE_URL ?? '' + data.ui.action,
  76. headers: {
  77. authorization: 'authorization-text',
  78. },
  79. defaultFileList: fileList,
  80. onChange(info) {
  81. setUploading(true);
  82. if (info.file.status !== 'uploading') {
  83. setUploading(false);
  84. }
  85. if (info.file.status === 'done') {
  86. setUploading(false);
  87. message.success(`${info.file.response.data[0].file_name} ${t('Upload_Data_Successfully')}`);
  88. getUploadSuccessUrl(info.file.response.data[0].uri);
  89. } else if (info.file.status === 'error') {
  90. setUploading(false);
  91. message.error(`${info.file.response.data[0].file_name} ${t('Upload_Data_Failed')}`);
  92. }
  93. },
  94. };
  95. if (!uploadType && data.ui?.file_types && Array.isArray(data.ui?.file_types)) {
  96. setUploadType(data.ui?.file_types.toString());
  97. }
  98. return (
  99. <div className='p-2 text-sm text-center'>
  100. <Upload
  101. onRemove={handleFileRemove}
  102. {...props}
  103. {...attr}
  104. multiple={data.is_list ? true : false}
  105. accept={uploadType}
  106. >
  107. <Button loading={uploading} icon={<UploadOutlined />}>
  108. {t('Upload_Data')}
  109. </Button>
  110. </Upload>
  111. </div>
  112. );
  113. };