diff --git a/src/components/devices/DroneVideoPlayer.tsx b/src/components/devices/DroneVideoPlayer.tsx index e37b559..8dccd51 100644 --- a/src/components/devices/DroneVideoPlayer.tsx +++ b/src/components/devices/DroneVideoPlayer.tsx @@ -12,8 +12,8 @@ interface DroneVideoPlayerProps { height?: number | string; showSourceSelect?: boolean; defaultSource?: 'station' | 'drone'; - videoSource?: 'station' | 'drone'; // 新增:支持受控模式 - onSourceChange?: (source: 'station' | 'drone') => void; // 新增:支持受控模式 + videoSource?: 'station' | 'drone'; + onSourceChange?: (source: 'station' | 'drone') => void; } const DroneVideoPlayer: React.FC = ({ @@ -25,36 +25,29 @@ const DroneVideoPlayer: React.FC = ({ onSourceChange }) => { const [internalVideoSource, setInternalVideoSource] = useState<'station' | 'drone'>(defaultSource); - - // 如果提供了 propsVideoSource,则使用受控值,否则使用内部状态 const videoSource = propsVideoSource !== undefined ? propsVideoSource : internalVideoSource; const setVideoSource = (source: 'station' | 'drone') => { - if (onSourceChange) { - onSourceChange(source); - } else { - setInternalVideoSource(source); - } + if (onSourceChange) onSourceChange(source); + else setInternalVideoSource(source); }; - const [selectedCamera, setSelectedCamera] = useState<'indoor' | 'outdoor'>('outdoor'); + + const [selectedCamera, setSelectedCamera] = useState<'indoor' | 'outdoor'>('indoor'); const [selectedLens, setSelectedLens] = useState<'wide' | 'zoom' | 'ir'>('wide'); + // 当前选中的摄像头 index + const [selectedDroneCameraIdx, setSelectedDroneCameraIdx] = useState(0); const [streamData, setStreamData] = useState(null); const [loading, setLoading] = useState(false); - // 防抖锁:防止重复点击(核心优化) const switchingLock = useRef(false); // 加载机场摄像头 const loadStationCamera = async (cameraPosition: 'indoor' | 'outdoor') => { setSelectedCamera(cameraPosition); - if (!device?.gateway_sn || switchingLock.current) return; switchingLock.current = true; setLoading(true); try { - const cameraIndex = device.gateway_camera_list?.find( - (item: any) => item.camera_position === cameraPosition - )?.camera_index || ""; - + const cameraIndex = device.gateway_camera_list[0]?.camera_index || ""; // 目前先默认用第一个摄像头 if (!cameraIndex) { setStreamData(null); utils.message.warning(`未找到${cameraPosition === 'indoor' ? '室内' : '室外'}摄像头配置`); @@ -69,11 +62,8 @@ const DroneVideoPlayer: React.FC = ({ videoExpire: 7200, }); - if (res.code === 200) { - setStreamData(res.data); - } else { - utils.message.error(res.msg || '切换机场摄像头失败'); - } + if (res.code == 200) setStreamData(res.data); + else utils.message.error(res.msg || '切换机场摄像头失败'); } catch (err) { console.error('切换机场摄像头失败', err); } finally { @@ -82,23 +72,18 @@ const DroneVideoPlayer: React.FC = ({ } }; - // 加载无人机镜头(优化版) + // 加载无人机镜头(使用当前选中的摄像头索引) const loadDroneLens = async (lensType: 'wide' | 'zoom' | 'ir') => { setSelectedLens(lensType); - if (!device?.device_sn || switchingLock.current) return; - // 如果已经是当前镜头,不重复请求(超级重要!) - if (selectedLens === lensType && streamData) { - return; - } - switchingLock.current = true; setLoading(true); try { - const cameraIndex = device.drone_camera_list?.[1]?.camera_index || - device.drone_camera_list?.[0]?.camera_index || ""; + const cameraList = device.drone_camera_list || []; + const targetCamera = cameraList[selectedDroneCameraIdx]; + const cameraIndex = targetCamera?.camera_index || ""; if (!cameraIndex) { utils.message.warning('未找到无人机相机配置'); @@ -113,11 +98,8 @@ const DroneVideoPlayer: React.FC = ({ videoExpire: 7200, }); - if (res.code === 200) { - setStreamData(res.data); - } else { - utils.message.error(res.msg || '切换无人机镜头失败'); - } + if (res.code === 200) setStreamData(res.data); + else utils.message.error(res.msg || '切换无人机镜头失败'); } catch (err) { console.error('切换无人机镜头失败', err); } finally { @@ -126,7 +108,12 @@ const DroneVideoPlayer: React.FC = ({ } }; - // 自动加载默认画面 + // 切换无人机摄像头 + const switchDroneCamera = (idx: number) => { + setSelectedDroneCameraIdx(idx); + }; + + // 自动加载 useEffect(() => { if (device) { setStreamData(null); @@ -136,7 +123,13 @@ const DroneVideoPlayer: React.FC = ({ loadDroneLens(selectedLens); } } - }, [device, videoSource]); + }, [device, videoSource, selectedDroneCameraIdx]); + + // 生成摄像头下拉选项 + const droneCameraOptions = device?.drone_camera_list?.map((item: any, idx: number) => ({ + label: item.camera_name || `摄像头 ${idx + 1}`, + value: idx, + })) || []; return (
@@ -163,7 +156,7 @@ const DroneVideoPlayer: React.FC = ({ overflow: 'hidden', background: '#000' }}> - {/* 切换按钮覆盖层 */} + {/* 视频控制按钮 */}
= ({ display: 'flex', gap: '4px', zIndex: 10, + flexWrap: 'wrap', }}> {videoSource === 'station' ? ( <> - - + + + ) : ( <> - - - + {/* 动态摄像头下拉框 */} + {droneCameraOptions.length > 0 && ( +