Files
gofly-platform-services/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.tsx
T

212 lines
5.7 KiB
TypeScript
Raw Normal View History

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';
import { ordersApi } from '@/services/orders';
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 },
];
const HomeCarouselPage: React.FC = () => {
const actionRef = useRef<ActionType>();
const [createOpen, setCreateOpen] = useState(false);
const [editOpen, setEditOpen] = useState(false);
const [current, setCurrent] = useState<HomeCarouselItem | undefined>(undefined);
const [searchParams, setSearchParams] = useState<Record<string, any>>({});
// 处理删除
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>[] = [
{
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) => (
<Space>
<Link
onClick={() => {
setCurrent(record);
setEditOpen(true);
}}
>
<EditOutlined /> 编辑
</Link>
<Popconfirm
title="确认删除?"
description="删除后将无法恢复,请谨慎操作"
onConfirm={() => handleDelete(record)}
>
<Link><DeleteOutlined /> 删除</Link>
</Popconfirm>
</Space>
),
},
];
return (
<PageContainer>
<QueryFilter
defaultCollapsed={false}
2025-09-29 19:29:41 +08:00
split
span={8}
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
/>
<CreateForm
open={createOpen}
onOpenChange={setCreateOpen}
onFinish={async (values) => {
const hide = message.loading('保存中...');
try {
await ordersApi.saveOrUpdateHomeCarousel(values);
message.success('新增成功');
actionRef.current?.reload();
return true;
} catch (e) {
message.error('保存失败');
return false;
} finally {
hide();
}
}}
/>
<HomeCarouselForm
open={editOpen}
initialValues={current}
onOpenChange={setEditOpen}
onFinish={async (values) => {
const hide = message.loading('保存中...');
try {
await ordersApi.saveOrUpdateHomeCarousel({ ...(current as any), ...values });
message.success('修改成功');
actionRef.current?.reload();
return true;
} catch (e) {
message.error('保存失败');
return false;
} finally {
hide();
}
}}
/>
</PageContainer>
);
};
export default HomeCarouselPage;