From abe03155368db42095fbaca0dfc8e83a3f440d08 Mon Sep 17 00:00:00 2001 From: yitaikarma Date: Mon, 20 Apr 2026 20:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=A1=8C=E4=B8=BA=E6=A0=91?= =?UTF-8?q?=E6=8C=82=E8=BD=BD=E5=88=B0=E5=B9=B3=E5=8F=B0=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=8F=91=E9=80=81=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E5=8F=98=E9=87=8F=E6=9C=AA=E6=AD=A3=E7=A1=AE=E6=B8=B2?= =?UTF-8?q?=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Copilot --- .../src/views/decision/communication/api.ts | 6 +- .../decision/communication/communication.vue | 14 +++- .../src/views/decision/communication/node.vue | 26 ++++---- .../src/views/decision/designer/designer.vue | 64 ++++++++++--------- .../views/decision/graph/data-converter.ts | 2 + modeler/src/views/decision/types/platform.ts | 1 + 6 files changed, 65 insertions(+), 48 deletions(-) diff --git a/modeler/src/views/decision/communication/api.ts b/modeler/src/views/decision/communication/api.ts index 881dd56..8887f2d 100644 --- a/modeler/src/views/decision/communication/api.ts +++ b/modeler/src/views/decision/communication/api.ts @@ -51,7 +51,11 @@ export const getAllBehaviorTreesBySceneId = (sceneId: number): Promise<{ code: n return req.get<{ code: number; msg: string; data: BehaviorTree[] }>(`/system/scene/getAllTree/${sceneId}`); }; -// 更新行为树(挂载到平台) +// 更新平台的行为树id +export const updateBehaviorTreeIdOfPlatform = (query: { id: number, behaviortreeId: number}): Promise => { + return req.putJson(`/system/behaviortree/behaviortreeId`, query); +}; +// 更新行为树的平台id export const updateBehaviorTree = (behaviorTree: BehaviorTree): Promise => { return req.putJson(`/system/behaviortree`, behaviorTree); }; \ No newline at end of file diff --git a/modeler/src/views/decision/communication/communication.vue b/modeler/src/views/decision/communication/communication.vue index 63b14a8..55f1761 100644 --- a/modeler/src/views/decision/communication/communication.vue +++ b/modeler/src/views/decision/communication/communication.vue @@ -76,6 +76,8 @@ import { findOneScenarioById, saveScenario, findRelations, getAllBehaviorTreesBy import { resolveConnectionRelation } from './relation'; import { generateRandomCommunicationData } from '../graph/random-data-generator'; import { convertRecordsToGraphContainer, type CommunicationRecord } from '../graph/data-converter'; +import { log } from 'echarts/types/src/util/log.js'; +import { router } from '@/router'; const TeleportContainer = defineComponent(getTeleport()); @@ -434,13 +436,19 @@ export default defineComponent({ handleGraphEvent('node:dblclick', (args: any) => { const node = args.node as Node; const element = node.getData() as GraphTaskElement; - console.error('element',element) - window.location.href = `/app/decision/designer?scenario=${currentScenario.value?.id}&platform=${element?.platformId?? ''}` + + window.location.href = `/app/decision/designer?scenarioId=${currentScenario.value?.id}&behaviortreeId=${element?.behaviortreeId ?? ''}` // destroy() // window.location.href = '/app/decision/designer' + + // 通过router跳转在初始化节点上存在渲染时机的问题,导致节点未渲染 // router.push({ - // path: '/app/decision/designer' + // path: '/app/decision/designer', + // query: { + // scenarioId: currentScenario.value?.id?.toString() ?? '', + // behaviortreeId: element?.behaviortreeId?.toString() ?? '', + // }, // }) }); diff --git a/modeler/src/views/decision/communication/node.vue b/modeler/src/views/decision/communication/node.vue index aedd136..e7af43e 100644 --- a/modeler/src/views/decision/communication/node.vue +++ b/modeler/src/views/decision/communication/node.vue @@ -101,7 +101,7 @@ import { DeleteOutlined, LinkOutlined, CheckOutlined, SettingOutlined } from '@a import type { Graph } from '@antv/x6'; import { message } from 'ant-design-vue'; import { substring } from '@/utils/strings'; -import { updateBehaviorTree } from './api'; +import { updateBehaviorTree, updateBehaviorTreeIdOfPlatform } from './api'; import type { BehaviorTree } from '../designer/tree'; export default defineComponent({ @@ -156,7 +156,7 @@ export default defineComponent({ // 判断行为树是否已挂载到当前节点 const isTreeMounted = (treeId: number): boolean => { if (!element.value) return false; - const currentTreeId = (element.value as any).behaviorTreeId as number | undefined; + const currentTreeId = (element.value as any).behaviortreeId as number | undefined; return currentTreeId === treeId; }; @@ -188,25 +188,23 @@ export default defineComponent({ if (!element.value) return; try { - // 更新节点的behaviorTreeId属性 - const updatedElement = { ...(element.value as any), behaviorTreeId: tree.id }; + // 更新节点的behaviortreeId属性 + const updatedElement = { ...(element.value as any), behaviortreeId: tree.id }; - // 调用后端API更新行为树(将platformId关联到该平台) - const platformIdValue = (element.value as any).platformId as number | undefined; - const treeToUpdate = { - ...tree, - platformId: platformIdValue ?? null - }; - - const updateResponse = await updateBehaviorTree(treeToUpdate); - if (updateResponse.code === 200) { + // 调用后端API:同时更新平台表的 behaviortreeId 和行为树表的 platformId + const platformIdValue = (element.value as any).platformId as number; + const [platformRes, treeRes] = await Promise.all([ + updateBehaviorTreeIdOfPlatform({ id: platformIdValue, behaviortreeId: tree.id }), + updateBehaviorTree({ ...tree, platformId: platformIdValue }), + ]); + if (platformRes.code === 200 && treeRes.code === 200) { // 更新本地节点数据 if (_props.node) { _props.node.setData(updatedElement); } message.success(`已成功挂载行为树: ${tree.name}`); } else { - message.error(updateResponse.msg || '挂载失败'); + message.error(platformRes.msg || treeRes.msg || '挂载失败'); } } catch (error) { console.error('挂载行为树失败:', error); diff --git a/modeler/src/views/decision/designer/designer.vue b/modeler/src/views/decision/designer/designer.vue index 05fd343..550663f 100644 --- a/modeler/src/views/decision/designer/designer.vue +++ b/modeler/src/views/decision/designer/designer.vue @@ -295,27 +295,29 @@ export default defineComponent({ const createElements = () => { clearGraph(); nextTick(() => { - if (currentBehaviorTree.value?.graph && graph.value) { - if (currentBehaviorTree.value?.graph.nodes) { - currentBehaviorTree.value?.graph.nodes.forEach(ele => { - const node = createGraphTaskElement(ele as GraphTaskElement); - // 将节点添加到画布 - graph.value?.addNode(node as Node); - }); - } - setTimeout(() => { - // 然后添加所有边,确保包含桩点信息 - currentBehaviorTree.value?.graph.edges.forEach(edgeData => { - graph.value?.addEdge({ - ...edgeData, - ...createLineOptions(), + setTimeout(() => { + if (currentBehaviorTree.value?.graph && graph.value) { + if (currentBehaviorTree.value?.graph.nodes) { + currentBehaviorTree.value?.graph.nodes.forEach(ele => { + const node = createGraphTaskElement(ele as GraphTaskElement); + // 将节点添加到画布 + graph.value?.addNode(node as Node); + }); + } + setTimeout(() => { + // 然后添加所有边,确保包含桩点信息 + currentBehaviorTree.value?.graph.edges.forEach(edgeData => { + graph.value?.addEdge({ + ...edgeData, + ...createLineOptions(), + }); }); }); - }); - fitToScreen(); - - console.info('create elements: ', currentBehaviorTree.value?.graph); - } + fitToScreen(); + + console.info('create elements: ', currentBehaviorTree.value?.graph); + } + }); }); }; @@ -334,7 +336,6 @@ export default defineComponent({ }; const loadTargetTree = (tree: BehaviorTree) => { - currentTreeId.value = tree.id; resolveBehaviorTreeGraph(tree.id, tree.xmlContent).then(nodeGraph => { applyBehaviorTree({ ...tree, @@ -363,6 +364,7 @@ export default defineComponent({ console.info('handleSelectTree', tree); findOneTreeById(tree.id).then(r => { if (r.data) { + currentTreeId.value = tree.id; loadTargetTree(r.data); } else { message.error(r.msg ?? '行为树不存在.'); @@ -372,11 +374,11 @@ export default defineComponent({ const resolveQuery = ()=> { console.log(currentRoute); - if (!currentRoute.query.scenario) { + if (!currentRoute.query.scenarioId) { return } - let scenarioId = Number(currentRoute.query.scenario); + let scenarioId = Number(currentRoute.query.scenarioId); if (!isNaN(scenarioId)) { currentScenarioId.value = scenarioId; fromScenarioPage.value = true; @@ -384,11 +386,11 @@ export default defineComponent({ fromScenarioPage.value = false; } - let platformId = Number(currentRoute.query.platform); - if (!isNaN(platformId)) { - currentPlatformId.value = platformId; + let behaviortreeId = Number(currentRoute.query.behaviortreeId); + if (!isNaN(behaviortreeId)) { + currentTreeId.value = behaviortreeId; } else { - currentPlatformId.value = null; + currentTreeId.value = null; } } @@ -432,7 +434,6 @@ export default defineComponent({ handleGraphEvent('node:click', (args: any) => { const node = args.node as Node; const newElement = node.getData() as GraphTaskElement; - console.error('ddd', args); selectedModelNode.value = node; selectedNodeTaskElement.value = JSON.parse(JSON.stringify(newElement || {})) as GraphTaskElement; @@ -458,6 +459,8 @@ export default defineComponent({ const init = () => { console.info('init'); + + resolveQuery(); nextTick(() => { initGraphConfig(); @@ -466,11 +469,12 @@ export default defineComponent({ window.addEventListener('resize', handleResize); - resolveQuery(); + - if (currentPlatformId.value) { - findOneTreeByPlatformId(currentPlatformId.value).then(r => { + if (currentTreeId.value) { + findOneTreeById(currentTreeId.value).then(r => { if (r.data) { + currentPlatformId.value = r.data.platformId ?? null; loadTargetTree(r.data); } else { handleCreateTree(); diff --git a/modeler/src/views/decision/graph/data-converter.ts b/modeler/src/views/decision/graph/data-converter.ts index 228904a..2583c9f 100644 --- a/modeler/src/views/decision/graph/data-converter.ts +++ b/modeler/src/views/decision/graph/data-converter.ts @@ -735,6 +735,7 @@ const createNodeObjects = ( type: 'scenario', name: platform, platformId: platformId, + behaviortreeId: platformData?.behaviortreeId ?? null, scenarioId: nodeScenarioId, // 修复:优先使用platformData.scenarioId components: components, template: 0, @@ -829,6 +830,7 @@ const createRootNode = ( type: 'scenario', name: rootPlatformName, platformId: platformId, + behaviortreeId: platformData?.behaviortreeId ?? null, scenarioId: rootNodeScenarioId, // 修复:优先使用platformData.scenarioId components: components, template: 0, diff --git a/modeler/src/views/decision/types/platform.ts b/modeler/src/views/decision/types/platform.ts index ab834a5..928e9bf 100644 --- a/modeler/src/views/decision/types/platform.ts +++ b/modeler/src/views/decision/types/platform.ts @@ -33,6 +33,7 @@ export interface PlatformComponent { export interface PlatformWithComponents extends Platform { components: PlatformComponent[], + behaviortreeId?: number | null, [key: string]: unknown; }