2024-08-04 15:58:33 +08:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2024-08-06 21:02:06 +08:00
|
|
|
import { Cate } from '@/types/cate';
|
2024-08-04 15:58:33 +08:00
|
|
|
import { addCateDataAPI, delCateDataAPI, editCateDataAPI, getCateDataAPI, getCateListAPI } from '@/api/Cate';
|
2024-08-06 21:02:06 +08:00
|
|
|
import { DownOutlined } from '@ant-design/icons';
|
2024-08-07 23:30:01 +08:00
|
|
|
import { Form, Input, Button, Tree, Modal, Spin, Dropdown, Card, MenuProps, Popconfirm, message } from 'antd';
|
2024-08-07 14:35:10 +08:00
|
|
|
import Title from '@/components/Title';
|
2024-08-08 15:27:57 +08:00
|
|
|
import "./index.scss"
|
2024-08-04 15:58:33 +08:00
|
|
|
|
2024-08-08 12:47:44 +08:00
|
|
|
const CatePage: React.FC = () => {
|
2024-08-04 15:58:33 +08:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [model, setModel] = useState(false);
|
2024-08-08 18:47:49 +08:00
|
|
|
const [cate, setCate] = useState<Cate>({} as Cate);
|
2024-08-04 15:58:33 +08:00
|
|
|
const [list, setList] = useState<Cate[]>([]);
|
2024-08-08 18:47:49 +08:00
|
|
|
const [isMethod, setIsMethod] = useState<'create' | 'edit'>('create');
|
2024-08-04 15:58:33 +08:00
|
|
|
|
|
|
|
|
const getCateList = async () => {
|
|
|
|
|
const { data } = await getCateListAPI();
|
|
|
|
|
setList(data as Cate[]);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const addCateData = (id: number) => {
|
2024-08-08 18:47:49 +08:00
|
|
|
setIsMethod("create")
|
2024-08-04 15:58:33 +08:00
|
|
|
setModel(true);
|
|
|
|
|
setCate({ ...cate, level: id });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const editCateData = async (id: number) => {
|
2024-08-08 18:47:49 +08:00
|
|
|
setIsMethod("edit")
|
2024-08-04 15:58:33 +08:00
|
|
|
setLoading(true);
|
|
|
|
|
setModel(true);
|
|
|
|
|
const { data } = await getCateDataAPI(id);
|
|
|
|
|
setCate(data);
|
2024-08-08 18:47:49 +08:00
|
|
|
form.setFieldsValue(data);
|
2024-08-04 15:58:33 +08:00
|
|
|
setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-08 13:46:28 +08:00
|
|
|
const delCateData = async (id: number) => {
|
2024-08-08 18:47:49 +08:00
|
|
|
setLoading(true);
|
2024-08-08 13:46:28 +08:00
|
|
|
await delCateDataAPI(id);
|
|
|
|
|
message.success('🎉 删除分类成功');
|
|
|
|
|
getCateList();
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-08 18:47:49 +08:00
|
|
|
const [form] = Form.useForm();
|
|
|
|
|
|
2024-08-04 15:58:33 +08:00
|
|
|
const submit = async () => {
|
2024-08-08 18:47:49 +08:00
|
|
|
form.validateFields().then(async (values: Cate) => {
|
|
|
|
|
if (isMethod === "edit") {
|
|
|
|
|
await editCateDataAPI({ ...cate, ...values });
|
|
|
|
|
message.success('🎉 修改分类成功');
|
|
|
|
|
} else {
|
|
|
|
|
await addCateDataAPI({ ...cate, ...values });
|
|
|
|
|
message.success('🎉 新增分类成功');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化表单状态
|
|
|
|
|
form.resetFields();
|
|
|
|
|
setCate({} as Cate);
|
|
|
|
|
|
|
|
|
|
setModel(false);
|
|
|
|
|
getCateList();
|
|
|
|
|
setIsMethod("create")
|
|
|
|
|
})
|
2024-08-04 15:58:33 +08:00
|
|
|
};
|
|
|
|
|
|
2024-08-06 21:02:06 +08:00
|
|
|
const closeModel = () => {
|
2024-08-08 18:47:49 +08:00
|
|
|
setIsMethod("create")
|
2024-08-04 15:58:33 +08:00
|
|
|
setModel(false);
|
2024-08-08 18:47:49 +08:00
|
|
|
form.resetFields();
|
|
|
|
|
setCate({} as Cate);
|
2024-08-04 15:58:33 +08:00
|
|
|
};
|
|
|
|
|
|
2024-08-05 02:42:35 +08:00
|
|
|
// 将数据转换为树形结构
|
|
|
|
|
const treeData = (data: Cate[]): any[] => (
|
|
|
|
|
data.map(item => {
|
|
|
|
|
const items: MenuProps['items'] = [
|
|
|
|
|
{
|
|
|
|
|
key: '1',
|
|
|
|
|
label: <span onClick={() => addCateData(item.id!)}>新增</span>,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: '2',
|
|
|
|
|
label: <span onClick={() => editCateData(item.id!)}>编辑</span>,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: '3',
|
2024-08-06 21:02:06 +08:00
|
|
|
label: (
|
2024-08-06 23:04:42 +08:00
|
|
|
<Popconfirm title="警告" description="你确定要删除吗" okText="确定" cancelText="取消" onConfirm={() => delCateData(item.id!)}>
|
2024-08-06 21:02:06 +08:00
|
|
|
<span>删除</span>
|
|
|
|
|
</Popconfirm>
|
|
|
|
|
),
|
2024-08-05 02:42:35 +08:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return ({
|
|
|
|
|
title: (
|
|
|
|
|
<div className='group w-full flex justify-between items-center'>
|
|
|
|
|
<h3>{item.name}</h3>
|
|
|
|
|
|
|
|
|
|
<div className='controls hidden'>
|
|
|
|
|
<Dropdown menu={{ items }} arrow>
|
|
|
|
|
<Button type='link' size='small'>操作 <DownOutlined /></Button>
|
|
|
|
|
</Dropdown>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
key: item.id,
|
|
|
|
|
children: item.children ? treeData(item.children) : [],
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-08-08 18:47:49 +08:00
|
|
|
setLoading(true);
|
2024-08-05 02:42:35 +08:00
|
|
|
getCateList();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2024-08-04 15:58:33 +08:00
|
|
|
return (
|
2024-08-07 14:35:10 +08:00
|
|
|
<>
|
|
|
|
|
<Title value="分类管理" />
|
|
|
|
|
|
2024-08-08 15:27:57 +08:00
|
|
|
<Card className={`CatePage [&>.ant-card-body]:!p-2 [&>.ant-card-body]:!pb-6 mt-2`}>
|
2024-08-07 14:35:10 +08:00
|
|
|
<div className='my-2 text-center'>
|
|
|
|
|
<Button type="primary" onClick={() => setModel(true)}>新增一级分类</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Spin spinning={loading}>
|
|
|
|
|
<Tree defaultExpandAll={true} treeData={treeData(list)} />
|
|
|
|
|
</Spin>
|
|
|
|
|
|
2024-08-08 18:47:49 +08:00
|
|
|
<Modal title={isMethod === "edit" ? "编辑分类" : "新增分类"} open={model} onCancel={closeModel} footer={null}>
|
|
|
|
|
<Form form={form} layout="vertical" initialValues={cate} size='large' className='mt-6'>
|
|
|
|
|
<Form.Item label="名称" name="name" rules={[{ required: true, message: '分类名称不能为空' }]}>
|
2024-08-07 14:35:10 +08:00
|
|
|
<Input placeholder="大前端" />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
2024-08-08 18:47:49 +08:00
|
|
|
<Form.Item label="标识" name="mark" rules={[{ required: true, message: '分类标识不能为空' }]}>
|
2024-08-07 14:35:10 +08:00
|
|
|
<Input placeholder="dqd" />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item label="图标" name="icon">
|
|
|
|
|
<Input placeholder="🎉" />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item label="链接(选填)" name="url">
|
2024-08-07 23:30:01 +08:00
|
|
|
<Input placeholder="https://blog.liuyuyang.net/" />
|
2024-08-07 14:35:10 +08:00
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item className='!mb-0 flex justify-end'>
|
|
|
|
|
<Button onClick={closeModel}>取消</Button>
|
|
|
|
|
<Button type="primary" onClick={submit} className='ml-2'>确定</Button>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
</Form>
|
|
|
|
|
</Modal>
|
|
|
|
|
</Card>
|
|
|
|
|
</>
|
2024-08-04 15:58:33 +08:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-08 12:47:44 +08:00
|
|
|
export default CatePage;
|