222 lines
6.6 KiB
TypeScript
222 lines
6.6 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { createPortal } from 'react-dom';
|
|
import { Button, Space, Badge, message, Select, Tooltip } from 'antd';
|
|
import {
|
|
ArrowLeftOutlined,
|
|
ReloadOutlined,
|
|
FullscreenOutlined,
|
|
FullscreenExitOutlined,
|
|
GlobalOutlined,
|
|
EnvironmentOutlined,
|
|
} from '@ant-design/icons';
|
|
import { history } from 'umi';
|
|
import MapContainer from './components/MapContainer';
|
|
import { ordersApi } from '@/services/orders';
|
|
import { tenantApi } from '@/services/tenant';
|
|
import styles from './index.less';
|
|
|
|
const { Option } = Select;
|
|
|
|
const Map3D: React.FC = () => {
|
|
const [tourismList, setTourismList] = useState<any[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
const [params, setParams] = useState<{ tenantId?: string; includeChildren?: boolean }>({});
|
|
const [portalReady, setPortalReady] = useState(false);
|
|
|
|
// 用于 3D 模式下的外部选择控制
|
|
const [selectedTourism, setSelectedTourism] = useState<any>(null);
|
|
|
|
// 首次进入时,默认按"当前租户 + 包含下级租户"查询
|
|
useEffect(() => {
|
|
setPortalReady(true);
|
|
(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({ tenantId, includeChildren: true });
|
|
}
|
|
} catch {
|
|
// 忽略错误
|
|
}
|
|
})();
|
|
|
|
// 监听租户变化事件
|
|
const handleTenantChange = (e: CustomEvent) => {
|
|
const { tenantId, includeChildren } = e.detail || {};
|
|
if (tenantId) {
|
|
setParams({ tenantId, includeChildren: includeChildren ?? true });
|
|
}
|
|
};
|
|
window.addEventListener('tenant-change', handleTenantChange as EventListener);
|
|
|
|
return () => {
|
|
window.removeEventListener('tenant-change', handleTenantChange as EventListener);
|
|
};
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const onFullscreenChange = () => {
|
|
setIsFullscreen(!!document.fullscreenElement);
|
|
};
|
|
document.addEventListener('fullscreenchange', onFullscreenChange);
|
|
return () => document.removeEventListener('fullscreenchange', onFullscreenChange);
|
|
}, []);
|
|
|
|
// 获取景区列表
|
|
const fetchTourismList = async () => {
|
|
if (!params.tenantId) {
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
try {
|
|
const response = await ordersApi.tourism({
|
|
current: 1,
|
|
pageSize: 1000,
|
|
...params,
|
|
});
|
|
setTourismList(response.data || []);
|
|
} catch (error) {
|
|
message.error('获取景区列表失败');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
// 景区点击事件 - 3D模式下不跳转,直接在地图上展示
|
|
const handleTourismClick = (tourism: any) => {
|
|
setSelectedTourism(tourism);
|
|
};
|
|
|
|
// 下拉框选择事件
|
|
const handleDropdownSelect = (tourismId: number) => {
|
|
const tourism = tourismList.find(t => t.id === tourismId);
|
|
if (!tourism) return;
|
|
setSelectedTourism(tourism);
|
|
};
|
|
|
|
// 清空景区选择
|
|
const handleClearSelection = () => {
|
|
setSelectedTourism(null);
|
|
};
|
|
|
|
// 返回大屏
|
|
const goBack = () => {
|
|
history.push('/dashboard');
|
|
};
|
|
|
|
// 全屏切换
|
|
const toggleFullscreen = () => {
|
|
if (!document.fullscreenElement) {
|
|
document.documentElement.requestFullscreen();
|
|
setIsFullscreen(true);
|
|
} else {
|
|
document.exitFullscreen();
|
|
setIsFullscreen(false);
|
|
}
|
|
};
|
|
|
|
// 租户ID获取后,加载景区列表
|
|
useEffect(() => {
|
|
if (params.tenantId) {
|
|
fetchTourismList();
|
|
}
|
|
}, [params.tenantId]);
|
|
|
|
// 过滤有效景区 (有经纬度)
|
|
const validTourismList = tourismList.filter(t => {
|
|
const lat = parseFloat(t.latitude);
|
|
const lon = parseFloat(t.longitude);
|
|
return !isNaN(lat) && !isNaN(lon) && lat !== 0 && lon !== 0;
|
|
});
|
|
|
|
const leftSlot = portalReady ? document.getElementById('map3d-header-left') : null;
|
|
const centerSlot = portalReady ? document.getElementById('map3d-header-center') : null;
|
|
const rightSlot = portalReady ? document.getElementById('map3d-header-right') : null;
|
|
|
|
const headerLeft = (
|
|
<div className={styles.headerLeft}>
|
|
<Tooltip title="返回监控大屏" placement="bottom">
|
|
<Button
|
|
aria-label="返回监控大屏"
|
|
shape="circle"
|
|
icon={<ArrowLeftOutlined />}
|
|
onClick={goBack}
|
|
className={styles.backIconBtn}
|
|
/>
|
|
</Tooltip>
|
|
|
|
<Select
|
|
showSearch
|
|
placeholder="请选择景区进入..."
|
|
optionFilterProp="children"
|
|
value={selectedTourism?.id}
|
|
onChange={handleDropdownSelect}
|
|
allowClear
|
|
onClear={handleClearSelection}
|
|
filterOption={(input, option) =>
|
|
(option?.children as unknown as string).toLowerCase().indexOf(input.toLowerCase()) >= 0
|
|
}
|
|
className={styles.tourismSelect}
|
|
suffixIcon={<EnvironmentOutlined style={{ color: '#00f0ff' }} />}
|
|
classNames={{ popup: { root: styles.tourismSelectPopup } }}
|
|
>
|
|
{validTourismList.map(t => (
|
|
<Option key={t.id} value={t.id}>{t.tourismName}</Option>
|
|
))}
|
|
</Select>
|
|
|
|
<Badge status="success" text={`共${tourismList.length}个景区`} className={styles.statsBadge} />
|
|
</div>
|
|
);
|
|
|
|
const headerCenter = (
|
|
<div className={styles.headerCenter}>
|
|
<div className={styles.headerTitle}>
|
|
<GlobalOutlined />
|
|
<span className={styles.headerTitleText}>3D地图监控中心</span>
|
|
<span className={styles.headerTitleDeco} />
|
|
</div>
|
|
</div>
|
|
);
|
|
|
|
const headerRight = (
|
|
<Space size={10} className={styles.headerRight}>
|
|
<Button icon={<ReloadOutlined />} onClick={fetchTourismList} loading={loading}>
|
|
刷新
|
|
</Button>
|
|
<Button icon={isFullscreen ? <FullscreenExitOutlined /> : <FullscreenOutlined />} onClick={toggleFullscreen}>
|
|
{isFullscreen ? '退出全屏' : '全屏'}
|
|
</Button>
|
|
</Space>
|
|
);
|
|
|
|
return (
|
|
<div className={styles.mapDashboard}>
|
|
{leftSlot && createPortal(headerLeft, leftSlot)}
|
|
{centerSlot && createPortal(headerCenter, centerSlot)}
|
|
{rightSlot && createPortal(headerRight, rightSlot)}
|
|
|
|
{/* 全屏地图 */}
|
|
<div className={styles.mapWrapper}>
|
|
<MapContainer
|
|
mode="3d"
|
|
tourismList={tourismList}
|
|
onTourismClick={handleTourismClick}
|
|
selectedTourism={selectedTourism}
|
|
onClearSelection={handleClearSelection}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Map3D;
|