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

230 lines
9.5 KiB
TypeScript
Raw Normal View History

2025-10-25 18:04:01 +08:00
import React, { useEffect, useRef, useState } from 'react';
import { ActionBar, GlassCard, SearchBarPlus } from '@/components/ui';
2025-10-25 18:04:01 +08:00
import { PageContainer, ProFormSelect } from '@ant-design/pro-components';
import { Button, Image, Popconfirm, Space, Tag, message, Pagination } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import { ordersApi } from '@/services/orders';
2025-10-25 18:04:01 +08:00
import TourismFileForm, { TourismFileItem } from './components/TourismFileForm';
2025-10-25 18:04:01 +08:00
// 资源类型枚举(用于卡片展示的颜色)
const TYPE_OPTIONS = [
{ label: '视频', value: 5, color: '#f5222d' },
{ label: '首页 Banner', value: 6, color: '#2f54eb' },
2025-10-25 18:04:01 +08:00
{ label: '展示图片', value: 4, color: '#52c41a' },
{ label: '景区详情', value: 3, color: '#722ed1' },
{ label: '景区小图展示', value: 2, color: '#13c2c2' },
{ label: '活动', value: 1, color: '#1890ff' },
] as const;
const STATUS_OPTIONS = [
{ label: '启用', value: 0 },
{ label: '禁用', value: 1 },
] as const;
const TourismFilePage: React.FC = () => {
const [query, setQuery] = useState<Record<string, any>>({});
const [tourismOptions, setTourismOptions] = useState<Array<{ label: string; value: number }>>([]);
2025-10-25 18:04:01 +08:00
const [createOpen, setCreateOpen] = useState(false);
const [editOpen, setEditOpen] = useState(false);
const [current, setCurrent] = useState<any>();
2025-10-25 18:04:01 +08:00
// 分组数据(按项目分组)与分页
const [groups, setGroups] = useState<Array<{ tourismId: number; tourismName?: string; files: any[] }>>([]);
const [loading, setLoading] = useState(false);
const fetchingRef = useRef(false);
const [pagination, setPagination] = useState<{ current: number; pageSize: number; total: number }>({ current: 1, pageSize: 10, total: 0 });
// 加载可选项目列表(用于筛选与表单下拉)
useEffect(() => {
(async () => {
try {
2025-10-25 18:04:01 +08:00
const res = await ordersApi.tourism({ page: 1, limit: 1000, tenantId: query?.tenantId, includeChildren: query?.includeChildren });
const list: Array<{ id: number; tourismName: string }> = (res as any)?.data || [];
setTourismOptions(list.map((it: any) => ({ label: it.tourismName, value: it.id })));
} catch {}
})();
2025-10-25 18:04:01 +08:00
// 租户切换时刷新
}, [query?.tenantId, query?.includeChildren]);
2025-10-25 18:04:01 +08:00
// 加载查询结果(含分页)
const loadGroups = async (extra?: Record<string, any>) => {
const params = { ...(query || {}), ...(extra || {}), current: pagination.current, pageSize: pagination.pageSize };
if (fetchingRef.current) return; // 防抖去重
fetchingRef.current = true;
setLoading(true);
try {
const res = await ordersApi.tourismFilesGroupedByTenant(params as any);
setGroups(Array.isArray((res as any)?.data) ? (res as any).data : []);
setPagination((p) => ({ ...p, total: Number((res as any)?.total || 0) }));
} catch (e) {
setGroups([]);
setPagination((p) => ({ ...p, total: 0 }));
} finally {
fetchingRef.current = false;
setLoading(false);
}
};
2025-10-25 18:04:01 +08:00
// 首次与查询/分页变化时刷新
useEffect(() => {
loadGroups();
}, [query, pagination.current, pagination.pageSize]);
return (
2025-10-25 18:04:01 +08:00
<PageContainer header={{ title: '景区素材管理' }}>
<SearchBarPlus
defaultCollapsed
split
span={8 as any}
hideReset
onFinish={async (values) => {
// 查询提交后重置到第1页
setPagination((p) => ({ ...p, current: 1 }));
setQuery(values);
}}
>
<ProFormSelect name="status" label="状态" placeholder="请选择" options={STATUS_OPTIONS as any} />
</SearchBarPlus>
<ActionBar left={<Button size="middle" type="primary" icon={<PlusOutlined />} onClick={() => setCreateOpen(true)}>新增</Button>} />
<GlassCard>
2025-10-25 18:04:01 +08:00
{loading ? (
<div style={{ padding: 24 }}>加载中...</div>
) : (
<div>
{groups && groups.length ? (
groups.map((g) => (
<div key={g.tourismId} style={{ marginBottom: 24 }}>
<h3 style={{ marginBottom: 12 }}>{g.tourismName || `项目#${g.tourismId}`}</h3>
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, minmax(320px, 1fr))', gap: 16 }}>
{(g.files || []).map((record: any) => (
<div key={record.id} style={{ border: '1px solid #f0f0f0', borderRadius: 8, padding: 12 }}>
<div style={{ marginBottom: 8 }}>
{Number(record.type) === 5 ? (
<video src={record.imageUrl} controls style={{ width: '100%', height: 180, objectFit: 'cover', borderRadius: 4 }} />
) : (
<Image src={record.imageUrl} width={'100%'} height={180} style={{ objectFit: 'cover', borderRadius: 4 }} />
)}
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
<Tag color={Number(record.status) === 0 ? 'green' : 'default'}>{Number(record.status) === 0 ? '启用' : '禁用'}</Tag>
<Tag color={(TYPE_OPTIONS.find((i) => i.value === Number(record.type)) || ({} as any)).color || 'default'}>
{(TYPE_OPTIONS.find((i) => i.value === Number(record.type)) || ({} as any)).label || '未知类型'}
</Tag>
</div>
<div style={{ fontSize: 12, color: '#666', marginBottom: 8 }}>{record.description || '-'}</div>
{record.linkUrl ? (
<div style={{ fontSize: 12, color: '#999' }}>
链接:
<a href={record.linkUrl} target="_blank" rel="noreferrer">
{record.linkUrl}
</a>
</div>
) : null}
<div style={{ marginTop: 8 }}>
<Space>
<a
style={{ color: '#1677ff' }}
onClick={() => {
setCurrent(record);
setEditOpen(true);
}}
>
编辑
</a>
<Popconfirm
title={`确认删除资源[${record.id}]?`}
onConfirm={async () => {
const hide = message.loading('正在删除...');
try {
await ordersApi.deleteTourismFile(record.id);
message.success('删除成功');
loadGroups();
} catch (e) {
message.error('删除失败');
} finally {
hide();
}
}}
>
<a style={{ color: '#ff4d4f' }}>删除</a>
</Popconfirm>
</Space>
</div>
</div>
))}
</div>
</div>
))
) : (
<div style={{ padding: 24, color: '#999' }}>暂无数据</div>
)}
</div>
)}
{/* 分页 */}
<div style={{ marginTop: 16, textAlign: 'right' }}>
<span style={{ marginRight: 12, color: '#999' }}>共 {pagination.total} 条</span>
<Pagination
current={pagination.current}
pageSize={pagination.pageSize}
total={pagination.total}
showSizeChanger
showQuickJumper
onChange={(cur, size) => {
setPagination({ current: cur, pageSize: (size as number) || pagination.pageSize, total: pagination.total });
}}
/>
</div>
</GlassCard>
2025-10-25 18:04:01 +08:00
<TourismFileForm
title="新增资源"
open={createOpen}
2025-10-25 18:04:01 +08:00
tourismOptions={tourismOptions}
onOpenChange={setCreateOpen}
onFinish={async (values: TourismFileItem) => {
const hide = message.loading('保存中...');
try {
2025-10-25 18:04:01 +08:00
await ordersApi.saveOrUpdateTourismFile({ ...values, status: values.status ?? 0 });
message.success('新增成功');
2025-10-25 18:04:01 +08:00
loadGroups();
return true;
} catch (e) {
2025-10-25 18:04:01 +08:00
message.error('新增失败');
return false;
} finally {
hide();
}
}}
2025-10-25 18:04:01 +08:00
/>
2025-10-25 18:04:01 +08:00
<TourismFileForm
title="编辑资源"
open={editOpen}
initialValues={current}
2025-10-25 18:04:01 +08:00
tourismOptions={tourismOptions}
onOpenChange={setEditOpen}
onFinish={async (values: TourismFileItem) => {
const hide = message.loading('保存中...');
try {
2025-10-25 18:04:01 +08:00
await ordersApi.saveOrUpdateTourismFile({ ...(current || {}), ...values });
message.success('修改成功');
2025-10-25 18:04:01 +08:00
loadGroups();
return true;
} catch (e) {
message.error('修改失败');
return false;
} finally {
hide();
}
}}
2025-10-25 18:04:01 +08:00
/>
</PageContainer>
);
};
export default TourismFilePage;