From 93311f8bd7a950253d6d71f6830c607676039b1a Mon Sep 17 00:00:00 2001 From: puhui999 Date: Thu, 7 Aug 2025 17:09:17 +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=94=B6?= =?UTF-8?q?=E6=8B=A2=E5=B8=B8=E9=87=8F=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/iot/device/device/index.ts | 12 -- .../sink/config/components/KeyValueEditor.vue | 1 - .../scene/form/configs/ConditionConfig.vue | 76 ++----- .../form/configs/MainConditionInnerConfig.vue | 37 ++-- .../scene/form/sections/ActionSection.vue | 24 +-- .../scene/form/sections/TriggerSection.vue | 21 +- .../scene/form/selectors/DeviceSelector.vue | 39 ++-- .../scene/form/selectors/PropertySelector.vue | 137 +++--------- src/views/iot/utils/constants.ts | 196 ++++++++++++++++++ 9 files changed, 282 insertions(+), 261 deletions(-) diff --git a/src/api/iot/device/device/index.ts b/src/api/iot/device/device/index.ts index 80055266f..2311a9a64 100644 --- a/src/api/iot/device/device/index.ts +++ b/src/api/iot/device/device/index.ts @@ -148,18 +148,6 @@ export const DeviceApi = { return await request.get({ url: `/iot/device/get-auth-info`, params: { id } }) }, - // 根据 ProductKey 和 DeviceNames 获取设备列表 - // TODO @puhui999:有没可能搞成基于 id 的查询哈? - getDevicesByProductKeyAndNames: async (productKey: string, deviceNames: string[]) => { - return await request.get({ - url: `/iot/device/list-by-product-key-and-names`, - params: { - productKey, - deviceNames: deviceNames.join(',') - } - }) - }, - // 查询设备消息分页 getDeviceMessagePage: async (params: any) => { return await request.get({ url: `/iot/device/message/page`, params }) diff --git a/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue b/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue index a2e543064..d0b115cdc 100644 --- a/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue +++ b/src/views/iot/rule/data/sink/config/components/KeyValueEditor.vue @@ -58,7 +58,6 @@ const updateModelValue = () => { emit('update:modelValue', result) } -// TODO @puhui999:有告警的地方,尽量用 cursor 处理下 /** 监听项目变化 */ watch(items, updateModelValue, { deep: true }) watch( diff --git a/src/views/iot/rule/scene/form/configs/ConditionConfig.vue b/src/views/iot/rule/scene/form/configs/ConditionConfig.vue index 278253dd1..41e82d5a2 100644 --- a/src/views/iot/rule/scene/form/configs/ConditionConfig.vue +++ b/src/views/iot/rule/scene/form/configs/ConditionConfig.vue @@ -13,7 +13,7 @@ class="w-full" > -
+
@@ -60,7 +63,7 @@ class="w-full" > -
+
- @@ -156,7 +161,10 @@ import ValueInput from '../inputs/ValueInput.vue' import type { TriggerCondition } from '@/api/iot/rule/scene' import { IotRuleSceneTriggerConditionTypeEnum, - IotRuleSceneTriggerConditionParameterOperatorEnum + IotRuleSceneTriggerConditionParameterOperatorEnum, + getConditionTypeOptions, + getDeviceStatusOptions, + getStatusOperatorOptions } from '@/views/iot/utils/constants' /** 单个条件配置组件 */ @@ -173,48 +181,6 @@ const emit = defineEmits<{ const condition = useVModel(props, 'modelValue', emit) -// 常量定义 -const ConditionTypeEnum = IotRuleSceneTriggerConditionTypeEnum - -// 条件类型选项 -const conditionTypeOptions = [ - { - value: IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS, - label: '设备状态' - }, - { - value: IotRuleSceneTriggerConditionTypeEnum.DEVICE_PROPERTY, - label: '设备属性' - }, - { - value: IotRuleSceneTriggerConditionTypeEnum.CURRENT_TIME, - label: '当前时间' - } -] - -// 设备状态选项 -const deviceStatusOptions = [ - { - value: 'online', - label: '在线' - }, - { - value: 'offline', - label: '离线' - } -] -// 状态操作符选项 -const statusOperatorOptions = [ - { - value: IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.value, - label: IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.name - }, - { - value: IotRuleSceneTriggerConditionParameterOperatorEnum.NOT_EQUALS.value, - label: IotRuleSceneTriggerConditionParameterOperatorEnum.NOT_EQUALS.name - } -] - // 状态 const propertyType = ref('string') const propertyConfig = ref(null) @@ -222,8 +188,8 @@ const propertyConfig = ref(null) // 计算属性:判断是否为设备相关条件 const isDeviceCondition = computed(() => { return ( - condition.value.type === ConditionTypeEnum.DEVICE_STATUS || - condition.value.type === ConditionTypeEnum.DEVICE_PROPERTY + condition.value.type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS || + condition.value.type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_PROPERTY ) }) @@ -240,7 +206,7 @@ const updateCondition = (newCondition: TriggerCondition) => { const handleConditionTypeChange = (type: number) => { // 清理不相关的字段 - if (type === ConditionTypeEnum.DEVICE_STATUS) { + if (type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS) { condition.value.identifier = undefined // 清理时间相关字段(如果存在) if ('timeValue' in condition.value) { @@ -249,11 +215,11 @@ const handleConditionTypeChange = (type: number) => { if ('timeValue2' in condition.value) { delete (condition.value as any).timeValue2 } - } else if (type === ConditionTypeEnum.CURRENT_TIME) { + } else if (type === IotRuleSceneTriggerConditionTypeEnum.CURRENT_TIME) { condition.value.identifier = undefined condition.value.productId = undefined condition.value.deviceId = undefined - } else if (type === ConditionTypeEnum.DEVICE_PROPERTY) { + } else if (type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_PROPERTY) { // 清理时间相关字段(如果存在) if ('timeValue' in condition.value) { delete (condition.value as any).timeValue diff --git a/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue b/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue index 9066a0064..780696d62 100644 --- a/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue +++ b/src/views/iot/rule/scene/form/configs/MainConditionInnerConfig.vue @@ -157,8 +157,12 @@ placeholder="请选择操作符" class="w-full" > - - + @@ -168,7 +172,7 @@

- 当前触发事件类型:{{ getTriggerTypeText(triggerType) }} + 当前触发事件类型:{{ getTriggerTypeLabel(triggerType) }}

此触发类型暂不需要配置额外条件 @@ -186,7 +190,12 @@ import ValueInput from '../inputs/ValueInput.vue' import JsonParamsInput from '../inputs/JsonParamsInput.vue' import type { Trigger } from '@/api/iot/rule/scene' -import { IotRuleSceneTriggerTypeEnum, getTriggerTypeOptions } from '@/views/iot/utils/constants' +import { + IotRuleSceneTriggerTypeEnum, + getTriggerTypeOptions, + getTriggerTypeLabel, + getDeviceStatusChangeOptions +} from '@/views/iot/utils/constants' import { useVModel } from '@vueuse/core' /** 主条件内部配置组件 */ @@ -249,25 +258,9 @@ const eventConfig = computed(() => { return undefined }) -// 获取触发类型文本 -// TODO @puhui999:是不是有枚举可以服用哈; -const getTriggerTypeText = (type: number) => { - switch (type) { - case IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST: - return '设备属性上报' - case IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST: - return '设备事件上报' - case IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE: - return '设备服务调用' - case IotRuleSceneTriggerTypeEnum.DEVICE_STATE_UPDATE: - return '设备状态变化' - default: - return '未知类型' - } -} - -// 触发器类型选项 +// 使用标准化的选项获取函数 const triggerTypeOptions = getTriggerTypeOptions() +const deviceStatusChangeOptions = getDeviceStatusChangeOptions() // 事件处理 const updateConditionField = (field: keyof Trigger, value: any) => { diff --git a/src/views/iot/rule/scene/form/sections/ActionSection.vue b/src/views/iot/rule/scene/form/sections/ActionSection.vue index 23fe572b6..7bc6597d0 100644 --- a/src/views/iot/rule/scene/form/sections/ActionSection.vue +++ b/src/views/iot/rule/scene/form/sections/ActionSection.vue @@ -45,7 +45,7 @@ 执行器 {{ index + 1 }} - {{ getActionTypeName(action.type) }} + {{ getActionTypeLabel(action.type) }}

@@ -139,7 +139,9 @@ import { isDeviceAction, isAlertAction, getActionTypeLabel, - getActionTypeOptions + getActionTypeOptions, + getActionTypeTag, + SCENE_RULE_CONFIG } from '@/views/iot/utils/constants' /** 执行器配置组件 */ @@ -169,22 +171,8 @@ const createDefaultActionData = (): Action => { } } -const maxActions = 5 // 最大执行器数量 - -// 工具函数 -const getActionTypeName = (type: number) => { - return getActionTypeLabel(type) -} - -const getActionTypeTag = (type: number) => { - const actionTypeTags = { - [ActionTypeEnum.DEVICE_PROPERTY_SET]: 'primary', - [ActionTypeEnum.DEVICE_SERVICE_INVOKE]: 'success', - [ActionTypeEnum.ALERT_TRIGGER]: 'danger', - [ActionTypeEnum.ALERT_RECOVER]: 'warning' - } - return actionTypeTags[type] || 'info' -} +// 使用标准化的常量和函数 +const maxActions = SCENE_RULE_CONFIG.MAX_ACTIONS /** 添加执行器 */ const addAction = () => { diff --git a/src/views/iot/rule/scene/form/sections/TriggerSection.vue b/src/views/iot/rule/scene/form/sections/TriggerSection.vue index fb45b670f..6f1510064 100644 --- a/src/views/iot/rule/scene/form/sections/TriggerSection.vue +++ b/src/views/iot/rule/scene/form/sections/TriggerSection.vue @@ -118,9 +118,9 @@ import DeviceTriggerConfig from '../configs/DeviceTriggerConfig.vue' import { Crontab } from '@/components/Crontab' import type { Trigger } from '@/api/iot/rule/scene' import { - getTriggerTypeOptions, + getTriggerTypeLabel, + getTriggerTagType, IotRuleSceneTriggerTypeEnum as TriggerTypeEnum, - IotRuleSceneTriggerTypeEnum, isDeviceTrigger } from '@/views/iot/utils/constants' @@ -137,23 +137,6 @@ const emit = defineEmits<{ const triggers = useVModel(props, 'triggers', emit) -// 触发器类型选项(从 constants 中获取) -const triggerTypeOptions = getTriggerTypeOptions() - -// 工具函数 -// TODO @puhui999:这里是不是重复了哈; -const getTriggerTypeLabel = (type: number): string => { - const option = triggerTypeOptions.find((opt) => opt.value === type) - return option?.label || '未知类型' -} - -const getTriggerTagType = (type: number): string => { - if (type === IotRuleSceneTriggerTypeEnum.TIMER) { - return 'warning' - } - return isDeviceTrigger(type) ? 'success' : 'info' -} - // 事件处理函数 const addTrigger = () => { const newTrigger: Trigger = { diff --git a/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue b/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue index 28e7e689a..b24452baa 100644 --- a/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue +++ b/src/views/iot/rule/scene/form/selectors/DeviceSelector.vue @@ -24,11 +24,11 @@
{{ device.deviceKey }}
- - {{ getStatusText(device.status) }} + + {{ getDeviceEnableStatusText(device.status) }} - - {{ device.activeTime ? '已激活' : '未激活' }} + + {{ getDeviceActiveStatus(device.activeTime).text }}
@@ -38,6 +38,12 @@