| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- "use client";
- import {fetchApi} from "@/app/_modules/func";
- import {DeleteOutlined, ExclamationCircleFilled, PlusOutlined, ReloadOutlined,} from "@ant-design/icons";
- import type {ActionType, ProColumns, ProFormInstance,} from "@ant-design/pro-components";
- import {
- ModalForm,
- PageContainer,
- ProForm,
- ProFormDigit,
- ProFormRadio,
- ProFormSelect,
- ProFormText,
- ProFormTreeSelect,
- ProTable,
- } from "@ant-design/pro-components";
- import {App, Button, Modal, Space, Tag} from "antd";
- import {useRouter} from "next/navigation";
- import {
- faArrowsUpDown,
- faCheck,
- faPenToSquare,
- faToggleOff,
- faToggleOn,
- faXmark,
- } from "@fortawesome/free-solid-svg-icons";
- import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
- import {IconMap} from "@/app/_modules/definies";
- import {useRef, useState} from "react";
- import globalMessage from "@/app/_modules/globalMessage";
- //查询表格数据API
- const queryAPI = "/api/system/menu/list";
- //新建数据API
- const newAPI = "/api/system/menu";
- //修改数据API
- const modifyAPI = "/api/system/menu";
- //查询详情数据API
- const queryDetailAPI = "/api/system/menu";
- //删除API
- const deleteAPI = "/api/system/menu";
- export default function SysMenu() {
- const { message } = App.useApp();
- const { push } = useRouter();
- // 添加用于控制删除确认模态框的状态
- const [deleteModalVisible, setDeleteModalVisible] = useState(false);
- const [deleteRecord, setDeleteRecord] = useState<any>(null);
-
- // 添加用于控制清空确认模态框的状态(如果需要的话)
- const [clearModalVisible, setClearModalVisible] = useState(false);
- //表格列定义
- const columns: ProColumns[] = [
- {
- title: "菜单名称",
- fieldProps: {
- placeholder: "请输入菜单名称",
- },
- dataIndex: "menuName",
- order: 2,
- },
- {
- title: "图标",
- dataIndex: "icon",
- search: false,
- },
- {
- title: "排序",
- dataIndex: "orderNum",
- search: false,
- },
- {
- title: "权限标识",
- dataIndex: "perms",
- ellipsis: true,
- search: false,
- },
- {
- title: "状态",
- fieldProps: {
- placeholder: "请选择菜单状态",
- },
- dataIndex: "status",
- valueType: "select",
- render: (_, record) => {
- return (
- <Space>
- <Tag
- color={record.status === "0" ? "green" : "red"}
- icon={
- record.status == 0 ? (
- <FontAwesomeIcon icon={faCheck} />
- ) : (
- <FontAwesomeIcon icon={faXmark} />
- )
- }
- >
- {_}
- </Tag>
- </Space>
- );
- },
- valueEnum: {
- 0: {
- text: "正常",
- status: "0",
- },
- 1: {
- text: "停用",
- status: "1",
- },
- },
- order: 1,
- },
- {
- title: "创建时间",
- dataIndex: "createTime",
- valueType: "dateTime",
- search: false,
- },
- {
- title: "操作",
- key: "option",
- search: false,
- render: (_, record) => [
- <Button
- key="modifyBtn"
- type="link"
- icon={<FontAwesomeIcon icon={faPenToSquare} />}
- onClick={() => onClickShowRowModifyModal(record)}
- >
- 修改
- </Button>,
- <Button
- key="newBtn"
- type="link"
- icon={<PlusOutlined />}
- onClick={() => onClickAdd(record)}
- >
- 新建
- </Button>,
- <Button
- key="deleteBtn"
- type="link"
- danger
- icon={<DeleteOutlined />}
- onClick={() => onClickDeleteRow(record)}
- >
- 删除
- </Button>,
- ],
- },
- ];
- //0.查询表格数据
- //原始的可展开的所有行的 id
- const [defaultExpandKeys, setDefaultExpandKeys] = useState<any[]>([]);
- //控制行展开的数据
- const [expandKeys, setExpandKeys] = useState<any[]>([]);
- const queryTableData = async (params: any, sorter: any, filter: any) => {
- const searchParams = {
- ...params,
- };
- const queryParams = new URLSearchParams(searchParams);
- Object.keys(sorter).forEach((key) => {
- queryParams.append("orderByColumn", key);
- if (sorter[key] === "ascend") {
- queryParams.append("isAsc", "ascending");
- } else {
- queryParams.append("isAsc", "descending");
- }
- });
- const body = await fetchApi(`${queryAPI}?${queryParams}`, push);
- const firstLevel = getFirstLevel(body.data);
- firstLevel.forEach((first) => {
- getChildren(body.data, first);
- });
- const newExpandedKeys: any[] = [];
- const render = (treeDatas: any[]) => {
- // 获取到所有可展开的父节点
- treeDatas.map((item) => {
- if (item.children) {
- newExpandedKeys.push(item.menuId);
- render(item.children);
- }
- });
- return newExpandedKeys;
- };
- const keys = render(firstLevel);
- setDefaultExpandKeys(keys);
- setExpandKeys([]);
- return firstLevel;
- };
- const getFirstLevel = (data: any[]) => {
- const firstLevel: any[] = [];
- for (let index = 0; index < data.length; index++) {
- const item = data[index];
- if (item.parentId === 0) {
- firstLevel.push(item);
- }
- }
- return firstLevel;
- };
- const getChildren = (data: any[], parentNode: any) => {
- for (let index = 0; index < data.length; index++) {
- const item = data[index];
- if (item.parentId === parentNode.menuId) {
- parentNode.children.push(item);
- getChildren(data, item);
- }
- }
- if (parentNode.children.length == 0) {
- delete parentNode.children;
- }
- };
- //1.新建
- const [showAddModal, setShowAddModal] = useState(false);
- //新建表单是否带有父节点id
- const [rowParentId, setRowParentId] = useState(0);
- //点击新建,如果从行点击新建,给定父节点
- const onClickAdd = (record?: any) => {
- setRowParentId(record.menuId);
- setShowAddModal(true);
- };
- const cancelAddModal = () => {
- setShowAddModal(false);
- setRowParentId(0);
- };
- //确定新建数据
- const executeAddData = async (values: any) => {
- const body = await fetchApi(newAPI, push, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(values),
- });
- if (body != undefined) {
- if (body.code == 200) {
- globalMessage.success(body.msg);
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- setShowAddModal(false);
- return true;
- }
- globalMessage.error(body.msg);
- return false;
- }
- return false;
- };
- //2.修改
- //是否展示修改对话框
- const [isShowModifyDataModal, setIsShowModifyDataModal] = useState(false);
- //展示修改对话框
- const onClickShowRowModifyModal = (record: any) => {
- queryRowData(record);
- setIsShowModifyDataModal(true);
- };
- //修改数据表单引用
- const modifyFormRef = useRef<ProFormInstance>(null);
- //操作当前数据的附加数据
- const [operatRowData, setOperateRowData] = useState<{
- [key: string]: any;
- }>({});
- //查询并加载待修改数据的详细信息
- const queryRowData = async (record: any) => {
- const menuId = record.menuId;
- operatRowData["menuId"] = menuId;
- setOperateRowData(operatRowData);
- if (menuId !== undefined) {
- const body = await fetchApi(`${queryDetailAPI}/${menuId}`, push);
- if (body !== undefined) {
- if (body.code == 200) {
- modifyFormRef?.current?.setFieldsValue({
- //需要加载到修改表单中的数据
- parentId: body.data.parentId,
- menuName: body.data.menuName,
- orderNum: body.data.orderNum,
- path: body.data.path,
- isFrame: body.data.isFrame,
- menuType: body.data.menuType,
- perms: body.data.perms,
- icon: body.data.icon,
- visible: body.data.visible,
- status: body.data.status,
- });
- setIsCatalog(body.data.menuType === "M");
- setIsMenu(body.data.menuType === "C");
- setIsButton(body.data.menuType === "F");
- }
- }
- }
- };
- //确认修改数据
- const executeModifyData = async (values: any) => {
- values["menuId"] = operatRowData["menuId"];
- const body = await fetchApi(modifyAPI, push, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(values),
- });
- if (body !== undefined) {
- if (body.code == 200) {
- globalMessage.success(body.msg);
- //刷新列表
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- setIsShowModifyDataModal(false);
- return true;
- }
- globalMessage.error(body.msg);
- return false;
- }
- };
- //3.展开/折叠
- //点击展开/折叠按钮
- const onClickExpandRow = () => {
- if (expandKeys.length > 0) {
- setExpandKeys([]);
- } else {
- setExpandKeys(defaultExpandKeys);
- }
- };
- //处理行的展开/折叠逻辑
- const handleExpand = (expanded: boolean, record: any) => {
- console.log("has keys:", expandKeys);
- let keys = [...expandKeys];
- if (expanded) {
- keys.push(record.menuId);
- } else {
- keys = keys.filter((key: number) => key !== record.menuId);
- }
- console.log("now keys:", keys);
- setExpandKeys(keys);
- };
- //4.导出
- //5.选择行
- //搜索栏显示状态
- const [showSearch, setShowSearch] = useState(true);
- //action对象引用
- const actionTableRef = useRef<ActionType>(null);
- //搜索表单对象引用
- const searchTableFormRef = useRef<ProFormInstance>(null!);
- const getMenuList = async () => {
- const body = await fetchApi(queryAPI, push);
- if (body !== undefined) {
- const firstLevel = getFirstLevel(body.data);
- firstLevel.forEach((first) => {
- getChildren(body.data, first);
- });
- const root: any = {
- menuId: 0,
- menuName: "根目录",
- children: [],
- };
- firstLevel.forEach((first: any) => {
- root.children.push(first as never);
- });
- return [root];
- }
- return [];
- };
- //点击删除按钮
- const onClickDeleteRow = (record: any) => {
- setDeleteRecord(record);
- setDeleteModalVisible(true);
- };
- //确定删除选中的菜单
- const executeDeleteRow = async () => {
- if (!deleteRecord) return;
-
- const menuId = deleteRecord.menuId;
- const body = await fetchApi(`${deleteAPI}/${menuId}`, push, {
- method: "DELETE",
- });
- if (body !== undefined) {
- if (body.code == 200) {
- globalMessage.success("删除成功");
- //刷新列表
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- } else {
- globalMessage.error(body.msg);
- }
- }
- setDeleteModalVisible(false);
- setDeleteRecord(null);
- };
- const [isCatalog, setIsCatalog] = useState(true);
- const [isMenu, setIsMenu] = useState(false);
- const [isButton, setIsButton] = useState(false);
- const onChangeType = (e: any) => {
- const type = e.target.value;
- setIsCatalog(type === "M");
- setIsMenu(type === "C");
- setIsButton(type === "F");
- };
- const IconData = () => {
- const iconData = { ...IconMap };
- Object.keys(iconData).forEach((key) => {
- iconData[key] = (
- <>
- <span style={{ marginRight: 8 }}>{iconData[key]}</span>
- {key}
- </>
- );
- });
- return iconData;
- };
- return (
- <PageContainer title={false}>
- <ProTable
- formRef={searchTableFormRef}
- rowKey="menuId"
- columns={columns}
- expandable={{
- expandedRowKeys: expandKeys,
- onExpand: handleExpand,
- }}
- request={async (params: any, sorter: any, filter: any) => {
- // 表单搜索项会从 params 传入,传递给后端接口。
- const data = await queryTableData(params, sorter, filter);
- if (data !== undefined) {
- return Promise.resolve({
- data: data,
- success: true,
- total: data.length,
- });
- }
- return Promise.resolve({
- data: [],
- success: true,
- });
- }}
- pagination={false}
- search={
- showSearch
- ? {
- defaultCollapsed: false,
- searchText: "搜索",
- }
- : false
- }
- dateFormatter="string"
- actionRef={actionTableRef}
- toolbar={{
- actions: [
- <ModalForm
- key="addmodal"
- title="添加菜单"
- open={showAddModal}
- trigger={
- <Button icon={<PlusOutlined />} type="primary">
- 新建
- </Button>
- }
- autoFocusFirstInput
- modalProps={{
- destroyOnHidden: true,
- onCancel: () => {
- cancelAddModal();
- },
- }}
- submitTimeout={2000}
- onFinish={executeAddData}
- >
- <ProForm.Group>
- <ProFormTreeSelect
- width="md"
- name="parentId"
- initialValue={rowParentId}
- label="上级菜单"
- placeholder="请选择上级菜单"
- rules={[{ required: true, message: "请选择上级菜单" }]}
- request={getMenuList}
- fieldProps={{
- filterTreeNode: true,
- showSearch: true,
- treeNodeFilterProp: "label",
- fieldNames: {
- label: "menuName",
- value: "menuId",
- },
- }}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormRadio.Group
- name="menuType"
- width="md"
- label="类型"
- initialValue="M"
- fieldProps={{
- onChange: (e: any) => onChangeType(e),
- }}
- options={[
- {
- label: "目录",
- value: "M",
- },
- {
- label: "菜单",
- value: "C",
- },
- {
- label: "按钮",
- value: "F",
- },
- ]}
- />
- </ProForm.Group>
- {(isCatalog || isMenu) && (
- <ProForm.Group>
- <ProFormSelect
- width="md"
- name="icon"
- label="菜单图标"
- fieldProps={{
- showSearch,
- }}
- valueEnum={IconData}
- placeholder="请选择菜单图标"
- rules={[{ required: true, message: "请选择菜单图标" }]}
- />
- </ProForm.Group>
- )}
- <ProForm.Group>
- <ProFormText
- width="md"
- name="menuName"
- label="菜单名称"
- placeholder="请输入菜单名称"
- rules={[{ required: true, message: "请输入菜单名称" }]}
- />
- <ProFormDigit
- fieldProps={{ precision: 0 }}
- width="md"
- name="orderNum"
- initialValue="1"
- label="排序"
- placeholder="请输入排序"
- rules={[{ required: true, message: "请输入排序" }]}
- />
- </ProForm.Group>
- {(isCatalog || isMenu) && (
- <ProForm.Group>
- <ProFormText
- width="md"
- name="path"
- label="路由地址"
- placeholder="请输入路由地址"
- rules={[{ required: true, message: "请输入路由地址" }]}
- />
- <ProFormRadio.Group
- name="isFrame"
- width="md"
- label="是否外链"
- initialValue="1"
- options={[
- {
- label: "是",
- value: "0",
- },
- {
- label: "否",
- value: "1",
- },
- ]}
- />
- </ProForm.Group>
- )}
- {isMenu && (
- <ProForm.Group>
- <ProFormText
- width="md"
- name="perms"
- label="权限字符"
- placeholder="请输入权限字符"
- />
- </ProForm.Group>
- )}
- <ProForm.Group>
- <ProFormRadio.Group
- name="visible"
- width="md"
- label="显示状态"
- initialValue="0"
- options={[
- {
- label: "显示",
- value: "0",
- },
- {
- label: "隐藏",
- value: "1",
- },
- ]}
- />
- <ProFormRadio.Group
- name="status"
- width="md"
- label="菜单状态"
- initialValue="0"
- options={[
- {
- label: "正常",
- value: "0",
- },
- {
- label: "停用",
- value: "1",
- },
- ]}
- />
- </ProForm.Group>
- </ModalForm>,
- <Button
- key="expand"
- icon={<FontAwesomeIcon icon={faArrowsUpDown} />}
- onClick={() => onClickExpandRow()}
- >
- 折叠/展开
- </Button>,
- ],
- settings: [
- {
- key: "switch",
- icon: showSearch ? (
- <FontAwesomeIcon icon={faToggleOn} />
- ) : (
- <FontAwesomeIcon icon={faToggleOff} />
- ),
- tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
- onClick: (key: string | undefined) => {
- setShowSearch(!showSearch);
- },
- },
- {
- key: "refresh",
- tooltip: "刷新",
- icon: <ReloadOutlined />,
- onClick: (key: string | undefined) => {
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- },
- },
- ],
- }}
- />
- <ModalForm
- key="modifymodal"
- title="修改菜单"
- formRef={modifyFormRef}
- open={isShowModifyDataModal}
- autoFocusFirstInput
- modalProps={{
- destroyOnHidden: true,
- onCancel: () => {
- setIsShowModifyDataModal(false);
- },
- }}
- submitTimeout={2000}
- onFinish={executeModifyData}
- >
- <ProForm.Group>
- <ProFormTreeSelect
- width="md"
- name="parentId"
- initialValue={rowParentId}
- label="上级菜单"
- placeholder="请选择上级菜单"
- rules={[{ required: true, message: "请选择上级菜单" }]}
- request={getMenuList}
- fieldProps={{
- filterTreeNode: true,
- showSearch: true,
- treeNodeFilterProp: "label",
- fieldNames: {
- label: "menuName",
- value: "menuId",
- },
- }}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormRadio.Group
- name="menuType"
- width="md"
- label="类型"
- fieldProps={{
- onChange: (e: any) => onChangeType(e),
- }}
- options={[
- {
- label: "目录",
- value: "M",
- },
- {
- label: "菜单",
- value: "C",
- },
- {
- label: "按钮",
- value: "F",
- },
- ]}
- />
- </ProForm.Group>
- {(isCatalog || isMenu) && (
- <ProForm.Group>
- <ProFormSelect
- width="md"
- name="icon"
- label="菜单图标"
- fieldProps={{
- showSearch,
- }}
- valueEnum={IconData}
- placeholder="请选择菜单图标"
- rules={[{ required: true, message: "请选择菜单图标" }]}
- />
- </ProForm.Group>
- )}
- <ProForm.Group>
- <ProFormText
- width="md"
- name="menuName"
- label="菜单名称"
- placeholder="请输入菜单名称"
- rules={[{ required: true, message: "请输入菜单名称" }]}
- />
- <ProFormDigit
- fieldProps={{ precision: 0 }}
- width="md"
- name="orderNum"
- initialValue="1"
- label="排序"
- placeholder="请输入排序"
- rules={[{ required: true, message: "请输入排序" }]}
- />
- </ProForm.Group>
- {(isCatalog || isMenu) && (
- <ProForm.Group>
- <ProFormText
- width="md"
- name="path"
- label="路由地址"
- placeholder="请输入路由地址"
- rules={[{ required: true, message: "请输入路由地址" }]}
- />
- <ProFormRadio.Group
- name="isFrame"
- width="md"
- label="是否外链"
- initialValue="1"
- options={[
- {
- label: "是",
- value: "0",
- },
- {
- label: "否",
- value: "1",
- },
- ]}
- />
- </ProForm.Group>
- )}
- {isMenu && (
- <ProForm.Group>
- <ProFormText
- width="md"
- name="perms"
- label="权限字符"
- placeholder="请输入权限字符"
- />
- </ProForm.Group>
- )}
- <ProForm.Group>
- <ProFormRadio.Group
- name="visible"
- width="md"
- label="显示状态"
- initialValue="0"
- options={[
- {
- label: "显示",
- value: "0",
- },
- {
- label: "隐藏",
- value: "1",
- },
- ]}
- />
- <ProFormRadio.Group
- name="status"
- width="md"
- label="菜单状态"
- initialValue="0"
- options={[
- {
- label: "正常",
- value: "0",
- },
- {
- label: "停用",
- value: "1",
- },
- ]}
- />
- </ProForm.Group>
- </ModalForm>
- {/* 删除确认模态框 */}
- <Modal
- title={
- <div style={{ display: 'flex', alignItems: 'center' }}>
- <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
- <span>系统提示</span>
- </div>
- }
- open={deleteModalVisible}
- onOk={executeDeleteRow}
- onCancel={() => {
- setDeleteModalVisible(false);
- setDeleteRecord(null);
- }}
- okText="确认"
- cancelText="取消"
- >
- <p>{`确定删除菜单名称为“${deleteRecord?.menuName}”的数据项?`}</p>
- </Modal>
- </PageContainer>
- );
- }
|