From 36b6856a90b12c380afcce00e186a560ec68fdcc Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Tue, 3 Nov 2026 12:42:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=20=E5=9C=A8=E7=BA=BF=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E6=9B=B4=E6=96=B0=20=20=20i=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E8=BF=94=E8=88=AA=E7=89=A9=E5=92=8C=E9=9A=9C=E7=A2=8D?= =?UTF-8?q?=E7=89=A9=E4=BF=9D=E5=AD=98=E5=92=8C=E6=96=B0=E5=A2=9E=20?= =?UTF-8?q?=E4=BA=92=E6=96=A5=20=20=20=20=E9=9A=9C=E7=A2=8D=E7=89=A9?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9F=A9=E5=BD=A2=20=E6=89=8B=E6=9F=84?= =?UTF-8?q?=E6=8B=96=E6=8B=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Map.jsx | 300 ++++++--- .../devices/CarReverseGuideLine.tsx | 43 +- src/components/devices/DeviceStatusPage.tsx | 44 +- src/components/devices/MapEditorPage.tsx | 636 +++++++----------- 4 files changed, 502 insertions(+), 521 deletions(-) diff --git a/src/components/Map.jsx b/src/components/Map.jsx index 4857f4c..0531f54 100644 --- a/src/components/Map.jsx +++ b/src/components/Map.jsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback } from 'react'; +import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback } from 'react'; import carImg from '../assets/images/carplane.png'; import planeImg from '../assets/images/planeicon.png'; import cameraImg from '../assets/images/camera.png'; @@ -30,6 +30,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, const navLineRef = useRef(null); // 完成点实时轨迹 const markedPointsRef = useRef([]); // 标记点实体 const obstacleCirclesRef = useRef([]); // 障碍物点圆形实体 + const markedObstacleHandlesRef = useRef([]); // 编辑中障碍物拖拽手柄 const markedLineRef = useRef(null); // 标记点连线实体 const lastMarkedPointsRef = useRef([]); // 上次渲染的标记点数据,用于增量更新 const onMarkPointRef = useRef(onMarkPoint); // 总是指向最新的 onMarkPoint,避免闭包滞后 @@ -124,6 +125,37 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, ); }; + // 拖拽手柄时显示障碍物尺寸(长×宽 / 半径),跟随手柄位置 + const OBSTACLE_DRAG_TIP_ID = 'obstacle-drag-tip'; + + const updateObstacleDragTip = (viewer, position, text) => { + if (!viewer || !position) return; + const existing = viewer.entities.getById(OBSTACLE_DRAG_TIP_ID); + if (existing) { + existing.position = new Cesium.ConstantPositionProperty(position); + if (existing.label?.text?.setValue) { + existing.label.text.setValue(text); + } else if (existing.label) { + existing.label.text = text; + } + return; + } + viewer.entities.add({ + id: OBSTACLE_DRAG_TIP_ID, + position, + label: { + text, + font: 'bold 13px sans-serif', + fillColor: Cesium.Color.WHITE, + showBackground: true, + backgroundColor: Cesium.Color.fromCssColorString('#ff4d4f').withAlpha(0.9), + pixelOffset: new Cesium.Cartesian2(0, -18), + disableDepthTestDistance: Number.POSITIVE_INFINITY, + show: true, + }, + }); + }; + // 打印矩形障碍物信息(center 为 WGS84,size 为以 center 为中心的长宽,单位米) const logRectObstacleInfo = (obstacle, index, lon, lat, width, height) => { const info = { @@ -831,6 +863,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, // 同时删除对应的 obstacle circle const circleId = `marked-obstacle-circle-${id ?? i}`; viewer.entities.removeById(circleId); + //viewer.entities.removeById(`marked-obstacle-handle-${baseId}`); return false; } return true; @@ -845,6 +878,14 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, return true; }); + markedObstacleHandlesRef.current = markedObstacleHandlesRef.current.filter(id => { + if (!newPoints.some((p, i) => `marked-obstacle-handle-${p.id ?? i}` === id)) { + viewer.entities.removeById(id); + return false; + } + return true; + }); + // 2) 新增或更新标记点 entity newPoints.forEach((p, i) => { const id = `marked-point-${p.id ?? i}`; @@ -882,31 +923,64 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, markedPointsRef.current.push(id); } - // 障碍物点:创建/更新圆形区域(EllipseGraphics,单位是米) - //if (isObstacle) { - // const radius = Math.max(0.5, Number(p.radius) || 2.0); - // const circleEntity = viewer.entities.getById(circleId); - // if (circleEntity) { - // circleEntity.position = position; - // circleEntity.ellipse.semiMajorAxis = radius; - // circleEntity.ellipse.semiMinorAxis = radius; - // } else { - // viewer.entities.add({ - // id: circleId, - // position, - // ellipse: { - // semiMajorAxis: radius, - // semiMinorAxis: radius, - // material: new Cesium.Color(1.0, 0.3, 0.3, 0.25), - // outline: true, - // outlineColor: Cesium.Color.RED, - // outlineWidth: 2, - // height: 0, - // }, - // }); - // obstacleCirclesRef.current.push(circleId); - // } - //} + // 障碍物点:根据形状创建预览 + 拖拽手柄 + if (isObstacle) { + const shapeType = p.shapeType === 'circle' ? 'circle' : 'rect'; + const sizeX = Math.max(0.1, Number(p.sizeX) || Number(p.size_x) || 2); + const sizeY = Math.max(0.1, Number(p.sizeY) || Number(p.size_y) || 2); + const radius = Math.max(0.1, Number(p.radius) || 2); + const entity = viewer.entities.getById(circleId); + if (shapeType === 'rect') { + if (entity?.rectangle) { + entity.rectangle.coordinates.setValue(getObstacleRectCoordinates(Number(p.lon), Number(p.lat), sizeX, sizeY)); + } else { + viewer.entities.add({ + id: circleId, position, + rectangle: { + coordinates: getObstacleRectCoordinates(Number(p.lon), Number(p.lat), sizeX, sizeY), + height: 0, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, + material: new Cesium.Color(1.0, 0.3, 0.3, 0.25), + outline: true, outlineColor: Cesium.Color.RED, outlineWidth: 2, + }, + }); + } + } else { + if (entity?.ellipse) { + entity.ellipse.semiMajorAxis.setValue(radius); + entity.ellipse.semiMinorAxis.setValue(radius); + } else { + viewer.entities.add({ + id: circleId, position, + ellipse: { + semiMajorAxis: radius, semiMinorAxis: radius, height: 0, + heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, + material: new Cesium.Color(1.0, 0.3, 0.3, 0.25), + outline: true, outlineColor: Cesium.Color.RED, outlineWidth: 2, + }, + }); + } + } + if (!obstacleCirclesRef.current.includes(circleId)) obstacleCirclesRef.current.push(circleId); + + // 拖拽手柄 + const handleId = `marked-obstacle-handle-${p.id ?? i}`; + const handlePos = shapeType === 'rect' + ? getObstacleRectHandlePosition(Number(p.lon), Number(p.lat), sizeX, sizeY) + : getObstacleHandlePosition(Number(p.lon), Number(p.lat), radius); + const existingHandle = viewer.entities.getById(handleId); + if (existingHandle) { + existingHandle.position = new Cesium.ConstantPositionProperty(handlePos); + } else { + viewer.entities.add({ + id: handleId, + obstacleHandleData: { index: i, pointId: p.id, source: 'marked' }, + position: handlePos, + point: { pixelSize: 13, color: Cesium.Color.WHITE, outlineColor: Cesium.Color.RED, outlineWidth: 3, disableDepthTestDistance: Number.POSITIVE_INFINITY }, + label: { text: '\u2194', font: 'bold 12px sans-serif', fillColor: Cesium.Color.RED, pixelOffset: new Cesium.Cartesian2(0, -1), disableDepthTestDistance: Number.POSITIVE_INFINITY }, + }); + if (!markedObstacleHandlesRef.current.includes(handleId)) markedObstacleHandlesRef.current.push(handleId); + } + } }); // 3) 连线:只有坐标/数量真正变化时才重建 @@ -1730,13 +1804,14 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, outlineColor: Cesium.Color.WHITE, outlineWidth: 2, disableDepthTestDistance: Number.POSITIVE_INFINITY, + show: true, }, // 名称与半径标签 label: { text: isRect - ? `${obstacle?.alias || `障碍物点-${idx + 1}`} (${rectWidth.toFixed(2)}×${rectHeight.toFixed(2)}m)` - : `${obstacle?.alias || `障碍物点-${idx + 1}`} (${radius}m)`, + ? `${obstacle?.alias || obstacle?.name || `障碍物点-${idx + 1}`} (${rectWidth.toFixed(2)}×${rectHeight.toFixed(2)}m)` + : `${obstacle?.alias || obstacle?.name || `障碍物点-${idx + 1}`} (${radius}m)`, font: isHighlighted ? 'bold 14px sans-serif' : '12px sans-serif', fillColor: Cesium.Color.WHITE, showBackground: true, @@ -1747,6 +1822,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, 0, 100000 ), + show: true, }, }); @@ -1773,6 +1849,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, outlineColor: Cesium.Color.RED, outlineWidth: 3, disableDepthTestDistance: Number.POSITIVE_INFINITY, + show: true, }, label: { @@ -1781,6 +1858,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, fillColor: Cesium.Color.RED, pixelOffset: new Cesium.Cartesian2(0, -1), disableDepthTestDistance: Number.POSITIVE_INFINITY, + show: true, }, }); @@ -1788,7 +1866,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, }); viewer.scene.requestRender(); - }, [stationOverlay, highlightReturnPointId, obstacleShapeSignature]); + }, [stationOverlay, highlightReturnPointId, obstacleShapeSignature, activeType]); useEffect(() => { @@ -1827,48 +1905,42 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, if (!handleData) return; const index = Number(handleData.index); - const obstacles = stationOverlay.tempObstaclePoints || []; - const obstacle = obstacles[index]; + const handleSource = handleData.source || 'station'; + let lon, lat, radius, shapeType, rectWidth, rectHeight, entityId, handleId; - if (!obstacle) return; - - const lon = Number( - obstacle.position?.lng ?? obstacle.position?.lon - ); - const lat = Number(obstacle.position?.lat); - const radius = Number(obstacle.radius ?? 2); - - if ( - !Number.isFinite(lon) || - !Number.isFinite(lat) || - !Number.isFinite(radius) - ) { - return; + if (handleSource === 'marked') { + const obstacle = markedPoints[index]; + if (!obstacle) return; + lon = Number(obstacle.lon); + lat = Number(obstacle.lat); + shapeType = obstacle.shapeType === 'circle' ? 'circle' : 'rect'; + radius = Math.max(0.1, Number(obstacle.radius) || 2); + rectWidth = shapeType === 'rect' ? (Number(obstacle.sizeX) || Number(obstacle.size_x) || 2) : radius * 2; + rectHeight = shapeType === 'rect' ? (Number(obstacle.sizeY) || Number(obstacle.size_y) || 2) : radius * 2; + entityId = `marked-obstacle-circle-${obstacle.id ?? index}`; + handleId = `marked-obstacle-handle-${obstacle.id ?? index}`; + } else { + const obstacles = stationOverlay.tempObstaclePoints || []; + const obstacle = obstacles[index]; + if (!obstacle) return; + lon = Number(obstacle.position?.lng ?? obstacle.position?.lon); + lat = Number(obstacle.position?.lat); + radius = Number(obstacle.radius ?? 2); + const shapeConfig = obstacleShapes?.[index] || {}; + shapeType = shapeConfig.shapeType === 'rect' ? 'rect' : 'circle'; + rectWidth = Math.max(0.1, Number(shapeConfig.width) || radius * 2); + rectHeight = Math.max(0.1, Number(shapeConfig.height) || radius * 2); + entityId = `station-obstacle-point-${index}`; + handleId = `station-obstacle-handle-${index}`; } - // 形状配置:圆形 / 矩形 - const shapeConfig = obstacleShapes?.[index] || {}; - const isRect = shapeConfig.shapeType === 'rect'; - const rectWidth = Math.max( - 0.1, - Number(shapeConfig.width) || radius * 2 - ); - const rectHeight = Math.max( - 0.1, - Number(shapeConfig.height) || radius * 2 - ); + if (!Number.isFinite(lon) || !Number.isFinite(lat) || !Number.isFinite(radius)) return; draggingObstacleRef.current = { - index, - entityId: `station-obstacle-point-${index}`, - handleId: `station-obstacle-handle-${index}`, + index, source: handleSource, entityId, handleId, center: Cesium.Cartesian3.fromDegrees(lon, lat, 0), - lon, - lat, - radius, - shapeType: isRect ? 'rect' : 'circle', - width: rectWidth, - height: rectHeight, + lon, lat, radius, shapeType, + width: rectWidth, height: rectHeight, }; setIsDraggingObstacle(true); @@ -2000,24 +2072,23 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, ) ); } + const handlePos = getObstacleRectHandlePosition( + dragging.lon, + dragging.lat, + width, + height + ); if (rectHandle) { - rectHandle.position = new Cesium.ConstantPositionProperty( - getObstacleRectHandlePosition( - dragging.lon, - dragging.lat, - width, - height - ) - ); - } - if (rectEntity?.label) { - const obstacle = - stationOverlay.tempObstaclePoints?.[dragging.index]; - rectEntity.label.text.setValue( - `${obstacle?.alias || `障碍物点-${dragging.index + 1}`} (${width.toFixed(2)}×${height.toFixed(2)}m)` - ); + rectHandle.position = new Cesium.ConstantPositionProperty(handlePos); } + // 拖拽过程中实时显示长宽 + updateObstacleDragTip( + viewer, + handlePos, + `${width.toFixed(2)} × ${height.toFixed(2)} m` + ); + viewer.scene.requestRender(); return; } @@ -2044,24 +2115,38 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, } // 同步更新手柄位置 + const circleHandlePos = getObstacleHandlePosition( + dragging.lon, + dragging.lat, + nextRadius + ); if (handle) { - handle.position = new Cesium.ConstantPositionProperty( - getObstacleHandlePosition( - dragging.lon, - dragging.lat, - nextRadius - ) - ); + handle.position = new Cesium.ConstantPositionProperty(circleHandlePos); } - // 同步更新圆圈标签 - if (circle?.label) { - const obstacle = - stationOverlay.tempObstaclePoints?.[dragging.index]; + // 拖拽过程中实时显示直径 + updateObstacleDragTip( + viewer, + circleHandlePos, + `直径 ${(nextRadius * 2).toFixed(2)} m` + ); - circle.label.text.setValue( - `${obstacle?.alias || `障碍物点-${dragging.index + 1}`} (${nextRadius.toFixed(2)}m)` - ); + // 同步更新圆圈标签 + if (circle) { + const obstacle = dragging.source === 'marked' + ? markedPoints?.[dragging.index] + : stationOverlay.tempObstaclePoints?.[dragging.index]; + + const labelText = `${obstacle?.alias || obstacle?.name || `障碍物点-${dragging.index + 1}`} (${nextRadius.toFixed(2)}m)`; + + if (circle.label) { + if (circle.label.text && circle.label.text.setValue) { + circle.label.text.setValue(labelText); + } else { + circle.label.text = labelText; + } + circle.label.show = new Cesium.ConstantProperty(true); + } } viewer.scene.requestRender(); @@ -2084,22 +2169,27 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, ctrl.enableRotate = true; ctrl.enableTranslate = true; ctrl.enableZoom = true; + + // 移除拖拽尺寸提示 + const tip = viewer.entities.getById(OBSTACLE_DRAG_TIP_ID); + if (tip) viewer.entities.remove(tip); } // 矩形:打印信息并回传长宽给父组件(暂不走圆形保存逻辑) if (dragging.shapeType === 'rect') { - const obstacle = - stationOverlay.tempObstaclePoints?.[index]; + const obstacle = dragging.source === 'marked' + ? markedPoints?.[index] + : stationOverlay.tempObstaclePoints?.[index]; const { width, height } = dragging; - logRectObstacleInfo( - obstacle, - index, - dragging.lon, - dragging.lat, - width, - height - ); + //logRectObstacleInfo( + // obstacle, + // index, + // dragging.lon, + // dragging.lat, + // width, + // height + //); onObstacleRectResize?.( index, @@ -2120,7 +2210,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, viewer.scene.screenSpaceCameraController.enableRotate = true; viewer.scene.screenSpaceCameraController.enableTranslate = true; }; - }, [stationOverlay]); + }, [stationOverlay, markedPoints, activeType]); // 点击已配置列表:定位到对应返航点/分区;返航点额外高亮 useEffect(() => { diff --git a/src/components/devices/CarReverseGuideLine.tsx b/src/components/devices/CarReverseGuideLine.tsx index 4747cbb..39eac89 100644 --- a/src/components/devices/CarReverseGuideLine.tsx +++ b/src/components/devices/CarReverseGuideLine.tsx @@ -101,53 +101,56 @@ const CarReverseGuideLine: React.FC = ({ resizeObserver.observe(container); resize(); - /** + /** * 计算当前转向量(-1 左 … 0 直行 … +1 右)。 * * 优先使用实时状态:曲率 κ = ω / v * linear_speed : 前进为正,后退为负(只取大小参与曲率,方向由视角决定) * angular_speed : 左转为正,右转为负 * 曲率 > 0(左转)对应画面左侧,因此转向量取负号; - * 静止 / 极低速时不产生行驶轨迹,按直行处理。 - * 无实时数据时回退到方向盘转角 steer。 + * 静止 / 极低速时回退到方向盘转角 steer(表达"想转向"的意图)。 + * 无实时数据时也回退到方向盘转角 steer。 */ const computeTurn = () => { const v = linearSpeedRef.current; const w = angularSpeedRef.current; - + const hasRealtime = typeof v === 'number' && Number.isFinite(v) && typeof w === 'number' && Number.isFinite(w); - + + // 统一用 steer 计算意图转向(-1 ~ +1) + const steerAngle = Math.max(-45, Math.min(45, steerRef.current)); + const steerIntent = Math.tan((steerAngle * Math.PI) / 180); + + // 没有实时数据 → 完全依赖方向盘意图 if (!hasRealtime) { - const angle = Math.max( - -45, - Math.min(45, steerRef.current) - ); - - // 方向盘转角转换为曲率响应 - return Math.tan((angle * Math.PI) / 180); + return steerIntent; } - - // 静止 / 极低速:没有行驶轨迹,保持直线 - if (Math.abs(v) < 0.03) return 0; - + + // 静止 / 极低速(机器人没真在动)→ 用方向盘意图 + // 此时 curvature = w/v 不稳定(v≈0 会放大),而且用户拨动摇杆就是想让它转 + if (Math.abs(v) < 0.05) { + return steerIntent; + } + + // 真在跑 → 用实时曲率反映实际运动轨迹 // 低速时限制分母,避免曲率被放大到满量程 const speed = Math.max(Math.abs(v), 0.15); - + // 曲率:ω / v,正值左转 const kappa = w / speed; - + // 满量程曲率(约对应 1m 转弯半径) const KAPPA_FULL_SCALE = 1.0; - + const normalized = Math.max( -1, Math.min(1, kappa / KAPPA_FULL_SCALE) ); - + // 左转为正 → 画面左侧为负偏移 return -normalized; }; diff --git a/src/components/devices/DeviceStatusPage.tsx b/src/components/devices/DeviceStatusPage.tsx index 7f24547..3ca241b 100644 --- a/src/components/devices/DeviceStatusPage.tsx +++ b/src/components/devices/DeviceStatusPage.tsx @@ -1016,6 +1016,33 @@ export default function DeviceStatusPage() { }, [device, wsDeviceInfo, isUAV, locationInfo]); + // 统一在线状态判断:优先实时数据,兜底接口数据 + const isDeviceOnline = useMemo(() => { + // 1. 优先检查实时数据源(WebSocket 推送的 wsDeviceInfo / locationInfo) + const hasWsData = wsDeviceInfo && Object.keys(wsDeviceInfo).length > 0; + const hasLocData = locationInfo && Object.keys(locationInfo).length > 0; + + if (hasWsData || hasLocData) { + return true; + } + + // 2. 检查 runningStatus 里是否有有效经纬度或实时状态字段 + const runningLon = runningStatus?.longitude; + const runningLat = runningStatus?.latitude; + const hasValidCoords = runningLon !== undefined && runningLon !== null && runningLon !== 0 + && runningLat !== undefined && runningLat !== null && runningLat !== 0; + + const hasFeStatus = runningStatus?.feStatus !== 3; + const hasStatusField = runningStatus?.status !== 0; + + if (hasValidCoords || hasFeStatus || hasStatusField) { + return true; + } + + // 3. 兜底用接口返回的 onlineStatus + return device?.onlineStatus === 1; + }, [device, wsDeviceInfo, locationInfo, runningStatus]); + const { video: robotVideo, error, ready } = useWebRTCPlayer( @@ -1552,7 +1579,8 @@ export default function DeviceStatusPage() { const blockIfGamepadConnected = useCallback(() => { - if (!isGamepadConnected) return false; + // 没连接手柄 或 还没获取控制权限 → 不拦截(没控制权限本来就发不出消息) + if (!isGamepadConnected || !isControlled) return false; const now = Date.now(); @@ -1566,7 +1594,7 @@ export default function DeviceStatusPage() { return true; - }, [isGamepadConnected, t, messageApi]); + }, [isGamepadConnected, isControlled, t, messageApi]); @@ -2281,9 +2309,9 @@ export default function DeviceStatusPage() { - + - {device?.onlineStatus === 1 ? t('devices.online') : t('devices.offline')} + {isDeviceOnline ? t('devices.online') : t('devices.offline')} @@ -2537,7 +2565,7 @@ export default function DeviceStatusPage() { title={<> {t('devices.deviceStatus')}} - badge={} + badge={} > @@ -2555,11 +2583,11 @@ export default function DeviceStatusPage() { -
+
- + - {device?.onlineStatus === 1 ? t('devices.online') : t('devices.offline')} + {isDeviceOnline ? t('devices.online') : t('devices.offline')}
diff --git a/src/components/devices/MapEditorPage.tsx b/src/components/devices/MapEditorPage.tsx index b854aef..969914e 100644 --- a/src/components/devices/MapEditorPage.tsx +++ b/src/components/devices/MapEditorPage.tsx @@ -29,6 +29,9 @@ interface MarkedPoint { name: string; deviceId?: string; radius?: number; + shapeType?: 'circle' | 'rect'; + size_x?: number; + size_y?: number; } interface ReturnTaskForm { @@ -121,7 +124,7 @@ export default function MapEditorPage() { // 传给Cesium地图渲染的点位 const drawingPoints = useMemo(() => { - return currentList.map(p => ({ id: p.id, lon: p.lon, lat: p.lat, radius: p.radius })); + return currentList.map(p => ({ id: p.id, lon: p.lon, lat: p.lat, radius: p.radius, shapeType: p.shapeType, size_x: p.size_x, size_y: p.size_y })); }, [currentList]); // 切换到非路径生成类型时清除预设路径预览 @@ -149,36 +152,58 @@ export default function MapEditorPage() { getSiteMapInfo({ siteId: stationId }).then((res: any) => { if (res?.code === 200 && res.data) { setSiteMapInfoId(res.data.id ?? null); - setReturnPointConfigList(res?.data?.restPoints || []); - // 同时加载已保存的障碍物点(tempObstaclePoints),方便编辑 - const savedObstacles = (res?.data?.tempObstaclePoints || []).map((op: any) => ({ - id: genId(), - lon: Number(op.position?.lng ?? op.position?.lon ?? 0), - lat: Number(op.position?.lat ?? 0), - name: op.alias || '', - radius: Number(op.radius) || 2.0, - //shapeType: - // op.shapeType === 'rect' || op.shape_type === 'aabb' - // ? 'rect' - // : 'circle', - //width: Number(op.width ?? op.size?.[0]) || (Number(op.radius) || 2) * 2, - //height: Number(op.height ?? op.size?.[1]) || (Number(op.radius) || 2) * 2, - })); + + // 同步到已配置列表(仅用于显示/删除) + const restPoints = res?.data?.restPoints || []; + setReturnPointConfigList(restPoints); + + // 同步到编辑列表(用于编辑/保存) + const mappedReturnPoints = restPoints.map((p: any, idx: number) => { + const lon = Number(p.position?.lng ?? p.position?.lon ?? p.position?.longitude ?? p.longitude ?? 0); + const lat = Number(p.position?.lat ?? p.position?.latitude ?? p.latitude ?? 0); + return { + id: genId(), + lon, + lat, + name: (p.alias || '').trim() || `返航点-${String(idx + 1).padStart(2, '0')}`, + deviceId: p.bindDeviceId || undefined, + }; + }); + setReturnPoints(mappedReturnPoints); + + // 同时加载已保存的障碍物点(tempObstaclePoints),同步到编辑列表和已配置列表 + const savedObstacles = (res?.data?.tempObstaclePoints || []).map((op: any, idx: number) => { + const lon = Number(op.position?.lng ?? op.position?.lon ?? op.position?.longitude ?? op.center?.lng ?? op.center?.lon ?? op.center?.longitude ?? op.longitude ?? 0); + const lat = Number(op.position?.lat ?? op.position?.latitude ?? op.center?.lat ?? op.center?.latitude ?? op.latitude ?? 0); + const name = (op.alias || op.name || '').trim() || `障碍物点-${idx + 1}`; + return { + id: op.id || genId(), + lon, + lat, + name, + alias: name, + radius: Number(op.radius) || 0.5, + shapeType: op.shapeType === 'circle' ? 'circle' : 'rect', + size_x: Number(op.size_x) || Number(op.sizeX) || 0.5, + size_y: Number(op.size_y) || Number(op.sizeY) || 0.5, + }; + }); setObstaclePointConfigList(savedObstacles); + setObstaclePoints(savedObstacles); } else { setSiteMapInfoId(null); - setReturnPointConfigList([]); + setReturnPoints([]); setObstaclePointConfigList([]); - + setObstaclePoints([]); } }).catch(() => { setSiteMapInfoId(null); - setReturnPointConfigList([]); + setReturnPoints([]); setObstaclePointConfigList([]); - + setObstaclePoints([]); }); } @@ -295,7 +320,10 @@ export default function MapEditorPage() { { ...point, name: `障碍物点-${prev.length + 1}`, - radius: "" + radius: 0.5, + shapeType: 'rect', + size_x: 0.5, + size_y: 0.5, }, ]); } @@ -363,82 +391,69 @@ export default function MapEditorPage() { else if (activeType === 'restArea') setRestAreas(prev => prev.map(p => p.id === id ? { ...p, deviceId } : p)); }; - // ========== 保存返航点 / 分区 ========== + // ========== 保存返航点 / 障碍物点 (统一保存接口) ========== const handleSaveReturnOrRest = () => { - if (activeType === 'returnPoint') { - // 返航点:走 saveSiteMapInfo - console.log('[MapEditor] 保存返航点 currentList:', currentList); - const restPoints = currentList.map(p => ({ - position: { lat: p.lat, lng: p.lon }, - bindDeviceId: p.deviceId, + if (activeType === 'returnPoint' || activeType === 'obstaclePoint') { + // 1. 构造返航点数据 + const restPoints = returnPoints.map(p => ({ + position: { lat: Number(p.lat), lng: Number(p.lon) }, + bindDeviceId: p.deviceId || null, alias: p.name, - })); - const idArr = restPoints.map(item => item.bindDeviceId); - const repeatIds: string[] = []; - const seen = new Set(); - - for (const id of idArr) { - if (seen.has(id)) { - repeatIds.push(id); - } else { - seen.add(id); - } - } - - if (repeatIds.length > 0) { - // 去重展示重复id - const dupSet = [...new Set(repeatIds)]; + // 校验绑定设备重复 + const bindDeviceIds = restPoints.map(p => p.bindDeviceId).filter(id => id); + if (new Set(bindDeviceIds).size !== bindDeviceIds.length) { messageApi.warning(t('devices.mapEditor.duplicateBindDevice')); return; } + console.log(obstaclePoints, "obstaclePoints") + //return; + + // 2. 构造障碍物点数据 + const tempObstaclePoints = obstaclePoints.map(p => { + const item: Record = { + center: { lat: Number(p.lat), lng: Number(p.lon) }, + position: { lat: Number(p.lat), lng: Number(p.lon) }, // 同时传 position 和 center 确保兼容性 + shapeType: p.shapeType || 'rect', + alias: p.name, + source: 'platform' + }; + if (p.shapeType === 'circle' && p.radius) { + item.radius = p.radius; + } + if (p.shapeType === 'rect') { + if (p.size_x) item.size_x = p.size_x; + if (p.size_y) item.size_y = p.size_y; + } + return item; + }); + + // 3. 构造保存 Payload const payload: any = { restPoints, - siteId: stationId, - orgId: currentStation?.orgId, - }; - // 编辑时带上站点地图信息主键 id - if (siteMapInfoId != null) payload.id = siteMapInfoId; - console.log(payload, "保存返航点payload"); - saveSiteMapInfo(payload).then((res: any) => { - if (res?.code == 200) { - utils.message.success(t('devices.mapEditor.saveSuccess')); - setOverlayRefreshKey(k => k + 1); - // 保存后刷新返航点列表 - getSiteMapInfoFn(); - } else { - utils.message.error(res?.msg || t('common.saveFail')); - } - }); - } else if (activeType === 'obstaclePoint') { - // 障碍物点:走 saveSiteMapInfo,用 tempObstaclePoints 字段 - console.log('[MapEditor] 保存障碍物点 currentList:', currentList); - const tempObstaclePoints = currentList.map(p => ({ - position: { lat: p.lat, lng: p.lon }, - radius: p.radius ?? 2.0, - alias: p.name, - })); - const payload: any = { - restPoints: [], tempObstaclePoints, siteId: stationId, orgId: currentStation?.orgId, }; if (siteMapInfoId != null) payload.id = siteMapInfoId; - console.log(payload, "保存障碍物点 payload"); + + console.log('[MapEditor] 统一保存 Payload:', payload); saveSiteMapInfo(payload).then((res: any) => { if (res?.code == 200) { utils.message.success(t('devices.mapEditor.saveSuccess')); setOverlayRefreshKey(k => k + 1); - setObstaclePoints([]); + // 保存后重新拉取数据以同步所有状态 getSiteMapInfoFn(); } else { utils.message.error(res?.msg || t('common.saveFail')); } + }).catch(err => { + console.error('[MapEditor] save failed:', err); + utils.message.error(t('common.saveFail')); }); } else { - // 分区:弹出 Modal 让用户填写 regionName / bindRoutes 等,再调 saveRegion + // 分区保存逻辑不变 if (currentList.length === 0) { messageApi.warning(t('devices.mapEditor.needAtLeast2Points')); return; @@ -591,11 +606,12 @@ export default function MapEditorPage() { ) => { const updated = obstaclePointConfigList.map((point: any) => String(point.id) === String(item.id) - ? { ...point, name: alias } + ? { ...point, alias: alias, name: alias } : point ); setObstaclePointConfigList(updated); + setObstaclePoints(updated); // 同时更新编辑中的列表,触发地图重绘 const payload: any = { tempObstaclePoints: updated.map((point: any) => ({ @@ -608,8 +624,12 @@ export default function MapEditorPage() { 0 ), }, - radius: Number(point.radius) || 2, - alias: point.name || '', + radius: Number(point.radius) || 0, + size_x: Number(point.size_x) || 0, + size_y: Number(point.size_y) || 0, + shapeType: point.shapeType || 'rect', + alias: point.alias || point.name || '', + source: 'platform' })), siteId: stationId, orgId: currentStation?.orgId, @@ -750,59 +770,22 @@ export default function MapEditorPage() { idFromMap: number | null = null ) => { const roundedRadius = Number(radius.toFixed(2)); + setObstaclePoints(prev => prev.map((p, i) => i === index ? { ...p, radius: roundedRadius } : p)); + }; - // 以服务端最新数据为准:本地列表可能为空,或顺序与地图上的不一致 - let infoRes: any = null; - try { - infoRes = await getSiteMapInfo({ siteId: stationId }); - } catch (e) { - infoRes = null; - } - - const remoteList: any[] = - infoRes?.code === 200 && Array.isArray(infoRes.data?.tempObstaclePoints) - ? infoRes.data.tempObstaclePoints - : []; - const infoId: number | null = - (infoRes?.code === 200 ? infoRes.data?.id : null) ?? idFromMap ?? null; - - // 远端列表优先,为空时退回本地已配置列表 - const baseList: any[] = remoteList.length > 0 ? remoteList : obstaclePointConfigList; - - if (!baseList.length || index < 0 || index >= baseList.length) { - messageApi.error('未找到障碍物点数据,请刷新后重试'); - return; - } - - const nextPoints = baseList.map((item: any, idx: number) => ({ - radius: idx === index ? roundedRadius : Number(item.radius) || 0, - position: { - lat: Number(item.position?.lat ?? item.lat ?? 0), - lng: Number(item.position?.lng ?? item.position?.lon ?? item.lon ?? 0), - }, - alias: item.alias ?? item.name ?? '', - })); - - const payload: any = { - tempObstaclePoints: nextPoints, - siteId: stationId, - orgId: currentStation?.orgId, - }; - // id 为空时不要传 id 字段,否则后端会把 null 主键当成非法新增 - if (infoId != null) payload.id = infoId; - - const res: any = await saveSiteMapInfo(payload); - - if (res?.code === 200) { - messageApi.success('障碍物点半径修改成功'); - getSiteMapInfoFn(); - setOverlayRefreshKey(k => k + 1); - } else { - messageApi.error( - res?.msg || '障碍物点半径修改失败' - ); + // ========== 地图拖拽矩形障碍物尺寸回调 ========== + const handleObstacleRectResize = async (index: number, width: number, height: number) => { + setObstaclePoints(prev => prev.map((p, i) => i === index ? { ...p, size_x: width, size_y: height } : p)); + }; + // ========== 地图飞到指定点位 ========== + const handleFlyToPoint = (point: { lon?: number; lat?: number; lng?: number }) => { + const lon = point.lon ?? point.lng; + const lat = point.lat; + if (lon != null && lat != null && !isNaN(Number(lon)) && !isNaN(Number(lat))) { + mapRef.current?.flyToPosition?.({ lon: Number(lon), lat: Number(lat) }, { height: 300 }); } }; + // ========== 点击预设路径在地图上预览(覆盖=框,单点=线) ========== const handlePreviewRoute = async (route: any) => { setSelectedRouteId(route.id); @@ -881,8 +864,12 @@ export default function MapEditorPage() { 0 ), }, - radius: Number(point.radius) || 2, + radius: Number(point.radius) || 0, + size_x: Number(point.size_x) || 0, + size_y: Number(point.size_y) || 0, + shapeType: point.shapeType || 'rect', alias: point.name ?? point.alias ?? '', + source: 'platform' })), siteId: stationId, orgId: currentStation?.orgId, @@ -911,32 +898,29 @@ export default function MapEditorPage() { } }, }); - }; // 修改已配置障碍物点的半径 - const handleUpdateObstacleRadius = async ( + }; // 修改已配置障碍物点的尺寸 + const handleUpdateObstacleDimensions = async ( item: any, - radius: number + dimensions: { radius?: number; size_x?: number; size_y?: number; shapeType?: 'circle' | 'rect' } ) => { try { - // 保留其他已配置障碍物点的数据,只修改当前项半径 + // 保留其他已配置障碍物点的数据,只修改当前项 const tempObstaclePoints = obstaclePointConfigList.map( - (point: any) => ({ - position: { - lat: Number( - point.position?.lat ?? point.lat ?? 0 - ), - lng: Number( - point.position?.lng ?? - point.position?.lon ?? - point.lon ?? - 0 - ), - }, - radius: - String(point.id) === String(item.id) - ? radius - : Number(point.radius), - alias: point.alias ?? point.name ?? '', - }) + (point: any) => { + const isTarget = String(point.id) === String(item.id); + return { + position: { + lat: Number(point.position?.lat ?? point.lat ?? 0), + lng: Number(point.position?.lng ?? point.position?.lon ?? point.lon ?? 0), + }, + radius: isTarget ? (dimensions.radius ?? point.radius) : Number(point.radius), + size_x: isTarget ? (dimensions.size_x ?? point.size_x) : Number(point.size_x), + size_y: isTarget ? (dimensions.size_y ?? point.size_y) : Number(point.size_y), + shapeType: isTarget ? (dimensions.shapeType ?? point.shapeType) : (point.shapeType || 'rect'), + alias: point.alias ?? point.name ?? '', + source: 'platform' + }; + } ); const payload: any = { @@ -945,7 +929,6 @@ export default function MapEditorPage() { orgId: currentStation?.orgId, }; - // 更新站点地图信息时传入接口返回的主键 ID if (siteMapInfoId != null) { payload.id = siteMapInfoId; } @@ -953,22 +936,15 @@ export default function MapEditorPage() { const res: any = await saveSiteMapInfo(payload); if (res?.code === 200) { - messageApi.success('障碍物点半径修改成功'); - + messageApi.success('障碍物点修改成功'); getSiteMapInfoFn(); setOverlayRefreshKey(k => k + 1); - } else { - messageApi.error( - res?.msg || '障碍物点半径修改失败' - ); + messageApi.error(res?.msg || '障碍物点修改失败'); } } catch (error) { - console.error( - '[MapEditor] update obstacle radius failed:', - error - ); - messageApi.error('障碍物点半径修改失败'); + console.error('[MapEditor] update obstacle dimensions failed:', error); + messageApi.error('障碍物点修改失败'); } }; @@ -1202,7 +1178,7 @@ export default function MapEditorPage() { focusOverlay={focusOverlay} onObstacleRadiusChange={handleObstacleRadiusChange} - + onObstacleRectResize={handleObstacleRectResize} siteMapInfoId={siteMapInfoId} @@ -1473,15 +1449,12 @@ export default function MapEditorPage() { } extra={ - @@ -1501,6 +1474,7 @@ export default function MapEditorPage() { {currentList.map((p) => (
handleFlyToPoint(p)} style={{ padding: '8px 10px', borderRadius: 6, @@ -1509,13 +1483,14 @@ export default function MapEditorPage() { display: 'flex', alignItems: 'center', gap: 8, + cursor: 'pointer' }} > {activeType === 'returnPoint' ? : } -
+
e.stopPropagation()}> {activeType === 'obstaclePoint' && ( - { - const val = parseFloat(e.target.value); - const radius = Math.max(0.01, val); - setObstaclePoints(prev => prev.map(pt => pt.id === p.id ? { ...pt, radius } : pt)); - }} style={{ width: '100%', marginTop: 4 }} - - /> +
+ { + const shapeType = e.target.value; + setObstaclePoints(prev => prev.map(pt => pt.id === p.id ? { ...pt, shapeType } : pt)); + }} + style={{ marginBottom: 4 }} + > + 矩形 + {/*圆形*/} + + {p.shapeType === 'circle' ? ( + { + const val = parseFloat(e.target.value); + const radius = isNaN(val) ? 0 : Math.max(0, val); + setObstaclePoints(prev => prev.map(pt => pt.id === p.id ? { ...pt, radius } : pt)); + }} + style={{ width: '100%' }} + /> + ) : ( +
+ { + const val = parseFloat(e.target.value); + const size_x = isNaN(val) ? 0 : Math.max(0, val); + setObstaclePoints(prev => prev.map(pt => pt.id === p.id ? { ...pt, size_x } : pt)); + }} + style={{ flex: 1 }} + /> + { + const val = parseFloat(e.target.value); + const size_y = isNaN(val) ? 0 : Math.max(0, val); + setObstaclePoints(prev => prev.map(pt => pt.id === p.id ? { ...pt, size_y } : pt)); + }} + style={{ flex: 1 }} + /> +
+ )} +
)} {activeType === 'returnPoint' && ( @@ -1558,7 +1582,10 @@ export default function MapEditorPage() { type="text" danger icon={} - onClick={() => removePoint(p.id)} + onClick={(e) => { + e.stopPropagation(); + removePoint(p.id); + }} />
))} @@ -1776,88 +1803,15 @@ export default function MapEditorPage() { minWidth: 0, }} > -
e.stopPropagation()}> - { - const value = e.target.value; - - if (isReturnPoint) { - setReturnPointConfigList(prev => - prev.map((p: any, i: number) => - i === idx ? { ...p, alias: value } : p - ) - ); - } else if (isObstaclePoint) { - setObstaclePointConfigList(prev => - prev.map((p: any, i: number) => - i === idx ? { ...p, name: value } : p - ) - ); - } else { - setRegionList(prev => - prev.map((p: any) => - String(p.id) === String(item.id) - ? { ...p, regionName: value } - : p - ) - ); - } - }} - onBlur={(e) => { - const value = e.target.value.trim(); - - // 名称不能为空 - if (!value) { - messageApi.warning('名称不能为空'); - getSiteMapInfoFn(); - - if (!isReturnPoint && !isObstaclePoint) { - getStationRegions({ siteId: stationId }).then((r: any) => { - if (r?.code === 200) { - const list = r.data || r.rows || []; - setRegionList(Array.isArray(list) ? list : []); - } - }); - } - - return; - } - - if (isReturnPoint) { - console.log(value, "43424", item.alias) - handleUpdateReturnPointAlias(idx, value); - } else if (isObstaclePoint) { - handleUpdateObstacleAlias(item, value); - } else { - handleUpdateRegionName(item, value); - } - }} - onPressEnter={(e) => { - (e.target as HTMLInputElement).blur(); - }} - style={{ - fontSize: 13, - fontWeight: 500, - }} - /> +
e.stopPropagation()} style={{ fontSize: 13, fontWeight: 500, padding: '4px 0' }}> + {isReturnPoint + ? (item.alias || `返航点-${idx + 1}`) + : isObstaclePoint + ? (item.name || item.alias || `障碍物点-${idx + 1}`) + : (item.regionName || `分区-${idx + 1}`)}
- {/* 已配置障碍物点:展示半径与坐标 */} + {/* 已配置障碍物点:展示类型、尺寸与坐标 */} {isObstaclePoint && (
-
- 半径: - { - const value = e.target.value; - setObstaclePointConfigList(prev => - prev.map((pt, i) => - i === idx - ? { - ...pt, - radius: value === '' ? '' : Number(value), - } - : pt - ) - ); - }} - onBlur={() => { - const radius = Number(item.radius); - - if (!Number.isFinite(radius)) { - //messageApi.warning('半径不能小于 0.1'); - return; - } - - handleUpdateObstacleRadius(item, radius); - }} - onPressEnter={(e) => { - (e.target as HTMLInputElement).blur(); - }} - /> -
- {/*
- 半径:{Number(item.radius ?? 2)} m -
*/} -
- 经度:{Number( - item?.position?.lng?.toFixed(2) ?? - item?.position?.lon?.toFixed(2) ?? - item?.lon?.toFixed(2) ?? - 0 - )} -
-
- 纬度:{Number( - item?.position?.lat?.toFixed(2) ?? - item?.lat?.toFixed(2) ?? - 0 - )} +
类型: {item.shapeType === 'circle' ? '圆形' : '矩形'}
+ {item.shapeType === 'circle' ? ( +
半径: {item.radius}m
+ ) : ( +
尺寸: {item.size_x}m x {item.size_y}m
+ )} +
+ 经度: {Number(item.lon ?? item.position?.lng ?? item.position?.lon ?? 0).toFixed(6)} + 纬度: {Number(item.lat ?? item.position?.lat ?? 0).toFixed(6)}
)} - - {/* 已配置返航点:保留设备绑定功能 */} {isReturnPoint && ( - - e.stopPropagation() - } - > -