From 717832aa4fd505da885bfdc8d4aac2e8af593d44 Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Mon, 13 Apr 2026 15:48:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=20=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E9=98=B2=E7=AB=9E=E6=80=81=20+=20isModified?= =?UTF-8?q?=20=E8=AE=BE=E5=A4=87=E6=A8=A1=E5=BC=8F=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E5=9F=BA=E5=87=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/aiot/device/algorithm/index.vue | 64 ++++++++++++++++--- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/apps/web-antd/src/views/aiot/device/algorithm/index.vue b/apps/web-antd/src/views/aiot/device/algorithm/index.vue index ef4754811..654afef46 100644 --- a/apps/web-antd/src/views/aiot/device/algorithm/index.vue +++ b/apps/web-antd/src/views/aiot/device/algorithm/index.vue @@ -85,6 +85,10 @@ const saving = ref(false); const formDataMap = ref>>({}); // 记录用户修改过的字段 const dirtyFieldsMap = ref>>({}); +// 设备模式下缓存的全局默认参数: { algoCode: { paramKey: value } } +const originalGlobalParamsMap = ref>>({}); +// 防竞态:loadAlgorithms 请求版本计数器 +let loadRequestId = 0; const currentAlgo = computed(() => { return algorithms.value.find((a) => a.algoCode === activeTab.value); @@ -131,9 +135,35 @@ async function onDeviceChange() { async function loadAlgorithms() { loading.value = true; + const currentRequestId = ++loadRequestId; try { + // 设备模式下,先请求全局默认参数作为比较基准 + if (isDeviceMode.value) { + try { + const globalData = await getAlgorithmList(undefined); + if (currentRequestId !== loadRequestId) return; + if (Array.isArray(globalData)) { + for (const algo of globalData) { + try { + const gp = JSON.parse(algo.globalParams || '{}'); + const schema = JSON.parse(algo.paramSchema || '{}'); + const baseline: Record = {}; + for (const k of Object.keys(schema)) { + baseline[k] = gp[k] !== undefined ? gp[k] : schema[k]?.default; + } + originalGlobalParamsMap.value[algo.algoCode] = baseline; + } catch { /* empty */ } + } + } + } catch { /* empty */ } + } + const deviceId = selectedDeviceId.value || undefined; const data = await getAlgorithmList(deviceId); + + // 防竞态:请求期间设备已切换,丢弃本次响应 + if (currentRequestId !== loadRequestId) return; + algorithms.value = Array.isArray(data) ? data : []; if (algorithms.value.length > 0) { // 保持当前 tab,如果不存在则切到第一个 @@ -149,9 +179,13 @@ async function loadAlgorithms() { activeTab.value = ''; } } catch { - message.error('加载算法列表失败'); + if (currentRequestId === loadRequestId) { + message.error('加载算法列表失败'); + } } finally { - loading.value = false; + if (currentRequestId === loadRequestId) { + loading.value = false; + } } } @@ -194,10 +228,20 @@ function getSchemaDefault(key: string): any { return currentSchema.value[key]?.default; } +/** 获取比较基准值:设备模式用全局默认,全局模式用 schema default */ +function getBaselineValue(key: string): any { + if (isDeviceMode.value) { + const algo = currentAlgo.value; + if (algo) { + const ogp = originalGlobalParamsMap.value[algo.algoCode]; + if (ogp && ogp[key] !== undefined) return ogp[key]; + } + } + return getSchemaDefault(key); +} + function isModified(key: string): boolean { - const form = currentFormData.value; - const defaultVal = getSchemaDefault(key); - return form[key] !== defaultVal; + return currentFormData.value[key] !== getBaselineValue(key); } async function handleSave() { @@ -209,11 +253,11 @@ async function handleSave() { const form = currentFormData.value; const schema = currentSchema.value; - // 只保存与 paramSchema default 不同的字段 + // 只保存与基准值不同的字段(设备模式对比全局默认,全局模式对比 schema default) const toSave: Record = {}; for (const key of Object.keys(schema)) { - const defaultVal = schema[key]?.default; - if (form[key] !== defaultVal) { + const baselineVal = getBaselineValue(key); + if (form[key] !== baselineVal) { toSave[key] = form[key]; } } @@ -248,9 +292,9 @@ async function handleSave() { function resetToDefault(key: string) { const code = activeTab.value; - const defaultVal = getSchemaDefault(key); + const baselineVal = getBaselineValue(key); if (formDataMap.value[code]) { - formDataMap.value[code]![key] = defaultVal; + formDataMap.value[code]![key] = baselineVal; onFieldChange(key); } }