Files
auto-solution/modeler/src/views/decision/rule/PlatformSelect.vue

151 lines
4.2 KiB
Vue
Raw Normal View History

2026-03-16 22:43:12 +08:00
<template>
<a-space>
<!-- 平台选择框 -->
<a-select
style="width:280px"
2026-03-17 10:06:55 +08:00
v-model:value="innerPlatformId"
placeholder="请选择平台"
:disabled="loading"
2026-03-16 22:43:12 +08:00
>
<a-select-option
v-for="item in platforms"
2026-03-17 10:06:55 +08:00
:key="`platform-${item.id}`"
2026-03-16 22:43:12 +08:00
:value="item.id"
>
{{ item.description || item.name || '未命名平台' }}
</a-select-option>
</a-select>
<!-- 组件选择框 -->
<a-select
style="width:280px;"
2026-03-17 10:06:55 +08:00
v-model:value="innerComponentId"
placeholder="请选择组件"
:disabled="!innerPlatformId || loading"
2026-03-16 22:43:12 +08:00
>
<a-select-option
2026-03-17 00:36:16 +08:00
v-for="item in selectedPlatformComponents"
2026-03-17 10:06:55 +08:00
:key="`component-${item.id}`"
2026-03-16 22:43:12 +08:00
:value="item.id"
>
{{ item.description || item.name || '未命名组件' }}
</a-select-option>
</a-select>
</a-space>
</template>
<script lang="ts">
import { computed, defineComponent, onMounted, type PropType, ref, watch } from 'vue';
2026-03-17 10:06:55 +08:00
import type { Platform, PlatformComponent, PlatformComponentPayload, PlatformWithComponents, PlatformWithComponentsResponse } from '../types';
2026-03-16 22:43:12 +08:00
import { findAllPlatformWithComponents } from './api';
export default defineComponent({
2026-03-17 10:06:55 +08:00
name: 'PlatformSelect',
2026-03-16 22:43:12 +08:00
props: {
2026-03-17 10:06:55 +08:00
platformId: {
type: [Number, null] as PropType<number | null>,
required: false,
default: null,
2026-03-16 22:43:12 +08:00
},
2026-03-17 10:06:55 +08:00
componentId: {
type: [Number, null] as PropType<number | null>,
2026-03-16 22:43:12 +08:00
required: false,
2026-03-17 10:06:55 +08:00
default: null,
2026-03-16 22:43:12 +08:00
},
},
2026-03-17 10:06:55 +08:00
emits: {
change: (payload: PlatformComponentPayload) => true,
},
2026-03-16 22:43:12 +08:00
setup(props, { emit }) {
2026-03-17 10:06:55 +08:00
const loading = ref<boolean>(true);
2026-03-16 22:43:12 +08:00
const platforms = ref<PlatformWithComponents[]>([]);
2026-03-17 10:06:55 +08:00
const platformMap = ref<Map<number, Platform>>(new Map());
const componentMap = ref<Map<number, PlatformComponent>>(new Map());
const innerPlatformId = ref<number | null>(null);
const innerComponentId = ref<number | null>(null);
const selectedPlatformComponents = computed(() => {
const platform = platforms.value.find(p => p.id === innerPlatformId.value);
return platform?.components || [];
});
const loadData = async () => {
try {
loading.value = true;
const res: PlatformWithComponentsResponse = await findAllPlatformWithComponents();
const data = res.data || [];
platforms.value = data;
platformMap.value.clear();
componentMap.value.clear();
data.forEach(platform => {
platformMap.value.set(platform.id, platform);
platform.components?.forEach(component => {
componentMap.value.set(component.id, component);
});
2026-03-17 00:36:16 +08:00
});
2026-03-17 10:06:55 +08:00
innerPlatformId.value = props.platformId;
innerComponentId.value = props.componentId;
} catch (err) {
console.error('加载平台组件失败:', err);
} finally {
loading.value = false;
2026-03-17 00:36:16 +08:00
}
2026-03-17 10:06:55 +08:00
};
2026-03-17 00:36:16 +08:00
2026-03-17 10:06:55 +08:00
const emitChange = () => {
// 根据ID获取完整对象
const platform = platformMap.value.get(innerPlatformId.value || -1) || null;
const component = componentMap.value.get(innerComponentId.value || -1) || null;
2026-03-17 00:36:16 +08:00
2026-03-17 10:06:55 +08:00
const validComponent = component && component.platformId === innerPlatformId.value
? component
: null;
2026-03-17 00:36:16 +08:00
2026-03-17 10:06:55 +08:00
emit('change', {
platform: platform ? {
id: platform.id,
name: platform.name,
description: platform.description,
scenarioId: platform.scenarioId,
} : null,
component: validComponent,
} as PlatformComponentPayload);
};
2026-03-17 00:36:16 +08:00
2026-03-17 10:06:55 +08:00
watch([() => props.platformId, () => props.componentId],
([newPlatformId, newComponentId]) => {
if (!loading.value) {
innerPlatformId.value = newPlatformId;
innerComponentId.value = newComponentId;
}
},
{ immediate: true, deep: false },
);
watch(innerPlatformId, (newVal) => {
if (newVal !== innerPlatformId.value) {
innerComponentId.value = null;
2026-03-17 00:36:16 +08:00
}
2026-03-17 10:06:55 +08:00
emitChange();
});
2026-03-17 00:36:16 +08:00
2026-03-17 10:06:55 +08:00
watch(innerComponentId, emitChange);
2026-03-16 22:43:12 +08:00
2026-03-17 10:06:55 +08:00
onMounted(() => {
loadData();
});
2026-03-16 22:43:12 +08:00
return {
2026-03-17 10:06:55 +08:00
loading,
2026-03-16 22:43:12 +08:00
platforms,
2026-03-17 00:36:16 +08:00
selectedPlatformComponents,
2026-03-17 10:06:55 +08:00
innerPlatformId,
innerComponentId,
2026-03-16 22:43:12 +08:00
};
2026-03-17 10:06:55 +08:00
}
2026-03-16 22:43:12 +08:00
});
</script>