feat: 优化场景编辑器和运动监控页面的场景加载逻辑,新增对空场景和多楼层场景的处理,提升用户体验

This commit is contained in:
xudan 2025-10-14 09:37:39 +08:00
parent 21c5a20215
commit d1e63284da
2 changed files with 89 additions and 9 deletions

View File

@ -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('场景文件为空或格式不正确');
}
};

View File

@ -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<string>('');
//
const floorScenes = ref<any[]>([]);
//
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);
}
};
</script>
<template>
@ -298,6 +353,19 @@ const handleAutoCreateStorageCancel = () => {
<a-space align="center">
<a-button @click="backToCards"> 返回 </a-button>
<a-button type="primary" :loading="isSaving" @click="handleSaveViewState"> 保存比例 </a-button>
<a-divider v-if="isMultiFloor" type="vertical" />
<a-select
v-if="isMultiFloor"
v-model:value="currentFloorIndex"
style="width: 120px"
@change="handleFloorChange"
>
<a-select-option v-for="(scene, index) in floorScenes" :key="index" :value="index">
楼层 {{ index + 1 }}
</a-select-option>
</a-select>
<a-button v-if="editable" class="warning" @click="editable = false">
<i class="icon exit size-18 mr-8" />
<span>{{ $t('退出编辑器') }}</span>