2025-10-30 18:31:39 +08:00
|
|
|
import React, { useRef, useState } from 'react';
|
|
|
|
|
import { PageContainer, ProTable, QueryFilter, ProFormText, ProFormSelect } from '@ant-design/pro-components';
|
|
|
|
|
import { Popconfirm, Space, Button, message, Typography, Image as AntImage } from 'antd';
|
|
|
|
|
import { PlusOutlined, DeleteOutlined, EditOutlined, UploadOutlined } from '@ant-design/icons';
|
|
|
|
|
import type { ActionType, ProColumns } from '@ant-design/pro-components';
|
2025-09-23 10:32:35 +08:00
|
|
|
import { ordersApi } from '@/services/orders';
|
2025-10-30 18:31:39 +08:00
|
|
|
import HomeCarouselForm from './components/HomeCarouselForm';
|
|
|
|
|
import CreateForm from './components/CreateForm';
|
|
|
|
|
import type { HomeCarouselItem } from './components/HomeCarouselForm';
|
|
|
|
|
import styles from './index.less';
|
|
|
|
|
|
|
|
|
|
const { Link } = Typography;
|
|
|
|
|
|
|
|
|
|
// 常量定义
|
|
|
|
|
const STATUS_OPTIONS = [
|
|
|
|
|
{ label: '启用', value: 1 },
|
|
|
|
|
{ label: '禁用', value: 0 },
|
|
|
|
|
];
|
2025-09-23 10:32:35 +08:00
|
|
|
|
|
|
|
|
const HomeCarouselPage: React.FC = () => {
|
2025-10-30 18:31:39 +08:00
|
|
|
const actionRef = useRef<ActionType>();
|
2025-09-23 10:32:35 +08:00
|
|
|
const [createOpen, setCreateOpen] = useState(false);
|
|
|
|
|
const [editOpen, setEditOpen] = useState(false);
|
|
|
|
|
const [current, setCurrent] = useState<HomeCarouselItem | undefined>(undefined);
|
2025-10-30 18:31:39 +08:00
|
|
|
const [searchParams, setSearchParams] = useState<Record<string, any>>({});
|
2025-09-23 10:32:35 +08:00
|
|
|
|
2025-10-30 18:31:39 +08:00
|
|
|
// 处理删除
|
|
|
|
|
const handleDelete = async (record: HomeCarouselItem) => {
|
|
|
|
|
const hide = message.loading('删除中...');
|
|
|
|
|
try {
|
|
|
|
|
await ordersApi.deleteHomeCarousel(record.id!);
|
|
|
|
|
message.success('删除成功');
|
|
|
|
|
actionRef.current?.reload();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
message.error('删除失败');
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
hide();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const columns: ProColumns<HomeCarouselItem>[] = [
|
2025-09-23 10:32:35 +08:00
|
|
|
{
|
2025-10-30 18:31:39 +08:00
|
|
|
dataIndex: 'index',
|
|
|
|
|
valueType: 'indexBorder',
|
|
|
|
|
width: 48,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '图片',
|
|
|
|
|
dataIndex: 'imageUrl',
|
|
|
|
|
width: 120,
|
|
|
|
|
render: (imageUrl: string) => (
|
|
|
|
|
<AntImage
|
|
|
|
|
src={imageUrl}
|
|
|
|
|
width={100}
|
|
|
|
|
placeholder={
|
|
|
|
|
<div style={{ background: '#f5f5f5', height: 100, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
|
|
|
|
加载中...
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '跳转链接',
|
|
|
|
|
dataIndex: 'linkUrl',
|
|
|
|
|
ellipsis: true,
|
|
|
|
|
render: (url: string) => (
|
|
|
|
|
<Link href={url} target="_blank">{url}</Link>
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '排序',
|
|
|
|
|
dataIndex: 'sort',
|
|
|
|
|
width: 80,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '状态',
|
|
|
|
|
dataIndex: 'status',
|
|
|
|
|
width: 80,
|
|
|
|
|
valueEnum: {
|
|
|
|
|
1: { text: '启用', status: 'Success' },
|
|
|
|
|
0: { text: '禁用', status: 'Error' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '操作',
|
|
|
|
|
valueType: 'option',
|
|
|
|
|
width: 180,
|
|
|
|
|
render: (_, record) => (
|
2025-09-23 10:32:35 +08:00
|
|
|
<Space>
|
2025-10-30 18:31:39 +08:00
|
|
|
<Link
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setCurrent(record);
|
|
|
|
|
setEditOpen(true);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<EditOutlined /> 编辑
|
|
|
|
|
</Link>
|
|
|
|
|
<Popconfirm
|
|
|
|
|
title="确认删除?"
|
|
|
|
|
description="删除后将无法恢复,请谨慎操作"
|
|
|
|
|
onConfirm={() => handleDelete(record)}
|
|
|
|
|
>
|
|
|
|
|
<Link><DeleteOutlined /> 删除</Link>
|
2025-09-23 10:32:35 +08:00
|
|
|
</Popconfirm>
|
|
|
|
|
</Space>
|
2025-10-30 18:31:39 +08:00
|
|
|
),
|
2025-09-23 10:32:35 +08:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return (
|
2025-10-30 18:31:39 +08:00
|
|
|
<PageContainer>
|
|
|
|
|
<QueryFilter
|
|
|
|
|
defaultCollapsed={false}
|
2025-09-29 19:29:41 +08:00
|
|
|
split
|
|
|
|
|
span={8}
|
2025-10-30 18:31:39 +08:00
|
|
|
className={styles['query-filter']}
|
|
|
|
|
onFinish={async (values) => {
|
|
|
|
|
setSearchParams(values);
|
|
|
|
|
actionRef.current?.reload();
|
|
|
|
|
}}
|
|
|
|
|
style={{ marginBottom: 16 }}
|
|
|
|
|
>
|
|
|
|
|
<ProFormText
|
|
|
|
|
name="title"
|
|
|
|
|
label="标题"
|
|
|
|
|
placeholder="请输入标题关键字"
|
|
|
|
|
fieldProps={{
|
|
|
|
|
allowClear: true,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<ProFormSelect
|
|
|
|
|
name="status"
|
|
|
|
|
label="状态"
|
|
|
|
|
placeholder="请选择状态"
|
|
|
|
|
options={STATUS_OPTIONS}
|
|
|
|
|
fieldProps={{
|
|
|
|
|
allowClear: true,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</QueryFilter>
|
|
|
|
|
|
|
|
|
|
<ProTable<HomeCarouselItem>
|
|
|
|
|
rowKey="id"
|
|
|
|
|
headerTitle="轮播图列表"
|
|
|
|
|
actionRef={actionRef}
|
|
|
|
|
columns={columns}
|
|
|
|
|
request={(p) => ordersApi.homeCarousel({ ...(p as any), ...searchParams })}
|
|
|
|
|
search={false}
|
|
|
|
|
pagination={{
|
|
|
|
|
defaultPageSize: 10,
|
|
|
|
|
showSizeChanger: true,
|
|
|
|
|
showQuickJumper: true,
|
|
|
|
|
}}
|
|
|
|
|
toolBarRender={() => [
|
|
|
|
|
<Button
|
|
|
|
|
key="add"
|
|
|
|
|
type="primary"
|
|
|
|
|
onClick={() => setCreateOpen(true)}
|
|
|
|
|
>
|
|
|
|
|
<PlusOutlined /> 新增轮播图
|
|
|
|
|
</Button>,
|
|
|
|
|
]}
|
2025-09-29 19:29:41 +08:00
|
|
|
/>
|
2025-09-23 10:32:35 +08:00
|
|
|
|
2025-10-30 18:31:39 +08:00
|
|
|
<CreateForm
|
2025-09-23 10:32:35 +08:00
|
|
|
open={createOpen}
|
|
|
|
|
onOpenChange={setCreateOpen}
|
|
|
|
|
onFinish={async (values) => {
|
|
|
|
|
const hide = message.loading('保存中...');
|
|
|
|
|
try {
|
|
|
|
|
await ordersApi.saveOrUpdateHomeCarousel(values);
|
|
|
|
|
message.success('新增成功');
|
2025-10-30 18:31:39 +08:00
|
|
|
actionRef.current?.reload();
|
2025-09-23 10:32:35 +08:00
|
|
|
return true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
message.error('保存失败');
|
|
|
|
|
return false;
|
2025-10-30 18:31:39 +08:00
|
|
|
} finally {
|
|
|
|
|
hide();
|
|
|
|
|
}
|
2025-09-23 10:32:35 +08:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<HomeCarouselForm
|
|
|
|
|
open={editOpen}
|
|
|
|
|
initialValues={current}
|
|
|
|
|
onOpenChange={setEditOpen}
|
|
|
|
|
onFinish={async (values) => {
|
|
|
|
|
const hide = message.loading('保存中...');
|
|
|
|
|
try {
|
|
|
|
|
await ordersApi.saveOrUpdateHomeCarousel({ ...(current as any), ...values });
|
|
|
|
|
message.success('修改成功');
|
2025-10-30 18:31:39 +08:00
|
|
|
actionRef.current?.reload();
|
2025-09-23 10:32:35 +08:00
|
|
|
return true;
|
|
|
|
|
} catch (e) {
|
2025-10-30 18:31:39 +08:00
|
|
|
message.error('保存失败');
|
2025-09-23 10:32:35 +08:00
|
|
|
return false;
|
2025-10-30 18:31:39 +08:00
|
|
|
} finally {
|
|
|
|
|
hide();
|
|
|
|
|
}
|
2025-09-23 10:32:35 +08:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</PageContainer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default HomeCarouselPage;
|
|
|
|
|
|