Files
gofly-platform-services/zlt-web/react-web-next/src/pages/orders/TourismFile/index.tsx
T
2025-10-25 18:04:01 +08:00

230 lines
9.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React, { useEffect, useRef, useState } from 'react';
import { ActionBar, GlassCard, SearchBarPlus } from '@/components/ui';
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';
import TourismFileForm, { TourismFileItem } from './components/TourismFileForm';
// 资源类型枚举(用于卡片展示的颜色)
const TYPE_OPTIONS = [
{ label: '视频', value: 5, color: '#f5222d' },
{ label: '首页 Banner', value: 6, color: '#2f54eb' },
{ 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 }>>([]);
const [createOpen, setCreateOpen] = useState(false);
const [editOpen, setEditOpen] = useState(false);
const [current, setCurrent] = useState<any>();
// 分组数据(按项目分组)与分页
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 {
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 {}
})();
// 租户切换时刷新
}, [query?.tenantId, query?.includeChildren]);
// 加载查询结果(含分页)
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);
}
};
// 首次与查询/分页变化时刷新
useEffect(() => {
loadGroups();
}, [query, pagination.current, pagination.pageSize]);
return (
<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>
{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>
<TourismFileForm
title="新增资源"
open={createOpen}
tourismOptions={tourismOptions}
onOpenChange={setCreateOpen}
onFinish={async (values: TourismFileItem) => {
const hide = message.loading('保存中...');
try {
await ordersApi.saveOrUpdateTourismFile({ ...values, status: values.status ?? 0 });
message.success('新增成功');
loadGroups();
return true;
} catch (e) {
message.error('新增失败');
return false;
} finally {
hide();
}
}}
/>
<TourismFileForm
title="编辑资源"
open={editOpen}
initialValues={current}
tourismOptions={tourismOptions}
onOpenChange={setEditOpen}
onFinish={async (values: TourismFileItem) => {
const hide = message.loading('保存中...');
try {
await ordersApi.saveOrUpdateTourismFile({ ...(current || {}), ...values });
message.success('修改成功');
loadGroups();
return true;
} catch (e) {
message.error('修改失败');
return false;
} finally {
hide();
}
}}
/>
</PageContainer>
);
};
export default TourismFilePage;