153 lines
5.1 KiB
TypeScript
153 lines
5.1 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
||
import { PageContainer, ProFormText, ProTable } from '@ant-design/pro-components';
|
||
import { Popconfirm, Space, Tag, message } from 'antd';
|
||
import { ActionBar, GlassCard, GradientButton, SearchBarPlus } from '@/components/ui';
|
||
import { PlusOutlined } from '@ant-design/icons';
|
||
import { ordersApi } from '@/services/orders';
|
||
import TourismForm, { TourismItem } from './components/TourismForm';
|
||
import { tenantApi } from '@/services/tenant';
|
||
|
||
const TourismPage: React.FC = () => {
|
||
const [createOpen, setCreateOpen] = useState(false);
|
||
const [editOpen, setEditOpen] = useState(false);
|
||
const [current, setCurrent] = useState<TourismItem | undefined>(undefined);
|
||
const [params, setParams] = useState<Record<string, any>>({});
|
||
|
||
// 首次进入时,默认按“当前租户 + 包含下级租户”查询,避免全量数据
|
||
useEffect(() => {
|
||
(async () => {
|
||
try {
|
||
let tenantId: string | undefined;
|
||
const cur: any = await tenantApi.getCurrentTenant();
|
||
tenantId = cur?.data?.tenantId ?? cur?.tenantId;
|
||
if (!tenantId) {
|
||
const stats: any = await tenantApi.getScopeStats();
|
||
tenantId = stats?.data?.currentTenantId ?? stats?.currentTenantId;
|
||
}
|
||
if (tenantId) setParams((prev) => ({ ...(prev || {}), tenantId, includeChildren: true }));
|
||
} catch {
|
||
// 忽略错误,保留手动查询能力
|
||
}
|
||
})();
|
||
}, []);
|
||
|
||
const columns: any[] = [
|
||
{ dataIndex: 'index', valueType: 'indexBorder', width: 48 },
|
||
{ title: '旅游项目', dataIndex: 'tourismName' },
|
||
{ title: '地址', dataIndex: 'tourismAddress' },
|
||
{ title: '经度', dataIndex: 'longitude' },
|
||
{ title: '纬度', dataIndex: 'latitude' },
|
||
{
|
||
title: '首页展示',
|
||
dataIndex: 'showInHomepage',
|
||
render: (v: number) => <Tag color={v === 1 ? 'green' : 'default'}>{v === 1 ? '是' : '否'}</Tag>,
|
||
},
|
||
{ title: '联系电话', dataIndex: 'tourismPhone' },
|
||
{ title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime' },
|
||
{
|
||
title: '状态',
|
||
dataIndex: 'status',
|
||
render: (v: number) => <Tag color={v === 1 ? 'green' : 'default'}>{v === 1 ? '启用' : '禁用'}</Tag>,
|
||
},
|
||
{
|
||
title: '操作',
|
||
key: 'action',
|
||
render: (_: any, record: TourismItem) => (
|
||
<Space>
|
||
<a onClick={() => { setCurrent(record); setEditOpen(true); }}>编辑</a>
|
||
<Popconfirm
|
||
title={`确认删除项目[${record.tourismName}]?`}
|
||
onConfirm={async () => {
|
||
const hide = message.loading('正在删除...');
|
||
try {
|
||
await ordersApi.deleteTourism(record.id!);
|
||
message.success('删除成功');
|
||
} catch {
|
||
message.error('删除失败');
|
||
} finally { hide(); }
|
||
}}
|
||
>
|
||
<a style={{ color: '#1677ff' }}>删除</a>
|
||
</Popconfirm>
|
||
</Space>
|
||
),
|
||
},
|
||
];
|
||
|
||
return (
|
||
<PageContainer header={{ title: '旅游项目' }}>
|
||
<SearchBarPlus
|
||
defaultCollapsed
|
||
split
|
||
span={8}
|
||
onTenantChanged={(tenantId, includeChildren) => {
|
||
setParams((prev) => ({ ...(prev || {}), tenantId, includeChildren }));
|
||
}}
|
||
onFinish={async (values) => setParams(values)}
|
||
>
|
||
<ProFormText name="tourismName" label="景区名称" placeholder="请输入景区名称" />
|
||
</SearchBarPlus>
|
||
|
||
<ActionBar
|
||
left={
|
||
<GradientButton size="middle" type="primary" icon={<PlusOutlined />} onClick={() => setCreateOpen(true)}>
|
||
新增
|
||
</GradientButton>
|
||
}
|
||
/>
|
||
|
||
<GlassCard>
|
||
{params?.tenantId ? (
|
||
<ProTable<TourismItem>
|
||
rowKey="id"
|
||
params={params}
|
||
request={(p) => ordersApi.tourism({ ...(p as any), ...params })}
|
||
columns={columns}
|
||
search={false}
|
||
pagination={{ showSizeChanger: true, showQuickJumper: true }}
|
||
toolBarRender={false}
|
||
/>
|
||
) : null}
|
||
</GlassCard>
|
||
|
||
<TourismForm
|
||
title="新增项目"
|
||
open={createOpen}
|
||
onOpenChange={setCreateOpen}
|
||
onFinish={async (values) => {
|
||
const hide = message.loading('保存中...');
|
||
try {
|
||
await ordersApi.saveOrUpdateTourism(values);
|
||
message.success('新增成功');
|
||
return true;
|
||
} catch {
|
||
message.error('新增失败');
|
||
return false;
|
||
} finally { hide(); }
|
||
}}
|
||
/>
|
||
|
||
<TourismForm
|
||
title="编辑项目"
|
||
open={editOpen}
|
||
initialValues={current}
|
||
onOpenChange={setEditOpen}
|
||
onFinish={async (values) => {
|
||
const hide = message.loading('保存中...');
|
||
try {
|
||
await ordersApi.saveOrUpdateTourism({ ...(current as any), ...values });
|
||
message.success('修改成功');
|
||
return true;
|
||
} catch {
|
||
message.error('修改失败');
|
||
return false;
|
||
} finally { hide(); }
|
||
}}
|
||
/>
|
||
</PageContainer>
|
||
);
|
||
};
|
||
|
||
export default TourismPage;
|
||
|