index.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import React, { useCallback, useMemo, useRef, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import ImageLazy from "@/components/ImageLazy";
  4. import {
  5. PlusOutlined,
  6. EyeOutlined,
  7. CloseOutlined,
  8. DownloadOutlined,
  9. UploadOutlined,
  10. } from "@ant-design/icons";
  11. import store from "@/store";
  12. import { baseURL } from "@/utils/http";
  13. import classNames from "classnames";
  14. import { Button } from "antd";
  15. import { MessageFu } from "@/utils/message";
  16. import { fileDomInitialFu } from "@/utils/domShow";
  17. import { API_upFile } from "@/store/action/layout";
  18. import { forwardRef, useImperativeHandle } from "react";
  19. import MyPopconfirm from "../MyPopconfirm";
  20. type MyTypeType = "thumb" | "video" | "audio" | "model" | "pdf";
  21. // 这个组件 只处理 上传 一张图片或者 视频 音频 模型 pdf 的情况
  22. type Props = {
  23. fileCheck: boolean; //有没有点击过确定
  24. size: number; //上传附件大小(M)
  25. dirCode: string; //文件的code码
  26. myUrl: string; //请求地址
  27. format: string[]; //上传格式 ["image/jpeg", "image/png"] ["video/mp4"] ,application/pdf
  28. formatTxt: string; //上传图片提示
  29. checkTxt: string;
  30. upTxt: string;
  31. myType: MyTypeType;
  32. isLook?: boolean; //是不是查看
  33. fromData?: any;
  34. ref: any; //当前自己的ref,给父组件调用
  35. };
  36. function ZupOne(
  37. {
  38. fileCheck,
  39. size,
  40. dirCode,
  41. myUrl,
  42. format,
  43. formatTxt,
  44. checkTxt,
  45. upTxt,
  46. myType,
  47. isLook = false,
  48. fromData,
  49. }: Props,
  50. ref: any
  51. ) {
  52. const [fileUrl, setFileUrl] = useState({
  53. fileName: "",
  54. filePath: "",
  55. });
  56. const myInput = useRef<HTMLInputElement>(null);
  57. // 上传封面图
  58. const handeUpPhoto = useCallback(
  59. async (e: React.ChangeEvent<HTMLInputElement>) => {
  60. if (e.target.files) {
  61. // 拿到files信息
  62. const filesInfo = e.target.files[0];
  63. // console.log("-----", filesInfo.type);
  64. // 校验格式
  65. const type = format;
  66. if (myType === "pdf") {
  67. if (!filesInfo.type.includes("pdf")) {
  68. e.target.value = "";
  69. return MessageFu.warning(`只支持${formatTxt}格式!`);
  70. }
  71. } else {
  72. if (!type.includes(filesInfo.type)) {
  73. e.target.value = "";
  74. return MessageFu.warning(`只支持${formatTxt}格式!`);
  75. }
  76. }
  77. // 校验大小
  78. if (filesInfo.size > size * 1024 * 1024) {
  79. e.target.value = "";
  80. return MessageFu.warning(`最大支持${size}M!`);
  81. }
  82. // 创建FormData对象
  83. const fd = new FormData();
  84. // 把files添加进FormData对象(‘photo’为后端需要的字段)
  85. let myTypeRes: string = myType;
  86. if (["pdf"].includes(myTypeRes)) myTypeRes = "doc";
  87. fd.append("type", myTypeRes);
  88. fd.append("dirCode", dirCode);
  89. fd.append("file", filesInfo);
  90. if (fromData) {
  91. for (const k in fromData) {
  92. if (fromData[k]) fd.append(k, fromData[k]);
  93. }
  94. }
  95. e.target.value = "";
  96. try {
  97. const res = await API_upFile(fd, myUrl);
  98. if (res.code === 0) {
  99. MessageFu.success("上传成功!");
  100. setFileUrl(res.data);
  101. }
  102. fileDomInitialFu();
  103. } catch (error) {
  104. fileDomInitialFu();
  105. }
  106. }
  107. },
  108. [dirCode, format, formatTxt, fromData, myType, myUrl, size]
  109. );
  110. // 让父组件调用的 回显 附件 地址
  111. const setFileComFileFu = useCallback(
  112. (valObj: { fileName: string; filePath: string }) => {
  113. setFileUrl(valObj);
  114. },
  115. []
  116. );
  117. // 让父组件调用的返回 附件 名字和路径
  118. const fileComFileResFu = useCallback(() => {
  119. return fileUrl;
  120. }, [fileUrl]);
  121. // 可以让父组件调用子组件的方法
  122. useImperativeHandle(ref, () => ({
  123. setFileComFileFu,
  124. fileComFileResFu,
  125. }));
  126. const acceptRes = useMemo(() => {
  127. let accept = ".png,.jpg,.jpeg";
  128. if (myType === "video") accept = ".mp4";
  129. else if (myType === "audio") accept = ".mp3";
  130. else if (myType === "model") accept = ".4dage";
  131. else if (myType === "pdf") accept = ".pdf";
  132. return accept;
  133. }, [myType]);
  134. // 点击 预览(除了图片)
  135. const lookFileNoImgFu = useCallback(
  136. (type: MyTypeType) => {
  137. if (type === "pdf" || type === "thumb") {
  138. // 新窗口打开
  139. window.open(baseURL + fileUrl.filePath);
  140. } else {
  141. store.dispatch({
  142. type: "layout/lookDom",
  143. payload: { src: fileUrl.filePath, type },
  144. });
  145. }
  146. // if (type === "pdf") {
  147. // } else {
  148. // }
  149. },
  150. [fileUrl.filePath]
  151. );
  152. return (
  153. <div className={styles.ZupOne}>
  154. <input
  155. id="upInput"
  156. type="file"
  157. accept={acceptRes}
  158. ref={myInput}
  159. onChange={(e) => handeUpPhoto(e)}
  160. />
  161. {myType === "thumb" ? (
  162. <div
  163. hidden={fileUrl.filePath !== ""}
  164. className="file_upIcon"
  165. onClick={() => myInput.current?.click()}
  166. >
  167. <PlusOutlined rev={undefined} />
  168. </div>
  169. ) : (
  170. <Button
  171. hidden={fileUrl.filePath !== ""}
  172. onClick={() => myInput.current?.click()}
  173. icon={<UploadOutlined rev={undefined} />}
  174. >
  175. 上传
  176. </Button>
  177. )}
  178. {/* 为图片的情况-------------- */}
  179. {myType === "thumb" ? (
  180. <div className="file_img" hidden={fileUrl.filePath === ""}>
  181. {fileUrl ? (
  182. <ImageLazy width={100} height={100} src={fileUrl.filePath} noLook />
  183. ) : null}
  184. {/* 删除 */}
  185. <div className="file_closeBox" hidden={isLook}>
  186. <MyPopconfirm
  187. txtK="删除"
  188. onConfirm={() => setFileUrl({ fileName: "", filePath: "" })}
  189. Dom={<CloseOutlined rev={undefined} />}
  190. />
  191. </div>
  192. {/* 预览 下载 */}
  193. <div className="file_lookBox">
  194. <EyeOutlined
  195. onClick={() =>
  196. store.dispatch({
  197. type: "layout/lookBigImg",
  198. payload: { url: baseURL + fileUrl.filePath, show: true },
  199. })
  200. }
  201. rev={undefined}
  202. />
  203. <a
  204. href={baseURL + fileUrl.filePath}
  205. download
  206. target="_blank"
  207. rel="noreferrer"
  208. >
  209. <DownloadOutlined rev={undefined} />
  210. </a>
  211. </div>
  212. </div>
  213. ) : fileUrl.filePath ? (
  214. <div className="fileInfo">
  215. <div className="upSuccTxt">{fileUrl.fileName}</div>
  216. {/* 视频预览 */}
  217. <div
  218. className="clearCover"
  219. hidden={!fileUrl.filePath}
  220. onClick={() => lookFileNoImgFu(myType)}
  221. >
  222. <EyeOutlined rev={undefined} />
  223. </div>
  224. {/* 视频下载 */}
  225. <a
  226. href={baseURL + fileUrl.filePath}
  227. download
  228. target="_blank"
  229. className="clearCover"
  230. rel="noreferrer"
  231. >
  232. <DownloadOutlined rev={undefined} />
  233. </a>
  234. {/* 视频删除 */}
  235. <MyPopconfirm
  236. txtK="删除"
  237. onConfirm={() => setFileUrl({ fileName: "", filePath: "" })}
  238. Dom={<CloseOutlined className="clearCover" rev={undefined} />}
  239. />
  240. </div>
  241. ) : null}
  242. <div className="fileBoxRow_r_tit" hidden={isLook}>
  243. 格式要求:支持{formatTxt}格式;最大支持{size}M。{upTxt}
  244. <br />
  245. <div
  246. className={classNames(
  247. "noUpThumb",
  248. !fileUrl.filePath && fileCheck ? "noUpThumbAc" : ""
  249. )}
  250. >
  251. {checkTxt}
  252. </div>
  253. </div>
  254. </div>
  255. );
  256. }
  257. export default forwardRef(ZupOne);