|
@@ -0,0 +1,252 @@
|
|
|
|
|
+import React, {
|
|
|
|
|
+ useCallback,
|
|
|
|
|
+ useEffect,
|
|
|
|
|
+ useMemo,
|
|
|
|
|
+ useRef,
|
|
|
|
|
+ useState,
|
|
|
|
|
+} from "react";
|
|
|
|
|
+import styles from "./index.module.scss";
|
|
|
|
|
+import { ExclamationOutlined } from "@ant-design/icons";
|
|
|
|
|
+
|
|
|
|
|
+// 表格拖动排序-----------------
|
|
|
|
|
+import { DndProvider, useDrag, useDrop } from "react-dnd";
|
|
|
|
|
+import { HTML5Backend } from "react-dnd-html5-backend";
|
|
|
|
|
+import { MessageFu } from "@/utils/message";
|
|
|
|
|
+import { Button, Popconfirm, Table, Tooltip } from "antd";
|
|
|
|
|
+import { useDispatch, useSelector } from "react-redux";
|
|
|
|
|
+import { RootState } from "@/store";
|
|
|
|
|
+import { A6TableType } from "@/types";
|
|
|
|
|
+import { A6_APIdel, A6_APIgetList, A6_APIsort } from "@/store/action/A6Swpage";
|
|
|
|
|
+import SwAdd from "./SwAdd";
|
|
|
|
|
+import ImageLazy from "@/components/ImageLazy";
|
|
|
|
|
+
|
|
|
|
|
+function A6Swpage() {
|
|
|
|
|
+ const dispatch = useDispatch();
|
|
|
|
|
+
|
|
|
|
|
+ // 发送请求获取列表数据
|
|
|
|
|
+ const getList = useCallback(() => {
|
|
|
|
|
+ dispatch(A6_APIgetList());
|
|
|
|
|
+ }, [dispatch]);
|
|
|
|
|
+
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ getList();
|
|
|
|
|
+ }, [getList]);
|
|
|
|
|
+
|
|
|
|
|
+ // 点击删除
|
|
|
|
|
+ const delTableFu = useCallback(
|
|
|
|
|
+ async (id: number) => {
|
|
|
|
|
+ const res = await A6_APIdel(id);
|
|
|
|
|
+ if (res.code === 0) {
|
|
|
|
|
+ MessageFu.success("删除成功!");
|
|
|
|
|
+ getList();
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ [getList]
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 有关表格数据
|
|
|
|
|
+ const { tableInfo: results } = useSelector(
|
|
|
|
|
+ (state: RootState) => state.swpageReducer
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ const columns = useMemo(() => {
|
|
|
|
|
+ return [
|
|
|
|
|
+ {
|
|
|
|
|
+ title: () => (
|
|
|
|
|
+ <div className="incoTitle">
|
|
|
|
|
+ 序号
|
|
|
|
|
+ {results.length >= 2 ? (
|
|
|
|
|
+ <Tooltip title="按住鼠标可拖动表格调整顺序">
|
|
|
|
|
+ <div className="hotTitleInco1">
|
|
|
|
|
+ <ExclamationOutlined />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </Tooltip>
|
|
|
|
|
+ ) : null}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ),
|
|
|
|
|
+ width: 100,
|
|
|
|
|
+ render: (text: any, item: any, index: number) => index + 1,
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ title: "标题",
|
|
|
|
|
+ render: (item: A6TableType) => (item.name ? item.name : "(空)"),
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ title: "正文",
|
|
|
|
|
+ render: (item: A6TableType) =>
|
|
|
|
|
+ item.description ? (
|
|
|
|
|
+ item.description.length > 35 ? (
|
|
|
|
|
+ <span style={{ cursor: "pointer" }} title={item.description}>
|
|
|
|
|
+ {item.description.substring(0, 35) + "..."}
|
|
|
|
|
+ </span>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ item.description
|
|
|
|
|
+ )
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ "(空)"
|
|
|
|
|
+ ),
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ title: "图片",
|
|
|
|
|
+ render: (item: A6TableType) => (
|
|
|
|
|
+ <div className="tableImgAuto">
|
|
|
|
|
+ <ImageLazy width={60} height={60} src={item.thumb!} />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ),
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ title: "最近编辑时间",
|
|
|
|
|
+ dataIndex: "updateTime",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ title: "展示状态",
|
|
|
|
|
+ width: 100,
|
|
|
|
|
+ render: (item: A6TableType) => (item.display === 1 ? "展示" : "不展示"),
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ title: "操作",
|
|
|
|
|
+ render: (item: A6TableType) => (
|
|
|
|
|
+ <>
|
|
|
|
|
+ <Button size="small" type="text" onClick={() => setEditId(item.id)}>
|
|
|
|
|
+ 编辑
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ <Popconfirm
|
|
|
|
|
+ title="删除后无法恢复,是否删除?"
|
|
|
|
|
+ okText="删除"
|
|
|
|
|
+ cancelText="取消"
|
|
|
|
|
+ onConfirm={() => delTableFu(item.id!)}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Button size="small" type="text" danger>
|
|
|
|
|
+ 删除
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </Popconfirm>
|
|
|
|
|
+ </>
|
|
|
|
|
+ ),
|
|
|
|
|
+ },
|
|
|
|
|
+ ];
|
|
|
|
|
+ }, [delTableFu, results.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 }) => {
|
|
|
|
|
+ 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 = results[dragIndex].id;
|
|
|
|
|
+ const afterId = results[hoverIndex].id;
|
|
|
|
|
+
|
|
|
|
|
+ const res = await A6_APIsort(beforeId, afterId);
|
|
|
|
|
+
|
|
|
|
|
+ if (res.code === 0) getList();
|
|
|
|
|
+ },
|
|
|
|
|
+ [getList, results]
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 点击新增/编辑出来的页面
|
|
|
|
|
+ const [editId, setEditId] = useState(0);
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <div className={styles.A6Swpage}>
|
|
|
|
|
+ <div className="pageTitle">
|
|
|
|
|
+ 轮播图管理
|
|
|
|
|
+ <div className="titleBtn" onClick={() => setEditId(-1)}>
|
|
|
|
|
+ <Button type="primary">新增</Button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div className="mainBox">
|
|
|
|
|
+ <DndProvider backend={HTML5Backend}>
|
|
|
|
|
+ <Table
|
|
|
|
|
+ scroll={{ y: 680 }}
|
|
|
|
|
+ dataSource={results}
|
|
|
|
|
+ columns={columns}
|
|
|
|
|
+ rowKey="id"
|
|
|
|
|
+ pagination={false}
|
|
|
|
|
+ components={components}
|
|
|
|
|
+ onRow={(_, index) => {
|
|
|
|
|
+ const attr = {
|
|
|
|
|
+ index,
|
|
|
|
|
+ moveRow,
|
|
|
|
|
+ };
|
|
|
|
|
+ return attr as React.HTMLAttributes<any>;
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </DndProvider>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {editId ? (
|
|
|
|
|
+ <SwAdd
|
|
|
|
|
+ id={editId}
|
|
|
|
|
+ closeMoalFu={() => setEditId(0)}
|
|
|
|
|
+ addModelFu={() => getList()}
|
|
|
|
|
+ />
|
|
|
|
|
+ ) : null}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ );
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const MemoA6Swpage = React.memo(A6Swpage);
|
|
|
|
|
+
|
|
|
|
|
+export default MemoA6Swpage;
|