index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import React, { useCallback, useEffect, useRef, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { FileImgListType } from "@/types";
  4. import { API_upFile } from "@/store/action/layout";
  5. import { MessageFu } from "@/utils/message";
  6. import { fileDomInitialFu } from "@/utils/domShow";
  7. import { forwardRef, useImperativeHandle } from "react";
  8. import { Button, Popconfirm } from "antd";
  9. import {
  10. EyeOutlined,
  11. UploadOutlined,
  12. CloseOutlined,
  13. DownloadOutlined,
  14. } from "@ant-design/icons";
  15. import classNames from "classnames";
  16. import { baseURL } from "@/utils/http";
  17. import filesLookFu from "@/utils/filesLook";
  18. import { A1_APIremoveSure } from "@/store/action/A1Project";
  19. type Props = {
  20. max: number; //最多传多少个文件
  21. isLook: boolean; //是否是查看
  22. ref: any; //当前自己的ref,给父组件调用
  23. fileCheck: boolean;
  24. dirCode: string; //文件的code码
  25. myUrl: string;
  26. fromData?: any;
  27. lookData: FileImgListType[]; //编辑或者 查看 回显
  28. accept?: string;
  29. // result:成果 | list:清单
  30. type?: string;
  31. };
  32. function Z3upFiles(
  33. { max, isLook, type="doc", fileCheck, dirCode, myUrl, fromData, lookData, accept=".zip" }: Props,
  34. ref: any
  35. ) {
  36. const [fileList, setFileList] = useState<FileImgListType[]>([]);
  37. useEffect(() => {
  38. if (lookData && lookData.length > 0) setFileList(lookData);
  39. }, [lookData]);
  40. const myInput = useRef<HTMLInputElement>(null);
  41. // 上传文件
  42. const handeUpPhoto = useCallback(
  43. async (e: React.ChangeEvent<HTMLInputElement>) => {
  44. if (e.target.files) {
  45. // 拿到files信息
  46. const filesInfo = e.target.files[0];
  47. // 校验格式
  48. if (!filesInfo.name.includes(".zip") && accept !== '*') {
  49. e.target.value = "";
  50. return MessageFu.warning(`只支持zip格式!`);
  51. }
  52. // 创建FormData对象
  53. const fd = new FormData();
  54. // 把files添加进FormData对象(‘photo’为后端需要的字段)
  55. fd.append("type", type);
  56. fd.append("dirCode", dirCode);
  57. fd.append("file", filesInfo);
  58. if (fromData) {
  59. for (const k in fromData) {
  60. if (fromData[k]) fd.append(k, fromData[k]);
  61. }
  62. }
  63. e.target.value = "";
  64. try {
  65. const res = await API_upFile(fd, myUrl);
  66. if (res.code === 0) {
  67. MessageFu.success("上传成功!");
  68. setFileList([...fileList, res.data]);
  69. }
  70. fileDomInitialFu();
  71. } catch (error) {
  72. fileDomInitialFu();
  73. }
  74. }
  75. },
  76. [dirCode, fileList, fromData, myUrl]
  77. );
  78. // 列表删除某一个文件
  79. const delImgListFu = useCallback(
  80. async (id: number) => {
  81. const res = await A1_APIremoveSure([id + ""]);
  82. if (res.code === 0) {
  83. const newItems = fileList.filter((v) => v.id !== id);
  84. setFileList(newItems);
  85. }
  86. },
  87. [fileList, setFileList]
  88. );
  89. // 让父组件调用,拿到 附件信息
  90. const filesIdRes = useCallback(() => {
  91. return fileList.map((v) => v.id);
  92. }, [fileList]);
  93. // 可以让父组件调用子组件的方法
  94. useImperativeHandle(ref, () => ({
  95. filesIdRes,
  96. }));
  97. return (
  98. <div className={styles.Z3upFiles}>
  99. <input
  100. id="upInput"
  101. type="file"
  102. accept={accept}
  103. ref={myInput}
  104. onChange={(e) => handeUpPhoto(e)}
  105. />
  106. <div className="Z3Btn">
  107. {fileList.length < max && !isLook ? (
  108. <Button
  109. onClick={() => myInput.current?.click()}
  110. icon={<UploadOutlined rev={undefined} />}
  111. >
  112. 上传
  113. </Button>
  114. ) : null}
  115. <div className="Z3files">
  116. {fileList.map((v) => (
  117. <div className="Z3filesRow" key={v.id}>
  118. <div className="Z3files1" title={v.fileName}>
  119. {v.fileName}
  120. </div>
  121. <div className="Z3files2">
  122. {filesLookFu(v.fileName) ? (
  123. <>
  124. <EyeOutlined
  125. rev={undefined}
  126. title="查看"
  127. onClick={() => filesLookFu(v.fileName, v.filePath)}
  128. />
  129. &emsp;
  130. </>
  131. ) : null}
  132. <a
  133. title="下载"
  134. href={baseURL + v.filePath}
  135. download={v.fileName}
  136. target="_blank"
  137. rel="noreferrer"
  138. >
  139. <DownloadOutlined rev={undefined} />
  140. </a>
  141. &emsp;
  142. <Popconfirm
  143. title="删除后无法恢复,是否删除?"
  144. okText="删除"
  145. cancelText="取消"
  146. onConfirm={() => delImgListFu(v.id)}
  147. okButtonProps={{ loading: false }}
  148. >
  149. <CloseOutlined rev={undefined} title="删除" hidden={isLook} />
  150. </Popconfirm>
  151. </div>
  152. </div>
  153. ))}
  154. </div>
  155. <div className="fileTit" hidden={isLook}>
  156. 此处的附件为对外的项目成果文件,仅支持zip格式,最多10个
  157. <br />
  158. <div
  159. className={classNames(
  160. "noUpThumb",
  161. fileList.length <= 0 && fileCheck ? "noUpThumbAc" : ""
  162. )}
  163. >
  164. 请上传视频!
  165. </div>
  166. </div>
  167. </div>
  168. {isLook && fileList.length <= 0 ? (
  169. <div className="lookNone">(空)</div>
  170. ) : null}
  171. </div>
  172. );
  173. }
  174. export default forwardRef(Z3upFiles);