index.tsx 5.7 KB

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