fix(flow): 优化机器人设备绑定逻辑并改进状态提示

- 保留原始设备ID和选项作为默认值,避免数据丢失
- 当机器人无匹配在线设备时显示警告并保留原有设置
- 改进设备选择逻辑,支持设备种类过滤和自动填充
- 添加未触动手动设备节点统计功能
- 更新绑定面板状态显示,区分缺失设备和手动设备
- 优化错误处理机制,失败时恢复原始参数值
- 完善设备匹配成功和失败的消息提示内容
This commit is contained in:
lixiaolong 2026-08-05 10:46:11 +08:00
parent 65d219062e
commit b2593bf857
2 changed files with 57 additions and 29 deletions

View File

@ -19,7 +19,8 @@
<div class="binding-panel__text" v-if="bindingLoading">正在读取机器人在线设备...</div>
<div class="binding-panel__text" v-else-if="bindingSummary.robotId">
已匹配 {{ bindingSummary.matched }} 个设备节点
<span v-if="bindingSummary.missing">{{ bindingSummary.missing }} 个节点暂无匹配设备</span>
<span v-if="bindingSummary.missing">{{ bindingSummary.missing }} 个节点未匹配并保留原值</span>
<span v-if="bindingSummary.untouched">{{ bindingSummary.untouched }} 个手动设备节点保持不变</span>
</div>
<div class="binding-panel__text" v-else>选择机器人后将按节点类型自动填充该机器人上报的在线设备</div>
</div>
@ -90,7 +91,8 @@ const ruleFormRef = ref()
const rules = ref({});
const tableData = ref([])
const bindingLoading = ref(false)
const bindingSummary = ref({ robotId: '', matched: 0, missing: 0 })
const emptyBindingSummary = (robotId = '') => ({ robotId, matched: 0, missing: 0, untouched: 0 })
const bindingSummary = ref(emptyBindingSummary())
const emits = defineEmits(['close', 'changeState'])
@ -104,7 +106,7 @@ watch(() => props.drawer,
if (newVal) {
const data = getStartNodeFormData()
tableData.value = data
bindingSummary.value = { robotId: '', matched: 0, missing: 0 }
bindingSummary.value = emptyBindingSummary()
loadRobotOptions()
}
}
@ -142,7 +144,7 @@ const confirm = () => {
const robotIdOptions = ref([])
const handleRobotChange = async (robotId) => {
bindingSummary.value = { robotId: robotId || '', matched: 0, missing: 0 }
bindingSummary.value = emptyBindingSummary(robotId || '')
if (!robotId) return
bindingLoading.value = true
try {
@ -150,6 +152,7 @@ const handleRobotChange = async (robotId) => {
const devices = Array.isArray(response.data) ? response.data : []
let matched = 0
let missing = 0
let untouched = 0
const missingNodes = []
const { nodes } = lf.getGraphData()
@ -166,30 +169,37 @@ const handleRobotChange = async (robotId) => {
}
const kind = getDeviceKindForAction(node.properties.action)
const candidates = kind
? devices.filter(device => Number(device.deviceKind) === kind)
: devices
deviceParam.input = candidates[0]?.deviceId || ''
if (!kind) {
untouched += 1
if (robotParam) lf.setProperties(node.id, { ...node.properties, nodeParams })
return
}
const candidates = devices.filter(device => Number(device.deviceKind) === kind)
if (!candidates.length) {
missing += 1
missingNodes.push(node.properties.name || node.id)
if (robotParam) lf.setProperties(node.id, { ...node.properties, nodeParams })
return
}
const currentCandidate = candidates.find(device => device.deviceId === deviceParam.input)
deviceParam.input = (currentCandidate || candidates[0]).deviceId
deviceParam.componentType = 'select'
deviceParam.selectOptions = toDeviceOptions(candidates)
deviceParam.deviceLocked = candidates.length === 1
lf.setProperties(node.id, { ...node.properties, nodeParams })
if (candidates.length > 0) matched += 1
else {
missing += 1
missingNodes.push(node.properties.name || node.id)
}
matched += 1
})
bindingSummary.value = { robotId, matched, missing }
bindingSummary.value = { robotId, matched, missing, untouched }
if (missingNodes.length) {
ElMessage.warning(`以下节点没有匹配的在线设备${missingNodes.join('、')}`)
ElMessage.warning(`以下节点没有匹配的在线设备,已保留原值:${missingNodes.join('、')}`)
} else if (matched > 0) {
ElMessage.success(`已自动匹配 ${matched} 个设备节点`)
}
} catch (error) {
bindingSummary.value = { robotId, matched: 0, missing: 0 }
bindingSummary.value = emptyBindingSummary(robotId)
console.error('自动匹配机器人设备失败:', error)
ElMessage.error('读取机器人设备失败')
} finally {

View File

@ -913,29 +913,47 @@ const singleNodeDeviceId = computed(() =>
const handleSingleNodeRobotChange = async (robotId) => {
const deviceParam = formData.nodeParams.find((item) => item.name === "deviceId");
if (!deviceParam) return;
deviceParam.input = "";
deviceParam.deviceLocked = false;
deviceParam.componentType = "select";
deviceParam.selectOptions = [];
const originalDeviceId = deviceParam.input;
const originalOptions = [...(deviceParam.selectOptions || [])];
const originalLocked = Boolean(deviceParam.deviceLocked);
const stationParam = formData.nodeParams.find((item) => item.name === "stationId");
if (stationParam) {
stationParam.input = "";
stationParam.selectOptions = [];
}
if (!robotId) return;
try {
const deviceKind = getDeviceKindForAction(nodeAction.value);
const response = await getRobotDevicesByRobotId(robotId,
deviceKind ? { deviceKind } : {});
const devices = Array.isArray(response.data) ? response.data : [];
if (!devices.length) {
deviceParam.input = originalDeviceId;
deviceParam.selectOptions = originalOptions;
deviceParam.deviceLocked = originalLocked;
ElMessage.warning("当前机器人没有匹配的在线设备,已保留原设备");
return;
}
if (stationParam) {
stationParam.input = "";
stationParam.selectOptions = [];
}
deviceParam.componentType = "select";
deviceParam.selectOptions = toDeviceOptions(devices);
if (devices.length > 0) {
deviceParam.input = devices[0].deviceId;
if (deviceKind) {
const currentDevice = devices.find((device) => device.deviceId === originalDeviceId);
deviceParam.input = (currentDevice || devices[0]).deviceId;
deviceParam.deviceLocked = devices.length === 1;
} else {
ElMessage.warning("当前机器人没有匹配的在线设备");
return;
}
const originalStillAvailable = devices.some((device) => device.deviceId === originalDeviceId);
deviceParam.input = originalDeviceId || "";
deviceParam.deviceLocked = devices.length === 1 && originalStillAvailable;
if (originalDeviceId && !originalStillAvailable) {
ElMessage.warning("已保留通用指令原设备;该设备未出现在当前机器人的在线列表中");
}
} catch (error) {
deviceParam.input = originalDeviceId;
deviceParam.selectOptions = originalOptions;
deviceParam.deviceLocked = originalLocked;
console.error("加载机器人设备失败:", error);
ElMessage.error("加载机器人设备失败");
}