From 9f1c3eb660b5770a483577b3fbf1d802574b0872 Mon Sep 17 00:00:00 2001 From: zxr <271055687@qq.com> Date: Sun, 5 Jul 2026 17:48:49 +0800 Subject: [PATCH] feat: add snmp oid editor state helpers --- .../device-collect/components/FormDialog.vue | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) 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 389ea3f..e191900 100644 --- a/src/views/ops/pages/dc/device-collect/components/FormDialog.vue +++ b/src/views/ops/pages/dc/device-collect/components/FormDialog.vue @@ -211,6 +211,18 @@ const confirmLoading = ref(false) const policyOptions = ref([]) const roomOptions = ref([]) +type SnmpOidEditMode = 'table' | 'json' + +interface SnmpOidRow { + oid: string + metric_name: string + metric_unit: string + type: string +} + +const snmpOidEditMode = ref('table') +const snmpOidRows = ref([]) + const isEdit = computed(() => !!props.record?.id) const formData = reactive({ @@ -246,6 +258,53 @@ const rules = { device_category: [{ required: true, message: '请选择设备分类' }], } +const createEmptySnmpOidRow = (): SnmpOidRow => ({ + oid: '', + metric_name: '', + metric_unit: '', + type: '', +}) + +const normalizeSnmpOidRows = (rows: SnmpOidRow[]) => + rows + .map((row) => ({ + oid: row.oid.trim(), + metric_name: row.metric_name.trim(), + metric_unit: row.metric_unit.trim(), + type: row.type.trim(), + })) + .filter((row) => row.oid || row.metric_name || row.metric_unit || row.type) + +const stringifySnmpOidRows = (rows: SnmpOidRow[]) => { + const normalizedRows = normalizeSnmpOidRows(rows) + return normalizedRows.length ? JSON.stringify(normalizedRows, null, 2) : '' +} + +const parseSnmpOidRows = (raw: string): SnmpOidRow[] => { + const text = raw.trim() + if (!text) { + return [] + } + const parsed = JSON.parse(text) + if (!Array.isArray(parsed)) { + throw new Error('SNMP OID 配置必须是 JSON 数组') + } + return parsed.map((item) => ({ + oid: String(item?.oid ?? ''), + metric_name: String(item?.metric_name ?? ''), + metric_unit: String(item?.metric_unit ?? ''), + type: String(item?.type ?? ''), + })) +} + +const syncSnmpOidRowsFromJson = () => { + snmpOidRows.value = parseSnmpOidRows(formData.snmp_oids || '') +} + +const syncSnmpOidJsonFromRows = () => { + formData.snmp_oids = stringifySnmpOidRows(snmpOidRows.value) +} + const loadPolicyOptions = async () => { try { const response: any = await fetchPolicyOptions({ enabled: true })