import { MessageFu } from "@/utils/message"; import { Button, Form, Input, Popconfirm, Radio } from "antd"; import React, { useCallback, useEffect, useRef, useState } from "react"; import { PlusOutlined, CloseOutlined } from "@ant-design/icons"; import classNames from "classnames"; import styles from "./index.module.scss"; import ImageLazy from "@/components/ImageLazy"; import { ImgListType, WallSaveAPIType } from "@/types"; import WallLook from "../WallLook"; import { getWallDetailAPI, goodsUploadAPI, setWallSave, } from "@/store/action/B3Wall"; import { domShowFu, progressDomFu } from "@/utils/domShow"; type Props = { id: number; closeMoalFu: (txt: string) => void; }; function WallAdd({ id, closeMoalFu }: Props) { const getInfoFu = useCallback(async (id: number) => { const res = await getWallDetailAPI(id); FormBoxRef.current.setFieldsValue({ name: res.data.entity.name }); setImgNum(res.data.entity.type); const imgListRes = res.data.file; setImgList(imgListRes); imgListRef.current = imgListRes; }, []); useEffect(() => { if (id > 0) getInfoFu(id); }, [getInfoFu, id]); // 表单的ref const FormBoxRef = useRef({}); // 上传封面图的ref const myInput = useRef(null); // 版式的选择 const [imgNum, setImgNum] = useState<1 | 2 | 4 | 6 | 8>(1); // 上传图片的校验 const [imgCheck, setImgCheck] = useState(false); // 上传图片的全部数据(最多8张来进行切割) const imgListRef = useRef([]); // 在页面展示的图片 const [imgList, setImgList] = useState([]); // 版式的选择改变,切割数组 useEffect(() => { if (imgListRef.current.length) { const newData = imgListRef.current.slice(0, imgNum); setImgList(newData); } }, [imgNum]); // ---------附件图片的拖动 const [dragImg, setDragImg] = useState(null); const handleDragOver = useCallback( (e: React.DragEvent, item: ImgListType) => { e.dataTransfer.dropEffect = "move"; }, [] ); const handleDragEnter = useCallback( (e: React.DragEvent, item: ImgListType) => { e.dataTransfer.effectAllowed = "move"; if (item === dragImg) return; const newItems = [...imgList]; //拷贝一份数据进行交换操作。 const src = newItems.indexOf(dragImg); //获取数组下标 const dst = newItems.indexOf(item); newItems.splice(dst, 0, ...newItems.splice(src, 1)); //交换位置 setImgList(newItems); }, [dragImg, imgList] ); // 删除某一张图片 const delImgListFu = useCallback( (id: number) => { const newItems = imgList.filter((v) => v.id !== id); setImgList(newItems); imgListRef.current = imgListRef.current.filter((v) => v.id !== id); }, [imgList] ); // 没有通过校验 const onFinishFailed = useCallback(() => { setImgCheck(true); }, []); // 上传封面图 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 > 30 * 1024 * 1024) { e.target.value = ""; return MessageFu.warning("最大支持30M!"); } // 创建FormData对象 const fd = new FormData(); // 把files添加进FormData对象(‘photo’为后端需要的字段) fd.append("type", "img"); fd.append("file", filesInfo); e.target.value = ""; const res: any = await goodsUploadAPI(fd); if (res.code === 0) { MessageFu.success("上传成功!"); setImgList([res.data, ...imgList]); imgListRef.current.unshift(res.data); } domShowFu("#UpAsyncLoding", false); progressDomFu("0%"); } }, [imgList] ); // 通过校验点击确定 const onFinish = useCallback( async (value: { name: string }) => { console.log("通过校验,点击确定"); setImgCheck(true); if (imgList.length < imgNum) return; const obj: WallSaveAPIType = { id: id > 0 ? id : null, fileIds: imgList.map((v) => v.id).join(","), name: value.name, type: imgNum, }; const res = await setWallSave(obj); if (res.code === 0) { MessageFu.success(id > 0 ? "编辑成功!" : "新增成功!"); closeMoalFu(id > 0 ? "编辑" : "新增"); } }, [closeMoalFu, id, imgList, imgNum] ); // 点击预览效果 const [lookImg, setLookImg] = useState(false); return (
e.target.value.replace(/\s+/g, "")} >
* 版式:
setImgNum(e.target.value)} value={imgNum} > 1 2 4 6 8
{/* 图片上传 */}
handeUpPhoto(e)} />
{imgList.map((v) => (
setDragImg(v)} onDragOver={(e) => handleDragOver(e, v)} onDragEnter={(e) => handleDragEnter(e, v)} onDragEnd={() => setDragImg(null)} > {v.filePath ? ( ) : null} delImgListFu(v.id!)} >
))}
{imgList.length && imgList.length >= 2 ? ( <> 按住鼠标可拖动图片调整顺序。
) : null} 建议尺寸:{12960 / imgNum}*1920
支持png、jpg和jpeg的图片格式;最大支持30M。
{/* 校验提示 */}
请上传 {imgNum} 张图片!
{/* 确定和取消按钮 */}
closeMoalFu("取消")} >
{/* 点击预览效果出来的页面 */} {lookImg ? ( setLookImg(false)} /> ) : null}
); } const MemoWallAdd = React.memo(WallAdd); export default MemoWallAdd;