refactor(cesium): 重构 Cesium Viewer 状态管理和 Composable
Some checks failed
/ build-and-deploy-to-vercel (push) Successful in 2m53s
/ lint-build-and-check (push) Successful in 5m9s
/ surge (push) Successful in 2m46s
/ playwright (push) Failing after 3m4s

This commit is contained in:
严浩
2025-04-07 10:00:30 +08:00
parent d7d704d120
commit 2721b4c2f3
8 changed files with 125 additions and 134 deletions

View File

@ -10,11 +10,11 @@ import type {
} from '@/components/h-cesium-viewer/useHCesiumManager.types';
// 地面站和选中状态
const stationState = reactive<HCesiumManagerStationState>({
const 站点State = reactive<HCesiumManagerStationState>({
stations: [],
selectedIds: new Set<string>(),
});
stationState.stations = [
站点State.stations = [
{ height: 50, id: 'gs-bj', latitude: 39.9042, longitude: 116.4074, name: '北京站' },
{ height: 10, id: 'gs-sh', latitude: 31.2304, longitude: 121.4737, name: '上海站' },
{ height: 20, id: 'gs-gz', latitude: 23.1291, longitude: 113.2644, name: '广州站' },
@ -27,6 +27,9 @@ const satelliteState = reactive<HCesiumManagerSatelliteState>({
});
onMounted(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
// setTimeout(() => {
// satelliteState.selectedIds = new Set([satelliteState.satellites[0].id]);
// }, 1500);
// 初始化卫星列表
satelliteState.satellites = [
{
@ -44,7 +47,7 @@ onMounted(async () => {
1 25544U 98067A 25091.51178241 .00016717 00000+0 30771-3 0 9997
2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391587775`,
showOrbit: false,
showCoverage: true,
showCoverage: false,
showPath: true,
},
{
@ -80,7 +83,7 @@ const selectedSatelliteIdsArray = computed({
// 计算 Checkbox Group 的 options
const stationCheckboxOptions = computed(() =>
stationState.stations.map((station) => ({ label: station.name, value: station.id })),
站点State.stations.map((station) => ({ label: station.name, value: station.id })),
);
// 添加一个随机站点
@ -89,7 +92,7 @@ const addRandomStation = () => {
const randomLon = Math.random() * 360 - 180; // -180 to 180
const randomLat = Math.random() * 180 - 90; // -90 to 90
const randomHeight = Math.random() * 1000; // 0 to 1000 meters
stationState.stations.push({
站点State.stations.push({
height: randomHeight,
id: randomId,
latitude: randomLat,
@ -97,37 +100,37 @@ const addRandomStation = () => {
name: `随机站 ${randomId.slice(-4)}`,
});
// 创建新的 Set 以触发响应式更新
stationState.selectedIds = new Set([randomId, ...stationState.selectedIds]); // ESLint: 调整顺序
consola.info('添加随机站点:', stationState.stations.at(-1)); // 使用 .at() 访问最后一个元素
站点State.selectedIds = new Set([randomId, ...站点State.selectedIds]); // ESLint: 调整顺序
consola.info('添加随机站点:', 站点State.stations.at(-1)); // 使用 .at() 访问最后一个元素
};
// 移除最后一个站点
const removeLastStation = () => {
if (stationState.stations.length > 0) {
const removedStation = stationState.stations.pop();
if (站点State.stations.length > 0) {
const removedStation = 站点State.stations.pop();
if (removedStation) {
consola.info('移除站点:', removedStation);
// 同时从选中项中移除
// 创建新的 Set 以触发响应式更新
const newSelectedIds = new Set(stationState.selectedIds);
const newSelectedIds = new Set(站点State.selectedIds);
newSelectedIds.delete(removedStation.id);
stationState.selectedIds = newSelectedIds;
站点State.selectedIds = newSelectedIds;
}
}
};
// 清空所有选中项
const clearAllStations = () => {
stationState.stations = [];
stationState.selectedIds = new Set(); // 创建新的空 Set 以触发响应式更新
站点State.stations = [];
站点State.selectedIds = new Set(); // 创建新的空 Set 以触发响应式更新
consola.info('清空所有站点');
};
// 为 a-checkbox-group 创建计算属性,处理 Set 和 Array 的转换
const selectedStationIdsArray = computed({
get: () => [...stationState.selectedIds], // 从 Set 转换为 Array
get: () => [...站点State.selectedIds], // 从 Set 转换为 Array
set: (val: string[]) => {
stationState.selectedIds = new Set(val); // 从 Array 转换回 Set
站点State.selectedIds = new Set(val); // 从 Array 转换回 Set
},
});
</script>
@ -139,17 +142,17 @@ const selectedStationIdsArray = computed({
<a-card title="地面站控制" size="small">
<div class="space-x-2">
<a-button size="small" type="primary" @click="addRandomStation">添加随机站点</a-button>
<a-button size="small" @click="removeLastStation" :disabled="stationState.stations.length === 0"
<a-button size="small" @click="removeLastStation" :disabled="站点State.stations.length === 0"
>移除最后一个</a-button
>
<a-button size="small" danger @click="clearAllStations" :disabled="stationState.stations.length === 0"
<a-button size="small" danger @click="clearAllStations" :disabled="站点State.stations.length === 0"
>清空所有</a-button
>
<span>当前站点数: {{ stationState.stations.length }}</span>
<span> | 选中站点数: {{ stationState.selectedIds.size }}</span>
<span>当前站点数: {{ 站点State.stations.length }}</span>
<span> | 选中站点数: {{ 站点State.selectedIds.size }}</span>
</div>
<!-- 站点选择 -->
<div mt-2 v-if="stationState.stations.length > 0">
<div mt-2 v-if="站点State.stations.length > 0">
<span class="mr-2">选择要显示的站点:</span>
<a-checkbox-group v-model:value="selectedStationIdsArray" :options="stationCheckboxOptions" />
</div>
@ -172,7 +175,7 @@ const selectedStationIdsArray = computed({
</div>
<div class="flex-grow w-full rounded-lg border overflow-hidden">
<!-- 将地面站和卫星状态都传递给组件 -->
<h-cesium-viewer :station-state="stationState" :satellite-state="satelliteState">
<h-cesium-viewer :station-state="站点State" :satellite-state="satelliteState">
<div class="absolute top-0 left-0 z-10 p-2 bg-black/30 rounded-br-lg">
<span class="text-white text-xs">叠加 UI 示例</span>
</div>