diff --git a/src/pages/movement-supervision.vue b/src/pages/movement-supervision.vue index 4e9ae5a..4028ce9 100644 --- a/src/pages/movement-supervision.vue +++ b/src/pages/movement-supervision.vue @@ -159,15 +159,27 @@ const readScene = async () => { // 检查返回的json是数组(多楼层)还是对象(单楼层) if (Array.isArray(res?.json)) { - // 多楼层 - floorScenes.value = res.json; - currentFloorIndex.value = 0; // 默认显示第一层 - editor.value?.load(floorScenes.value[0]); - } else if (res?.json) { + if (res.json.length > 0) { + // 多楼层 + floorScenes.value = res.json; + currentFloorIndex.value = 0; // 默认显示第一层 + editor.value?.load(floorScenes.value[0]); + } else { + // 空数组,当作空场景处理 + floorScenes.value = []; + editor.value?.load('{}'); // 加载空场景 + message.warn('场景文件为空'); + } + } else if (res?.json && Object.keys(res.json).length > 0) { // 单楼层,包装成数组以统一处理 floorScenes.value = [res.json]; currentFloorIndex.value = 0; editor.value?.load(res.json); + } else { + // 空对象或null/undefined,也当作空场景处理 + floorScenes.value = []; + editor.value?.load('{}'); // 加载空场景 + message.warn('场景文件为空或格式不正确'); } }; diff --git a/src/pages/scene-editor.vue b/src/pages/scene-editor.vue index 86574fa..6e36e3e 100644 --- a/src/pages/scene-editor.vue +++ b/src/pages/scene-editor.vue @@ -30,13 +30,47 @@ const { isConverting, convertSmapToScene, exportSceneToSmap, convertSceneToIray const readScene = async () => { const res = await getSceneById(props.id); title.value = res?.label ?? ''; - editor.value?.load(res?.json, editable.value); + let sceneJson = res?.json; + + // 适配不同的场景数据格式 + if (Array.isArray(sceneJson)) { + if (sceneJson.length > 0) { + // 多楼层 + floorScenes.value = sceneJson; + currentFloorIndex.value = 0; + editor.value?.load(floorScenes.value[0], editable.value); + } else { + // 空数组 + message.warn('场景文件为空数组'); + floorScenes.value = [{}]; // 创建一个默认的空楼层 + currentFloorIndex.value = 0; + editor.value?.load('{}', editable.value); + } + } else if (sceneJson && Object.keys(sceneJson).length > 0) { + // 单楼层,统一为多楼层数组格式 + floorScenes.value = [sceneJson]; + currentFloorIndex.value = 0; + editor.value?.load(floorScenes.value[0], editable.value); + } else { + message.warn('场景文件为空或格式不正确'); + floorScenes.value = [{}]; // 创建一个默认的空楼层 + currentFloorIndex.value = 0; + editor.value?.load('{}', editable.value); + } }; const saveScene = async () => { - const json = editor.value?.save(); - if (!json) return Promise.reject('无法获取场景数据'); - const res = await saveSceneById(props.id, json); + // 保存前,先将当前编辑器的内容更新到 floorScenes 中 + const currentJson = editor.value?.save(); + if (currentJson) { + floorScenes.value[currentFloorIndex.value] = JSON.parse(currentJson); + } + + // 根据楼层数量决定保存单层对象还是多层数组 + const dataToSave = floorScenes.value.length > 1 ? floorScenes.value : floorScenes.value[0]; + + if (!dataToSave) return Promise.reject('无法获取场景数据'); + const res = await saveSceneById(props.id, dataToSave); if (!res) return Promise.reject('保存失败'); if (editor.value?.store) { @@ -58,6 +92,13 @@ const pushScene = async () => { //#endregion const title = ref(''); +// 新增:用于存储多楼层场景数据的数组 +const floorScenes = ref([]); +// 新增:当前楼层的索引 +const currentFloorIndex = ref(0); + +// 新增:判断是否为多楼层模式 +const isMultiFloor = computed(() => floorScenes.value.length > 1); onMounted(() => { document.title = '场景编辑器'; @@ -288,6 +329,20 @@ const handleAutoCreateStorageConfirm = (actionPoints: any[]) => { const handleAutoCreateStorageCancel = () => { autoCreateStorageVisible.value = false; }; + +const handleFloorChange = async (value: any) => { + const newFloorIndex = value as number; + if (editor.value && floorScenes.value[newFloorIndex]) { + // 切换前,先保存当前楼层的状态 + const currentJson = editor.value.save(); + floorScenes.value[currentFloorIndex.value] = JSON.parse(currentJson); + + // 加载新楼层 + currentFloorIndex.value = newFloorIndex; + await editor.value.load(floorScenes.value[newFloorIndex], editable.value); + } +}; +