diff --git a/src/views/ops/pages/dc/device-collect/components/FormDialog.vue b/src/views/ops/pages/dc/device-collect/components/FormDialog.vue index 5e5fd7a..0c943c0 100644 --- a/src/views/ops/pages/dc/device-collect/components/FormDialog.vue +++ b/src/views/ops/pages/dc/device-collect/components/FormDialog.vue @@ -489,6 +489,40 @@ const switchSnmpOidEditMode = () => { } } +const validateSnmpOidConfigBeforeSubmit = () => { + if (snmpOidEditMode.value === 'table') { + const rows = normalizeSnmpOidRows(snmpOidRows.value) + const invalidIndex = rows.findIndex((row) => !row.oid || !row.metric_name) + if (invalidIndex >= 0) { + Message.warning(`SNMP OID 第 ${invalidIndex + 1} 行请填写 OID 和指标名称`) + return false + } + formData.snmp_oids = rows.length ? JSON.stringify(rows, null, 2) : '' + return true + } + + if (!formData.snmp_oids?.trim()) { + snmpOidRows.value = [] + return true + } + + try { + const rows = parseSnmpOidRows(formData.snmp_oids) + const normalizedRows = normalizeSnmpOidRows(rows) + const invalidIndex = normalizedRows.findIndex((row) => !row.oid || !row.metric_name) + if (invalidIndex >= 0) { + Message.warning(`SNMP OID 第 ${invalidIndex + 1} 行请填写 OID 和指标名称`) + return false + } + snmpOidRows.value = normalizedRows + formData.snmp_oids = normalizedRows.length ? JSON.stringify(normalizedRows, null, 2) : '' + return true + } catch { + Message.warning('SNMP OID 配置必须是合法 JSON 数组') + return false + } +} + const handleOk = async () => { try { await formRef.value?.validate() @@ -518,13 +552,8 @@ const handleOk = async () => { Message.warning('SNMP v2c 模式下请填写 community') return } - if (formData.snmp_oids?.trim()) { - try { - JSON.parse(formData.snmp_oids) - } catch { - Message.warning('SNMP OID 配置必须是合法 JSON') - return - } + if (!validateSnmpOidConfigBeforeSubmit()) { + return } }