import React, { useCallback, useEffect, useRef, useState } from "react"; import styles from "./index.module.scss"; import { FileImgListType } from "@/types"; import { API_upFile } from "@/store/action/layout"; import { MessageFu } from "@/utils/message"; import { fileDomInitialFu } from "@/utils/domShow"; import { forwardRef, useImperativeHandle } from "react"; import { Button, Popconfirm } from "antd"; import { EyeOutlined, UploadOutlined, CloseOutlined, DownloadOutlined, } from "@ant-design/icons"; import classNames from "classnames"; import { baseURL } from "@/utils/http"; import { A1_APIremoveSure } from "@/store/action/A1Project"; import { authFilesLookFu } from "@/utils/authFilesLook"; type Props = { max: number; //最多传多少个文件 isLook: boolean; //是否是查看 ref: any; //当前自己的ref,给父组件调用 fileCheck: boolean; dirCode: string; //文件的code码 myUrl: string; fromData?: any; lookData: FileImgListType[]; //编辑或者 查看 回显 accept?: string; // result:成果 | list:清单 type?: string; tips?: string; }; function Z3upFiles( { max, isLook, type = "doc", fileCheck, dirCode, myUrl, fromData, lookData, accept = ".zip", tips = "此处的附件为对外的项目成果文件,仅支持zip格式,最多10个", }: Props, ref: any ) { const [fileList, setFileList] = useState([]); useEffect(() => { if (lookData && lookData.length > 0) setFileList(lookData); }, [lookData]); const myInput = useRef(null); // 上传文件 const handeUpPhoto = useCallback( async (e: React.ChangeEvent) => { if (e.target.files) { // 拿到files信息 const filesInfo = e.target.files[0]; // 校验格式 if (!filesInfo.name.includes(".zip") && accept !== "*") { e.target.value = ""; return MessageFu.warning(`只支持zip格式!`); } // 创建FormData对象 const fd = new FormData(); // 把files添加进FormData对象(‘photo’为后端需要的字段) fd.append("type", type); fd.append("dirCode", dirCode); fd.append("file", filesInfo); if (fromData) { for (const k in fromData) { if (fromData[k]) fd.append(k, fromData[k]); } } e.target.value = ""; try { const res = await API_upFile(fd, myUrl); if (res.code === 0) { MessageFu.success("上传成功!"); setFileList([...fileList, res.data]); } fileDomInitialFu(); } catch (error) { fileDomInitialFu(); } } }, [accept, dirCode, fileList, fromData, myUrl, type] ); // 列表删除某一个文件 const delImgListFu = useCallback( async (id: number) => { const res = await A1_APIremoveSure([id + ""]); if (res.code === 0) { const newItems = fileList.filter((v) => v.id !== id); setFileList(newItems); } }, [fileList, setFileList] ); // 让父组件调用,拿到 附件信息 const filesIdRes = useCallback(() => { return fileList.map((v) => v.id); }, [fileList]); // 可以让父组件调用子组件的方法 useImperativeHandle(ref, () => ({ filesIdRes, })); return (
handeUpPhoto(e)} />
{fileList.length < max && !isLook ? ( ) : null}
{fileList.map((v) => (
{v.fileName}
{authFilesLookFu(v.fileName, "") ? ( <> authFilesLookFu(v.fileName, v.filePath, true) } />   ) : null} delImgListFu(v.id)} okButtonProps={{ loading: false }} >
))}
{isLook && fileList.length <= 0 ? (
(空)
) : null}
); } export default forwardRef(Z3upFiles);