import { RootState } from "@/store"; import { Button, Checkbox, Form, Input, Popconfirm, Radio, Select, Switch, } from "antd"; import React, { useCallback, useEffect, useMemo, useRef, useState, } from "react"; import { useDispatch, useSelector } from "react-redux"; import TextArea from "antd/es/input/TextArea"; import styles from "./index.module.scss"; import { MessageFu } from "@/utils/message"; import ImageLazy from "@/components/ImageLazy"; import classNames from "classnames"; import { PlusOutlined, CloseCircleOutlined, UploadOutlined, CloseOutlined, PlayCircleOutlined, } from "@ant-design/icons"; import { getGoodsInfoByIdAPI, getGoodsSaveAPI, goodsUploadAPI, setGoodsImgBoderAPI, } from "@/store/action/B2Goods"; import { baseURL } from "@/utils/http"; import { FileListType, GoodsTableType } from "@/types"; type Props = { id: number; closeMoalFu: () => void; addListFu: () => void; editListFu: () => void; }; // 上传附件的进度条 const UpAsyncLodingDom: any = document.querySelector("#UpAsyncLoding"); const progressDom: any = document.querySelector("#progress"); function GoodsAdd({ id, closeMoalFu, addListFu, editListFu }: Props) { const dispatch = useDispatch(); // 后面添加的的知识驿站 const [valueZS1, setValueZS1] = useState(""); const [valueZS2, setValueZS2] = useState([]); // 上传附件的信息 const [fileList, setFileList] = useState({ model: {} as FileListType, img: [] as FileListType[], audio: {} as FileListType, video: {} as FileListType, }); // 表单的ref const FormBoxRef = useRef({}); // 文件的dirCode码 const [dirCode, setDirCode] = useState(""); const getInfoInAPIFu = useCallback(async (id: number) => { const res = await getGoodsInfoByIdAPI(id); FormBoxRef.current.setFieldsValue(res.data.entity); setCover(res.data.entity.thumb); if (res.data.entity.type) setTypeCheck(res.data.entity.type.split(",")); setValueZS1(res.data.entity.tagType); if (res.data.entity.tagCountry) setValueZS2(res.data.entity.tagCountry.split(",")); const data: FileListType[] = res.data.file; const obj = { model: {} as FileListType, img: [] as FileListType[], audio: {} as FileListType, video: {} as FileListType, }; data.forEach((v) => { if (v.type === "img") obj.img.push(v); else obj[v.type!] = v; }); setFileList(obj); setDirCode(res.data.entity.dirCode); }, []); useEffect(() => { if (id > 0) getInfoInAPIFu(id); else { setDirCode(Date.now() + ""); FormBoxRef.current.setFieldsValue({ topic: "不展示", display: 1, isBarrage: 0, }); } }, [getInfoInAPIFu, id]); // 从仓库获取下拉列表数据 const dictList = useSelector((state: RootState) => state.loginStore.dictList); // 上传封面图的ref const [coverCheck, setCoverCheck] = useState(false); const [cover, setCover] = useState(""); const myInput = useRef(null); // 上传封面图 const handeUpPhoto = useCallback( async (e: React.ChangeEvent) => { if (e.target.files) { // 拿到files信息 const filesInfo = e.target.files[0]; // 校验格式 const type = ["image/jpeg", "image/png"]; if (!type.includes(filesInfo.type)) { e.target.value = ""; return MessageFu.warning("只支持jpg、png格式!"); } // 校验大小 if (filesInfo.size > 20 * 1024 * 1024) { e.target.value = ""; return MessageFu.warning("最大支持20M!"); } // 创建FormData对象 const fd = new FormData(); // 把files添加进FormData对象(‘photo’为后端需要的字段) fd.append("type", "thumb"); fd.append("dirCode", dirCode); fd.append("file", filesInfo); e.target.value = ""; const res: any = await goodsUploadAPI(fd); if (res.code === 0) { MessageFu.success("上传成功!"); setCover(res.data.filePath); } UpAsyncLodingDom.style.opacity = 0; progressDom.style.width = "0%"; } }, [dirCode] ); // 选中附件类型 const [typeCheck, setTypeCheck] = useState([]); const [typeOk, setTypeOk] = useState(false); const typeCheckArr = useMemo(() => { return [ { label: "模型", value: "model" }, { label: "图片", value: "img" }, { label: "音频", value: "audio" }, { label: "视频", value: "video" }, ]; }, []); // 上传附件图片的有无边框的选择 const upImgDoneFu = useCallback( async (val: boolean, id: number) => { const res = await setGoodsImgBoderAPI(id, val ? 1 : 0); if (res.code === 0) { const newData = { ...fileList, img: fileList.img.map((v) => { return { ...v, isFrame: v.id === id ? val : v.isFrame, }; }), }; setFileList(newData); } }, [fileList] ); // 附件信息的校验 const fileCheckFu = useMemo(() => { let flag = false; if (typeCheck.length === 0) flag = true; if (typeCheck.includes("model") && !fileList.model.id) flag = true; if (typeCheck.includes("img") && fileList.img.length === 0) flag = true; if (typeCheck.includes("audio") && !fileList.audio.id) flag = true; if (typeCheck.includes("video") && !fileList.video.id) flag = true; return flag; }, [fileList, typeCheck]); // 点击上传附件按钮 const myInput2 = useRef(null); const [fileOneType, setFileOneType] = useState(""); useEffect(() => { if (fileOneType) myInput2.current?.click(); }, [fileOneType]); const upFileFu = useCallback((type: string) => { setFileOneType(""); window.setTimeout(() => { setFileOneType(type); }, 100); }, []); // 上传附件的处理函数 const handeUpPhoto2 = useCallback( async (e: React.ChangeEvent) => { if (e.target.files) { // 拿到files信息 const filesInfo = e.target.files[0]; let anType = ["image/jpeg", "image/png", "image/gif"]; let anTit1 = "只支持png、jpg、gif和jpeg格式!"; let anTit2 = "最大支持20M!"; let anSize = 20 * 1024 * 1024; if (fileOneType === "audio") { anType = ["audio/mpeg"]; anTit1 = "只支持mp3格式!"; anTit2 = "最大支持10M!"; anSize = 10 * 1024 * 1024; } else if (fileOneType === "video") { anType = ["video/mp4"]; anTit1 = "只支持mp4格式!"; anTit2 = "最大支持500M!"; anSize = 500 * 1024 * 1024; } else if (fileOneType === "model") { anType = [""]; anTit1 = "只支持4dage格式!"; anTit2 = "最大支持500M!"; anSize = 500 * 1024 * 1024; } // 校验格式 if (fileOneType !== "model") { if (!anType.includes(filesInfo.type)) { e.target.value = ""; return MessageFu.warning(anTit1); } } else { if (!filesInfo.name.includes(".4dage")) { e.target.value = ""; return MessageFu.warning(anTit1); } } // 校验大小 if (filesInfo.size > anSize) { e.target.value = ""; return MessageFu.warning(anTit2); } // 创建FormData对象 const fd = new FormData(); // 把files添加进FormData对象(‘photo’为后端需要的字段) fd.append("type", fileOneType); fd.append("dirCode", dirCode); fd.append("file", filesInfo); e.target.value = ""; const res: any = await goodsUploadAPI(fd); if (res.code === 0) { MessageFu.success("上传成功!"); if (fileOneType === "img") setFileList({ ...fileList, img: [res.data, ...fileList.img] }); else setFileList({ ...fileList, [fileOneType]: res.data }); } UpAsyncLodingDom.style.opacity = 0; progressDom.style.width = "0%"; } }, [dirCode, fileList, fileOneType] ); // 附件图片的拖动 const [dragImg, setDragImg] = useState(null); const handleDragOver = useCallback( (e: React.DragEvent, item: FileListType) => { e.dataTransfer.dropEffect = "move"; }, [] ); const handleDragEnter = useCallback( (e: React.DragEvent, item: FileListType) => { e.dataTransfer.effectAllowed = "move"; if (item === dragImg) return; const newItems = [...fileList.img]; //拷贝一份数据进行交换操作。 const src = newItems.indexOf(dragImg); //获取数组下标 const dst = newItems.indexOf(item); newItems.splice(dst, 0, ...newItems.splice(src, 1)); //交换位置 setFileList({ ...fileList, img: newItems }); }, [dragImg, fileList] ); // 删除某一张图片 const delImgListFu = useCallback( (id: number) => { const newItems = fileList.img.filter((v) => v.id !== id); setFileList({ ...fileList, img: newItems }); }, [fileList] ); // 没有通过校验 const onFinishFailed = useCallback(() => { setCoverCheck(true); setTypeOk(true); return MessageFu.warning("有表单不符号规则!"); }, []); // 通过校验点击确定 const onFinish = useCallback( async (value: GoodsTableType) => { console.log("通过校验,点击确定"); setCoverCheck(true); setTypeOk(true); if (typeCheck.length === 0 || cover === "" || fileCheckFu) return MessageFu.warning("有表单不符号规则!"); const fileIds = []; if (fileList.model.id && typeCheck.includes("model")) fileIds.push(fileList.model.id); if (fileList.audio.id && typeCheck.includes("audio")) fileIds.push(fileList.audio.id); if (fileList.video.id && typeCheck.includes("video")) fileIds.push(fileList.video.id); if (typeCheck.includes("img")) { fileList.img.forEach((v) => { if (v.id) fileIds.push(v.id); }); } const obj = { ...value, id: id > 0 ? id : null, dirCode, fileIds: fileIds.join(","), thumb: cover, type: typeCheck.join(","), tagType: valueZS1, tagCountry: valueZS2.join(","), } as GoodsTableType; const res = await getGoodsSaveAPI(obj); if (res.code === 0) { MessageFu.success(id > 0 ? "编辑成功!" : "新增成功!"); closeMoalFu(); if (id > 0) editListFu(); else addListFu(); } }, [ typeCheck, cover, fileCheckFu, fileList.model.id, fileList.audio.id, fileList.video.id, fileList.img, id, dirCode, valueZS1, valueZS2, closeMoalFu, editListFu, addListFu, ] ); return (
e.target.value.replace(/\s+/g, "")} > e.target.value.replace(/\s+/g, "")} >