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-05 02:42:35 +08:00
|
|
|
import "./index.scss"
|
2024-08-07 14:35:10 +08:00
|
|
|
import Title from '@/components/Title';
|
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);
|
|
|
|
|
const [cate, setCate] = useState<Cate>({ name: '', mark: '', url: '', icon: '', level: 0 });
|
|
|
|
|
const [list, setList] = useState<Cate[]>([]);
|
|
|
|
|
const formRef = useRef<any>(null);
|
|
|
|
|
|
|
|
|
|
const getCateList = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
const { data } = await getCateListAPI();
|
|
|
|
|
setList(data as Cate[]);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const addCateData = (id: number) => {
|
|
|
|
|
setModel(true);
|
|
|
|
|
setCate({ ...cate, level: id });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const editCateData = async (id: number) => {
|
|
|
|
|
setLoading(true);
|
2024-08-05 02:42:35 +08:00
|
|
|
|
2024-08-04 15:58:33 +08:00
|
|
|
setModel(true);
|
|
|
|
|
const { data } = await getCateDataAPI(id);
|
|
|
|
|
setCate(data);
|
2024-08-05 02:42:35 +08:00
|
|
|
|
2024-08-04 15:58:33 +08:00
|
|
|
setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
|
formRef.current
|
|
|
|
|
.validateFields()
|
|
|
|
|
.then(async (values: any) => {
|
|
|
|
|
if (cate.id) {
|
|
|
|
|
await editCateDataAPI({ ...cate, ...values });
|
2024-08-07 23:30:01 +08:00
|
|
|
message.success('🎉 修改分类成功');
|
2024-08-04 15:58:33 +08:00
|
|
|
} else {
|
|
|
|
|
await addCateDataAPI({ ...cate, ...values });
|
2024-08-07 23:30:01 +08:00
|
|
|
message.success('🎉 新增分类成功');
|
2024-08-04 15:58:33 +08:00
|
|
|
}
|
2024-08-05 02:42:35 +08:00
|
|
|
|
|
|
|
|
// 初始化表单状态
|
2024-08-04 15:58:33 +08:00
|
|
|
formRef.current.resetFields();
|
|
|
|
|
setCate({ name: '', mark: '', url: '', icon: '', level: 0 });
|
2024-08-05 02:42:35 +08:00
|
|
|
|
2024-08-04 15:58:33 +08:00
|
|
|
setModel(false);
|
|
|
|
|
getCateList();
|
|
|
|
|
})
|
|
|
|
|
.catch((errorInfo: any) => {
|
|
|
|
|
console.error('Validate Failed:', errorInfo);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-06 21:02:06 +08:00
|
|
|
const delCateData = async (id: number) => {
|
|
|
|
|
await delCateDataAPI(id);
|
2024-08-07 23:30:01 +08:00
|
|
|
message.success('🎉 删除分类成功');
|
2024-08-06 21:02:06 +08:00
|
|
|
getCateList();
|
2024-08-04 15:58:33 +08:00
|
|
|
};
|
|
|
|
|
|
2024-08-06 21:02:06 +08:00
|
|
|
const closeModel = () => {
|
2024-08-04 15:58:33 +08:00
|
|
|
setModel(false);
|
|
|
|
|
formRef.current.resetFields();
|
|
|
|
|
};
|
|
|
|
|
|
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(() => {
|
|
|
|
|
getCateList();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (model && formRef.current) {
|
|
|
|
|
formRef.current.setFieldsValue(cate);
|
|
|
|
|
}
|
|
|
|
|
}, [cate, model]);
|
2024-08-04 15:58:33 +08:00
|
|
|
|
|
|
|
|
return (
|
2024-08-07 14:35:10 +08:00
|
|
|
<>
|
|
|
|
|
<Title value="分类管理" />
|
|
|
|
|
|
|
|
|
|
<Card className={`[&>.ant-card-body]:!p-2 [&>.ant-card-body]:!pb-6 mt-2`}>
|
|
|
|
|
<div className='my-2 text-center'>
|
|
|
|
|
<Button type="primary" onClick={() => setModel(true)}>新增一级分类</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Spin spinning={loading}>
|
|
|
|
|
<Tree defaultExpandAll={true} treeData={treeData(list)} />
|
|
|
|
|
</Spin>
|
|
|
|
|
|
|
|
|
|
<Modal title="新增分类导航" open={model} onCancel={closeModel} footer={null}>
|
|
|
|
|
<Form ref={formRef} layout="vertical" initialValues={cate} size='large' className='mt-6'>
|
|
|
|
|
<Form.Item label="名称" name="name" rules={[{ required: true, message: '分类名称不能为空' }, { min: 1, max: 10, message: '分类名称限制为 1 ~ 10 个字符' }]}>
|
|
|
|
|
<Input placeholder="大前端" />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
<Form.Item label="标识" name="mark" rules={[{ required: true, message: '分类标识不能为空' }, { min: 1, max: 10, message: '分类标识限制为 1 ~ 10 个字符' }]}>
|
|
|
|
|
<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;
|