import React, { useState } from 'react'; import { Button, Space, Drawer, Select, message, Card, Spin, Typography,Descriptions,DatePicker, } from 'antd'; import { EnvironmentOutlined, ClockCircleOutlined, UserOutlined, GlobalOutlined } from '@ant-design/icons'; import { useQuery, } from '@tanstack/react-query'; import 'dayjs/locale/zh-cn'; import AMap from './components_amap.tsx'; // 导入地图组件 // 从share/types.ts导入所有类型,包括MapMode import type { MarkerData, LoginLocation, LoginLocationDetail, User } from '../share/types.ts'; import { UserAPI } from './api/index.ts'; import { MapAPI } from './api/index.ts'; import dayjs from 'dayjs'; const { RangePicker } = DatePicker; // 地图页面组件 export const LoginMapPage = () => { const [selectedTimeRange, setSelectedTimeRange] = useState<[dayjs.Dayjs, dayjs.Dayjs] | null>(null); const [selectedUserId, setSelectedUserId] = useState(null); const [selectedMarker, setSelectedMarker] = useState(null); const [drawerVisible, setDrawerVisible] = useState(false); // 获取登录位置数据 const { data: locations = [], isLoading: markersLoading } = useQuery({ queryKey: ['loginLocations', selectedTimeRange, selectedUserId], queryFn: async () => { try { let params: any = {}; if (selectedTimeRange) { params.startTime = selectedTimeRange[0].format('YYYY-MM-DD HH:mm:ss'); params.endTime = selectedTimeRange[1].format('YYYY-MM-DD HH:mm:ss'); } if (selectedUserId) { params.userId = selectedUserId; } const result = await MapAPI.getMarkers(params); return result.data; } catch (error) { console.error("获取登录位置数据失败:", error); message.error("获取登录位置数据失败"); return []; } }, refetchInterval: 30000 // 30秒刷新一次 }); // 获取用户列表 const { data: users = [] } = useQuery({ queryKey: ['users'], queryFn: async () => { try { const response = await UserAPI.getUsers(); return response.data || []; } catch (error) { console.error("获取用户列表失败:", error); message.error("获取用户列表失败"); return []; } } }); // 获取选中标记点的详细信息 const { data: markerDetail, isLoading: detailLoading } = useQuery({ queryKey: ['loginLocation', selectedMarker?.id], queryFn: async () => { if (!selectedMarker?.id) return undefined; try { const result = await MapAPI.getLocationDetail(Number(selectedMarker.id)); return result.data; } catch (error) { console.error("获取登录位置详情失败:", error); message.error("获取登录位置详情失败"); return undefined; } }, enabled: !!selectedMarker?.id }); // 处理标记点点击 const handleMarkerClick = (marker: MarkerData) => { setSelectedMarker(marker); setDrawerVisible(true); }; // 渲染地图标记点 const renderMarkers = (locations: LoginLocation[] = []): MarkerData[] => { if (!Array.isArray(locations)) return []; return locations .filter(location => location?.longitude !== null && location?.latitude !== null) .map(location => ({ id: location.id?.toString() || '', longitude: location.longitude as number, latitude: location.latitude as number, title: location.user?.nickname || location.user?.username || '未知用户', description: `登录时间: ${dayjs(location.loginTime).format('YYYY-MM-DD HH:mm:ss')}\nIP地址: ${location.ipAddress}`, status: 'online', type: 'login', extraData: location })); }; return (
setSelectedTimeRange(dates as [dayjs.Dayjs, dayjs.Dayjs])} placeholder={['开始时间', '结束时间']} />