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

236 lines
6.3 KiB
TypeScript
Raw Normal View History

import React, { useRef, useState } from 'react';
2025-11-04 11:53:35 +08:00
import { PageContainer, ProTable, ProFormText, ProFormSelect } from '@ant-design/pro-components';
import { Popconfirm, message, Space, Image as AntImage, Typography } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
2025-11-04 11:53:35 +08:00
import { ActionBar, GlassCard, GradientButton, SearchBarPlus } from '@/components/ui';
import { ordersApi } from '@/services/orders';
import CreateForm from './components/CreateForm';
2025-11-04 11:53:35 +08:00
import type { HomeCarouselItem } from './components/CreateForm';
const { Link } = Typography;
// 常量定义
const STATUS_OPTIONS = [
{ label: '启用', value: 1 },
{ label: '禁用', value: 0 },
];
const TYPE_OPTIONS = [
{ label: '首页轮播', value: 0 },
{ label: '活动', value: 1 },
{ label: '其他', value: 2 },
];
const HomeCarouselPage: React.FC = () => {
const actionRef = useRef<ActionType>();
2025-11-04 11:53:35 +08:00
const [formOpen, setFormOpen] = 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: 'title',
ellipsis: true,
copyable: true,
},
{
title: '类型',
dataIndex: 'type',
width: 100,
valueEnum: TYPE_OPTIONS.reduce((acc, curr) => ({
...acc,
[curr.value]: { text: curr.label },
}), {}),
},
2025-11-04 11:53:35 +08:00
{
title: '图片',
dataIndex: 'imageUrl',
width: 120,
render: (_: any, record: HomeCarouselItem) => (
record.imageUrl ? (
<AntImage
src={record.imageUrl}
width={100}
placeholder={
<div style={{ background: '#f5f5f5', height: 100, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
加载中...
</div>
}
/>
) : (
<span>-</span>
)
),
},
{
title: '跳转链接',
dataIndex: 'redirectUrl',
ellipsis: true,
render: (_: any, record: HomeCarouselItem) => (
record.linkUrl ? <Link href={record.linkUrl} target="_blank">{record.linkUrl}</Link> : '-'
),
},
{
title: '描述',
dataIndex: 'description',
ellipsis: true,
},
{
title: '排序',
2025-11-04 11:53:35 +08:00
dataIndex: 'sortOrder',
width: 80,
},
{
title: '状态',
dataIndex: 'status',
width: 80,
valueEnum: {
1: { text: '启用', status: 'Success' },
0: { text: '禁用', status: 'Error' },
},
},
{
title: '操作',
valueType: 'option',
width: 180,
fixed: 'right',
2025-11-04 11:53:35 +08:00
render: (_, record) => (
<Space>
<a
onClick={() => {
setCurrent(record);
setFormOpen(true);
}}
>
编辑
</a>
<Popconfirm
title="确认删除轮播图?"
onConfirm={() => handleDelete(record)}
okText="确认"
cancelText="取消"
>
<a style={{ color: '#1677ff' }}>删除</a>
</Popconfirm>
</Space>
),
},
];
return (
2025-11-04 11:53:35 +08:00
<PageContainer header={{ title: '轮播图管理' }}>
{/*<SearchBarPlus*/}
{/* defaultCollapsed*/}
{/* split*/}
{/* span={8}*/}
{/* hideReset*/}
{/* showTenant={false}*/}
{/* onFinish={async (values) => {*/}
{/* setSearchParams(values);*/}
{/* actionRef.current?.reload();*/}
{/* }}*/}
{/*>*/}
{/* <ProFormText*/}
{/* name="title"*/}
{/* label="标题"*/}
{/* placeholder="请输入标题关键字"*/}
{/* />*/}
{/* <ProFormSelect*/}
{/* name="status"*/}
{/* label="状态"*/}
{/* placeholder="请选择状态"*/}
{/* options={STATUS_OPTIONS}*/}
{/* fieldProps={{*/}
{/* allowClear: true,*/}
{/* }}*/}
{/* />*/}
{/*</SearchBarPlus>*/}
2025-11-04 11:53:35 +08:00
<ActionBar
left={
<GradientButton size="middle" type="primary" icon={<PlusOutlined />} onClick={() => {
setCurrent(undefined);
setFormOpen(true);
}}>
新增轮播图
</GradientButton>
}
2025-09-29 19:29:41 +08:00
/>
2025-11-04 11:53:35 +08:00
<GlassCard>
<ProTable<HomeCarouselItem>
rowKey="id"
actionRef={actionRef}
columns={columns}
request={(p) => ordersApi.homeCarousel({ ...(p as any), ...searchParams })}
search={false}
pagination={{
defaultPageSize: 10,
showSizeChanger: true,
showQuickJumper: true,
}}
toolBarRender={false}
/>
</GlassCard>
<CreateForm
2025-11-04 11:53:35 +08:00
open={formOpen}
initialValues={current}
onOpenChange={(visible) => {
setFormOpen(visible);
if (!visible) {
setCurrent(undefined);
}
}}
onFinish={async (values) => {
const hide = message.loading('保存中...');
try {
2025-11-04 11:53:35 +08:00
if (current?.id) {
// 编辑模式
await ordersApi.saveOrUpdateHomeCarousel({ ...(current as any), ...values });
message.success('修改成功');
} else {
// 新增模式
await ordersApi.saveOrUpdateHomeCarousel(values);
message.success('新增成功');
}
actionRef.current?.reload();
return true;
} catch (e) {
message.error('保存失败');
return false;
} finally {
hide();
}
}}
/>
</PageContainer>
);
};
export default HomeCarouselPage;