2025-07-17 21:31:12 +08:00
|
|
|
<template>
|
2025-08-01 14:58:39 +08:00
|
|
|
<!-- 场景联动规则表单抽屉 - 优化高度和布局 -->
|
2025-07-17 21:31:12 +08:00
|
|
|
<el-drawer
|
|
|
|
|
v-model="drawerVisible"
|
|
|
|
|
:title="drawerTitle"
|
|
|
|
|
size="80%"
|
|
|
|
|
direction="rtl"
|
|
|
|
|
:close-on-click-modal="false"
|
|
|
|
|
:close-on-press-escape="false"
|
|
|
|
|
@close="handleClose"
|
|
|
|
|
>
|
2025-08-01 16:14:11 +08:00
|
|
|
<el-form ref="formRef" :model="formData" :rules="formRules" label-width="110px">
|
2025-08-01 14:58:39 +08:00
|
|
|
<!-- 基础信息配置 -->
|
|
|
|
|
<BasicInfoSection v-model="formData" :rules="formRules" />
|
|
|
|
|
|
|
|
|
|
<!-- 触发器配置 -->
|
2025-08-01 16:14:11 +08:00
|
|
|
<TriggerSection v-model:triggers="formData.triggers" @validate="handleTriggerValidate" />
|
2025-08-01 14:58:39 +08:00
|
|
|
|
|
|
|
|
<!-- 执行器配置 -->
|
|
|
|
|
<ActionSection v-model:actions="formData.actions" @validate="handleActionValidate" />
|
|
|
|
|
</el-form>
|
2025-07-17 21:31:12 +08:00
|
|
|
<template #footer>
|
2025-08-01 14:58:39 +08:00
|
|
|
<div class="drawer-footer">
|
|
|
|
|
<el-button :disabled="submitLoading" type="primary" @click="handleSubmit">
|
|
|
|
|
<Icon icon="ep:check" />
|
|
|
|
|
确 定
|
|
|
|
|
</el-button>
|
|
|
|
|
<el-button @click="handleClose">
|
|
|
|
|
<Icon icon="ep:close" />
|
|
|
|
|
取 消
|
|
|
|
|
</el-button>
|
|
|
|
|
</div>
|
2025-07-17 21:31:12 +08:00
|
|
|
</template>
|
|
|
|
|
</el-drawer>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { useVModel } from '@vueuse/core'
|
|
|
|
|
import BasicInfoSection from './sections/BasicInfoSection.vue'
|
|
|
|
|
import TriggerSection from './sections/TriggerSection.vue'
|
|
|
|
|
import ActionSection from './sections/ActionSection.vue'
|
2025-07-27 22:44:06 +08:00
|
|
|
import {
|
|
|
|
|
IotRuleScene,
|
|
|
|
|
IotRuleSceneActionTypeEnum,
|
2025-08-01 16:14:11 +08:00
|
|
|
RuleSceneFormData,
|
|
|
|
|
TriggerFormData
|
2025-07-27 22:44:06 +08:00
|
|
|
} from '@/api/iot/rule/scene/scene.types'
|
2025-08-01 16:14:11 +08:00
|
|
|
import { IotRuleSceneTriggerTypeEnum } from '@/views/iot/utils/constants'
|
2025-07-26 22:35:59 +08:00
|
|
|
import { ElMessage } from 'element-plus'
|
2025-07-27 22:44:06 +08:00
|
|
|
import { generateUUID } from '@/utils'
|
2025-07-17 21:31:12 +08:00
|
|
|
|
2025-08-01 16:14:11 +08:00
|
|
|
// 导入全局的 CommonStatusEnum
|
|
|
|
|
const CommonStatusEnum = {
|
|
|
|
|
ENABLE: 0, // 开启
|
|
|
|
|
DISABLE: 1 // 关闭
|
|
|
|
|
} as const
|
|
|
|
|
|
2025-07-18 23:55:51 +08:00
|
|
|
/** IoT 场景联动规则表单 - 主表单组件 */
|
2025-07-17 21:31:12 +08:00
|
|
|
defineOptions({ name: 'RuleSceneForm' })
|
|
|
|
|
|
2025-08-01 14:58:39 +08:00
|
|
|
/** 组件属性定义 */
|
|
|
|
|
const props = defineProps<{
|
|
|
|
|
/** 抽屉显示状态 */
|
2025-07-17 21:31:12 +08:00
|
|
|
modelValue: boolean
|
2025-08-01 14:58:39 +08:00
|
|
|
/** 编辑的规则数据(新增时为空) */
|
2025-07-17 21:31:12 +08:00
|
|
|
ruleScene?: IotRuleScene
|
2025-08-01 14:58:39 +08:00
|
|
|
}>()
|
2025-07-17 21:31:12 +08:00
|
|
|
|
2025-08-01 14:58:39 +08:00
|
|
|
/** 组件事件定义 */
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
/** 更新抽屉显示状态 */
|
|
|
|
|
'update:modelValue': [value: boolean]
|
|
|
|
|
/** 操作成功事件 */
|
|
|
|
|
success: []
|
|
|
|
|
}>()
|
2025-07-17 21:31:12 +08:00
|
|
|
|
2025-07-28 23:25:54 +08:00
|
|
|
const drawerVisible = useVModel(props, 'modelValue', emit) // 是否可见
|
2025-07-17 21:31:12 +08:00
|
|
|
|
2025-07-28 23:25:54 +08:00
|
|
|
/** 创建默认的表单数据 */
|
2025-07-27 22:44:06 +08:00
|
|
|
const createDefaultFormData = (): RuleSceneFormData => {
|
|
|
|
|
return {
|
|
|
|
|
name: '',
|
|
|
|
|
description: '',
|
|
|
|
|
status: CommonStatusEnum.ENABLE, // 默认启用状态
|
2025-08-01 16:14:11 +08:00
|
|
|
triggers: [
|
|
|
|
|
{
|
|
|
|
|
type: IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST,
|
|
|
|
|
productId: undefined,
|
|
|
|
|
deviceId: undefined,
|
|
|
|
|
identifier: undefined,
|
|
|
|
|
operator: undefined,
|
|
|
|
|
value: undefined,
|
|
|
|
|
cronExpression: undefined,
|
|
|
|
|
mainCondition: undefined,
|
|
|
|
|
conditionGroup: undefined
|
|
|
|
|
}
|
|
|
|
|
],
|
2025-07-27 22:44:06 +08:00
|
|
|
actions: []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-28 23:25:54 +08:00
|
|
|
* 将表单数据转换为 API 请求格式
|
2025-07-27 22:44:06 +08:00
|
|
|
*/
|
2025-08-01 14:58:39 +08:00
|
|
|
const convertFormToVO = (formData: RuleSceneFormData): IotRuleScene => {
|
2025-08-01 16:14:11 +08:00
|
|
|
// 构建单个触发器的条件
|
|
|
|
|
const buildTriggerConditions = (trigger: TriggerFormData) => {
|
2025-08-01 14:58:39 +08:00
|
|
|
const conditions: any[] = []
|
|
|
|
|
|
|
|
|
|
// 处理主条件
|
2025-08-01 16:14:11 +08:00
|
|
|
if (trigger.mainCondition) {
|
|
|
|
|
const mainCondition = trigger.mainCondition
|
2025-08-01 14:58:39 +08:00
|
|
|
conditions.push({
|
|
|
|
|
type: mainCondition.type === 2 ? 'property' : 'event',
|
|
|
|
|
identifier: mainCondition.identifier || '',
|
|
|
|
|
parameters: [
|
|
|
|
|
{
|
|
|
|
|
operator: mainCondition.operator,
|
|
|
|
|
value: mainCondition.param
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理条件组
|
2025-08-01 16:14:11 +08:00
|
|
|
if (trigger.conditionGroup?.subGroups) {
|
|
|
|
|
trigger.conditionGroup.subGroups.forEach((subGroup) => {
|
2025-08-01 14:58:39 +08:00
|
|
|
subGroup.conditions.forEach((condition) => {
|
|
|
|
|
conditions.push({
|
|
|
|
|
type: condition.type === 2 ? 'property' : 'event',
|
|
|
|
|
identifier: condition.identifier || '',
|
|
|
|
|
parameters: [
|
|
|
|
|
{
|
|
|
|
|
operator: condition.operator,
|
|
|
|
|
value: condition.param
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conditions
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-27 22:44:06 +08:00
|
|
|
return {
|
|
|
|
|
id: formData.id,
|
|
|
|
|
name: formData.name,
|
|
|
|
|
description: formData.description,
|
|
|
|
|
status: Number(formData.status),
|
2025-08-01 16:14:11 +08:00
|
|
|
triggers: formData.triggers.map((trigger) => ({
|
|
|
|
|
type: trigger.type,
|
|
|
|
|
productKey: trigger.productId ? `product_${trigger.productId}` : undefined,
|
|
|
|
|
deviceNames: trigger.deviceId ? [`device_${trigger.deviceId}`] : undefined,
|
|
|
|
|
cronExpression: trigger.cronExpression,
|
|
|
|
|
conditions: buildTriggerConditions(trigger)
|
|
|
|
|
})),
|
2025-07-27 22:44:06 +08:00
|
|
|
actions:
|
|
|
|
|
formData.actions?.map((action) => ({
|
|
|
|
|
type: action.type,
|
|
|
|
|
alertConfigId: action.alertConfigId,
|
|
|
|
|
deviceControl:
|
|
|
|
|
action.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET ||
|
|
|
|
|
action.type === IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE
|
|
|
|
|
? {
|
|
|
|
|
productKey: action.productId ? `product_${action.productId}` : '',
|
|
|
|
|
deviceNames: action.deviceId ? [`device_${action.deviceId}`] : [],
|
2025-08-01 14:58:39 +08:00
|
|
|
type:
|
|
|
|
|
action.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET
|
|
|
|
|
? 'property'
|
|
|
|
|
: 'service',
|
|
|
|
|
identifier:
|
|
|
|
|
action.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET ? 'set' : 'invoke',
|
2025-07-27 22:44:06 +08:00
|
|
|
params: action.params || {}
|
|
|
|
|
}
|
|
|
|
|
: undefined
|
|
|
|
|
})) || []
|
|
|
|
|
} as IotRuleScene
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将 API 响应数据转换为表单格式
|
|
|
|
|
*/
|
2025-08-01 14:58:39 +08:00
|
|
|
const convertVOToForm = (apiData: IotRuleScene): RuleSceneFormData => {
|
2025-08-01 16:14:11 +08:00
|
|
|
// 解析单个触发器的条件
|
2025-08-01 14:58:39 +08:00
|
|
|
const parseConditions = (trigger: any) => {
|
|
|
|
|
if (!trigger?.conditions?.length) {
|
|
|
|
|
return {
|
|
|
|
|
mainCondition: undefined,
|
|
|
|
|
conditionGroup: undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 简化处理:将第一个条件作为主条件
|
|
|
|
|
const firstCondition = trigger.conditions[0]
|
|
|
|
|
const mainCondition = {
|
|
|
|
|
type: firstCondition.type === 'property' ? 2 : 3,
|
|
|
|
|
productId: undefined, // 需要从 productKey 解析
|
|
|
|
|
deviceId: undefined, // 需要从 deviceNames 解析
|
|
|
|
|
identifier: firstCondition.identifier,
|
|
|
|
|
operator: firstCondition.parameters?.[0]?.operator || '=',
|
|
|
|
|
param: firstCondition.parameters?.[0]?.value || ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
mainCondition,
|
|
|
|
|
conditionGroup: undefined // 暂时简化处理
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 16:14:11 +08:00
|
|
|
// 转换所有触发器
|
|
|
|
|
const triggers = apiData.triggers?.length
|
|
|
|
|
? apiData.triggers.map((trigger) => {
|
|
|
|
|
const conditionData = parseConditions(trigger)
|
|
|
|
|
return {
|
|
|
|
|
type: Number(trigger.type),
|
2025-08-01 14:58:39 +08:00
|
|
|
productId: undefined, // 需要从 productKey 解析
|
|
|
|
|
deviceId: undefined, // 需要从 deviceNames 解析
|
|
|
|
|
identifier: undefined,
|
|
|
|
|
operator: undefined,
|
|
|
|
|
value: undefined,
|
2025-08-01 16:14:11 +08:00
|
|
|
cronExpression: trigger.cronExpression,
|
2025-08-01 14:58:39 +08:00
|
|
|
...conditionData
|
2025-07-28 21:38:27 +08:00
|
|
|
}
|
2025-08-01 16:14:11 +08:00
|
|
|
})
|
|
|
|
|
: [
|
|
|
|
|
{
|
2025-07-28 21:38:27 +08:00
|
|
|
type: IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST,
|
|
|
|
|
productId: undefined,
|
|
|
|
|
deviceId: undefined,
|
|
|
|
|
identifier: undefined,
|
|
|
|
|
operator: undefined,
|
|
|
|
|
value: undefined,
|
|
|
|
|
cronExpression: undefined,
|
|
|
|
|
mainCondition: undefined,
|
|
|
|
|
conditionGroup: undefined
|
2025-08-01 16:14:11 +08:00
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...apiData,
|
|
|
|
|
status: Number(apiData.status),
|
|
|
|
|
triggers,
|
2025-07-27 22:44:06 +08:00
|
|
|
actions:
|
|
|
|
|
apiData.actions?.map((action) => ({
|
|
|
|
|
...action,
|
|
|
|
|
type: Number(action.type),
|
2025-08-01 14:58:39 +08:00
|
|
|
productId: undefined, // 需要从 deviceControl.productKey 解析
|
|
|
|
|
deviceId: undefined, // 需要从 deviceControl.deviceNames 解析
|
|
|
|
|
params: action.deviceControl?.params || {},
|
2025-07-27 22:44:06 +08:00
|
|
|
// 为每个执行器添加唯一标识符,解决组件索引重用问题
|
|
|
|
|
key: generateUUID()
|
|
|
|
|
})) || []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 21:31:12 +08:00
|
|
|
// 表单数据和状态
|
|
|
|
|
const formRef = ref()
|
|
|
|
|
const formData = ref<RuleSceneFormData>(createDefaultFormData())
|
2025-08-01 14:58:39 +08:00
|
|
|
const formRules = reactive({
|
|
|
|
|
name: [
|
|
|
|
|
{ required: true, message: '场景名称不能为空', trigger: 'blur' },
|
|
|
|
|
{ type: 'string', min: 1, max: 50, message: '场景名称长度应在1-50个字符之间', trigger: 'blur' }
|
|
|
|
|
],
|
|
|
|
|
status: [
|
|
|
|
|
{ required: true, message: '场景状态不能为空', trigger: 'change' },
|
|
|
|
|
{
|
|
|
|
|
type: 'enum',
|
|
|
|
|
enum: [CommonStatusEnum.ENABLE, CommonStatusEnum.DISABLE],
|
|
|
|
|
message: '状态值必须为启用或禁用',
|
|
|
|
|
trigger: 'change'
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
description: [
|
|
|
|
|
{ type: 'string', max: 200, message: '场景描述不能超过200个字符', trigger: 'blur' }
|
|
|
|
|
],
|
|
|
|
|
triggers: [
|
|
|
|
|
{ required: true, message: '触发器数组不能为空', trigger: 'change' },
|
|
|
|
|
{ type: 'array', min: 1, message: '至少需要一个触发器', trigger: 'change' }
|
|
|
|
|
],
|
|
|
|
|
actions: [
|
|
|
|
|
{ required: true, message: '执行器数组不能为空', trigger: 'change' },
|
|
|
|
|
{ type: 'array', min: 1, message: '至少需要一个执行器', trigger: 'change' }
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
2025-07-17 21:31:12 +08:00
|
|
|
const submitLoading = ref(false)
|
|
|
|
|
|
|
|
|
|
// 验证状态
|
|
|
|
|
const triggerValidation = ref({ valid: true, message: '' })
|
|
|
|
|
const actionValidation = ref({ valid: true, message: '' })
|
|
|
|
|
|
|
|
|
|
// 计算属性
|
|
|
|
|
const isEdit = computed(() => !!props.ruleScene?.id)
|
2025-07-26 22:35:59 +08:00
|
|
|
const drawerTitle = computed(() => (isEdit.value ? '编辑场景联动规则' : '新增场景联动规则'))
|
2025-07-17 21:31:12 +08:00
|
|
|
|
|
|
|
|
// 事件处理
|
|
|
|
|
const handleTriggerValidate = (result: { valid: boolean; message: string }) => {
|
|
|
|
|
triggerValidation.value = result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleActionValidate = (result: { valid: boolean; message: string }) => {
|
|
|
|
|
actionValidation.value = result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async () => {
|
2025-07-26 22:35:59 +08:00
|
|
|
// 校验表单
|
|
|
|
|
if (!formRef.value) return
|
|
|
|
|
const valid = await formRef.value.validate()
|
|
|
|
|
if (!valid) return
|
|
|
|
|
// 验证触发器和执行器
|
|
|
|
|
if (!triggerValidation.value.valid) {
|
|
|
|
|
ElMessage.error(triggerValidation.value.message)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!actionValidation.value.valid) {
|
|
|
|
|
ElMessage.error(actionValidation.value.message)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 提交请求
|
|
|
|
|
submitLoading.value = true
|
|
|
|
|
try {
|
|
|
|
|
// 转换数据格式
|
2025-08-01 14:58:39 +08:00
|
|
|
const apiData = convertFormToVO(formData.value)
|
|
|
|
|
|
|
|
|
|
// 调用API保存数据
|
|
|
|
|
if (isEdit.value) {
|
|
|
|
|
// 更新场景联动规则
|
|
|
|
|
// await RuleSceneApi.updateRuleScene(apiData)
|
|
|
|
|
console.log('更新数据:', apiData)
|
|
|
|
|
} else {
|
|
|
|
|
// 创建场景联动规则
|
|
|
|
|
// await RuleSceneApi.createRuleScene(apiData)
|
|
|
|
|
console.log('创建数据:', apiData)
|
|
|
|
|
}
|
2025-07-26 22:35:59 +08:00
|
|
|
|
|
|
|
|
// 模拟API调用
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
2025-07-17 21:54:40 +08:00
|
|
|
|
2025-07-26 22:35:59 +08:00
|
|
|
ElMessage.success(isEdit.value ? '更新成功' : '创建成功')
|
|
|
|
|
drawerVisible.value = false
|
2025-07-17 21:31:12 +08:00
|
|
|
emit('success')
|
2025-08-01 14:58:39 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('保存失败:', error)
|
|
|
|
|
ElMessage.error(isEdit.value ? '更新失败' : '创建失败')
|
2025-07-26 22:35:59 +08:00
|
|
|
} finally {
|
|
|
|
|
submitLoading.value = false
|
2025-07-17 21:31:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
|
drawerVisible.value = false
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 14:58:39 +08:00
|
|
|
/** 初始化表单数据 */
|
2025-07-17 21:31:12 +08:00
|
|
|
const initFormData = () => {
|
|
|
|
|
if (props.ruleScene) {
|
2025-08-01 14:58:39 +08:00
|
|
|
formData.value = convertVOToForm(props.ruleScene)
|
2025-07-17 21:31:12 +08:00
|
|
|
} else {
|
|
|
|
|
formData.value = createDefaultFormData()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 监听抽屉显示
|
|
|
|
|
watch(drawerVisible, (visible) => {
|
|
|
|
|
if (visible) {
|
|
|
|
|
initFormData()
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
formRef.value?.clearValidate()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-07-28 23:25:54 +08:00
|
|
|
// 监听 props 变化
|
2025-07-17 21:54:40 +08:00
|
|
|
watch(
|
|
|
|
|
() => props.ruleScene,
|
|
|
|
|
() => {
|
|
|
|
|
if (drawerVisible.value) {
|
|
|
|
|
initFormData()
|
|
|
|
|
}
|
2025-07-17 21:31:12 +08:00
|
|
|
}
|
2025-07-17 21:54:40 +08:00
|
|
|
)
|
2025-07-17 21:31:12 +08:00
|
|
|
</script>
|