|
|
@@ -0,0 +1,303 @@
|
|
|
+import { venueUploadAPI } from "@/store/action/A4Venue";
|
|
|
+import { ImgListType } from "@/types";
|
|
|
+import { domShowFu, progressDomFu } from "@/utils/domShow";
|
|
|
+import { MessageFu } from "@/utils/message";
|
|
|
+import { Button, Form, Input, Popconfirm } from "antd";
|
|
|
+import { PlusOutlined, CloseOutlined } from "@ant-design/icons";
|
|
|
+import TextArea from "antd/es/input/TextArea";
|
|
|
+import classNames from "classnames";
|
|
|
+import React, {
|
|
|
+ useCallback,
|
|
|
+ useEffect,
|
|
|
+ useMemo,
|
|
|
+ useRef,
|
|
|
+ useState,
|
|
|
+} from "react";
|
|
|
+import styles from "./index.module.scss";
|
|
|
+import ImageLazy from "@/components/ImageLazy";
|
|
|
+
|
|
|
+type Props = {
|
|
|
+ id: number;
|
|
|
+ closePageFu: () => void;
|
|
|
+ editTableFu: () => void;
|
|
|
+};
|
|
|
+
|
|
|
+function VenueEdit({ id, closePageFu, editTableFu }: Props) {
|
|
|
+ // 设置表单初始数据(区分编辑和新增)
|
|
|
+ const FormBoxRef = useRef<any>({});
|
|
|
+
|
|
|
+ // 文件的dirCode码
|
|
|
+ const [dirCode, setDirCode] = useState("");
|
|
|
+
|
|
|
+ const myInput = useRef<HTMLInputElement>(null);
|
|
|
+
|
|
|
+ // 通过id获取详情,回显数据
|
|
|
+ const getInfoFu = useCallback((id: number) => {
|
|
|
+ // setDirCode(res.data.entity.dirCode);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (id) getInfoFu(id);
|
|
|
+ else {
|
|
|
+ setDirCode(Date.now() + "");
|
|
|
+ }
|
|
|
+ }, [getInfoFu, id]);
|
|
|
+
|
|
|
+ // 多张图片附件
|
|
|
+ const [imgList, setImgList] = useState<ImgListType[]>([]);
|
|
|
+
|
|
|
+ // 上传附件的处理函数
|
|
|
+ const handeUpPhoto = useCallback(
|
|
|
+ async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
+ if (e.target.files) {
|
|
|
+ // 拿到files信息
|
|
|
+ const filesInfo = e.target.files[0];
|
|
|
+
|
|
|
+ let anType = ["image/jpeg", "image/png"];
|
|
|
+ let anTit1 = "只支持png、jpg和jpeg格式!";
|
|
|
+ let anTit2 = "最大支持20M!";
|
|
|
+ let anSize = 20 * 1024 * 1024;
|
|
|
+
|
|
|
+ // 校验格式
|
|
|
+ if (!anType.includes(filesInfo.type)) {
|
|
|
+ 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", "img");
|
|
|
+ fd.append("dirCode", dirCode);
|
|
|
+ fd.append("file", filesInfo);
|
|
|
+
|
|
|
+ e.target.value = "";
|
|
|
+
|
|
|
+ const res: any = await venueUploadAPI(fd);
|
|
|
+
|
|
|
+ if (res.code === 0) {
|
|
|
+ MessageFu.success("上传成功!");
|
|
|
+ setImgList([...imgList, res.data]);
|
|
|
+ }
|
|
|
+ domShowFu("#UpAsyncLoding", false);
|
|
|
+ progressDomFu("0%");
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [dirCode, imgList]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 附件图片的拖动
|
|
|
+ const [dragImg, setDragImg] = useState<any>(null);
|
|
|
+
|
|
|
+ const handleDragOver = useCallback(
|
|
|
+ (e: React.DragEvent<HTMLDivElement>, item: ImgListType) => {
|
|
|
+ e.dataTransfer.dropEffect = "move";
|
|
|
+ },
|
|
|
+ []
|
|
|
+ );
|
|
|
+
|
|
|
+ const handleDragEnter = useCallback(
|
|
|
+ (e: React.DragEvent<HTMLDivElement>, 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);
|
|
|
+ },
|
|
|
+ [imgList]
|
|
|
+ );
|
|
|
+
|
|
|
+ const [typeOk, setTypeOk] = useState(false);
|
|
|
+
|
|
|
+ // 附件信息的校验
|
|
|
+ const fileCheckFu = useMemo(() => {
|
|
|
+ let flag = false;
|
|
|
+ if (imgList.length === 0) flag = true;
|
|
|
+ return flag;
|
|
|
+ }, [imgList.length]);
|
|
|
+
|
|
|
+ // 没有通过校验
|
|
|
+ const onFinishFailed = useCallback(() => {
|
|
|
+ setTypeOk(true);
|
|
|
+ // return MessageFu.warning("有表单不符号规则!");
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ // 通过校验点击确定
|
|
|
+ const onFinish = useCallback(
|
|
|
+ (values: any) => {
|
|
|
+ setTypeOk(true);
|
|
|
+
|
|
|
+ if (fileCheckFu) return;
|
|
|
+
|
|
|
+ const obj = {
|
|
|
+ ...values,
|
|
|
+ id: id > 0 ? id : null,
|
|
|
+ dirCode,
|
|
|
+ fileIds: imgList.map((v) => v.id).join(","),
|
|
|
+ };
|
|
|
+ console.log(obj);
|
|
|
+ editTableFu();
|
|
|
+ closePageFu();
|
|
|
+ },
|
|
|
+ [closePageFu, dirCode, editTableFu, fileCheckFu, id, imgList]
|
|
|
+ );
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div className={styles.VenueEdit}>
|
|
|
+ <div className="pageTitle">编辑场馆</div>
|
|
|
+ <div className="formBox mySorrl">
|
|
|
+ <Form
|
|
|
+ ref={FormBoxRef}
|
|
|
+ name="basic"
|
|
|
+ labelCol={{ span: 3 }}
|
|
|
+ onFinish={onFinish}
|
|
|
+ onFinishFailed={onFinishFailed}
|
|
|
+ autoComplete="off"
|
|
|
+ >
|
|
|
+ <Form.Item
|
|
|
+ label="名称"
|
|
|
+ name="name"
|
|
|
+ rules={[{ required: true, message: "请输入标题!" }]}
|
|
|
+ >
|
|
|
+ <Input disabled />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item
|
|
|
+ label="位置"
|
|
|
+ name="myLoc"
|
|
|
+ rules={[{ required: true, message: "请输入位置!" }]}
|
|
|
+ getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
|
|
|
+ >
|
|
|
+ <Input maxLength={8} showCount placeholder="请输入内容" />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ <Form.Item
|
|
|
+ label="简介"
|
|
|
+ name="description"
|
|
|
+ // getValueFromEvent={(e) => e.target.value.trim()}
|
|
|
+ >
|
|
|
+ <TextArea
|
|
|
+ rows={5}
|
|
|
+ placeholder="请输入内容"
|
|
|
+ showCount
|
|
|
+ maxLength={200}
|
|
|
+ />
|
|
|
+ </Form.Item>
|
|
|
+
|
|
|
+ {/* 上传附件图片 */}
|
|
|
+ <div className="myformBox">
|
|
|
+ <input
|
|
|
+ id="upInput2"
|
|
|
+ type="file"
|
|
|
+ accept=".png,.jpg,.jpeg"
|
|
|
+ ref={myInput}
|
|
|
+ onChange={(e) => handeUpPhoto(e)}
|
|
|
+ />
|
|
|
+
|
|
|
+ <div className="label">
|
|
|
+ <span>*</span> 图片:
|
|
|
+ </div>
|
|
|
+ <>
|
|
|
+ <div className="fileBoxRow_r">
|
|
|
+ <div className="upImgBox">
|
|
|
+ <div
|
|
|
+ hidden={imgList.length >= 9}
|
|
|
+ className="fileBoxRow_up"
|
|
|
+ onClick={() => myInput.current?.click()}
|
|
|
+ >
|
|
|
+ <PlusOutlined />
|
|
|
+ </div>
|
|
|
+ {imgList.map((v) => (
|
|
|
+ <div
|
|
|
+ className="fileBoxRow_r_img"
|
|
|
+ key={v.id}
|
|
|
+ draggable="true"
|
|
|
+ onDragStart={() => setDragImg(v)}
|
|
|
+ onDragOver={(e) => handleDragOver(e, v)}
|
|
|
+ onDragEnter={(e) => handleDragEnter(e, v)}
|
|
|
+ onDragEnd={() => setDragImg(null)}
|
|
|
+ >
|
|
|
+ {v.filePath ? (
|
|
|
+ <ImageLazy
|
|
|
+ noLook={dragImg ? true : false}
|
|
|
+ width={100}
|
|
|
+ height={100}
|
|
|
+ src={v.filePath}
|
|
|
+ />
|
|
|
+ ) : null}
|
|
|
+
|
|
|
+ <Popconfirm
|
|
|
+ title="删除后无法恢复,是否删除?"
|
|
|
+ okText="删除"
|
|
|
+ cancelText="取消"
|
|
|
+ onConfirm={() => delImgListFu(v.id)}
|
|
|
+ >
|
|
|
+ <div className="clearCover">
|
|
|
+ <CloseOutlined />
|
|
|
+ </div>
|
|
|
+ </Popconfirm>
|
|
|
+ </div>
|
|
|
+ ))}
|
|
|
+ </div>
|
|
|
+ <div className="fileTit">
|
|
|
+ {imgList.length >= 2 ? (
|
|
|
+ <>
|
|
|
+ 按住鼠标可拖动图片调整顺序。
|
|
|
+ <br />
|
|
|
+ </>
|
|
|
+ ) : null}
|
|
|
+ 支持png、jpg和jpeg的图片格式;最大支持20M;最多支持9张。
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </>
|
|
|
+ </div>
|
|
|
+ <div
|
|
|
+ className={classNames(
|
|
|
+ "noUpThumb noUpThumb2",
|
|
|
+ fileCheckFu && typeOk ? "noUpThumbAc" : ""
|
|
|
+ )}
|
|
|
+ >
|
|
|
+ 请上传图片!
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* 确定和取消按钮 */}
|
|
|
+ <br />
|
|
|
+ <Form.Item wrapperCol={{ offset: 10, span: 16 }}>
|
|
|
+ <Button type="primary" htmlType="submit">
|
|
|
+ 提交
|
|
|
+ </Button>
|
|
|
+  
|
|
|
+ <Popconfirm
|
|
|
+ title="放弃编辑后,信息将不会保存!"
|
|
|
+ okText="放弃"
|
|
|
+ cancelText="取消"
|
|
|
+ onConfirm={closePageFu}
|
|
|
+ >
|
|
|
+ <Button>取消</Button>
|
|
|
+ </Popconfirm>
|
|
|
+ </Form.Item>
|
|
|
+ </Form>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+const MemoVenueEdit = React.memo(VenueEdit);
|
|
|
+
|
|
|
+export default MemoVenueEdit;
|