perf: 【IoT 物联网】场景联动注释优化

This commit is contained in:
puhui999
2025-08-07 21:36:27 +08:00
parent 85ba03b0ea
commit fd85c4d682
16 changed files with 338 additions and 91 deletions

View File

@ -63,9 +63,12 @@ const emit = defineEmits<{
(e: 'success'): void (e: 'success'): void
}>() }>()
const drawerVisible = useVModel(props, 'modelValue', emit) // 是否可见 const drawerVisible = useVModel(props, 'modelValue', emit) // 抽屉显示状态
/** 创建默认的表单数据 */ /**
* 创建默认的表单数据
* @returns 默认表单数据对象
*/
const createDefaultFormData = (): IotSceneRule => { const createDefaultFormData = (): IotSceneRule => {
return { return {
name: '', name: '',
@ -87,10 +90,15 @@ const createDefaultFormData = (): IotSceneRule => {
} }
} }
// 表单数据和状态 const formRef = ref() // 表单引用
const formRef = ref() const formData = ref<IotSceneRule>(createDefaultFormData()) // 表单数据
const formData = ref<IotSceneRule>(createDefaultFormData())
// 自定义校验器 /**
* 触发器校验器
* @param _rule 校验规则(未使用)
* @param value 校验值
* @param callback 回调函数
*/
const validateTriggers = (_rule: any, value: any, callback: any) => { const validateTriggers = (_rule: any, value: any, callback: any) => {
if (!value || !Array.isArray(value) || value.length === 0) { if (!value || !Array.isArray(value) || value.length === 0) {
callback(new Error('至少需要一个触发器')) callback(new Error('至少需要一个触发器'))
@ -142,6 +150,12 @@ const validateTriggers = (_rule: any, value: any, callback: any) => {
callback() callback()
} }
/**
* 执行器校验器
* @param _rule 校验规则(未使用)
* @param value 校验值
* @param callback 回调函数
*/
const validateActions = (_rule: any, value: any, callback: any) => { const validateActions = (_rule: any, value: any, callback: any) => {
if (!value || !Array.isArray(value) || value.length === 0) { if (!value || !Array.isArray(value) || value.length === 0) {
callback(new Error('至少需要一个执行器')) callback(new Error('至少需要一个执行器'))
@ -201,6 +215,7 @@ const validateActions = (_rule: any, value: any, callback: any) => {
} }
const formRules = reactive({ const formRules = reactive({
// 表单校验规则
name: [ name: [
{ required: true, message: '场景名称不能为空', trigger: 'blur' }, { required: true, message: '场景名称不能为空', trigger: 'blur' },
{ type: 'string', min: 1, max: 50, message: '场景名称长度应在1-50个字符之间', trigger: 'blur' } { type: 'string', min: 1, max: 50, message: '场景名称长度应在1-50个字符之间', trigger: 'blur' }
@ -221,13 +236,15 @@ const formRules = reactive({
actions: [{ required: true, validator: validateActions, trigger: 'change' }] actions: [{ required: true, validator: validateActions, trigger: 'change' }]
}) })
const submitLoading = ref(false) const submitLoading = ref(false) // 提交加载状态
const isEdit = ref(false) // 是否为编辑模式
// 计算属性 // 计算属性:抽屉标题
const isEdit = ref(false)
const drawerTitle = computed(() => (isEdit.value ? '编辑场景联动规则' : '新增场景联动规则')) const drawerTitle = computed(() => (isEdit.value ? '编辑场景联动规则' : '新增场景联动规则'))
/** 提交表单 */ /**
* 提交表单
*/
const handleSubmit = async () => { const handleSubmit = async () => {
// 校验表单 // 校验表单
if (!formRef.value) return if (!formRef.value) return
@ -258,11 +275,16 @@ const handleSubmit = async () => {
} }
} }
/**
* 处理抽屉关闭事件
*/
const handleClose = () => { const handleClose = () => {
drawerVisible.value = false drawerVisible.value = false
} }
/** 初始化表单数据 */ /**
* 初始化表单数据
*/
const initFormData = () => { const initFormData = () => {
if (props.ruleScene) { if (props.ruleScene) {
// 编辑模式:数据结构已对齐,直接使用后端数据 // 编辑模式:数据结构已对齐,直接使用后端数据

View File

@ -181,9 +181,8 @@ const emit = defineEmits<{
const condition = useVModel(props, 'modelValue', emit) const condition = useVModel(props, 'modelValue', emit)
// 状态 const propertyType = ref<string>('string') // 属性类型
const propertyType = ref<string>('string') const propertyConfig = ref<any>(null) // 属性配置
const propertyConfig = ref<any>(null)
// 计算属性:判断是否为设备相关条件 // 计算属性:判断是否为设备相关条件
const isDeviceCondition = computed(() => { const isDeviceCondition = computed(() => {
@ -193,17 +192,29 @@ const isDeviceCondition = computed(() => {
) )
}) })
// 事件处理 /**
* 更新条件字段
* @param field 字段名
* @param value 字段值
*/
const updateConditionField = (field: keyof TriggerCondition, value: any) => { const updateConditionField = (field: keyof TriggerCondition, value: any) => {
;(condition.value as any)[field] = value ;(condition.value as any)[field] = value
emit('update:modelValue', condition.value) emit('update:modelValue', condition.value)
} }
/**
* 更新整个条件对象
* @param newCondition 新的条件对象
*/
const updateCondition = (newCondition: TriggerCondition) => { const updateCondition = (newCondition: TriggerCondition) => {
condition.value = newCondition condition.value = newCondition
emit('update:modelValue', condition.value) emit('update:modelValue', condition.value)
} }
/**
* 处理条件类型变化事件
* @param type 条件类型
*/
const handleConditionTypeChange = (type: number) => { const handleConditionTypeChange = (type: number) => {
// 清理不相关的字段 // 清理不相关的字段
if (type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS) { if (type === IotRuleSceneTriggerConditionTypeEnum.DEVICE_STATUS) {
@ -234,17 +245,29 @@ const handleConditionTypeChange = (type: number) => {
condition.value.param = '' condition.value.param = ''
} }
/**
* 处理产品变化事件
* @param _ 产品ID未使用
*/
const handleProductChange = (_: number) => { const handleProductChange = (_: number) => {
// 产品变化时清空设备和属性 // 产品变化时清空设备和属性
condition.value.deviceId = undefined condition.value.deviceId = undefined
condition.value.identifier = '' condition.value.identifier = ''
} }
/**
* 处理设备变化事件
* @param _ 设备ID未使用
*/
const handleDeviceChange = (_: number) => { const handleDeviceChange = (_: number) => {
// 设备变化时清空属性 // 设备变化时清空属性
condition.value.identifier = '' condition.value.identifier = ''
} }
/**
* 处理属性变化事件
* @param propertyInfo 属性信息对象
*/
const handlePropertyChange = (propertyInfo: { type: string; config: any }) => { const handlePropertyChange = (propertyInfo: { type: string; config: any }) => {
propertyType.value = propertyInfo.type propertyType.value = propertyInfo.type
propertyConfig.value = propertyInfo.config propertyConfig.value = propertyInfo.config
@ -254,6 +277,9 @@ const handlePropertyChange = (propertyInfo: { type: string; config: any }) => {
condition.value.param = '' condition.value.param = ''
} }
/**
* 处理操作符变化事件
*/
const handleOperatorChange = () => { const handleOperatorChange = () => {
// 重置值 // 重置值
condition.value.param = '' condition.value.param = ''

View File

@ -154,7 +154,7 @@ const timeOperatorOptions = [
} }
] ]
// 计算属性 // 计算属性:是否需要时间输入
const needsTimeInput = computed(() => { const needsTimeInput = computed(() => {
const timeOnlyOperators = [ const timeOnlyOperators = [
IotRuleSceneTriggerTimeOperatorEnum.BEFORE_TIME.value, IotRuleSceneTriggerTimeOperatorEnum.BEFORE_TIME.value,
@ -165,15 +165,21 @@ const needsTimeInput = computed(() => {
return timeOnlyOperators.includes(condition.value.operator) return timeOnlyOperators.includes(condition.value.operator)
}) })
// 计算属性:是否需要日期输入
const needsDateInput = computed(() => { const needsDateInput = computed(() => {
return false // 暂时不支持日期输入,只支持时间 return false // 暂时不支持日期输入,只支持时间
}) })
// 计算属性:是否需要第二个时间输入
const needsSecondTimeInput = computed(() => { const needsSecondTimeInput = computed(() => {
return condition.value.operator === IotRuleSceneTriggerTimeOperatorEnum.BETWEEN_TIME.value return condition.value.operator === IotRuleSceneTriggerTimeOperatorEnum.BETWEEN_TIME.value
}) })
// 事件处理 /**
* 更新条件字段
* @param field 字段名
* @param value 字段值
*/
const updateConditionField = (field: keyof TriggerCondition, value: any) => { const updateConditionField = (field: keyof TriggerCondition, value: any) => {
condition.value[field] = value condition.value[field] = value
} }
@ -183,7 +189,8 @@ watch(
() => condition.value.operator, () => condition.value.operator,
(newOperator) => { (newOperator) => {
if (newOperator === IotRuleSceneTriggerTimeOperatorEnum.TODAY.value) { if (newOperator === IotRuleSceneTriggerTimeOperatorEnum.TODAY.value) {
;(condition.value as any).timeValue = undefined(condition.value as any).timeValue2 = undefined ;(condition.value as any).timeValue = undefined
;(condition.value as any).timeValue2 = undefined
} else if (!needsSecondTimeInput.value) { } else if (!needsSecondTimeInput.value) {
;(condition.value as any).timeValue2 = undefined ;(condition.value as any).timeValue2 = undefined
} }

View File

@ -102,7 +102,6 @@ const emit = defineEmits<{
const action = useVModel(props, 'modelValue', emit) const action = useVModel(props, 'modelValue', emit)
// 简化后的状态变量
const thingModelProperties = ref<ThingModelProperty[]>([]) // 物模型属性列表 const thingModelProperties = ref<ThingModelProperty[]>([]) // 物模型属性列表
const loadingThingModel = ref(false) // 物模型加载状态 const loadingThingModel = ref(false) // 物模型加载状态
const selectedService = ref<ThingModelService | null>(null) // 选中的服务对象 const selectedService = ref<ThingModelService | null>(null) // 选中的服务对象
@ -125,13 +124,13 @@ const paramsValue = computed({
} }
}) })
// 计算属性:是否为属性设置类型
const isPropertySetAction = computed(() => { const isPropertySetAction = computed(() => {
// 是否为属性设置类型
return action.value.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET return action.value.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET
}) })
// 计算属性:是否为服务调用类型
const isServiceInvokeAction = computed(() => { const isServiceInvokeAction = computed(() => {
// 是否为服务调用类型
return action.value.type === IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE return action.value.type === IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE
}) })
@ -314,8 +313,7 @@ const getDefaultValueForParam = (param: any) => {
} }
} }
// 防止重复初始化的标志 const isInitialized = ref(false) // 防止重复初始化的标志
const isInitialized = ref(false)
/** /**
* 初始化组件数据 * 初始化组件数据
@ -347,7 +345,7 @@ onMounted(() => {
initializeComponent() initializeComponent()
}) })
// 监听关键字段的变化,避免深度监听导致的性能问题 // 监听关键字段的变化,避免深度监听导致的性能问题
watch( watch(
() => [action.value.productId, action.value.type, action.value.identifier], () => [action.value.productId, action.value.type, action.value.identifier],
async ([newProductId, , newIdentifier], [oldProductId, , oldIdentifier]) => { async ([newProductId, , newIdentifier], [oldProductId, , oldIdentifier]) => {

View File

@ -185,21 +185,29 @@ const emit = defineEmits<{
const trigger = useVModel(props, 'modelValue', emit) const trigger = useVModel(props, 'modelValue', emit)
// 配置常量
const maxSubGroups = 3 // 最多 3 个子条件组 const maxSubGroups = 3 // 最多 3 个子条件组
const maxConditionsPerGroup = 3 // 每组最多 3 个条件 const maxConditionsPerGroup = 3 // 每组最多 3 个条件
// 事件处理 /**
* 更新条件
* @param condition 条件对象
*/
const updateCondition = (condition: Trigger) => { const updateCondition = (condition: Trigger) => {
trigger.value = condition trigger.value = condition
} }
/**
* 处理触发器类型变化事件
* @param type 触发器类型
*/
const handleTriggerTypeChange = (type: number) => { const handleTriggerTypeChange = (type: number) => {
trigger.value.type = type trigger.value.type = type
emit('trigger-type-change', type) emit('trigger-type-change', type)
} }
// 条件组相关方法 /**
* 添加子条件组
*/
const addSubGroup = async () => { const addSubGroup = async () => {
if (!trigger.value.conditionGroups) { if (!trigger.value.conditionGroups) {
trigger.value.conditionGroups = [] trigger.value.conditionGroups = []
@ -217,18 +225,30 @@ const addSubGroup = async () => {
} }
} }
/**
* 移除子条件组
* @param index 子条件组索引
*/
const removeSubGroup = (index: number) => { const removeSubGroup = (index: number) => {
if (trigger.value.conditionGroups) { if (trigger.value.conditionGroups) {
trigger.value.conditionGroups.splice(index, 1) trigger.value.conditionGroups.splice(index, 1)
} }
} }
/**
* 更新子条件组
* @param index 子条件组索引
* @param subGroup 子条件组数据
*/
const updateSubGroup = (index: number, subGroup: any) => { const updateSubGroup = (index: number, subGroup: any) => {
if (trigger.value.conditionGroups) { if (trigger.value.conditionGroups) {
trigger.value.conditionGroups[index] = subGroup trigger.value.conditionGroups[index] = subGroup
} }
} }
/**
* 移除整个条件组
*/
const removeConditionGroup = () => { const removeConditionGroup = () => {
trigger.value.conditionGroups = undefined trigger.value.conditionGroups = undefined
} }

View File

@ -211,12 +211,11 @@ const emit = defineEmits<{
(e: 'trigger-type-change', value: number): void (e: 'trigger-type-change', value: number): void
}>() }>()
// 响应式数据
const condition = useVModel(props, 'modelValue', emit) const condition = useVModel(props, 'modelValue', emit)
const propertyType = ref('') const propertyType = ref('') // 属性类型
const propertyConfig = ref<any>(null) const propertyConfig = ref<any>(null) // 属性配置
// 计算属性 // 计算属性:是否为设备属性触发器
const isDevicePropertyTrigger = computed(() => { const isDevicePropertyTrigger = computed(() => {
return ( return (
props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST || props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST ||
@ -225,11 +224,12 @@ const isDevicePropertyTrigger = computed(() => {
) )
}) })
// 计算属性:是否为设备状态触发器
const isDeviceStatusTrigger = computed(() => { const isDeviceStatusTrigger = computed(() => {
return props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_STATE_UPDATE return props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_STATE_UPDATE
}) })
// 服务配置 - 用于 JsonParamsInput // 计算属性:服务配置 - 用于 JsonParamsInput
const serviceConfig = computed(() => { const serviceConfig = computed(() => {
if ( if (
propertyConfig.value && propertyConfig.value &&
@ -245,7 +245,7 @@ const serviceConfig = computed(() => {
return undefined return undefined
}) })
// 事件配置 - 用于 JsonParamsInput // 计算属性:事件配置 - 用于 JsonParamsInput
const eventConfig = computed(() => { const eventConfig = computed(() => {
if (propertyConfig.value && props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST) { if (propertyConfig.value && props.triggerType === IotRuleSceneTriggerTypeEnum.DEVICE_EVENT_POST) {
return { return {
@ -258,30 +258,47 @@ const eventConfig = computed(() => {
return undefined return undefined
}) })
// 使用标准化的选项获取函数 const triggerTypeOptions = getTriggerTypeOptions() // 触发器类型选项
const triggerTypeOptions = getTriggerTypeOptions() const deviceStatusChangeOptions = getDeviceStatusChangeOptions() // 设备状态变化选项
const deviceStatusChangeOptions = getDeviceStatusChangeOptions()
// 事件处理 /**
* 更新条件字段
* @param field 字段名
* @param value 字段值
*/
const updateConditionField = (field: keyof Trigger, value: any) => { const updateConditionField = (field: keyof Trigger, value: any) => {
;(condition.value as any)[field] = value ;(condition.value as any)[field] = value
} }
/**
* 处理触发器类型变化事件
* @param type 触发器类型
*/
const handleTriggerTypeChange = (type: number) => { const handleTriggerTypeChange = (type: number) => {
emit('trigger-type-change', type) emit('trigger-type-change', type)
} }
/**
* 处理产品变化事件
*/
const handleProductChange = () => { const handleProductChange = () => {
// 产品变化时清空设备和属性 // 产品变化时清空设备和属性
condition.value.deviceId = undefined condition.value.deviceId = undefined
condition.value.identifier = '' condition.value.identifier = ''
} }
/**
* 处理设备变化事件
*/
const handleDeviceChange = () => { const handleDeviceChange = () => {
// 设备变化时清空属性 // 设备变化时清空属性
condition.value.identifier = '' condition.value.identifier = ''
} }
/**
* 处理属性变化事件
* @param propertyInfo 属性信息对象
*/
const handlePropertyChange = (propertyInfo: any) => { const handlePropertyChange = (propertyInfo: any) => {
if (propertyInfo) { if (propertyInfo) {
propertyType.value = propertyInfo.type propertyType.value = propertyInfo.type
@ -297,6 +314,9 @@ const handlePropertyChange = (propertyInfo: any) => {
} }
} }
/**
* 处理操作符变化事件
*/
const handleOperatorChange = () => { const handleOperatorChange = () => {
// 操作符变化处理 // 操作符变化处理
} }

View File

@ -103,10 +103,11 @@ const emit = defineEmits<{
const subGroup = useVModel(props, 'modelValue', emit) const subGroup = useVModel(props, 'modelValue', emit)
// 配置常 const maxConditions = computed(() => props.maxConditions || 3) // 最大条件数
const maxConditions = computed(() => props.maxConditions || 3)
// 事件处理 /**
* 添加条件
*/
const addCondition = async () => { const addCondition = async () => {
// 确保 subGroup.value 是一个数组 // 确保 subGroup.value 是一个数组
if (!subGroup.value) { if (!subGroup.value) {
@ -134,12 +135,21 @@ const addCondition = async () => {
} }
} }
/**
* 移除条件
* @param index 条件索引
*/
const removeCondition = (index: number) => { const removeCondition = (index: number) => {
if (subGroup.value) { if (subGroup.value) {
subGroup.value.splice(index, 1) subGroup.value.splice(index, 1)
} }
} }
/**
* 更新条件
* @param index 条件索引
* @param condition 条件对象
*/
const updateCondition = (index: number, condition: TriggerCondition) => { const updateCondition = (index: number, condition: TriggerCondition) => {
if (subGroup.value) { if (subGroup.value) {
subGroup.value[index] = condition subGroup.value[index] = condition

View File

@ -204,11 +204,10 @@ const localValue = useVModel(props, 'modelValue', emit, {
defaultValue: '' defaultValue: ''
}) })
// 状态 const paramsJson = ref('') // JSON参数字符串
const paramsJson = ref('') const jsonError = ref('') // JSON验证错误信息
const jsonError = ref('')
// 计算属性 // 计算属性:是否有配置
const hasConfig = computed(() => { const hasConfig = computed(() => {
// TODO @puhui999: 后续统一处理 // TODO @puhui999: 后续统一处理
console.log(props.config) console.log(props.config)
@ -221,6 +220,7 @@ const hasConfig = computed(() => {
return true return true
}) })
// 计算属性:参数列表
const paramsList = computed(() => { const paramsList = computed(() => {
switch (props.type) { switch (props.type) {
case JsonParamsInputTypeEnum.SERVICE: case JsonParamsInputTypeEnum.SERVICE:
@ -236,6 +236,7 @@ const paramsList = computed(() => {
} }
}) })
// 计算属性:标题
const title = computed(() => { const title = computed(() => {
switch (props.type) { switch (props.type) {
case JsonParamsInputTypeEnum.SERVICE: case JsonParamsInputTypeEnum.SERVICE:
@ -251,6 +252,7 @@ const title = computed(() => {
} }
}) })
// 计算属性:标题图标
const titleIcon = computed(() => { const titleIcon = computed(() => {
switch (props.type) { switch (props.type) {
case JsonParamsInputTypeEnum.SERVICE: case JsonParamsInputTypeEnum.SERVICE:
@ -266,6 +268,7 @@ const titleIcon = computed(() => {
} }
}) })
// 计算属性:参数图标
const paramsIcon = computed(() => { const paramsIcon = computed(() => {
switch (props.type) { switch (props.type) {
case JsonParamsInputTypeEnum.SERVICE: case JsonParamsInputTypeEnum.SERVICE:
@ -281,6 +284,7 @@ const paramsIcon = computed(() => {
} }
}) })
// 计算属性:参数标签
const paramsLabel = computed(() => { const paramsLabel = computed(() => {
switch (props.type) { switch (props.type) {
case JsonParamsInputTypeEnum.SERVICE: case JsonParamsInputTypeEnum.SERVICE:
@ -296,6 +300,7 @@ const paramsLabel = computed(() => {
} }
}) })
// 计算属性:空状态消息
const emptyMessage = computed(() => { const emptyMessage = computed(() => {
switch (props.type) { switch (props.type) {
case JsonParamsInputTypeEnum.SERVICE: case JsonParamsInputTypeEnum.SERVICE:
@ -311,6 +316,7 @@ const emptyMessage = computed(() => {
} }
}) })
// 计算属性:无配置消息
const noConfigMessage = computed(() => { const noConfigMessage = computed(() => {
switch (props.type) { switch (props.type) {
case JsonParamsInputTypeEnum.SERVICE: case JsonParamsInputTypeEnum.SERVICE:
@ -326,7 +332,9 @@ const noConfigMessage = computed(() => {
} }
}) })
// 事件处理 /**
* 处理参数变化事件
*/
const handleParamsChange = () => { const handleParamsChange = () => {
try { try {
jsonError.value = '' // 清除之前的错误 jsonError.value = '' // 清除之前的错误
@ -361,20 +369,28 @@ const handleParamsChange = () => {
} }
} }
// 快速填充示例数据 /**
* 快速填充示例数据
*/
const fillExampleJson = () => { const fillExampleJson = () => {
paramsJson.value = generateExampleJson() paramsJson.value = generateExampleJson()
handleParamsChange() handleParamsChange()
} }
// 清空参数 /**
* 清空参数
*/
const clearParams = () => { const clearParams = () => {
paramsJson.value = '' paramsJson.value = ''
localValue.value = '' localValue.value = ''
jsonError.value = '' jsonError.value = ''
} }
// 工具函数 /**
* 获取参数类型名称
* @param dataType 数据类型
* @returns 类型名称
*/
const getParamTypeName = (dataType: string) => { const getParamTypeName = (dataType: string) => {
// 使用 constants.ts 中已有的 getDataTypeName 函数逻辑 // 使用 constants.ts 中已有的 getDataTypeName 函数逻辑
const typeMap = { const typeMap = {
@ -391,6 +407,11 @@ const getParamTypeName = (dataType: string) => {
return typeMap[dataType] || dataType return typeMap[dataType] || dataType
} }
/**
* 获取参数类型标签样式
* @param dataType 数据类型
* @returns 标签样式
*/
const getParamTypeTag = (dataType: string) => { const getParamTypeTag = (dataType: string) => {
const tagMap = { const tagMap = {
[IoTDataSpecsDataTypeEnum.INT]: 'primary', [IoTDataSpecsDataTypeEnum.INT]: 'primary',
@ -406,12 +427,21 @@ const getParamTypeTag = (dataType: string) => {
return tagMap[dataType] || 'info' return tagMap[dataType] || 'info'
} }
/**
* 获取示例值
* @param param 参数对象
* @returns 示例值
*/
const getExampleValue = (param: any) => { const getExampleValue = (param: any) => {
const exampleConfig = const exampleConfig =
JSON_PARAMS_EXAMPLE_VALUES[param.dataType] || JSON_PARAMS_EXAMPLE_VALUES.DEFAULT JSON_PARAMS_EXAMPLE_VALUES[param.dataType] || JSON_PARAMS_EXAMPLE_VALUES.DEFAULT
return exampleConfig.display return exampleConfig.display
} }
/**
* 生成示例JSON
* @returns JSON字符串
*/
const generateExampleJson = () => { const generateExampleJson = () => {
if (paramsList.value.length === 0) { if (paramsList.value.length === 0) {
return '{}' return '{}'
@ -427,7 +457,10 @@ const generateExampleJson = () => {
return JSON.stringify(example, null, 2) return JSON.stringify(example, null, 2)
} }
// 处理数据回显的函数 /**
* 处理数据回显
* @param value 值字符串
*/
const handleDataDisplay = (value: string) => { const handleDataDisplay = (value: string) => {
if (!value || !value.trim()) { if (!value || !value.trim()) {
paramsJson.value = '' paramsJson.value = ''

View File

@ -160,13 +160,12 @@ const localValue = useVModel(props, 'modelValue', emit, {
defaultValue: '' defaultValue: ''
}) })
// 状态 const rangeStart = ref('') // 范围开始值
const rangeStart = ref('') const rangeEnd = ref('') // 范围结束值
const rangeEnd = ref('') const dateValue = ref('') // 日期值
const dateValue = ref('') const numberValue = ref<number>() // 数字值
const numberValue = ref<number>()
// 计算属性 // 计算属性:枚举选项
const enumOptions = computed(() => { const enumOptions = computed(() => {
if (props.propertyConfig?.enum) { if (props.propertyConfig?.enum) {
return props.propertyConfig.enum.map((item: any) => ({ return props.propertyConfig.enum.map((item: any) => ({
@ -177,6 +176,7 @@ const enumOptions = computed(() => {
return [] return []
}) })
// 计算属性:列表预览
const listPreview = computed(() => { const listPreview = computed(() => {
if (props.operator === 'in' && localValue.value) { if (props.operator === 'in' && localValue.value) {
return localValue.value return localValue.value
@ -187,7 +187,10 @@ const listPreview = computed(() => {
return [] return []
}) })
// 工具函数 /**
* 判断是否为数字类型
* @returns 是否为数字类型
*/
const isNumericType = () => { const isNumericType = () => {
return [ return [
IoTDataSpecsDataTypeEnum.INT, IoTDataSpecsDataTypeEnum.INT,
@ -196,6 +199,10 @@ const isNumericType = () => {
].includes(props.propertyType || '') ].includes(props.propertyType || '')
} }
/**
* 获取输入框类型
* @returns 输入框类型
*/
const getInputType = () => { const getInputType = () => {
switch (props.propertyType) { switch (props.propertyType) {
case IoTDataSpecsDataTypeEnum.INT: case IoTDataSpecsDataTypeEnum.INT:
@ -207,6 +214,10 @@ const getInputType = () => {
} }
} }
/**
* 获取占位符文本
* @returns 占位符文本
*/
const getPlaceholder = () => { const getPlaceholder = () => {
const typeMap = { const typeMap = {
[IoTDataSpecsDataTypeEnum.TEXT]: '请输入字符串', [IoTDataSpecsDataTypeEnum.TEXT]: '请输入字符串',
@ -219,27 +230,48 @@ const getPlaceholder = () => {
return typeMap[props.propertyType || ''] || '请输入值' return typeMap[props.propertyType || ''] || '请输入值'
} }
/**
* 获取数字精度
* @returns 数字精度
*/
const getPrecision = () => { const getPrecision = () => {
return props.propertyType === IoTDataSpecsDataTypeEnum.INT ? 0 : 2 return props.propertyType === IoTDataSpecsDataTypeEnum.INT ? 0 : 2
} }
/**
* 获取数字步长
* @returns 数字步长
*/
const getStep = () => { const getStep = () => {
return props.propertyType === IoTDataSpecsDataTypeEnum.INT ? 1 : 0.1 return props.propertyType === IoTDataSpecsDataTypeEnum.INT ? 1 : 0.1
} }
/**
* 获取最小值
* @returns 最小值
*/
const getMin = () => { const getMin = () => {
return props.propertyConfig?.min || undefined return props.propertyConfig?.min || undefined
} }
/**
* 获取最大值
* @returns 最大值
*/
const getMax = () => { const getMax = () => {
return props.propertyConfig?.max || undefined return props.propertyConfig?.max || undefined
} }
// 事件处理 /**
* 处理值变化事件
*/
const handleChange = () => { const handleChange = () => {
// 值变化处理 // 值变化处理
} }
/**
* 处理范围变化事件
*/
const handleRangeChange = () => { const handleRangeChange = () => {
if (rangeStart.value && rangeEnd.value) { if (rangeStart.value && rangeEnd.value) {
localValue.value = `${rangeStart.value},${rangeEnd.value}` localValue.value = `${rangeStart.value},${rangeEnd.value}`
@ -248,10 +280,18 @@ const handleRangeChange = () => {
} }
} }
/**
* 处理日期变化事件
* @param value 日期值
*/
const handleDateChange = (value: string) => { const handleDateChange = (value: string) => {
localValue.value = value || '' localValue.value = value || ''
} }
/**
* 处理数字变化事件
* @param value 数字值
*/
const handleNumberChange = (value: number | undefined) => { const handleNumberChange = (value: number | undefined) => {
localValue.value = value?.toString() || '' localValue.value = value?.toString() || ''
} }

View File

@ -157,8 +157,11 @@ const emit = defineEmits<{
const actions = useVModel(props, 'actions', emit) const actions = useVModel(props, 'actions', emit)
const maxActions = SCENE_RULE_CONFIG.MAX_ACTIONS // 最大执行器数量
/** /**
* 创建默认的执行器数据 * 创建默认的执行器数据
* @returns 默认执行器对象
*/ */
const createDefaultActionData = (): Action => { const createDefaultActionData = (): Action => {
return { return {
@ -171,10 +174,9 @@ const createDefaultActionData = (): Action => {
} }
} }
// 使用标准化的常量和函数 /**
const maxActions = SCENE_RULE_CONFIG.MAX_ACTIONS * 添加执行器
*/
/** 添加执行器 */
const addAction = () => { const addAction = () => {
if (actions.value.length >= maxActions) { if (actions.value.length >= maxActions) {
return return
@ -184,28 +186,47 @@ const addAction = () => {
actions.value.push(newAction) actions.value.push(newAction)
} }
/** 删除执行器 */ /**
* 删除执行器
* @param index 执行器索引
*/
const removeAction = (index: number) => { const removeAction = (index: number) => {
actions.value.splice(index, 1) actions.value.splice(index, 1)
} }
/** 更新执行器类型 */ /**
* 更新执行器类型
* @param index 执行器索引
* @param type 执行器类型
*/
const updateActionType = (index: number, type: number) => { const updateActionType = (index: number, type: number) => {
actions.value[index].type = type actions.value[index].type = type
onActionTypeChange(actions.value[index], type) onActionTypeChange(actions.value[index], type)
} }
/** 更新执行器 */ /**
* 更新执行器
* @param index 执行器索引
* @param action 执行器对象
*/
const updateAction = (index: number, action: Action) => { const updateAction = (index: number, action: Action) => {
actions.value[index] = action actions.value[index] = action
} }
/** 更新告警配置 */ /**
* 更新告警配置
* @param index 执行器索引
* @param alertConfigId 告警配置ID
*/
const updateActionAlertConfig = (index: number, alertConfigId?: number) => { const updateActionAlertConfig = (index: number, alertConfigId?: number) => {
actions.value[index].alertConfigId = alertConfigId actions.value[index].alertConfigId = alertConfigId
} }
/** 监听执行器类型变化 */ /**
* 监听执行器类型变化
* @param action 执行器对象
* @param type 执行器类型
*/
const onActionTypeChange = (action: Action, type: number) => { const onActionTypeChange = (action: Action, type: number) => {
// 清理不相关的配置,确保数据结构干净 // 清理不相关的配置,确保数据结构干净
if (isDeviceAction(type)) { if (isDeviceAction(type)) {

View File

@ -67,11 +67,12 @@ const props = defineProps<{
modelValue: IotSceneRule modelValue: IotSceneRule
rules?: any rules?: any
}>() }>()
const emit = defineEmits<{ const emit = defineEmits<{
(e: 'update:modelValue', value: IotSceneRule): void (e: 'update:modelValue', value: IotSceneRule): void
}>() }>()
const formData = useVModel(props, 'modelValue', emit) const formData = useVModel(props, 'modelValue', emit) // 表单数据
</script> </script>
<style scoped> <style scoped>

View File

@ -137,7 +137,9 @@ const emit = defineEmits<{
const triggers = useVModel(props, 'triggers', emit) const triggers = useVModel(props, 'triggers', emit)
// 事件处理函数 /**
* 添加触发器
*/
const addTrigger = () => { const addTrigger = () => {
const newTrigger: Trigger = { const newTrigger: Trigger = {
type: TriggerTypeEnum.DEVICE_STATE_UPDATE, type: TriggerTypeEnum.DEVICE_STATE_UPDATE,
@ -152,25 +154,49 @@ const addTrigger = () => {
triggers.value.push(newTrigger) triggers.value.push(newTrigger)
} }
/**
* 删除触发器
* @param index 触发器索引
*/
const removeTrigger = (index: number) => { const removeTrigger = (index: number) => {
if (triggers.value.length > 1) { if (triggers.value.length > 1) {
triggers.value.splice(index, 1) triggers.value.splice(index, 1)
} }
} }
/**
* 更新触发器类型
* @param index 触发器索引
* @param type 触发器类型
*/
const updateTriggerType = (index: number, type: number) => { const updateTriggerType = (index: number, type: number) => {
triggers.value[index].type = type triggers.value[index].type = type
onTriggerTypeChange(index, type) onTriggerTypeChange(index, type)
} }
/**
* 更新触发器设备配置
* @param index 触发器索引
* @param newTrigger 新的触发器对象
*/
const updateTriggerDeviceConfig = (index: number, newTrigger: Trigger) => { const updateTriggerDeviceConfig = (index: number, newTrigger: Trigger) => {
triggers.value[index] = newTrigger triggers.value[index] = newTrigger
} }
/**
* 更新触发器CRON配置
* @param index 触发器索引
* @param cronExpression CRON表达式
*/
const updateTriggerCronConfig = (index: number, cronExpression?: string) => { const updateTriggerCronConfig = (index: number, cronExpression?: string) => {
triggers.value[index].cronExpression = cronExpression triggers.value[index].cronExpression = cronExpression
} }
/**
* 处理触发器类型变化事件
* @param index 触发器索引
* @param _ 触发器类型(未使用)
*/
const onTriggerTypeChange = (index: number, _: number) => { const onTriggerTypeChange = (index: number, _: number) => {
const triggerItem = triggers.value[index] const triggerItem = triggers.value[index]
triggerItem.productId = undefined triggerItem.productId = undefined

View File

@ -58,17 +58,21 @@ const emit = defineEmits<{
(e: 'change', value?: number): void (e: 'change', value?: number): void
}>() }>()
// 状态 const deviceLoading = ref(false) // 设备加载状态
const deviceLoading = ref(false) const deviceList = ref<any[]>([]) // 设备列表
const deviceList = ref<any[]>([])
// 事件处理 /**
* 处理选择变化事件
* @param value 选中的设备ID
*/
const handleChange = (value?: number) => { const handleChange = (value?: number) => {
emit('update:modelValue', value) emit('update:modelValue', value)
emit('change', value) emit('change', value)
} }
// 获取设备列表 /**
* 获取设备列表
*/
const getDeviceList = async () => { const getDeviceList = async () => {
if (!props.productId) { if (!props.productId) {
deviceList.value = [] deviceList.value = []
@ -88,8 +92,6 @@ const getDeviceList = async () => {
} }
} }
// 设备状态处理函数已从 constants.ts 中导入
// 监听产品变化 // 监听产品变化
watch( watch(
() => props.productId, () => props.productId,

View File

@ -217,7 +217,7 @@ const allOperators = [
} }
] ]
// 计算属性 // 计算属性:可用的操作符
const availableOperators = computed(() => { const availableOperators = computed(() => {
if (!props.propertyType) { if (!props.propertyType) {
return allOperators return allOperators
@ -226,11 +226,15 @@ const availableOperators = computed(() => {
return allOperators.filter((op) => op.supportedTypes.includes(props.propertyType!)) return allOperators.filter((op) => op.supportedTypes.includes(props.propertyType!))
}) })
// 计算属性:当前选中的操作符
const selectedOperator = computed(() => { const selectedOperator = computed(() => {
return allOperators.find((op) => op.value === localValue.value) return allOperators.find((op) => op.value === localValue.value)
}) })
// 事件处理 /**
* 处理选择变化事件
* @param value 选中的操作符值
*/
const handleChange = (value: string) => { const handleChange = (value: string) => {
emit('change', value) emit('change', value)
} }

View File

@ -46,17 +46,21 @@ const emit = defineEmits<{
(e: 'change', value?: number): void (e: 'change', value?: number): void
}>() }>()
// 状态 const productLoading = ref(false) // 产品加载状态
const productLoading = ref(false) const productList = ref<any[]>([]) // 产品列表
const productList = ref<any[]>([])
// 事件处理 /**
* 处理选择变化事件
* @param value 选中的产品ID
*/
const handleChange = (value?: number) => { const handleChange = (value?: number) => {
emit('update:modelValue', value) emit('update:modelValue', value)
emit('change', value) emit('change', value)
} }
// 获取产品列表 /**
* 获取产品列表
*/
const getProductList = async () => { const getProductList = async () => {
try { try {
productLoading.value = true productLoading.value = true

View File

@ -212,12 +212,11 @@ const emit = defineEmits<{
const localValue = useVModel(props, 'modelValue', emit) const localValue = useVModel(props, 'modelValue', emit)
// 状态 const loading = ref(false) // 加载状态
const loading = ref(false) const propertyList = ref<PropertySelectorItem[]>([]) // 属性列表
const propertyList = ref<PropertySelectorItem[]>([]) const thingModelTSL = ref<IotThingModelTSLResp | null>(null) // 物模型TSL数据
const thingModelTSL = ref<IotThingModelTSLResp | null>(null)
// 计算属性 // 计算属性:属性分组
const propertyGroups = computed(() => { const propertyGroups = computed(() => {
const groups: { label: string; options: any[] }[] = [] const groups: { label: string; options: any[] }[] = []
@ -245,11 +244,15 @@ const propertyGroups = computed(() => {
return groups.filter((group) => group.options.length > 0) return groups.filter((group) => group.options.length > 0)
}) })
// 计算属性:当前选中的属性
const selectedProperty = computed(() => { const selectedProperty = computed(() => {
return propertyList.value.find((p) => p.identifier === localValue.value) return propertyList.value.find((p) => p.identifier === localValue.value)
}) })
// 事件处理 /**
* 处理选择变化事件
* @param value 选中的属性标识符
*/
const handleChange = (value: string) => { const handleChange = (value: string) => {
const property = propertyList.value.find((p) => p.identifier === value) const property = propertyList.value.find((p) => p.identifier === value)
if (property) { if (property) {
@ -289,7 +292,9 @@ const getThingModelTSL = async () => {
} }
} }
// 解析物模型TSL数据 /**
* 解析物模型TSL数据
*/
const parseThingModelData = () => { const parseThingModelData = () => {
const tsl = thingModelTSL.value const tsl = thingModelTSL.value
const properties: PropertySelectorItem[] = [] const properties: PropertySelectorItem[] = []
@ -352,7 +357,11 @@ const parseThingModelData = () => {
propertyList.value = properties propertyList.value = properties
} }
// 获取属性单位 /**
* 获取属性单位
* @param property 属性对象
* @returns 属性单位
*/
const getPropertyUnit = (property: any) => { const getPropertyUnit = (property: any) => {
if (!property) return undefined if (!property) return undefined
@ -364,7 +373,11 @@ const getPropertyUnit = (property: any) => {
return undefined return undefined
} }
// 获取属性范围描述 /**
* 获取属性范围描述
* @param property 属性对象
* @returns 属性范围描述
*/
const getPropertyRange = (property: any) => { const getPropertyRange = (property: any) => {
if (!property) return undefined if (!property) return undefined