From 9f3eb14a0f72696d3e3db2dde9c593ed84ee4bac Mon Sep 17 00:00:00 2001 From: puhui999 Date: Tue, 5 Aug 2025 22:12:19 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E3=80=90IoT=20=E7=89=A9=E8=81=94?= =?UTF-8?q?=E7=BD=91=E3=80=91=E5=9C=BA=E6=99=AF=E8=81=94=E5=8A=A8=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=99=A8=E5=92=8C=E8=A7=A6=E5=8F=91=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=80=BC=E7=B1=BB=E5=9E=8B=E9=83=BD=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E4=B8=BA=E4=BA=86=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../form/configs/MainConditionInnerConfig.vue | 21 +--- .../scene/form/inputs/JsonParamsInput.vue | 101 +++++++----------- 2 files changed, 40 insertions(+), 82 deletions(-) diff --git a/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue b/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue index 9bdf89134..e08264155 100644 --- a/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue +++ b/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue @@ -97,7 +97,7 @@ { return undefined }) -// 确保传递给 JsonParamsInput 的值始终是字符串类型 -const conditionValueAsString = computed({ - get: () => { - const value = condition.value.value - if (value === null || value === undefined) { - return '' - } - if (typeof value === 'object') { - return JSON.stringify(value, null, 2) - } - return String(value) - }, - set: (newValue: string) => { - condition.value.value = newValue - } -}) - // 获取触发类型文本 // TODO @puhui999:是不是有枚举可以服用哈; const getTriggerTypeText = (type: number) => { diff --git a/src/views/iot/rule/scene/form/inputs/JsonParamsInput.vue b/src/views/iot/rule/scene/form/inputs/JsonParamsInput.vue index ac94cb096..b7001f6a9 100644 --- a/src/views/iot/rule/scene/form/inputs/JsonParamsInput.vue +++ b/src/views/iot/rule/scene/form/inputs/JsonParamsInput.vue @@ -189,12 +189,15 @@ const jsonError = ref('') // 计算属性 const hasConfig = computed(() => { - return !!( - props.config?.service || - props.config?.event || - props.config?.properties || - props.config?.custom - ) + // TODO @puhui999: 后续统一处理 + console.log(props.config) + // return !!( + // props.config?.service || + // props.config?.event || + // props.config?.properties || + // props.config?.custom + // ) + return true }) const paramsList = computed(() => { @@ -440,44 +443,26 @@ const generateExampleJson = () => { return JSON.stringify(example, null, 2) } -// 初始化标志,防止重复初始化 -const isInitialized = ref(false) - -// 初始化数据 -const initializeData = () => { - if (isInitialized.value) return - - if (localValue.value) { - try { - // modelValue 已经是字符串类型,直接使用 - if (localValue.value.trim()) { - try { - // 尝试解析JSON,如果成功则格式化 - const parsed = JSON.parse(localValue.value) - paramsJson.value = JSON.stringify(parsed, null, 2) - } catch { - // 如果不是有效的JSON,直接使用原字符串 - paramsJson.value = localValue.value - } - } else { - paramsJson.value = '' - } - - jsonError.value = '' - } catch (error) { - console.error('初始化参数失败:', error) - jsonError.value = '初始参数格式错误' - } +// 处理数据回显的函数 +const handleDataDisplay = (value: string) => { + if (!value || !value.trim()) { + paramsJson.value = '' + jsonError.value = '' + return } - isInitialized.value = true + try { + // 尝试解析JSON,如果成功则格式化 + const parsed = JSON.parse(value) + paramsJson.value = JSON.stringify(parsed, null, 2) + jsonError.value = '' + } catch { + // 如果不是有效的JSON,直接使用原字符串 + paramsJson.value = value + jsonError.value = '' + } } -// 组件挂载时初始化 -onMounted(() => { - initializeData() -}) - // 监听外部值变化(编辑模式数据回显) watch( () => localValue.value, @@ -485,33 +470,23 @@ watch( // 避免循环更新 if (newValue === oldValue) return - try { - let newJsonString = '' - - if (newValue && newValue.trim()) { - try { - // 尝试解析JSON,如果成功则格式化 - const parsed = JSON.parse(newValue) - newJsonString = JSON.stringify(parsed, null, 2) - } catch { - // 如果不是有效的JSON,直接使用原字符串 - newJsonString = newValue - } - } - - // 只有当JSON字符串真正改变时才更新 - if (newJsonString !== paramsJson.value) { - paramsJson.value = newJsonString - jsonError.value = '' - } - } catch (error) { - console.error('数据回显失败:', error) - jsonError.value = '数据格式错误' - } + // 使用 nextTick 确保在下一个 tick 中处理数据 + nextTick(() => { + handleDataDisplay(newValue || '') + }) }, { immediate: true } ) +// 组件挂载后也尝试处理一次数据回显 +onMounted(() => { + nextTick(() => { + if (localValue.value) { + handleDataDisplay(localValue.value) + } + }) +}) + // 监听配置变化 watch( () => props.config,