index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { FC, useEffect, useState } from "react";
  2. import { Button, Empty, Modal, ModalProps, Table, Tag } from "antd";
  3. import { MaterialType, YES_OR_NO } from "@/types";
  4. import { getBaseURL } from "@dage/service";
  5. import { deleteFileApi, getFileListApi, saveManageFileApi } from "@/api";
  6. import { downloadFile, beforeUpload } from "@/utils";
  7. import {
  8. DageFileResponseType,
  9. DageLoading,
  10. DageTableActions,
  11. DageUpload,
  12. DageUploadConsumer,
  13. DageUploadProvider,
  14. DageUploadType,
  15. } from "@dage/pc-components";
  16. import classNames from "classnames";
  17. import style from "./index.module.scss";
  18. export interface IndexDetailModalProps extends Omit<ModalProps, "onOk"> {
  19. id: number;
  20. title: string;
  21. onCancel?: () => void;
  22. onOk?: (val: any) => void;
  23. }
  24. export const IndexDetailModal: FC<IndexDetailModalProps> = ({
  25. open,
  26. id,
  27. title,
  28. onOk,
  29. onCancel,
  30. ...rest
  31. }) => {
  32. const baseUrl = getBaseURL();
  33. const [loading, setLoading] = useState(false);
  34. const [list, setList] = useState<MaterialType[]>([]);
  35. // 已上传附件映射
  36. const [uploadedFileMap, setUploadedFileMap] = useState<
  37. Record<number, MaterialType[]>
  38. >({});
  39. const handleCancel = () => {
  40. onCancel?.();
  41. };
  42. const saveManageFile = async (
  43. item: MaterialType,
  44. list: DageFileResponseType[]
  45. ) => {
  46. // @ts-ignore
  47. const li = list.filter((i) => i.status === "done" && !i.uploaded);
  48. for (let i = 0; i < li.length; i++) {
  49. const file = li[i];
  50. if (!file.response) return;
  51. await saveManageFileApi({
  52. name: file.name,
  53. fileName: file.response.fileName,
  54. filePath: file.response.filePath,
  55. level: 2,
  56. suffix: item.suffix,
  57. parentId: item.id,
  58. module: "fill-norm",
  59. moduleId: id,
  60. });
  61. // @ts-ignore
  62. file.uploaded = true;
  63. }
  64. getList();
  65. };
  66. const getList = async () => {
  67. try {
  68. setLoading(true);
  69. const data = await getFileListApi(id, "norm");
  70. setList(data.filter((i) => i.module === "norm"));
  71. const temp: typeof uploadedFileMap = {};
  72. data
  73. .filter((i) => i.module === "fill-norm")
  74. .forEach((item) => {
  75. if (!item.parentId) return;
  76. if (Array.isArray(temp[item.parentId]))
  77. temp[item.parentId].push(item);
  78. else temp[item.parentId] = [item];
  79. });
  80. setUploadedFileMap(temp);
  81. } finally {
  82. setLoading(false);
  83. }
  84. };
  85. const handleDeleteFile = async (id: number) => {
  86. await deleteFileApi(id);
  87. getList();
  88. };
  89. useEffect(() => {
  90. if (open) getList();
  91. }, [open]);
  92. return (
  93. <Modal
  94. className={style.modal}
  95. title={title}
  96. open={open}
  97. width={900}
  98. footer={null}
  99. onCancel={handleCancel}
  100. {...rest}
  101. >
  102. {loading && <DageLoading />}
  103. {!list.length && <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />}
  104. {list.map((item) => {
  105. const isUpload = item.isUpload === YES_OR_NO.YES;
  106. return (
  107. <div key={item.id} className={style.fileUpload}>
  108. <div className={style.fileUploadHeader}>
  109. <div className={style.fileUploadHeaderInfo}>
  110. <Tag color={isUpload ? "red" : ""}>
  111. {isUpload ? "必填" : "选填"}
  112. </Tag>
  113. <h3>
  114. {item.name} | {item.suffix || "*"}
  115. </h3>
  116. </div>
  117. <Button
  118. type="primary"
  119. ghost
  120. onClick={() =>
  121. downloadFile(
  122. baseUrl + process.env.REACT_APP_IMG_PUBLIC + item.filePath,
  123. item.fileName
  124. )
  125. }
  126. >
  127. 下载模板
  128. </Button>
  129. <DageUploadProvider>
  130. <DageUploadConsumer>
  131. {(res) => (
  132. <DageUpload
  133. className={classNames(style.uploadBtn, "no-list")}
  134. dType={DageUploadType.DOC}
  135. action="/api/cms/fill/file/upload"
  136. // @ts-ignore
  137. accept={item.suffix}
  138. // @ts-ignore
  139. beforeUpload={beforeUpload.bind(undefined, item.suffix)}
  140. onChange={saveManageFile.bind(undefined, item)}
  141. >
  142. <Button type="primary" ghost loading={res?.uploading}>
  143. 上传附件
  144. </Button>
  145. </DageUpload>
  146. )}
  147. </DageUploadConsumer>
  148. </DageUploadProvider>
  149. </div>
  150. <Table
  151. rowKey="id"
  152. className="cus-table"
  153. pagination={false}
  154. dataSource={uploadedFileMap[item.id]}
  155. columns={[
  156. {
  157. title: "附件名称",
  158. dataIndex: "fileName",
  159. align: "center",
  160. },
  161. {
  162. title: "责任部门",
  163. dataIndex: "deptName",
  164. align: "center",
  165. },
  166. {
  167. title: "上传用户",
  168. dataIndex: "creatorName",
  169. align: "center",
  170. },
  171. {
  172. title: "上传时间",
  173. dataIndex: "updateTime",
  174. align: "center",
  175. },
  176. {
  177. title: "评定结果",
  178. dataIndex: "creatorName",
  179. align: "center",
  180. },
  181. {
  182. title: "评定意见",
  183. dataIndex: "updateTime",
  184. align: "center",
  185. },
  186. {
  187. title: "操作",
  188. align: "center",
  189. render: (val) => (
  190. <DageTableActions
  191. renderBefore={
  192. <Button
  193. size="small"
  194. type="link"
  195. onClick={() =>
  196. downloadFile(
  197. baseUrl +
  198. process.env.REACT_APP_IMG_PUBLIC +
  199. item.filePath,
  200. item.fileName
  201. )
  202. }
  203. >
  204. 下载
  205. </Button>
  206. }
  207. showEdit={false}
  208. onDelete={handleDeleteFile.bind(undefined, val.id)}
  209. />
  210. ),
  211. },
  212. ]}
  213. />
  214. </div>
  215. );
  216. })}
  217. </Modal>
  218. );
  219. };