import { useState, useEffect } from 'react'; import { Table, Button, Modal, Form, Input, Tabs, Card, Popconfirm, message } from 'antd'; import { getSwiperListAPI, addSwiperDataAPI, editSwiperDataAPI, delSwiperDataAPI } from '@/api/Swiper'; import { Swiper } from '@/types/app/swiper'; import Title from '@/components/Title'; import { ColumnsType } from 'antd/es/table'; const SwiperPage = () => { const [loading, setLoading] = useState(false); const [swiper, setSwiper] = useState({} as Swiper); const [list, setList] = useState([]); const [isModelOpen, setIsModelOpen] = useState(false); const [viewImage, setViewImage] = useState(''); const [tab, setTab] = useState('list'); const columns: ColumnsType = [ { title: 'ID', dataIndex: 'id', key: 'id' }, { title: '图片', dataIndex: 'image', key: 'image', width: 200, render: (text: string) => swiper { setViewImage(text); setIsModelOpen(true) }} /> }, { title: '标题', dataIndex: 'title', key: 'title' }, { title: '描述', dataIndex: 'description', key: 'description' }, { title: '操作', key: 'action', align: 'center', render: (text: string, record: Swiper) => ( <> delSwiperData(record.id!)}> ) } ]; const getSwiperList = async () => { const { data } = await getSwiperListAPI(); setList(data as Swiper[]); setLoading(false); }; useEffect(() => { setLoading(true); getSwiperList(); }, []); const [form] = Form.useForm(); const editSwiperData = (record: Swiper) => { setSwiper(record); form.setFieldsValue(record); setTab('operate'); }; const delSwiperData = async (id: number) => { setLoading(true); await delSwiperDataAPI(id); message.success('🎉 删除轮播图成功'); getSwiperList(); }; const onSubmit = async () => { setLoading(true); form.validateFields().then(async (values: Swiper) => { if (swiper.id) { await editSwiperDataAPI({ ...swiper, ...values }); message.success('🎉 编辑轮播图成功'); } else { await addSwiperDataAPI({ ...swiper, ...values }); message.success('🎉 新增轮播图成功'); } getSwiperList(); setTab('list'); form.resetFields(); setSwiper({} as Swiper); }) }; const handleTabChange = (key: string) => { setTab(key); form.resetFields(); setSwiper({} as Swiper); }; const tabItems = [ { label: '轮播图列表', key: 'list', children: ( ) }, { label: swiper.id ? '编辑轮播图' : '新增轮播图', key: 'operate', children: ( <>

{swiper.id ? '编辑轮播图' : '新增轮播图'}

) } ]; return ( <> <Card className="[&>.ant-card-body]:!pt-0 mt-2"> <Tabs activeKey={tab} onChange={handleTabChange} items={tabItems} /> </Card> <Modal open={isModelOpen} title="查看图片" footer={null} onCancel={() => setIsModelOpen(false)} > <img src={viewImage} alt="swiper" className="w-full rounded mt-4" /> </Modal> </> ); }; export default SwiperPage;