import { useState, useEffect } from 'react'; import { Table, Button, Image, Form, Input, Tabs, Card, Popconfirm, message, Spin } from 'antd'; import { getSwiperListAPI, addSwiperDataAPI, editSwiperDataAPI, delSwiperDataAPI, getSwiperDataAPI } from '@/api/Swiper'; import { Swiper } from '@/types/app/swiper'; import Title from '@/components/Title'; import { ColumnsType } from 'antd/es/table'; import { CloudUploadOutlined, PictureOutlined } from '@ant-design/icons'; import FileUpload from '@/components/FileUpload'; const SwiperPage = () => { const [loading, setLoading] = useState(false); const [btnLoading, setBtnLoading] = useState(false) const [editLoading, setEditLoading] = useState(false) const [form] = Form.useForm(); const [swiper, setSwiper] = useState({} as Swiper); const [list, setList] = useState([]); const [isModalOpen, setIsModalOpen] = useState(false); const [tab, setTab] = useState('list'); const columns: ColumnsType = [ { title: 'ID', dataIndex: 'id', key: 'id', align: 'center' }, { title: '图片', dataIndex: 'image', key: 'image', width: 200, render: (url: string) => }, { title: '标题', dataIndex: 'title', key: 'title' }, { title: '描述', dataIndex: 'description', key: 'description', width: 500, }, { title: '操作', key: 'action', align: 'center', render: (text: string, record: Swiper) => ( <> delSwiperData(record.id!)}> ) } ]; const getSwiperList = async () => { try { const { data } = await getSwiperListAPI(); setList(data as Swiper[]); } catch (error) { setLoading(false); } setLoading(false); }; useEffect(() => { setLoading(true); getSwiperList(); }, []); const editSwiperData = async (record: Swiper) => { setEditLoading(true); setTab('operate'); try { const { data } = await getSwiperDataAPI(record.id) setSwiper(data); form.setFieldsValue(record); } catch (error) { setEditLoading(false); } setEditLoading(false); }; const delSwiperData = async (id: number) => { setBtnLoading(true); try { await delSwiperDataAPI(id); await getSwiperList(); message.success('🎉 删除轮播图成功'); } catch (error) { setBtnLoading(false); } setBtnLoading(false); }; const onSubmit = async () => { setBtnLoading(true) try { form.validateFields().then(async (values: Swiper) => { if (swiper.id) { await editSwiperDataAPI({ ...swiper, ...values }); message.success('🎉 编辑轮播图成功'); } else { await addSwiperDataAPI({ ...swiper, ...values }); message.success('🎉 新增轮播图成功'); } await getSwiperList(); setTab('list'); form.resetFields(); setSwiper({} as Swiper); }) } catch (error) { setBtnLoading(false) } }; const handleTabChange = (key: string) => { setTab(key); form.resetFields(); setSwiper({} as Swiper); }; // 文件上传 const UploadBtn = () => ( setIsModalOpen(true)} /> ) const tabItems = [ { label: '轮播图列表', key: 'list', children: ( ) }, { label: swiper.id ? '编辑轮播图' : '新增轮播图', key: 'operate', children: (

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

} addonAfter={} className='customizeAntdInputAddonAfter' />
) } ]; return ( <> <Card className="[&>.ant-card-body]:!pt-0 mt-2 min-h-[calc(100vh-180px)]"> <Tabs activeKey={tab} onChange={handleTabChange} items={tabItems} /> </Card> <FileUpload dir="swiper" open={isModalOpen} onSuccess={(url: string[]) => form.setFieldValue("image", url.join("\n"))} onCancel={() => setIsModalOpen(false)} /> </> ); }; export default SwiperPage;