feat(movement-supervision): 增强场景数据解析逻辑,适配多种后端数据格式并优化楼层切换后定位功能

This commit is contained in:
xudan 2025-10-22 14:20:10 +08:00
parent 8cde103ac9
commit e69b0419de

View File

@ -38,6 +38,25 @@ const props = defineProps<Props>();
//
const route = useRoute();
// scene-editor.vue
const normalizeSceneJson = (raw: unknown): unknown => {
if (raw == null) return raw;
let result: unknown = raw;
const maxDepth = 2;
let depth = 0;
while (typeof result === 'string' && depth < maxDepth) {
const trimmed = (result as string).trim();
if (!trimmed) break;
try {
result = JSON.parse(trimmed);
} catch {
break;
}
depth += 1;
}
return result;
};
//#region Playback Mode
const mode = computed(() => (route.path.includes('/playback') ? 'playback' : 'live'));
const isPlaybackMode = computed(() => mode.value === 'playback');
@ -162,30 +181,33 @@ provide(EDITOR_KEY, editor);
const readScene = async () => {
const res = props.id ? await getSceneByGroupId(props.id, props.sid) : await getSceneById(props.sid);
title.value = res?.label ?? '';
const sceneJson = normalizeSceneJson(res?.json);
// json
if (Array.isArray(res?.json)) {
if (res.json.length > 0) {
if (Array.isArray(sceneJson)) {
if (sceneJson.length > 0) {
//
floorScenes.value = res.json;
floorScenes.value = sceneJson as any[];
currentFloorIndex.value = 0; //
editor.value?.load(floorScenes.value[0]);
await editor.value?.load(floorScenes.value[0]);
} else {
//
floorScenes.value = [];
editor.value?.load('{}'); //
message.warn('场景文件为空');
message.warn('场景文件为空数组');
floorScenes.value = [{}];
currentFloorIndex.value = 0;
await editor.value?.load('{}'); //
}
} else if (res?.json && Object.keys(res.json).length > 0) {
} else if (sceneJson && typeof sceneJson === 'object' && Object.keys(sceneJson as any).length > 0) {
//
floorScenes.value = [res.json];
floorScenes.value = [sceneJson];
currentFloorIndex.value = 0;
editor.value?.load(res.json);
await editor.value?.load(sceneJson as any);
} else {
// null/undefined
floorScenes.value = [];
editor.value?.load('{}'); //
message.warn('场景文件为空或格式不正确');
floorScenes.value = [{}];
currentFloorIndex.value = 0;
await editor.value?.load('{}'); //
}
};
@ -597,7 +619,7 @@ const selectRobot = (id: string) => {
//#endregion
//#region
const { saveViewState, autoSaveAndRestoreViewState, isSaving } = useViewState();
const { saveViewState, autoSaveAndRestoreViewState, isSaving, jumpToPosition, calculateCenterPoint } = useViewState();
/**
* 保存当前视图状态
@ -634,6 +656,13 @@ const handleFloorChange = async (value: any) => {
if (mode.value === 'live') {
await monitorScene();
}
// map-toolbar.vue
try {
const { centerX, centerY } = calculateCenterPoint(editor.value);
jumpToPosition(editor.value, centerX, centerY, false, 0.05);
} catch {
// ignore
}
}
};
//#endregion