|
@@ -1,9 +1,354 @@
|
|
|
-import React from "react";
|
|
|
+import React, {
|
|
|
+ useCallback,
|
|
|
+ useEffect,
|
|
|
+ useMemo,
|
|
|
+ useRef,
|
|
|
+ useState,
|
|
|
+} from "react";
|
|
|
import styles from "./index.module.scss";
|
|
|
+import { useDispatch, useSelector } from "react-redux";
|
|
|
+import { B1_APIgetList } from "@/store/action/B1Plate";
|
|
|
+import { RootState } from "@/store";
|
|
|
+import { Button, Input, Popconfirm, Table, Tooltip } from "antd";
|
|
|
+import { B2_APIdel, B2_APIgetList, B2_APIsort } from "@/store/action/B2Goods";
|
|
|
+import { ExclamationCircleFilled } from "@ant-design/icons";
|
|
|
+
|
|
|
+// 表格拖动排序-----------------
|
|
|
+import { DndProvider, useDrag, useDrop } from "react-dnd";
|
|
|
+import { HTML5Backend } from "react-dnd-html5-backend";
|
|
|
+import { MessageFu } from "@/utils/message";
|
|
|
+import { B2TableType } from "@/types";
|
|
|
+import ImageLazy from "@/components/ImageLazy";
|
|
|
+import B2Edit from "./B2Edit";
|
|
|
+
|
|
|
function B2Goods() {
|
|
|
+ const dispatch = useDispatch();
|
|
|
+ useEffect(() => {
|
|
|
+ dispatch(B1_APIgetList(""));
|
|
|
+ }, [dispatch]);
|
|
|
+
|
|
|
+ // 顶部筛选 列表
|
|
|
+ const topList = useSelector((state: RootState) => state.B1Plate.list);
|
|
|
+
|
|
|
+ //点击新增/编辑/查看
|
|
|
+ const [editId, setEditId] = useState(0);
|
|
|
+ const [isLook, setIsLook] = useState(false);
|
|
|
+
|
|
|
+ // 发送请求的参数
|
|
|
+ const [fromData, setFromData] = useState({
|
|
|
+ pageNum: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ searchKey: "",
|
|
|
+ type: "盛世",
|
|
|
+ });
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ dispatch(B2_APIgetList(fromData));
|
|
|
+ }, [dispatch, fromData]);
|
|
|
+
|
|
|
+ const [inputKey, setInputKey] = useState(1);
|
|
|
+
|
|
|
+ // 标题的输入
|
|
|
+ const nameTime = useRef(-1);
|
|
|
+ const nameChange = useCallback(
|
|
|
+ (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
+ clearTimeout(nameTime.current);
|
|
|
+ nameTime.current = window.setTimeout(() => {
|
|
|
+ setFromData({ ...fromData, searchKey: e.target.value });
|
|
|
+ }, 500);
|
|
|
+ },
|
|
|
+ [fromData]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 点击重置
|
|
|
+ const resetSelectFu = useCallback(
|
|
|
+ (val?: string) => {
|
|
|
+ setInputKey(Date.now());
|
|
|
+ setFromData({
|
|
|
+ pageNum: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ searchKey: "",
|
|
|
+ type: val ? val : fromData.type,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ [fromData.type]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 点击新增/编辑/查看
|
|
|
+ const openPageFu = useCallback((id: number, flag: boolean) => {
|
|
|
+ setEditId(id);
|
|
|
+ setIsLook(flag);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ // 点击删除
|
|
|
+ const delTableFu = useCallback(
|
|
|
+ async (id: number) => {
|
|
|
+ const res = await B2_APIdel(id);
|
|
|
+ if (res.code === 0) {
|
|
|
+ MessageFu.success("删除成功!");
|
|
|
+ dispatch(B2_APIgetList(fromData));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [dispatch, fromData]
|
|
|
+ );
|
|
|
+
|
|
|
+ const { tableInfo } = useSelector((state: RootState) => state.B2Goods);
|
|
|
+
|
|
|
+ const columns = useMemo(() => {
|
|
|
+ const arr: any = [
|
|
|
+ {
|
|
|
+ width: 100,
|
|
|
+ title: (
|
|
|
+ <div className="moveTit">
|
|
|
+ 序号
|
|
|
+ <Tooltip title="按住鼠标可拖动表格调整顺序">
|
|
|
+ <div className="inco" hidden={tableInfo.list.length < 2}>
|
|
|
+ <ExclamationCircleFilled />
|
|
|
+ </div>
|
|
|
+ </Tooltip>
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ render: (_1: any, _2: any, index: number) =>
|
|
|
+ index + 1 + (fromData.pageNum - 1) * fromData.pageSize,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "标题",
|
|
|
+ dataIndex: "name",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "图片",
|
|
|
+ render: (item: B2TableType) => (
|
|
|
+ <div className="tableImgAuto">
|
|
|
+ <ImageLazy width={60} height={60} src={item.thumb} />
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "正文",
|
|
|
+ render: (item: B2TableType) =>
|
|
|
+ item.content.length >= 50 ? (
|
|
|
+ <span style={{ cursor: "pointer" }} title={item.content}>
|
|
|
+ {item.content.substring(0, 50) + "..."}
|
|
|
+ </span>
|
|
|
+ ) : (
|
|
|
+ item.content
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "操作",
|
|
|
+ render: (item: B2TableType) => (
|
|
|
+ <>
|
|
|
+ <Button
|
|
|
+ size="small"
|
|
|
+ type="text"
|
|
|
+ onClick={() => openPageFu(item.id, true)}
|
|
|
+ >
|
|
|
+ 查看
|
|
|
+ </Button>
|
|
|
+ <Button
|
|
|
+ size="small"
|
|
|
+ type="text"
|
|
|
+ onClick={() => openPageFu(item.id, false)}
|
|
|
+ >
|
|
|
+ 编辑
|
|
|
+ </Button>
|
|
|
+ <Popconfirm
|
|
|
+ title="删除后无法恢复,是否删除?"
|
|
|
+ okText="删除"
|
|
|
+ cancelText="取消"
|
|
|
+ onConfirm={() => delTableFu(item.id)}
|
|
|
+ >
|
|
|
+ <Button size="small" type="text" danger>
|
|
|
+ 删除
|
|
|
+ </Button>
|
|
|
+ </Popconfirm>
|
|
|
+ </>
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ ];
|
|
|
+
|
|
|
+ return arr;
|
|
|
+ }, [
|
|
|
+ delTableFu,
|
|
|
+ fromData.pageNum,
|
|
|
+ fromData.pageSize,
|
|
|
+ openPageFu,
|
|
|
+ tableInfo.list.length,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 表格拖动排序
|
|
|
+ interface DraggableBodyRowProps
|
|
|
+ extends React.HTMLAttributes<HTMLTableRowElement> {
|
|
|
+ index: number;
|
|
|
+ moveRow: (dragIndex: number, hoverIndex: number) => void;
|
|
|
+ }
|
|
|
+
|
|
|
+ const type = "DraggableBodyRow";
|
|
|
+
|
|
|
+ const DraggableBodyRow = useCallback(
|
|
|
+ ({
|
|
|
+ index,
|
|
|
+ moveRow,
|
|
|
+ className,
|
|
|
+ style,
|
|
|
+ ...restProps
|
|
|
+ }: DraggableBodyRowProps) => {
|
|
|
+ // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
+ const ref = useRef<HTMLTableRowElement>(null);
|
|
|
+ // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
+ const [{ isOver, dropClassName }, drop] = useDrop({
|
|
|
+ accept: type,
|
|
|
+ collect: (monitor) => {
|
|
|
+ const { index: dragIndex } = monitor.getItem() || {};
|
|
|
+ if (dragIndex === index) {
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ isOver: monitor.isOver(),
|
|
|
+ dropClassName:
|
|
|
+ dragIndex < index ? " drop-over-downward" : " drop-over-upward",
|
|
|
+ };
|
|
|
+ },
|
|
|
+ drop: (item: { index: number }) => {
|
|
|
+ if (moveRow) moveRow(item.index, index);
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
+ const [, drag] = useDrag({
|
|
|
+ type,
|
|
|
+ item: { index },
|
|
|
+ collect: (monitor) => ({
|
|
|
+ isDragging: monitor.isDragging(),
|
|
|
+ }),
|
|
|
+ });
|
|
|
+ drop(drag(ref));
|
|
|
+
|
|
|
+ return (
|
|
|
+ <tr
|
|
|
+ ref={ref}
|
|
|
+ className={`${className}${isOver ? dropClassName : ""}`}
|
|
|
+ style={{ cursor: "move", ...style }}
|
|
|
+ {...restProps}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ },
|
|
|
+ []
|
|
|
+ );
|
|
|
+
|
|
|
+ const components = {
|
|
|
+ body: {
|
|
|
+ row: DraggableBodyRow,
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ const moveRow = useCallback(
|
|
|
+ async (dragIndex: number, hoverIndex: number) => {
|
|
|
+ if (dragIndex === hoverIndex) return;
|
|
|
+ // 交互位置-之前的id
|
|
|
+ const beforeId = tableInfo.list[dragIndex].id;
|
|
|
+ const afterId = tableInfo.list[hoverIndex].id;
|
|
|
+ // console.log("交换位置", beforeId, afterId);
|
|
|
+ const res = await B2_APIsort(beforeId, afterId);
|
|
|
+ if (res.code === 0) dispatch(B2_APIgetList(fromData));
|
|
|
+ },
|
|
|
+ [dispatch, fromData, tableInfo.list]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 页码变化
|
|
|
+ const paginationChange = useCallback(
|
|
|
+ () => (pageNum: number, pageSize: number) => {
|
|
|
+ setFromData({ ...fromData, pageNum, pageSize });
|
|
|
+ },
|
|
|
+ [fromData]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 从子页面编辑成功
|
|
|
+ const editFuSon = useCallback(
|
|
|
+ (val: string) => {
|
|
|
+ if (val === fromData.type) dispatch(B2_APIgetList(fromData));
|
|
|
+ else setFromData({ ...fromData, type: val });
|
|
|
+ },
|
|
|
+ [dispatch, fromData]
|
|
|
+ );
|
|
|
+
|
|
|
return (
|
|
|
<div className={styles.B2Goods}>
|
|
|
- <div className="pageTitle">藏品</div>
|
|
|
+ <div className="pageTitle">
|
|
|
+ {editId ? (isLook ? "查看" : editId > 0 ? "编辑" : "新增") : "藏品"}
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* 顶部搜索 */}
|
|
|
+ <div className="top">
|
|
|
+ <div className="topSelect">
|
|
|
+ {topList.map((v) => (
|
|
|
+ <div key={v.id}>
|
|
|
+ <Button
|
|
|
+ onClick={() => setFromData({ ...fromData, type: v.name })}
|
|
|
+ type={v.name === fromData.type ? "primary" : "default"}
|
|
|
+ >
|
|
|
+ {v.name}
|
|
|
+ </Button>
|
|
|
+  
|
|
|
+ </div>
|
|
|
+ ))}
|
|
|
+ </div>
|
|
|
+ <span>标题:</span>
|
|
|
+ <Input
|
|
|
+ key={inputKey}
|
|
|
+ maxLength={50}
|
|
|
+ style={{ width: 300 }}
|
|
|
+ placeholder="请输入关键字"
|
|
|
+ allowClear
|
|
|
+ onChange={(e) => nameChange(e)}
|
|
|
+ />
|
|
|
+   
|
|
|
+ <Button onClick={() => resetSelectFu("")}>重置</Button>
|
|
|
+   
|
|
|
+ <Button type="primary" onClick={() => openPageFu(-1, false)}>
|
|
|
+ 新增
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* 表格主体 */}
|
|
|
+ <div className="tableBox">
|
|
|
+ <DndProvider backend={HTML5Backend}>
|
|
|
+ <Table
|
|
|
+ scroll={{ y: 617 }}
|
|
|
+ columns={columns}
|
|
|
+ dataSource={tableInfo.list}
|
|
|
+ components={components}
|
|
|
+ rowKey="id"
|
|
|
+ pagination={{
|
|
|
+ showQuickJumper: true,
|
|
|
+ position: ["bottomCenter"],
|
|
|
+ showSizeChanger: true,
|
|
|
+ current: fromData.pageNum,
|
|
|
+ pageSize: fromData.pageSize,
|
|
|
+ total: tableInfo.total,
|
|
|
+ onChange: paginationChange(),
|
|
|
+ }}
|
|
|
+ onRow={(_, index) => {
|
|
|
+ const attr = {
|
|
|
+ index,
|
|
|
+ moveRow,
|
|
|
+ };
|
|
|
+ return attr as React.HTMLAttributes<any>;
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </DndProvider>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* 点击新增/编辑/查看 */}
|
|
|
+ {editId ? (
|
|
|
+ <B2Edit
|
|
|
+ editId={editId}
|
|
|
+ isLook={isLook}
|
|
|
+ closeFu={() => setEditId(0)}
|
|
|
+ editFu={(val) => editFuSon(val)}
|
|
|
+ addFu={(val) => resetSelectFu(val)}
|
|
|
+ myType={fromData.type}
|
|
|
+ />
|
|
|
+ ) : null}
|
|
|
</div>
|
|
|
);
|
|
|
}
|