feat: 优化场景编辑器和运动监控页面的场景加载逻辑,新增对空场景和多楼层场景的处理,提升用户体验
This commit is contained in:
parent
21c5a20215
commit
d1e63284da
@ -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('场景文件为空或格式不正确');
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user