FilesModal.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import React, { useEffect, useState, useRef, useMemo } from 'react';
  2. import { UploadOutlined } from '@ant-design/icons';
  3. import { Form } from '@ant-design/compatible';
  4. import '@ant-design/compatible/assets/index.css';
  5. import { Button, Modal, message, Table, Spin, Tabs, Upload, Select, Space } from 'antd';
  6. const { TabPane } = Tabs;
  7. const { confirm } = Modal;
  8. import moment from 'moment/moment';
  9. import PreviewFile from '@/components/PreviewFile';
  10. import { getToken, GetTokenFromUrl } from '@/utils/utils';
  11. import FileViewerModal from '@/components/FileViewer';
  12. // 历史清单
  13. function FilesModal(props) {
  14. const {
  15. projectId,
  16. visible,
  17. onClose,
  18. onUpload,
  19. data,
  20. // uploadProps,
  21. queryFiles,
  22. DeleteFile,
  23. downloadFile,
  24. loading,
  25. typeOptions,
  26. } = props;
  27. const [value, setValue] = useState();
  28. const [showData, setShowData] = useState();
  29. const [exportUrl, setExportUrl] = useState();
  30. const [fileVisible, setFileVisible] = useState(false);
  31. useEffect(() => {
  32. if (!value) {
  33. setShowData(data);
  34. return;
  35. }
  36. const newData = data.filter(item => item.classify_id == value);
  37. setShowData(newData);
  38. }, [data, value]);
  39. const uploadProps = useMemo(() => {
  40. const token = getToken() || GetTokenFromUrl();
  41. const uploadProps = {
  42. name: 'file',
  43. showUploadList: false,
  44. action: `/api/v1/purchase/attachment/${projectId}`,
  45. headers: {
  46. 'JWT-TOKEN': token,
  47. },
  48. data: {
  49. classify_id: value,
  50. },
  51. beforeUpload(file) {
  52. if (!value) {
  53. message.error('请先选择附件分类');
  54. return false;
  55. }
  56. },
  57. onChange(info) {
  58. if (info.file.status !== 'uploading') {
  59. console.log(info.file, info.fileList);
  60. }
  61. if (info.file.status === 'done') {
  62. message.success(`${info.file.name} 文件上传成功`);
  63. queryFiles();
  64. } else if (info.file.status === 'error') {
  65. message.error(`${info.file.name} 文件上传失败`);
  66. }
  67. },
  68. };
  69. return uploadProps;
  70. }, [value, projectId]);
  71. const columns = [
  72. {
  73. title: '预览',
  74. dataIndex: 'name',
  75. render: (text, item) => {
  76. return <PreviewFile name={item.name} src={item.url} />;
  77. },
  78. },
  79. {
  80. title: '上传时间',
  81. dataIndex: 'c_time',
  82. render: text => {
  83. return text ? moment(text).format('YYYY年MM月DD日 HH:mm:ss') : null;
  84. },
  85. },
  86. {
  87. title: '上传人',
  88. dataIndex: 'CreatorUser',
  89. render: record => record.CName || '',
  90. },
  91. {
  92. title: '分类',
  93. dataIndex: 'classify_id',
  94. render: id => typeOptions.find(item => item.id == id)?.name,
  95. },
  96. {
  97. title: '操作',
  98. render: record => (
  99. <>
  100. <a
  101. onClick={() => {
  102. downloadFile(record);
  103. }}
  104. >
  105. 下载
  106. </a>
  107. <a
  108. style={{ marginLeft: 10 }}
  109. onClick={() => {
  110. setExportUrl(record.url);
  111. setFileVisible(true);
  112. }}
  113. >
  114. 预览
  115. </a>
  116. <a
  117. onClick={() => {
  118. confirm({
  119. title: '提醒',
  120. content: '确认删除该文件,删除后无法复原',
  121. okText: '确认',
  122. cancelText: '取消',
  123. onOk() {
  124. DeleteFile(record.id);
  125. },
  126. });
  127. }}
  128. style={{ marginLeft: 10 }}
  129. >
  130. 删除
  131. </a>
  132. </>
  133. ),
  134. },
  135. ];
  136. return (
  137. <>
  138. <Modal title="附件列表" width="70%" onCancel={onClose} visible={visible} footer={false}>
  139. <Space style={{ display: 'flex', marginBottom: '20px' }}>
  140. <div>附件类型:</div>
  141. <Select
  142. style={{ width: 400 }}
  143. value={value}
  144. allowClear
  145. options={typeOptions}
  146. onChange={value => setValue(value)}
  147. />
  148. <Upload {...uploadProps}>
  149. <Button type="primary" loading={loading}>
  150. <UploadOutlined /> 上传文件
  151. </Button>
  152. </Upload>
  153. </Space>
  154. <Table rowKey="id" columns={columns} dataSource={showData} loading={loading} />
  155. </Modal>
  156. <FileViewerModal
  157. url={exportUrl}
  158. visible={fileVisible}
  159. onCancel={() => {
  160. setFileVisible(false);
  161. }}
  162. />
  163. </>
  164. );
  165. }
  166. export default FilesModal;