index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React, { useCallback, useEffect, useRef, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { Button, Popconfirm } from "antd";
  4. import { forwardRef, useImperativeHandle } from "react";
  5. import { MessageFu } from "@/utils/message";
  6. import { API_upFile } from "@/store/action/layout";
  7. import { fileDomInitialFu } from "@/utils/domShow";
  8. import {
  9. EyeOutlined,
  10. CloseOutlined,
  11. DownloadOutlined,
  12. } from "@ant-design/icons";
  13. import filesLookFu from "@/utils/filesLook";
  14. import { baseURL } from "@/utils/http";
  15. import { useSelector } from "react-redux";
  16. import { RootState } from "@/store";
  17. export type FileListType = {
  18. id: number;
  19. name: string;
  20. filePath: string;
  21. };
  22. type Props = {
  23. max: number; //最多上传多少个文件
  24. isLook: boolean; //是不是查看
  25. size: number; //上传文件大小(M)
  26. dirCode: string; //文件的code码
  27. myUrl: string;
  28. fromData?: any;
  29. ref: any; //当前自己的ref,给父组件调用
  30. oldFileList: FileListType[]; //就附件数组,用来回显数据
  31. };
  32. function Z_upFileOtherList(
  33. { max, isLook, size, dirCode, myUrl, fromData, oldFileList }: Props,
  34. ref: any
  35. ) {
  36. // isFileDonw 全局的是否允许下载
  37. const isFileDonw = useSelector(
  38. (state: RootState) => state.A0Layout.isFileDonw
  39. );
  40. useEffect(() => {
  41. if (oldFileList && oldFileList.length) setFileList(oldFileList);
  42. }, [oldFileList]);
  43. // 附件数组
  44. const [fileList, setFileList] = useState<FileListType[]>([]);
  45. const myInput = useRef<HTMLInputElement>(null);
  46. // 上传 附件 函数
  47. const handeUpPhoto = useCallback(
  48. async (e: React.ChangeEvent<HTMLInputElement>) => {
  49. if (e.target.files) {
  50. // 拿到files信息
  51. const filesInfo = e.target.files[0];
  52. // 校验大小
  53. if (filesInfo.size > size * 1024 * 1024) {
  54. e.target.value = "";
  55. return MessageFu.warning(`最大支持${size}M!`);
  56. }
  57. // 创建FormData对象
  58. const fd = new FormData();
  59. // 把files添加进FormData对象(‘photo’为后端需要的字段)
  60. fd.append("type", "doc");
  61. fd.append("dirCode", dirCode);
  62. fd.append("file", filesInfo);
  63. if (fromData) {
  64. for (const k in fromData) {
  65. if (fromData[k]) fd.append(k, fromData[k]);
  66. }
  67. }
  68. e.target.value = "";
  69. try {
  70. const res = await API_upFile(fd, myUrl);
  71. if (res.code === 0) {
  72. MessageFu.success("上传成功!");
  73. setFileList([...fileList, res.data]);
  74. }
  75. fileDomInitialFu();
  76. } catch (error) {
  77. fileDomInitialFu();
  78. }
  79. }
  80. },
  81. [dirCode, fileList, fromData, myUrl, size]
  82. );
  83. // 给父组件返回 fileIds
  84. const fileIdsRefFu = useCallback(() => {
  85. return fileList.map((v) => v.id).join(",");
  86. }, [fileList]);
  87. // 可以让父组件调用子组件的方法
  88. useImperativeHandle(ref, () => ({
  89. fileIdsRefFu,
  90. }));
  91. return (
  92. <div className={styles.Z_upFileOtherList}>
  93. <input
  94. id="upInput"
  95. type="file"
  96. ref={myInput}
  97. onChange={(e) => handeUpPhoto(e)}
  98. />
  99. {fileList.length < max && !isLook ? (
  100. <Button onClick={() => myInput.current?.click()}>上传</Button>
  101. ) : null}
  102. <div className="ZOlistBox mySorrl">
  103. {fileList.map((v) => (
  104. <div key={v.id} className="ZOlistRow">
  105. <div className="ZOlistRowll" title={v.name}>
  106. {v.name}
  107. </div>
  108. <div className="ZOlistRowrr">
  109. {filesLookFu(v.name) ? (
  110. <span>
  111. <EyeOutlined
  112. rev={undefined}
  113. onClick={() => filesLookFu(v.name, v.filePath)}
  114. />
  115. </span>
  116. ) : null}
  117. {isFileDonw ? (
  118. <a
  119. href={baseURL + v.filePath}
  120. download={v.name}
  121. target="_blank"
  122. rel="noreferrer"
  123. >
  124. <DownloadOutlined rev={undefined} />
  125. </a>
  126. ) : null}
  127. {isLook ? null : (
  128. <Popconfirm
  129. title="删除后无法恢复,是否删除?"
  130. okText="删除"
  131. cancelText="取消"
  132. onConfirm={() =>
  133. setFileList(fileList.filter((c) => c.id !== v.id))
  134. }
  135. >
  136. <span>
  137. <CloseOutlined rev={undefined} />
  138. </span>
  139. </Popconfirm>
  140. )}
  141. </div>
  142. </div>
  143. ))}
  144. </div>
  145. <div className="ZOtit">格式要求:最大支持500M,最多{max}个。</div>
  146. </div>
  147. );
  148. }
  149. export default forwardRef(Z_upFileOtherList);