This commit is contained in:
xingyu4j
2025-10-13 10:17:19 +08:00
parent 5f88a54d60
commit 4c4cd57ef0
27 changed files with 857 additions and 818 deletions

View File

@@ -90,6 +90,7 @@ export interface TriggerCondition {
operator?: string;
value?: any;
type?: string;
param?: string;
}
/** IoT 场景联动规则动作 */

View File

@@ -114,13 +114,13 @@ export interface ThingModelFormRules {
}
/** 验证布尔型名称 */
export const validateBoolName = (_rule: any, value: any, callback: any) => {
export function validateBoolName(_rule: any, value: any, callback: any) {
if (value) {
callback();
} else {
callback(new Error('枚举描述不能为空'));
}
};
}
/** 查询产品物模型分页 */
export function getThingModelPage(params: PageParam) {

View File

@@ -1,9 +1,15 @@
<script setup lang="ts">
import { CommonStatusEnum } from '@/utils/constants';
import { useVModel } from '@vueuse/core';
import { ElMessage } from 'element-plus';
import type { IotSceneRule } from '#/api/iot/rule/scene';
import { IotSceneRule, RuleSceneApi } from '#/api/iot/rule/scene';
import { computed, nextTick, reactive, ref, watch } from 'vue';
import { CommonStatusEnum } from '@vben/constants';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Button, Drawer, Form, message } from 'ant-design-vue';
import { createSceneRule, updateSceneRule } from '#/api/iot/rule/scene';
import {
IotRuleSceneActionTypeEnum,
IotRuleSceneTriggerTypeEnum,
@@ -37,14 +43,14 @@ const drawerVisible = useVModel(props, 'modelValue', emit); // 抽屉显示状
* 创建默认的表单数据
* @returns 默认表单数据对象
*/
const createDefaultFormData = (): IotSceneRule => {
function createDefaultFormData(): IotSceneRule {
return {
name: '',
description: '',
status: CommonStatusEnum.ENABLE, // 默认启用状态
triggers: [
{
type: IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST,
type: IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST.toString(),
productId: undefined,
deviceId: undefined,
identifier: undefined,
@@ -56,7 +62,7 @@ const createDefaultFormData = (): IotSceneRule => {
],
actions: [],
};
};
}
const formRef = ref(); // 表单引用
const formData = ref<IotSceneRule>(createDefaultFormData()); // 表单数据
@@ -67,7 +73,7 @@ const formData = ref<IotSceneRule>(createDefaultFormData()); // 表单数据
* @param value 校验值
* @param callback 回调函数
*/
const validateTriggers = (_rule: any, value: any, callback: any) => {
function validateTriggers(_rule: any, value: any, callback: any) {
if (!value || !Array.isArray(value) || value.length === 0) {
callback(new Error('至少需要一个触发器'));
return;
@@ -119,7 +125,7 @@ const validateTriggers = (_rule: any, value: any, callback: any) => {
}
callback();
};
}
/**
* 执行器校验器
@@ -127,7 +133,7 @@ const validateTriggers = (_rule: any, value: any, callback: any) => {
* @param value 校验值
* @param callback 回调函数
*/
const validateActions = (_rule: any, value: any, callback: any) => {
function validateActions(_rule: any, value: any, callback: any) {
if (!value || !Array.isArray(value) || value.length === 0) {
callback(new Error('至少需要一个执行器'));
return;
@@ -181,7 +187,7 @@ const validateActions = (_rule: any, value: any, callback: any) => {
}
callback();
};
}
const formRules = reactive({
name: [
@@ -224,7 +230,7 @@ const drawerTitle = computed(() =>
); // 抽屉标题
/** 提交表单 */
const handleSubmit = async () => {
async function handleSubmit() {
// 校验表单
if (!formRef.value) return;
const valid = await formRef.value.validate();
@@ -235,12 +241,12 @@ const handleSubmit = async () => {
try {
if (isEdit.value) {
// 更新场景联动规则
await RuleSceneApi.updateRuleScene(formData.value);
ElMessage.success('更新成功');
await updateSceneRule(formData.value);
message.success('更新成功');
} else {
// 创建场景联动规则
await RuleSceneApi.createRuleScene(formData.value);
ElMessage.success('创建成功');
await createSceneRule(formData.value);
message.success('创建成功');
}
// 关闭抽屉并触发成功事件
@@ -248,11 +254,11 @@ const handleSubmit = async () => {
emit('success');
} catch (error) {
console.error('保存失败:', error);
ElMessage.error(isEdit.value ? '更新失败' : '创建失败');
message.error(isEdit.value ? '更新失败' : '创建失败');
} finally {
submitLoading.value = false;
}
};
}
/** 处理抽屉关闭事件 */
const handleClose = () => {
@@ -260,14 +266,14 @@ const handleClose = () => {
};
/** 初始化表单数据 */
const initFormData = () => {
function initFormData() {
if (props.ruleScene) {
// 编辑模式:数据结构已对齐,直接使用后端数据
isEdit.value = true;
formData.value = {
...props.ruleScene,
// 确保触发器数组不为空
triggers: props.ruleScene.triggers?.length
triggers: (props.ruleScene.triggers?.length as any)
? props.ruleScene.triggers
: [
{
@@ -289,7 +295,7 @@ const initFormData = () => {
isEdit.value = false;
formData.value = createDefaultFormData();
}
};
}
/** 监听抽屉显示 */
watch(drawerVisible, async (visible) => {
@@ -314,43 +320,39 @@ watch(
</script>
<template>
<el-drawer
<Drawer
v-model="drawerVisible"
:title="drawerTitle"
size="80%"
width="80%"
direction="rtl"
:close-on-click-modal="false"
:close-on-press-escape="false"
@close="handleClose"
>
<el-form
<Form
ref="formRef"
:model="formData"
:rules="formRules"
:rules="formRules as any"
label-width="110px"
>
<!-- 基础信息配置 -->
<BasicInfoSection v-model="formData" :rules="formRules" />
<!-- 触发器配置 -->
<TriggerSection v-model:triggers="formData.triggers" />
<TriggerSection v-model:triggers="formData.triggers as any" />
<!-- 执行器配置 -->
<ActionSection v-model:actions="formData.actions" />
</el-form>
<ActionSection v-model:actions="formData.actions as any" />
</Form>
<template #footer>
<div class="drawer-footer">
<el-button
:disabled="submitLoading"
type="primary"
@click="handleSubmit"
>
<Icon icon="ep:check" />
<Button :disabled="submitLoading" type="primary" @click="handleSubmit">
<IconifyIcon icon="ep:check" />
</el-button>
<el-button @click="handleClose">
<Icon icon="ep:close" />
</Button>
<Button @click="handleClose">
<IconifyIcon icon="ep:close" />
</el-button>
</Button>
</div>
</template>
</el-drawer>
</Drawer>
</template>

View File

@@ -2,9 +2,20 @@
<script setup lang="ts">
import type { TriggerCondition } from '#/api/iot/rule/scene';
import { computed } from 'vue';
import { computed, watch } from 'vue';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import {
Col,
DatePicker,
Form,
Row,
Select,
Tag,
TimePicker,
} from 'ant-design-vue';
import { IotRuleSceneTriggerTimeOperatorEnum } from '#/views/iot/utils/constants';
@@ -108,15 +119,15 @@ const timeValue2 = computed(() => {
* @param field 字段名
* @param value 字段值
*/
const updateConditionField = (field: any, value: any) => {
function updateConditionField(field: any, value: any) {
condition.value[field] = value;
};
}
/**
* 处理第一个时间值变化
* @param value 时间值
*/
const handleTimeValueChange = (value: string) => {
function handleTimeValueChange(value: string) {
const currentParams = condition.value.param
? condition.value.param.split(',')
: [];
@@ -126,19 +137,19 @@ const handleTimeValueChange = (value: string) => {
condition.value.param = needsSecondTimeInput.value
? currentParams.slice(0, 2).join(',')
: currentParams[0];
};
}
/**
* 处理第二个时间值变化
* @param value 时间值
*/
const handleTimeValue2Change = (value: string) => {
function handleTimeValue2Change(value: string) {
const currentParams = condition.value.param
? condition.value.param.split(',')
: [''];
currentParams[1] = value || '';
condition.value.param = currentParams.slice(0, 2).join(',');
};
}
/** 监听操作符变化,清理不相关的时间值 */
watch(
@@ -160,19 +171,19 @@ watch(
<template>
<div class="gap-16px flex flex-col">
<el-row :gutter="16">
<Row :gutter="16">
<!-- 时间操作符选择 -->
<el-col :span="8">
<el-form-item label="时间条件" required>
<el-select
<Col :span="8">
<Form.Item label="时间条件" required>
<Select
:model-value="condition.operator"
@update:model-value="
(value) => updateConditionField('operator', value)
(value: any) => updateConditionField('operator', value)
"
placeholder="请选择时间条件"
class="w-full"
>
<el-option
<Select.Option
v-for="option in timeOperatorOptions"
:key="option.value"
:label="option.label"
@@ -180,22 +191,22 @@ watch(
>
<div class="flex w-full items-center justify-between">
<div class="gap-8px flex items-center">
<Icon :icon="option.icon" :class="option.iconClass" />
<IconifyIcon :icon="option.icon" :class="option.iconClass" />
<span>{{ option.label }}</span>
</div>
<el-tag :type="option.tag as any" size="small">
<Tag :type="option.tag as any" size="small">
{{ option.category }}
</el-tag>
</Tag>
</div>
</el-option>
</el-select>
</el-form-item>
</el-col>
</Select.Option>
</Select>
</Form.Item>
</Col>
<!-- 时间值输入 -->
<el-col :span="8">
<el-form-item label="时间值" required>
<el-time-picker
<Col :span="8">
<Form.Item label="时间值" required>
<TimePicker
v-if="needsTimeInput"
:model-value="timeValue"
@update:model-value="handleTimeValueChange"
@@ -204,7 +215,7 @@ watch(
value-format="HH:mm:ss"
class="w-full"
/>
<el-date-picker
<DatePicker
v-else-if="needsDateInput"
:model-value="timeValue"
@update:model-value="handleTimeValueChange"
@@ -217,13 +228,13 @@ watch(
<div v-else class="text-14px text-[var(--el-text-color-placeholder)]">
无需设置时间值
</div>
</el-form-item>
</el-col>
</Form.Item>
</Col>
<!-- 第二个时间值(范围条件) -->
<el-col :span="8" v-if="needsSecondTimeInput">
<el-form-item label="结束时间" required>
<el-time-picker
<Col :span="8" v-if="needsSecondTimeInput">
<Form.Item label="结束时间" required>
<TimePicker
v-if="needsTimeInput"
:model-value="timeValue2"
@update:model-value="handleTimeValue2Change"
@@ -232,7 +243,7 @@ watch(
value-format="HH:mm:ss"
class="w-full"
/>
<el-date-picker
<DatePicker
v-else
:model-value="timeValue2"
@update:model-value="handleTimeValue2Change"
@@ -242,8 +253,8 @@ watch(
value-format="YYYY-MM-DD HH:mm:ss"
class="w-full"
/>
</el-form-item>
</el-col>
</el-row>
</Form.Item>
</Col>
</Row>
</div>
</template>

View File

@@ -6,9 +6,14 @@ import type {
ThingModelService,
} from '#/api/iot/thingmodel';
import { useVModel } from '@vueuse/core';
import { computed, onMounted, ref, watch } from 'vue';
import { ThingModelApi } from '#/api/iot/thingmodel';
import { isObject } from '@vben/utils';
import { useVModel } from '@vueuse/core';
import { Col, Form, Row, Select, Tag } from 'ant-design-vue';
import { getThingModelListByProductId } from '#/api/iot/thingmodel';
import {
IoTDataSpecsDataTypeEnum,
IotRuleSceneActionTypeEnum,
@@ -42,7 +47,7 @@ const loadingServices = ref(false); // 服务加载状态
const paramsValue = computed({
get: () => {
// 如果 params 是对象,转换为 JSON 字符串(兼容旧数据)
if (action.value.params && typeof action.value.params === 'object') {
if (action.value.params && isObject(action.value.params)) {
return JSON.stringify(action.value.params, null, 2);
}
// 如果 params 已经是字符串,直接返回
@@ -56,19 +61,25 @@ const paramsValue = computed({
// 计算属性:是否为属性设置类型
const isPropertySetAction = computed(() => {
return action.value.type === IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET;
return (
action.value.type ===
IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET.toString()
);
});
// 计算属性:是否为服务调用类型
const isServiceInvokeAction = computed(() => {
return action.value.type === IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE;
return (
action.value.type ===
IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE.toString()
);
});
/**
* 处理产品变化事件
* @param productId 产品 ID
*/
const handleProductChange = (productId?: number) => {
function handleProductChange(productId?: number) {
// 当产品变化时,清空设备选择和参数配置
if (action.value.productId !== productId) {
action.value.deviceId = undefined;
@@ -86,24 +97,24 @@ const handleProductChange = (productId?: number) => {
loadServiceList(productId);
}
}
};
}
/**
* 处理设备变化事件
* @param deviceId 设备 ID
*/
const handleDeviceChange = (deviceId?: number) => {
function handleDeviceChange(deviceId?: number) {
// 当设备变化时,清空参数配置
if (action.value.deviceId !== deviceId) {
action.value.params = ''; // 清空参数,保存为空字符串
}
};
}
/**
* 处理服务变化事件
* @param serviceIdentifier 服务标识符
*/
const handleServiceChange = (serviceIdentifier?: string) => {
function handleServiceChange(serviceIdentifier?: any) {
// 根据服务标识符找到对应的服务对象
const service =
serviceList.value.find((s) => s.identifier === serviceIdentifier) || null;
@@ -121,29 +132,29 @@ const handleServiceChange = (serviceIdentifier?: string) => {
// 将默认参数转换为 JSON 字符串保存
action.value.params = JSON.stringify(defaultParams, null, 2);
}
};
}
/**
* 获取物模型TSL数据
* @param productId 产品ID
* @returns 物模型TSL数据
*/
const getThingModelTSL = async (productId: number) => {
async function getThingModelTSL(productId: number) {
if (!productId) return null;
try {
return await ThingModelApi.getThingModelTSLByProductId(productId);
return await getThingModelListByProductId(productId);
} catch (error) {
console.error('获取物模型TSL数据失败:', error);
return null;
}
};
}
/**
* 加载物模型属性(可写属性)
* @param productId 产品ID
*/
const loadThingModelProperties = async (productId: number) => {
async function loadThingModelProperties(productId: number) {
if (!productId) {
thingModelProperties.value = [];
return;
@@ -171,13 +182,13 @@ const loadThingModelProperties = async (productId: number) => {
} finally {
loadingThingModel.value = false;
}
};
}
/**
* 加载服务列表
* @param productId 产品ID
*/
const loadServiceList = async (productId: number) => {
async function loadServiceList(productId: number) {
if (!productId) {
serviceList.value = [];
return;
@@ -199,17 +210,17 @@ const loadServiceList = async (productId: number) => {
} finally {
loadingServices.value = false;
}
};
}
/**
* 从TSL加载服务信息用于编辑模式回显
* @param productId 产品ID
* @param serviceIdentifier 服务标识符
*/
const loadServiceFromTSL = async (
async function loadServiceFromTSL(
productId: number,
serviceIdentifier: string,
) => {
) {
// 先加载服务列表
await loadServiceList(productId);
@@ -220,14 +231,14 @@ const loadServiceFromTSL = async (
if (service) {
selectedService.value = service;
}
};
}
/**
* 根据参数类型获取默认值
* @param param 参数对象
* @returns 默认值
*/
const getDefaultValueForParam = (param: any) => {
function getDefaultValueForParam(param: any) {
switch (param.dataType) {
case IoTDataSpecsDataTypeEnum.BOOL: {
return false;
@@ -256,14 +267,14 @@ const getDefaultValueForParam = (param: any) => {
return '';
}
}
};
}
const isInitialized = ref(false); // 防止重复初始化的标志
/**
* 初始化组件数据
*/
const initializeComponent = async () => {
async function initializeComponent() {
if (isInitialized.value) return;
const currentAction = action.value;
@@ -285,7 +296,7 @@ const initializeComponent = async () => {
}
isInitialized.value = true;
};
}
/** 组件初始化 */
onMounted(() => {
@@ -329,30 +340,30 @@ watch(
<template>
<div class="gap-16px flex flex-col">
<!-- 产品和设备选择 - 与触发器保持一致的分离式选择器 -->
<el-row :gutter="16">
<el-col :span="12">
<el-form-item label="产品" required>
<Row :gutter="16">
<Col :span="12">
<Form.Item label="产品" required>
<ProductSelector
v-model="action.productId"
@change="handleProductChange"
/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="设备" required>
</Form.Item>
</Col>
<Col :span="12">
<Form.Item label="设备" required>
<DeviceSelector
v-model="action.deviceId"
:product-id="action.productId"
@change="handleDeviceChange"
/>
</el-form-item>
</el-col>
</el-row>
</Form.Item>
</Col>
</Row>
<!-- 服务选择 - 服务调用类型时显示 -->
<div v-if="action.productId && isServiceInvokeAction" class="space-y-16px">
<el-form-item label="服务" required>
<el-select
<Form.Item label="服务" required>
<Select
v-model="action.identifier"
placeholder="请选择服务"
filterable
@@ -361,7 +372,7 @@ watch(
:loading="loadingServices"
@change="handleServiceChange"
>
<el-option
<Select.Option
v-for="service in serviceList"
:key="service.identifier"
:label="service.name"
@@ -369,41 +380,41 @@ watch(
>
<div class="flex items-center justify-between">
<span>{{ service.name }}</span>
<el-tag
<Tag
:type="service.callType === 'sync' ? 'primary' : 'success'"
size="small"
>
{{ service.callType === 'sync' ? '同步' : '异步' }}
</el-tag>
</Tag>
</div>
</el-option>
</el-select>
</el-form-item>
</Select.Option>
</Select>
</Form.Item>
<!-- 服务参数配置 -->
<div v-if="action.identifier" class="space-y-16px">
<el-form-item label="服务参数" required>
<Form.Item label="服务参数" required>
<JsonParamsInput
v-model="paramsValue"
type="service"
:config="{ service: selectedService } as any"
placeholder="请输入 JSON 格式的服务参数"
/>
</el-form-item>
</Form.Item>
</div>
</div>
<!-- 控制参数配置 - 属性设置类型时显示 -->
<div v-if="action.productId && isPropertySetAction" class="space-y-16px">
<!-- 参数配置 -->
<el-form-item label="参数" required>
<Form.Item label="参数" required>
<JsonParamsInput
v-model="paramsValue"
type="property"
:config="{ properties: thingModelProperties }"
placeholder="请输入 JSON 格式的控制参数"
/>
</el-form-item>
</Form.Item>
</div>
</div>
</template>

View File

@@ -1,8 +1,13 @@
<!-- 设备触发配置组件 -->
<script setup lang="ts">
import type { Trigger } from '#/api/iot/rule/scene';
import type { RuleSceneApi } from '#/api/iot/rule/scene';
import { nextTick } from 'vue';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Button, Tag } from 'ant-design-vue';
import MainConditionInnerConfig from './MainConditionInnerConfig.vue';
import SubConditionGroupConfig from './SubConditionGroupConfig.vue';
@@ -12,12 +17,12 @@ defineOptions({ name: 'DeviceTriggerConfig' });
const props = defineProps<{
index: number;
modelValue: Trigger;
modelValue: RuleSceneApi.Trigger;
}>();
const emit = defineEmits<{
(e: 'update:modelValue', value: Trigger): void;
(e: 'trigger-type-change', type: number): void;
(e: 'update:modelValue', value: RuleSceneApi.Trigger): void;
(e: 'triggerTypeChange', type: number): void;
}>();
const trigger = useVModel(props, 'modelValue', emit);
@@ -29,21 +34,21 @@ const maxConditionsPerGroup = 3; // 每组最多 3 个条件
* 更新条件
* @param condition 条件对象
*/
const updateCondition = (condition: Trigger) => {
function updateCondition(condition: RuleSceneApi.Trigger) {
trigger.value = condition;
};
}
/**
* 处理触发器类型变化事件
* @param type 触发器类型
*/
const handleTriggerTypeChange = (type: number) => {
trigger.value.type = type;
emit('trigger-type-change', type);
};
function handleTriggerTypeChange(type: number) {
trigger.value.type = type.toString();
emit('triggerTypeChange', type);
}
/** 添加子条件组 */
const addSubGroup = async () => {
async function addSubGroup() {
if (!trigger.value.conditionGroups) {
trigger.value.conditionGroups = [];
}
@@ -56,35 +61,35 @@ const addSubGroup = async () => {
// 使用 nextTick 确保响应式更新完成后再添加新的子组
await nextTick();
if (trigger.value.conditionGroups) {
trigger.value.conditionGroups.push([]);
trigger.value.conditionGroups.push([] as any);
}
};
}
/**
* 移除子条件组
* @param index 子条件组索引
*/
const removeSubGroup = (index: number) => {
function removeSubGroup(index: number) {
if (trigger.value.conditionGroups) {
trigger.value.conditionGroups.splice(index, 1);
}
};
}
/**
* 更新子条件组
* @param index 子条件组索引
* @param subGroup 子条件组数据
*/
const updateSubGroup = (index: number, subGroup: any) => {
function updateSubGroup(index: number, subGroup: any) {
if (trigger.value.conditionGroups) {
trigger.value.conditionGroups[index] = subGroup;
}
};
}
/** 移除整个条件组 */
const removeConditionGroup = () => {
function removeConditionGroup() {
trigger.value.conditionGroups = undefined;
};
}
</script>
<template>
@@ -118,7 +123,7 @@ const removeConditionGroup = () => {
<MainConditionInnerConfig
:model-value="trigger"
@update:model-value="updateCondition"
:trigger-type="trigger.type"
:trigger-type="trigger.type as any"
@trigger-type-change="handleTriggerTypeChange"
/>
</div>
@@ -150,24 +155,19 @@ const removeConditionGroup = () => {
</el-tag>
</div>
<div class="gap-8px flex items-center">
<el-button
<Button
type="primary"
size="small"
@click="addSubGroup"
:disabled="(trigger.conditionGroups?.length || 0) >= maxSubGroups"
>
<Icon icon="ep:plus" />
<IconifyIcon icon="ep:plus" />
添加子条件组
</el-button>
<el-button
type="danger"
size="small"
text
@click="removeConditionGroup"
>
<Icon icon="ep:delete" />
</Button>
<Button danger size="small" text @click="removeConditionGroup">
<IconifyIcon icon="ep:delete" />
删除条件组
</el-button>
</Button>
</div>
</div>
@@ -201,36 +201,36 @@ const removeConditionGroup = () => {
</div>
<span>子条件组 {{ subGroupIndex + 1 }}</span>
</div>
<el-tag size="small" type="warning" class="font-500">
<Tag size="small" type="warning" class="font-500">
组内条件为"且"关系
</el-tag>
<el-tag size="small" type="info">
{{ subGroup?.length || 0 }}个条件
</el-tag>
</Tag>
<Tag size="small" type="info">
{{ (subGroup as any)?.length || 0 }}个条件
</Tag>
</div>
<el-button
type="danger"
<Button
danger
size="small"
text
@click="removeSubGroup(subGroupIndex)"
class="hover:bg-red-50"
>
<Icon icon="ep:delete" />
<IconifyIcon icon="ep:delete" />
删除组
</el-button>
</Button>
</div>
<SubConditionGroupConfig
:model-value="subGroup"
:model-value="subGroup as any"
@update:model-value="
(value) => updateSubGroup(subGroupIndex, value)
"
:trigger-type="trigger.type"
:trigger-type="trigger.type as any"
:max-conditions="maxConditionsPerGroup"
/>
</div>
<!-- 子条件组间的""连接符 -->
<!-- 子条件组间的'或'连接符 -->
<div
v-if="subGroupIndex < trigger.conditionGroups!.length - 1"
class="py-12px flex items-center justify-center"
@@ -258,7 +258,7 @@ const removeConditionGroup = () => {
class="p-24px rounded-8px border-2 border-dashed border-orange-200 bg-orange-50 text-center"
>
<div class="gap-12px flex flex-col items-center">
<Icon icon="ep:plus" class="text-32px text-orange-400" />
<IconifyIcon icon="ep:plus" class="text-32px text-orange-400" />
<div class="text-orange-600">
<p class="text-14px font-500 mb-4px">暂无子条件组</p>
<p class="text-12px">点击上方"添加子条件组"按钮开始配置</p>

View File

@@ -1,7 +1,10 @@
<script setup lang="ts">
import type { Trigger } from '#/api/iot/rule/scene';
import { computed, ref } from 'vue';
import { useVModel } from '@vueuse/core';
import { Col, Form, Row, Select } from 'ant-design-vue';
import {
getTriggerTypeLabel,
@@ -28,7 +31,7 @@ const props = defineProps<{
const emit = defineEmits<{
(e: 'update:modelValue', value: Trigger): void;
(e: 'trigger-type-change', value: number): void;
(e: 'triggerTypeChange', value: number): void;
}>();
/** 获取设备状态变更选项(用于触发器配置) */
@@ -123,36 +126,36 @@ const eventConfig = computed(() => {
* @param field 字段名
* @param value 字段值
*/
const updateConditionField = (field: any, value: any) => {
condition.value[field] = value;
};
function updateConditionField(field: any, value: any) {
(condition.value as any)[field] = value;
}
/**
* 处理触发器类型变化事件
* @param type 触发器类型
*/
const handleTriggerTypeChange = (type: number) => {
emit('trigger-type-change', type);
};
function handleTriggerTypeChange(type: number) {
emit('triggerTypeChange', type);
}
/** 处理产品变化事件 */
const handleProductChange = () => {
function handleProductChange() {
// 产品变化时清空设备和属性
condition.value.deviceId = undefined;
condition.value.identifier = '';
};
}
/** 处理设备变化事件 */
const handleDeviceChange = () => {
function handleDeviceChange() {
// 设备变化时清空属性
condition.value.identifier = '';
};
}
/**
* 处理属性变化事件
* @param propertyInfo 属性信息对象
*/
const handlePropertyChange = (propertyInfo: any) => {
function handlePropertyChange(propertyInfo: any) {
if (propertyInfo) {
propertyType.value = propertyInfo.type;
propertyConfig.value = propertyInfo.config;
@@ -166,34 +169,34 @@ const handlePropertyChange = (propertyInfo: any) => {
IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.value;
}
}
};
}
</script>
<template>
<div class="space-y-16px">
<!-- 触发事件类型选择 -->
<el-form-item label="触发事件类型" required>
<el-select
<Form.Item label="触发事件类型" required>
<Select
:model-value="triggerType"
@update:model-value="handleTriggerTypeChange"
placeholder="请选择触发事件类型"
class="w-full"
>
<el-option
<Select.Option
v-for="option in triggerTypeOptions"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</el-select>
</el-form-item>
</Select>
</Form.Item>
<!-- 设备属性条件配置 -->
<div v-if="isDevicePropertyTrigger" class="space-y-16px">
<!-- 产品设备选择 -->
<el-row :gutter="16">
<el-col :span="12">
<el-form-item label="产品" required>
<Row :gutter="16">
<Col :span="12">
<Form.Item label="产品" required>
<ProductSelector
:model-value="condition.productId"
@update:model-value="
@@ -201,10 +204,10 @@ const handlePropertyChange = (propertyInfo: any) => {
"
@change="handleProductChange"
/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="设备" required>
</Form.Item>
</Col>
<Col :span="12">
<Form.Item label="设备" required>
<DeviceSelector
:model-value="condition.deviceId"
@update:model-value="
@@ -213,15 +216,15 @@ const handlePropertyChange = (propertyInfo: any) => {
:product-id="condition.productId"
@change="handleDeviceChange"
/>
</el-form-item>
</el-col>
</el-row>
</Form.Item>
</Col>
</Row>
<!-- 属性配置 -->
<el-row :gutter="16">
<Row :gutter="16">
<!-- 属性/事件/服务选择 -->
<el-col :span="6">
<el-form-item label="监控项" required>
<Col :span="6">
<Form.Item label="监控项" required>
<PropertySelector
:model-value="condition.identifier"
@update:model-value="
@@ -232,12 +235,12 @@ const handlePropertyChange = (propertyInfo: any) => {
:device-id="condition.deviceId"
@change="handlePropertyChange"
/>
</el-form-item>
</el-col>
</Form.Item>
</Col>
<!-- 操作符选择 - 服务调用和事件上报不需要操作符 -->
<el-col v-if="needsOperatorSelector" :span="6">
<el-form-item label="操作符" required>
<Col v-if="needsOperatorSelector" :span="6">
<Form.Item label="操作符" required>
<OperatorSelector
:model-value="condition.operator"
@update:model-value="
@@ -245,12 +248,12 @@ const handlePropertyChange = (propertyInfo: any) => {
"
:property-type="propertyType"
/>
</el-form-item>
</el-col>
</Form.Item>
</Col>
<!-- 值输入 -->
<el-col :span="isWideValueColumn ? 18 : 12">
<el-form-item :label="valueInputLabel" required>
<Col :span="isWideValueColumn ? 18 : 12">
<Form.Item :label="valueInputLabel" required>
<!-- 服务调用参数配置 -->
<JsonParamsInput
v-if="
@@ -259,7 +262,7 @@ const handlePropertyChange = (propertyInfo: any) => {
"
v-model="condition.value"
type="service"
:config="serviceConfig"
:config="serviceConfig as any"
placeholder="请输入 JSON 格式的服务参数"
/>
<!-- 事件上报参数配置 -->
@@ -269,7 +272,7 @@ const handlePropertyChange = (propertyInfo: any) => {
"
v-model="condition.value"
type="event"
:config="eventConfig"
:config="eventConfig as any"
placeholder="请输入 JSON 格式的事件参数"
/>
<!-- 普通值输入 -->
@@ -283,17 +286,17 @@ const handlePropertyChange = (propertyInfo: any) => {
:operator="condition.operator"
:property-config="propertyConfig"
/>
</el-form-item>
</el-col>
</el-row>
</Form.Item>
</Col>
</Row>
</div>
<!-- 设备状态条件配置 -->
<div v-else-if="isDeviceStatusTrigger" class="space-y-16px">
<!-- 设备状态触发器使用简化的配置 -->
<el-row :gutter="16">
<el-col :span="12">
<el-form-item label="产品" required>
<Row :gutter="16">
<Col :span="12">
<Form.Item label="产品" required>
<ProductSelector
:model-value="condition.productId"
@update:model-value="
@@ -301,10 +304,10 @@ const handlePropertyChange = (propertyInfo: any) => {
"
@change="handleProductChange"
/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="设备" required>
</Form.Item>
</Col>
<Col :span="12">
<Form.Item label="设备" required>
<DeviceSelector
:model-value="condition.deviceId"
@update:model-value="
@@ -313,21 +316,21 @@ const handlePropertyChange = (propertyInfo: any) => {
:product-id="condition.productId"
@change="handleDeviceChange"
/>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="16">
<el-col :span="6">
<el-form-item label="操作符" required>
<el-select
</Form.Item>
</Col>
</Row>
<Row :gutter="16">
<Col :span="6">
<Form.Item label="操作符" required>
<Select
:model-value="condition.operator"
@update:model-value="
(value) => updateConditionField('operator', value)
(value: any) => updateConditionField('operator', value)
"
placeholder="请选择操作符"
class="w-full"
>
<el-option
<Select.Option
:label="
IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.name
"
@@ -335,29 +338,29 @@ const handlePropertyChange = (propertyInfo: any) => {
IotRuleSceneTriggerConditionParameterOperatorEnum.EQUALS.value
"
/>
</el-select>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="参数" required>
<el-select
</Select>
</Form.Item>
</Col>
<Col :span="6">
<Form.Item label="参数" required>
<Select
:model-value="condition.value"
@update:model-value="
(value) => updateConditionField('value', value)
(value: any) => updateConditionField('value', value)
"
placeholder="请选择操作符"
class="w-full"
>
<el-option
<Select.Option
v-for="option in deviceStatusChangeOptions"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</el-select>
</el-form-item>
</el-col>
</el-row>
</Select>
</Form.Item>
</Col>
</Row>
</div>
<!-- 其他触发类型的提示 -->

View File

@@ -1,9 +1,12 @@
<script setup lang="ts">
import type { TriggerCondition } from '#/api/iot/rule/scene';
import { nextTick } from 'vue';
import { computed, nextTick } from 'vue';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Button } from 'ant-design-vue';
import {
IotRuleSceneTriggerConditionParameterOperatorEnum,
@@ -42,7 +45,7 @@ const addCondition = async () => {
}
const newCondition: TriggerCondition = {
type: IotRuleSceneTriggerConditionTypeEnum.DEVICE_PROPERTY, // 默认为设备属性
type: IotRuleSceneTriggerConditionTypeEnum.DEVICE_PROPERTY.toString(), // 默认为设备属性
productId: undefined,
deviceId: undefined,
identifier: '',
@@ -61,22 +64,22 @@ const addCondition = async () => {
* 移除条件
* @param index 条件索引
*/
const removeCondition = (index: number) => {
function removeCondition(index: number) {
if (subGroup.value) {
subGroup.value.splice(index, 1);
}
};
}
/**
* 更新条件
* @param index 条件索引
* @param condition 条件对象
*/
const updateCondition = (index: number, condition: TriggerCondition) => {
function updateCondition(index: number, condition: TriggerCondition) {
if (subGroup.value) {
subGroup.value[index] = condition;
}
};
}
</script>
<template>
@@ -92,10 +95,10 @@ const updateCondition = (index: number, condition: TriggerCondition) => {
<p class="text-14px font-500 mb-4px">暂无条件</p>
<p class="text-12px">点击下方按钮添加第一个条件</p>
</div>
<el-button type="primary" @click="addCondition">
<Button type="primary" @click="addCondition">
<Icon icon="ep:plus" />
添加条件
</el-button>
</Button>
</div>
</div>
@@ -121,26 +124,28 @@ const updateCondition = (index: number, condition: TriggerCondition) => {
</div>
<span
class="text-12px font-500 text-[var(--el-text-color-primary)]"
>条件 {{ conditionIndex + 1 }}</span
>
条件 {{ conditionIndex + 1 }}
</span>
</div>
<el-button
type="danger"
<Button
danger
size="small"
text
@click="removeCondition(conditionIndex)"
v-if="subGroup!.length > 1"
class="hover:bg-red-50"
>
<Icon icon="ep:delete" />
</el-button>
<IconifyIcon icon="ep:delete" />
</Button>
</div>
<div class="p-12px">
<ConditionConfig
:model-value="condition"
@update:model-value="
(value) => updateCondition(conditionIndex, value)
(value: TriggerCondition) =>
updateCondition(conditionIndex, value)
"
:trigger-type="triggerType"
/>
@@ -155,10 +160,10 @@ const updateCondition = (index: number, condition: TriggerCondition) => {
"
class="py-16px text-center"
>
<el-button type="primary" plain @click="addCondition">
<Icon icon="ep:plus" />
<Button type="primary" plain @click="addCondition">
<IconifyIcon icon="ep:plus" />
继续添加条件
</el-button>
</Button>
<span
class="mt-8px text-12px block text-[var(--el-text-color-secondary)]"
>

View File

@@ -2,8 +2,12 @@
<script setup lang="ts">
import type { JsonParamsInputType } from '#/views/iot/utils/constants';
import { InfoFilled } from '@element-plus/icons-vue';
import { computed, nextTick, onMounted, ref, watch } from 'vue';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Button, Input, Popover, Tag } from 'ant-design-vue';
import {
IoTDataSpecsDataTypeEnum,
@@ -44,10 +48,10 @@ interface JsonParamsConfig {
}
interface Props {
modelValue?: string;
config?: JsonParamsConfig;
type?: JsonParamsInputType;
placeholder?: string;
modelValue: string;
config: JsonParamsConfig;
type: JsonParamsInputType;
placeholder: string;
}
interface Emits {
@@ -217,7 +221,7 @@ const noConfigMessage = computed(() => {
/**
* 处理参数变化事件
*/
const handleParamsChange = () => {
function handleParamsChange() {
try {
jsonError.value = ''; // 清除之前的错误
@@ -256,33 +260,33 @@ const handleParamsChange = () => {
: JSON_PARAMS_INPUT_CONSTANTS.UNKNOWN_ERROR,
);
}
};
}
/**
* 快速填充示例数据
*/
const fillExampleJson = () => {
function fillExampleJson() {
paramsJson.value = generateExampleJson();
handleParamsChange();
};
}
/**
* 清空参数
*/
const clearParams = () => {
function clearParams() {
paramsJson.value = '';
localValue.value = '';
jsonError.value = '';
};
}
/**
* 获取参数类型名称
* @param dataType 数据类型
* @returns 类型名称
*/
const getParamTypeName = (dataType: string) => {
function getParamTypeName(dataType: string) {
// 使用 constants.ts 中已有的 getDataTypeName 函数逻辑
const typeMap = {
const typeMap: Record<string, string> = {
[IoTDataSpecsDataTypeEnum.INT]: '整数',
[IoTDataSpecsDataTypeEnum.FLOAT]: '浮点数',
[IoTDataSpecsDataTypeEnum.DOUBLE]: '双精度',
@@ -294,15 +298,15 @@ const getParamTypeName = (dataType: string) => {
[IoTDataSpecsDataTypeEnum.ARRAY]: '数组',
};
return typeMap[dataType] || dataType;
};
}
/**
* 获取参数类型标签样式
* @param dataType 数据类型
* @returns 标签样式
*/
const getParamTypeTag = (dataType: string) => {
const tagMap = {
function getParamTypeTag(dataType: string) {
const tagMap: Record<string, string> = {
[IoTDataSpecsDataTypeEnum.INT]: 'primary',
[IoTDataSpecsDataTypeEnum.FLOAT]: 'success',
[IoTDataSpecsDataTypeEnum.DOUBLE]: 'success',
@@ -314,45 +318,45 @@ const getParamTypeTag = (dataType: string) => {
[IoTDataSpecsDataTypeEnum.ARRAY]: 'warning',
};
return tagMap[dataType] || 'info';
};
}
/**
* 获取示例值
* @param param 参数对象
* @returns 示例值
*/
const getExampleValue = (param: any) => {
const exampleConfig =
function getExampleValue(param: any) {
const exampleConfig: any =
JSON_PARAMS_EXAMPLE_VALUES[param.dataType] ||
JSON_PARAMS_EXAMPLE_VALUES.DEFAULT;
return exampleConfig.display;
};
}
/**
* 生成示例JSON
* @returns JSON字符串
*/
const generateExampleJson = () => {
function generateExampleJson() {
if (paramsList.value.length === 0) {
return '{}';
}
const example = {};
const example: Record<string, any> = {};
paramsList.value.forEach((param) => {
const exampleConfig =
const exampleConfig: any =
JSON_PARAMS_EXAMPLE_VALUES[param.dataType] ||
JSON_PARAMS_EXAMPLE_VALUES.DEFAULT;
example[param.identifier] = exampleConfig.value;
});
return JSON.stringify(example, null, 2);
};
}
/**
* 处理数据回显
* @param value 值字符串
*/
const handleDataDisplay = (value: string) => {
function handleDataDisplay(value: string) {
if (!value || !value.trim()) {
paramsJson.value = '';
jsonError.value = '';
@@ -369,7 +373,7 @@ const handleDataDisplay = (value: string) => {
paramsJson.value = value;
jsonError.value = '';
}
};
}
// 监听外部值变化(编辑模式数据回显)
watch(
@@ -414,9 +418,9 @@ watch(
<div class="space-y-12px w-full">
<!-- JSON 输入框 -->
<div class="relative">
<el-input
<Input.TextArea
v-model="paramsJson"
type="textarea"
type="text"
:rows="4"
:placeholder="placeholder"
@input="handleParamsChange"
@@ -424,8 +428,8 @@ watch(
/>
<!-- 查看详细示例弹出层 -->
<div class="top-8px right-8px absolute">
<el-popover
placement="left-start"
<Popover
placement="leftTop"
:width="450"
trigger="click"
:show-arrow="true"
@@ -433,19 +437,21 @@ watch(
popper-class="json-params-detail-popover"
>
<template #reference>
<el-button
type="info"
:icon="InfoFilled"
<Button
text
type="primary"
circle
size="small"
:title="JSON_PARAMS_INPUT_CONSTANTS.VIEW_EXAMPLE_TITLE"
/>
>
<IconifyIcon icon="ep:info-filled" />
</Button>
</template>
<!-- 弹出层内容 -->
<div class="json-params-detail-content">
<div class="gap-8px mb-16px flex items-center">
<Icon
<IconifyIcon
:icon="titleIcon"
class="text-18px text-[var(--el-color-primary)]"
/>
@@ -460,7 +466,7 @@ watch(
<!-- 参数列表 -->
<div v-if="paramsList.length > 0">
<div class="gap-8px mb-8px flex items-center">
<Icon
<IconifyIcon
:icon="paramsIcon"
class="text-14px text-[var(--el-color-primary)]"
/>
@@ -481,14 +487,14 @@ watch(
class="text-12px font-500 text-[var(--el-text-color-primary)]"
>
{{ param.name }}
<el-tag
<Tag
v-if="param.required"
size="small"
type="danger"
class="ml-4px"
>
{{ JSON_PARAMS_INPUT_CONSTANTS.REQUIRED_TAG }}
</el-tag>
</Tag>
</div>
<div
class="text-11px text-[var(--el-text-color-secondary)]"
@@ -497,12 +503,9 @@ watch(
</div>
</div>
<div class="gap-8px flex items-center">
<el-tag
:type="getParamTypeTag(param.dataType)"
size="small"
>
<Tag :type="getParamTypeTag(param.dataType)" size="small">
{{ getParamTypeName(param.dataType) }}
</el-tag>
</Tag>
<span
class="text-11px text-[var(--el-text-color-secondary)]"
>
@@ -536,14 +539,14 @@ watch(
</div>
</div>
</div>
</el-popover>
</Popover>
</div>
</div>
<!-- 验证状态和错误提示 -->
<div class="flex items-center justify-between">
<div class="gap-8px flex items-center">
<Icon
<IconifyIcon
:icon="
jsonError
? JSON_PARAMS_INPUT_ICONS.STATUS_ICONS.ERROR
@@ -573,12 +576,12 @@ watch(
<span class="text-12px text-[var(--el-text-color-secondary)]">{{
JSON_PARAMS_INPUT_CONSTANTS.QUICK_FILL_LABEL
}}</span>
<el-button size="small" type="primary" plain @click="fillExampleJson">
<Button size="small" type="primary" plain @click="fillExampleJson">
{{ JSON_PARAMS_INPUT_CONSTANTS.EXAMPLE_DATA_BUTTON }}
</el-button>
<el-button size="small" type="danger" plain @click="clearParams">
</Button>
<Button size="small" danger type="primary" @click="clearParams">
{{ JSON_PARAMS_INPUT_CONSTANTS.CLEAR_BUTTON }}
</el-button>
</Button>
</div>
</div>
</div>
@@ -595,7 +598,7 @@ watch(
max-width: 500px !important;
}
:global(.json-params-detail-popover .el-popover__content) {
:global(.json-params-detail-popover .ant-popover__content) {
padding: 16px !important;
}

View File

@@ -1,6 +1,9 @@
<!-- 值输入组件 -->
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useVModel } from '@vueuse/core';
import { DatePicker, Input, Select, Tag, Tooltip } from 'ant-design-vue';
import {
IoTDataSpecsDataTypeEnum,
@@ -61,16 +64,16 @@ const listPreview = computed(() => {
});
/** 判断是否为数字类型 */
const isNumericType = () => {
function isNumericType() {
return [
IoTDataSpecsDataTypeEnum.DOUBLE,
IoTDataSpecsDataTypeEnum.FLOAT,
IoTDataSpecsDataTypeEnum.INT,
].includes((props.propertyType || '') as any);
};
}
/** 获取输入框类型 */
const getInputType = () => {
function getInputType() {
switch (props.propertyType) {
case IoTDataSpecsDataTypeEnum.DOUBLE:
case IoTDataSpecsDataTypeEnum.FLOAT:
@@ -81,11 +84,11 @@ const getInputType = () => {
return 'text';
}
}
};
}
/** 获取占位符文本 */
const getPlaceholder = () => {
const typeMap = {
function getPlaceholder() {
const typeMap: Record<string, string> = {
[IoTDataSpecsDataTypeEnum.TEXT]: '请输入字符串',
[IoTDataSpecsDataTypeEnum.INT]: '请输入整数',
[IoTDataSpecsDataTypeEnum.FLOAT]: '请输入浮点数',
@@ -94,45 +97,45 @@ const getPlaceholder = () => {
[IoTDataSpecsDataTypeEnum.ARRAY]: '请输入数组格式数据',
};
return typeMap[props.propertyType || ''] || '请输入值';
};
}
/** 获取数字精度 */
const getPrecision = () => {
function getPrecision() {
return props.propertyType === IoTDataSpecsDataTypeEnum.INT ? 0 : 2;
};
}
/** 获取数字步长 */
const getStep = () => {
function getStep() {
return props.propertyType === IoTDataSpecsDataTypeEnum.INT ? 1 : 0.1;
};
}
/** 获取最小值 */
const getMin = () => {
function getMin() {
return props.propertyConfig?.min || undefined;
};
}
/** 获取最大值 */
const getMax = () => {
function getMax() {
return props.propertyConfig?.max || undefined;
};
}
/** 处理范围变化事件 */
const handleRangeChange = () => {
function handleRangeChange() {
localValue.value =
rangeStart.value && rangeEnd.value
? `${rangeStart.value},${rangeEnd.value}`
: '';
};
}
/** 处理日期变化事件 */
const handleDateChange = (value: string) => {
function handleDateChange(value: any) {
localValue.value = value || '';
};
}
/** 处理数字变化事件 */
const handleNumberChange = (value: number | undefined) => {
function handleNumberChange(value: number | undefined) {
localValue.value = value?.toString() || '';
};
}
/** 监听操作符变化 */
watch(
@@ -150,18 +153,18 @@ watch(
<template>
<div class="w-full min-w-0">
<!-- 布尔值选择 -->
<el-select
<Select
v-if="propertyType === IoTDataSpecsDataTypeEnum.BOOL"
v-model="localValue"
placeholder="请选择布尔值"
class="w-full!"
>
<el-option label="真 (true)" value="true" />
<el-option label="假 (false)" value="false" />
</el-select>
<Select.Option label="真 (true)" :value="true" />
<Select.Option label="假 (false)" :value="false" />
</Select>
<!-- 枚举值选择 -->
<el-select
<Select
v-else-if="
propertyType === IoTDataSpecsDataTypeEnum.ENUM && enumOptions.length > 0
"
@@ -169,13 +172,13 @@ watch(
placeholder="请选择枚举值"
class="w-full!"
>
<el-option
<Select.Option
v-for="option in enumOptions"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</el-select>
</Select>
<!-- 范围输入 (between 操作符) -->
<div
@@ -185,7 +188,7 @@ watch(
"
class="w-full! gap-8px flex items-center"
>
<el-input
<Input
v-model="rangeStart"
:type="getInputType()"
placeholder="最小值"
@@ -195,8 +198,10 @@ watch(
/>
<span
class="text-12px whitespace-nowrap text-[var(--el-text-color-secondary)]"
>至</span>
<el-input
>
</span>
<Input
v-model="rangeEnd"
:type="getInputType()"
placeholder="最大值"
@@ -212,38 +217,40 @@ watch(
"
class="w-full!"
>
<el-input
<Input
v-model="localValue"
placeholder="请输入值列表,用逗号分隔"
class="w-full!"
>
<template #suffix>
<el-tooltip content="多个值用逗号分隔1,2,3" placement="top">
<Icon
<Tooltip content="多个值用逗号分隔1,2,3" placement="top">
<IconifyIcon
icon="ep:question-filled"
class="cursor-help text-[var(--el-text-color-placeholder)]"
/>
</el-tooltip>
</Tooltip>
</template>
</el-input>
</Input>
<div
v-if="listPreview.length > 0"
class="mt-8px gap-6px flex flex-wrap items-center"
>
<span class="text-12px text-[var(--el-text-color-secondary)]">解析结果:</span>
<el-tag
<span class="text-12px text-[var(--el-text-color-secondary)]">
解析结果:
</span>
<Tag
v-for="(item, index) in listPreview"
:key="index"
size="small"
class="m-0"
>
{{ item }}
</el-tag>
</Tag>
</div>
</div>
<!-- 日期时间输入 -->
<el-date-picker
<DatePicker
v-else-if="propertyType === IoTDataSpecsDataTypeEnum.DATE"
v-model="dateValue"
type="datetime"
@@ -255,7 +262,7 @@ watch(
/>
<!-- 数字输入 -->
<el-input-number
<Input.Number
v-else-if="isNumericType()"
v-model="numberValue"
:precision="getPrecision()"
@@ -268,7 +275,7 @@ watch(
/>
<!-- 文本输入 -->
<el-input
<Input
v-else
v-model="localValue"
:type="getInputType()"
@@ -276,7 +283,7 @@ watch(
class="w-full!"
>
<template #suffix>
<el-tooltip
<Tooltip
v-if="propertyConfig?.unit"
:content="`单位:${propertyConfig.unit}`"
placement="top"
@@ -284,8 +291,8 @@ watch(
<span class="text-12px px-4px text-[var(--el-text-color-secondary)]">
{{ propertyConfig.unit }}
</span>
</el-tooltip>
</Tooltip>
</template>
</el-input>
</Input>
</div>
</template>

View File

@@ -2,7 +2,10 @@
<script setup lang="ts">
import type { Action } from '#/api/iot/rule/scene';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Button, Card, Empty, Form, Select, Tag } from 'ant-design-vue';
import {
getActionTypeLabel,
@@ -27,110 +30,116 @@ const emit = defineEmits<{
const actions = useVModel(props, 'actions', emit);
/** 获取执行器标签类型(用于 el-tag 的 type 属性) */
const getActionTypeTag = (
function getActionTypeTag(
type: number,
): 'danger' | 'info' | 'primary' | 'success' | 'warning' => {
const actionTypeTags = {
): 'danger' | 'info' | 'primary' | 'success' | 'warning' {
const actionTypeTags: Record<
number,
'danger' | 'info' | 'primary' | 'success' | 'warning'
> = {
[IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET]: 'primary',
[IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE]: 'success',
[IotRuleSceneActionTypeEnum.ALERT_TRIGGER]: 'danger',
[IotRuleSceneActionTypeEnum.ALERT_RECOVER]: 'warning',
} as const;
return actionTypeTags[type] || 'info';
};
}
/** 判断是否为设备执行器类型 */
const isDeviceAction = (type: number): boolean => {
function isDeviceAction(type: number): boolean {
const deviceActionTypes = [
IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET,
IotRuleSceneActionTypeEnum.DEVICE_SERVICE_INVOKE,
] as number[];
return deviceActionTypes.includes(type);
};
}
/** 判断是否为告警执行器类型 */
const isAlertAction = (type: number): boolean => {
function isAlertAction(type: number): boolean {
const alertActionTypes = [
IotRuleSceneActionTypeEnum.ALERT_TRIGGER,
IotRuleSceneActionTypeEnum.ALERT_RECOVER,
] as number[];
return alertActionTypes.includes(type);
};
}
/**
* 创建默认的执行器数据
* @returns 默认执行器对象
*/
const createDefaultActionData = (): Action => {
function createDefaultActionData(): Action {
return {
type: IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET, // 默认为设备属性设置
type: IotRuleSceneActionTypeEnum.DEVICE_PROPERTY_SET.toString(), // 默认为设备属性设置
productId: undefined,
deviceId: undefined,
identifier: undefined, // 物模型标识符(服务调用时使用)
params: undefined,
alertConfigId: undefined,
};
};
}
/**
* 添加执行器
*/
const addAction = () => {
function addAction() {
const newAction = createDefaultActionData();
actions.value.push(newAction);
};
}
/**
* 删除执行器
* @param index 执行器索引
*/
const removeAction = (index: number) => {
function removeAction(index: number) {
actions.value.splice(index, 1);
};
}
/**
* 更新执行器类型
* @param index 执行器索引
* @param type 执行器类型
*/
const updateActionType = (index: number, type: number) => {
actions.value[index].type = type;
onActionTypeChange(actions.value[index], type);
};
function updateActionType(index: number, type: number) {
actions.value[index].type = type.toString();
onActionTypeChange(actions.value[index] as Action, type);
}
/**
* 更新执行器
* @param index 执行器索引
* @param action 执行器对象
*/
const updateAction = (index: number, action: Action) => {
function updateAction(index: number, action: Action) {
actions.value[index] = action;
};
}
/**
* 更新告警配置
* @param index 执行器索引
* @param alertConfigId 告警配置ID
*/
const updateActionAlertConfig = (index: number, alertConfigId?: number) => {
function updateActionAlertConfig(index: number, alertConfigId?: number) {
actions.value[index].alertConfigId = alertConfigId;
};
if (actions.value[index]) {
actions.value[index].alertConfigId = alertConfigId;
}
}
/**
* 监听执行器类型变化
* @param action 执行器对象
* @param type 执行器类型
*/
const onActionTypeChange = (action: Action, type: number) => {
function onActionTypeChange(action: Action, type: any) {
// 清理不相关的配置,确保数据结构干净
if (isDeviceAction(type)) {
// 设备控制类型:清理告警配置,确保设备参数存在
action.alertConfigId = undefined;
if (!action.params) {
action.params = '';
if (!(action as any).params) {
(action as any).params = '';
}
// 如果从其他类型切换到设备控制类型清空identifier让用户重新选择
if (action.identifier && type !== action.type) {
if (action.identifier && type !== (action as any).type) {
action.identifier = undefined;
}
} else if (isAlertAction(type)) {
@@ -140,33 +149,23 @@ const onActionTypeChange = (action: Action, type: number) => {
action.params = undefined;
action.alertConfigId = undefined;
}
};
}
</script>
<template>
<el-card
class="rounded-8px border border-[var(--el-border-color-light)]"
shadow="never"
>
<template #header>
<Card class="rounded-8px border-primary border" shadow="never">
<template #title>
<div class="flex items-center justify-between">
<div class="gap-8px flex items-center">
<Icon
icon="ep:setting"
class="text-18px text-[var(--el-color-primary)]"
/>
<span class="text-16px font-600 text-[var(--el-text-color-primary)]"
>执行器配置</span
>
<el-tag size="small" type="info">
{{ actions.length }} 个执行器
</el-tag>
<IconifyIcon icon="ep:setting" class="text-18px text-primary" />
<span class="text-16px font-600 text-primary"> 执行器配置 </span>
<Tag size="small" type="info"> {{ actions.length }} 个执行器 </Tag>
</div>
<div class="gap-8px flex items-center">
<el-button type="primary" size="small" @click="addAction">
<Icon icon="ep:plus" />
<Button type="primary" size="small" @click="addAction">
<IconifyIcon icon="ep:plus" />
添加执行器
</el-button>
</Button>
</div>
</div>
</template>
@@ -174,12 +173,12 @@ const onActionTypeChange = (action: Action, type: number) => {
<div class="p-0">
<!-- 空状态 -->
<div v-if="actions.length === 0">
<el-empty description="暂无执行器配置">
<el-button type="primary" @click="addAction">
<Icon icon="ep:plus" />
<Empty description="暂无执行器配置">
<Button type="primary" @click="addAction">
<IconifyIcon icon="ep:plus" />
添加第一个执行器
</el-button>
</el-empty>
</Button>
</Empty>
</div>
<!-- 执行器列表 -->
@@ -204,26 +203,26 @@ const onActionTypeChange = (action: Action, type: number) => {
</div>
<span>执行器 {{ index + 1 }}</span>
</div>
<el-tag
:type="getActionTypeTag(action.type)"
<Tag
:type="getActionTypeTag(action.type as any)"
size="small"
class="font-500"
>
{{ getActionTypeLabel(action.type) }}
</el-tag>
{{ getActionTypeLabel(action.type as any) }}
</Tag>
</div>
<div class="gap-8px flex items-center">
<el-button
<Button
v-if="actions.length > 1"
type="danger"
danger
size="small"
text
@click="removeAction(index)"
class="hover:bg-red-50"
>
<Icon icon="ep:delete" />
<IconifyIcon icon="ep:delete" />
删除
</el-button>
</Button>
</div>
</div>
@@ -231,36 +230,39 @@ const onActionTypeChange = (action: Action, type: number) => {
<div class="p-16px space-y-16px">
<!-- 执行类型选择 -->
<div class="w-full">
<el-form-item label="执行类型" required>
<el-select
<Form.Item label="执行类型" required>
<Select
:model-value="action.type"
@update:model-value="
(value) => updateActionType(index, value)
(value: number) => updateActionType(index, value)
"
@change="(value) => onActionTypeChange(action, value)"
placeholder="请选择执行类型"
class="w-full"
>
<el-option
<Select.Option
v-for="option in getActionTypeOptions()"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</el-select>
</el-form-item>
</Select>
</Form.Item>
</div>
<!-- 设备控制配置 -->
<DeviceControlConfig
v-if="isDeviceAction(action.type)"
v-if="isDeviceAction(action.type as any)"
:model-value="action"
@update:model-value="(value) => updateAction(index, value)"
/>
<!-- 告警配置 - 只有恢复告警时才显示 -->
<AlertConfig
v-if="action.type === IotRuleSceneActionTypeEnum.ALERT_RECOVER"
v-if="
action.type ===
IotRuleSceneActionTypeEnum.ALERT_RECOVER.toString()
"
:model-value="action.alertConfigId"
@update:model-value="
(value) => updateActionAlertConfig(index, value)
@@ -269,19 +271,16 @@ const onActionTypeChange = (action: Action, type: number) => {
<!-- 触发告警提示 - 触发告警时显示 -->
<div
v-if="action.type === IotRuleSceneActionTypeEnum.ALERT_TRIGGER"
class="rounded-6px p-16px border border-[var(--el-border-color-light)] bg-[var(--el-fill-color-blank)]"
v-if="
action.type ===
IotRuleSceneActionTypeEnum.ALERT_TRIGGER.toString()
"
class="rounded-6px p-16px border-border bg-fill-color-blank border"
>
<div class="gap-8px mb-8px flex items-center">
<Icon
icon="ep:warning"
class="text-16px text-[var(--el-color-warning)]"
/>
<span
class="text-14px font-600 text-[var(--el-text-color-primary)]"
>触发告警</span
>
<el-tag size="small" type="warning">自动执行</el-tag>
<IconifyIcon icon="ep:warning" class="text-16px text-warning" />
<span class="text-14px font-600 text-primary">触发告警</span>
<Tag size="small" type="warning">自动执行</Tag>
</div>
<div
class="text-12px leading-relaxed text-[var(--el-text-color-secondary)]"
@@ -296,11 +295,11 @@ const onActionTypeChange = (action: Action, type: number) => {
<!-- 添加提示 -->
<div v-if="actions.length > 0" class="py-16px text-center">
<el-button type="primary" plain @click="addAction">
<Icon icon="ep:plus" />
<Button type="primary" plain @click="addAction">
<IconifyIcon icon="ep:plus" />
继续添加执行器
</el-button>
</Button>
</div>
</div>
</el-card>
</Card>
</template>

View File

@@ -2,9 +2,12 @@
<script setup lang="ts">
import type { IotSceneRule } from '#/api/iot/rule/scene';
import { DICT_TYPE, getIntDictOptions } from '@vben/constants';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Card, Col, Form, Input, Radio, Row } from 'ant-design-vue';
/** 基础信息配置组件 */
defineOptions({ name: 'BasicInfoSection' });
@@ -22,65 +25,62 @@ const formData = useVModel(props, 'modelValue', emit); // 表单数据
</script>
<template>
<el-card
class="rounded-8px mb-10px border border-[var(--el-border-color-light)]"
shadow="never"
>
<template #header>
<Card class="rounded-8px mb-10px border-primary border" shadow="never">
<template #title>
<div class="flex items-center justify-between">
<div class="gap-8px flex items-center">
<Icon
icon="ep:info-filled"
class="text-18px text-[var(--el-color-primary)]"
/>
<span class="text-16px font-600 text-[var(--el-text-color-primary)]">基础信息</span>
<IconifyIcon icon="ep:info-filled" class="text-18px text-primary" />
<span class="text-16px font-600 text-primary">基础信息</span>
</div>
<div class="gap-8px flex items-center">
<dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="formData.status" />
<DictTag :type="DICT_TYPE.COMMON_STATUS" :value="formData.status" />
</div>
</div>
</template>
<div class="p-0">
<el-row :gutter="24" class="mb-24px">
<el-col :span="12">
<el-form-item label="场景名称" prop="name" required>
<el-input
<Row :gutter="24" class="mb-24px">
<Col :span="12">
<Form.Item label="场景名称" prop="name" required>
<Input
v-model="formData.name"
placeholder="请输入场景名称"
maxlength="50"
:maxlength="50"
show-word-limit
clearable
/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="场景状态" prop="status" required>
<el-radio-group v-model="formData.status">
<el-radio
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
:key="dict.value"
</Form.Item>
</Col>
<Col :span="12">
<Form.Item label="场景状态" prop="status" required>
<Radio.Group v-model="formData.status">
<Radio
v-for="(dict, index) in getDictOptions(
DICT_TYPE.COMMON_STATUS,
'number',
)"
:key="index"
:label="dict.value"
>
{{ dict.label }}
</el-radio>
</el-radio-group>
</el-form-item>
</el-col>
</el-row>
<el-form-item label="场景描述" prop="description">
<el-input
</Radio>
</Radio.Group>
</Form.Item>
</Col>
</Row>
<Form.Item label="场景描述" prop="description">
<Input.TextArea
v-model="formData.description"
type="textarea"
type="text"
placeholder="请输入场景描述(可选)"
:rows="3"
maxlength="200"
:maxlength="200"
show-word-limit
resize="none"
/>
</el-form-item>
</Form.Item>
</div>
</el-card>
</Card>
</template>
<style scoped>

View File

@@ -1,9 +1,14 @@
<script setup lang="ts">
import type { Trigger } from '#/api/iot/rule/scene';
import { Crontab } from '@/components/Crontab';
import { useVModel } from '@vueuse/core';
import { onMounted } from 'vue';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Button, Card, Empty, Form, Tag } from 'ant-design-vue';
import { CronTab } from '#/components/cron-tab';
import {
getTriggerTypeLabel,
IotRuleSceneTriggerTypeEnum,
@@ -26,19 +31,19 @@ const emit = defineEmits<{
const triggers = useVModel(props, 'triggers', emit);
/** 获取触发器标签类型(用于 el-tag 的 type 属性) */
const getTriggerTagType = (
function getTriggerTagType(
type: number,
): 'danger' | 'info' | 'primary' | 'success' | 'warning' => {
): 'danger' | 'info' | 'primary' | 'success' | 'warning' {
if (type === IotRuleSceneTriggerTypeEnum.TIMER) {
return 'warning';
}
return isDeviceTrigger(type) ? 'success' : 'info';
};
}
/** 添加触发器 */
const addTrigger = () => {
function addTrigger() {
const newTrigger: Trigger = {
type: IotRuleSceneTriggerTypeEnum.DEVICE_STATE_UPDATE,
type: IotRuleSceneTriggerTypeEnum.DEVICE_STATE_UPDATE.toString(),
productId: undefined,
deviceId: undefined,
identifier: undefined,
@@ -48,52 +53,52 @@ const addTrigger = () => {
conditionGroups: [], // 空的条件组数组
};
triggers.value.push(newTrigger);
};
}
/**
* 删除触发器
* @param index 触发器索引
*/
const removeTrigger = (index: number) => {
function removeTrigger(index: number) {
if (triggers.value.length > 1) {
triggers.value.splice(index, 1);
}
};
}
/**
* 更新触发器类型
* @param index 触发器索引
* @param type 触发器类型
*/
const updateTriggerType = (index: number, type: number) => {
function updateTriggerType(index: number, type: number) {
triggers.value[index].type = type;
onTriggerTypeChange(index, type);
};
}
/**
* 更新触发器设备配置
* @param index 触发器索引
* @param newTrigger 新的触发器对象
*/
const updateTriggerDeviceConfig = (index: number, newTrigger: Trigger) => {
function updateTriggerDeviceConfig(index: number, newTrigger: Trigger) {
triggers.value[index] = newTrigger;
};
}
/**
* 更新触发器 CRON 配置
* @param index 触发器索引
* @param cronExpression CRON 表达式
*/
const updateTriggerCronConfig = (index: number, cronExpression?: string) => {
function updateTriggerCronConfig(index: number, cronExpression?: string) {
triggers.value[index].cronExpression = cronExpression;
};
}
/**
* 处理触发器类型变化事件
* @param index 触发器索引
* @param _ 触发器类型(未使用)
*/
const onTriggerTypeChange = (index: number, _: number) => {
function onTriggerTypeChange(index: number, _: number) {
const triggerItem = triggers.value[index];
triggerItem.productId = undefined;
triggerItem.deviceId = undefined;
@@ -102,7 +107,7 @@ const onTriggerTypeChange = (index: number, _: number) => {
triggerItem.value = undefined;
triggerItem.cronExpression = undefined;
triggerItem.conditionGroups = [];
};
}
/** 初始化:确保至少有一个触发器 */
onMounted(() => {
@@ -113,28 +118,18 @@ onMounted(() => {
</script>
<template>
<el-card
class="rounded-8px mb-10px border border-[var(--el-border-color-light)]"
shadow="never"
>
<template #header>
<Card class="rounded-8px mb-10px border-primary border" shadow="never">
<template #title>
<div class="flex items-center justify-between">
<div class="gap-8px flex items-center">
<Icon
icon="ep:lightning"
class="text-18px text-[var(--el-color-primary)]"
/>
<span class="text-16px font-600 text-[var(--el-text-color-primary)]"
>触发器配置</span
>
<el-tag size="small" type="info">
{{ triggers.length }} 个触发器
</el-tag>
<IconifyIcon icon="ep:lightning" class="text-18px text-primary" />
<span class="text-16px font-600 text-primary">触发器配置</span>
<Tag size="small" type="info"> {{ triggers.length }} 个触发器 </Tag>
</div>
<el-button type="primary" size="small" @click="addTrigger">
<Icon icon="ep:plus" />
<Button type="primary" size="small" @click="addTrigger">
<IconifyIcon icon="ep:plus" />
添加触发器
</el-button>
</Button>
</div>
</template>
@@ -161,26 +156,26 @@ onMounted(() => {
</div>
<span>触发器 {{ index + 1 }}</span>
</div>
<el-tag
<Tag
size="small"
:type="getTriggerTagType(triggerItem.type)"
:type="getTriggerTagType(triggerItem.type as any)"
class="font-500"
>
{{ getTriggerTypeLabel(triggerItem.type) }}
</el-tag>
{{ getTriggerTypeLabel(triggerItem.type as any) }}
</Tag>
</div>
<div class="gap-8px flex items-center">
<el-button
<Button
v-if="triggers.length > 1"
type="danger"
danger
size="small"
text
@click="removeTrigger(index)"
class="hover:bg-red-50"
>
<Icon icon="ep:delete" />
<IconifyIcon icon="ep:delete" />
删除
</el-button>
</Button>
</div>
</div>
@@ -188,7 +183,7 @@ onMounted(() => {
<div class="p-16px space-y-16px">
<!-- 设备触发配置 -->
<DeviceTriggerConfig
v-if="isDeviceTrigger(triggerItem.type)"
v-if="isDeviceTrigger(triggerItem.type as any)"
:model-value="triggerItem"
:index="index"
@update:model-value="
@@ -199,34 +194,33 @@ onMounted(() => {
<!-- 定时触发配置 -->
<div
v-else-if="triggerItem.type === IotRuleSceneTriggerTypeEnum.TIMER"
v-else-if="
triggerItem.type ===
IotRuleSceneTriggerTypeEnum.TIMER.toString()
"
class="gap-16px flex flex-col"
>
<div
class="gap-8px p-12px px-16px rounded-6px flex items-center border border-[var(--el-border-color-lighter)] bg-[var(--el-fill-color-light)]"
class="gap-8px p-12px px-16px rounded-6px border-primary bg-background flex items-center border"
>
<Icon
icon="ep:timer"
class="text-18px text-[var(--el-color-danger)]"
/>
<span
class="text-14px font-500 text-[var(--el-text-color-primary)]"
>定时触发配置</span
>
<IconifyIcon icon="ep:timer" class="text-18px text-danger" />
<span class="text-14px font-500 text-primary">
定时触发配置
</span>
</div>
<!-- CRON 表达式配置 -->
<div
class="p-16px rounded-6px border border-[var(--el-border-color-lighter)] bg-[var(--el-fill-color-blank)]"
class="p-16px rounded-6px border-primary bg-background border"
>
<el-form-item label="CRON表达式" required>
<Crontab
<Form.Item label="CRON表达式" required>
<CronTab
:model-value="triggerItem.cronExpression || '0 0 12 * * ?'"
@update:model-value="
(value) => updateTriggerCronConfig(index, value)
"
/>
</el-form-item>
</Form.Item>
</div>
</div>
</div>
@@ -235,19 +229,17 @@ onMounted(() => {
<!-- 空状态 -->
<div v-else class="py-40px text-center">
<el-empty description="暂无触发器">
<Empty description="暂无触发器">
<template #description>
<div class="space-y-8px">
<p class="text-[var(--el-text-color-secondary)]">
暂无触发器配置
</p>
<p class="text-12px text-[var(--el-text-color-placeholder)]">
<p class="text-secondary">暂无触发器配置</p>
<p class="text-12px text-primary">
请使用上方的"添加触发器"按钮来设置触发规则
</p>
</div>
</template>
</el-empty>
</Empty>
</div>
</div>
</el-card>
</Card>
</template>

View File

@@ -1,8 +1,13 @@
<!-- 设备选择器组件 -->
<script setup lang="ts">
import { ref, watch } from 'vue';
import { DICT_TYPE } from '@vben/constants';
import { DeviceApi } from '#/api/iot/device/device';
import { Select } from 'ant-design-vue';
import { getDeviceListByProductId } from '#/api/iot/device/device';
import { DictTag } from '#/components/dict-tag';
import { DEVICE_SELECTOR_OPTIONS } from '#/views/iot/utils/constants';
/** 设备选择器组件 */
@@ -25,15 +30,15 @@ const deviceList = ref<any[]>([]); // 设备列表
* 处理选择变化事件
* @param value 选中的设备ID
*/
const handleChange = (value?: number) => {
function handleChange(value?: number) {
emit('update:modelValue', value);
emit('change', value);
};
}
/**
* 获取设备列表
*/
const getDeviceList = async () => {
async function getDeviceList() {
if (!props.productId) {
deviceList.value = [];
return;
@@ -41,7 +46,7 @@ const getDeviceList = async () => {
try {
deviceLoading.value = true;
const res = await DeviceApi.getDeviceListByProductId(props.productId);
const res = await getDeviceListByProductId(props.productId);
deviceList.value = res || [];
} catch (error) {
console.error('获取设备列表失败:', error);
@@ -50,7 +55,7 @@ const getDeviceList = async () => {
deviceList.value.unshift(DEVICE_SELECTOR_OPTIONS.ALL_DEVICES);
deviceLoading.value = false;
}
};
}
// 监听产品变化
watch(
@@ -72,7 +77,7 @@ watch(
</script>
<template>
<el-select
<Select
:model-value="modelValue"
@update:model-value="handleChange"
placeholder="请选择设备"
@@ -82,7 +87,7 @@ watch(
:loading="deviceLoading"
:disabled="!productId"
>
<el-option
<Select.Option
v-for="device in deviceList"
:key="device.id"
:label="device.deviceName"
@@ -90,19 +95,17 @@ watch(
>
<div class="py-4px flex w-full items-center justify-between">
<div class="flex-1">
<div
class="text-14px font-500 mb-2px text-[var(--el-text-color-primary)]"
>
<div class="text-14px font-500 mb-2px text-primary">
{{ device.deviceName }}
</div>
<div class="text-12px text-[var(--el-text-color-secondary)]">
<div class="text-12px text-primary">
{{ device.deviceKey }}
</div>
</div>
<div class="gap-4px flex items-center" v-if="device.id > 0">
<dict-tag :type="DICT_TYPE.IOT_DEVICE_STATE" :value="device.state" />
<DictTag :type="DICT_TYPE.IOT_DEVICE_STATE" :value="device.state" />
</div>
</div>
</el-option>
</el-select>
</Select.Option>
</Select>
</template>

View File

@@ -1,6 +1,9 @@
<!-- 操作符选择器组件 -->
<script setup lang="ts">
import { computed, watch } from 'vue';
import { useVModel } from '@vueuse/core';
import { Select } from 'ant-design-vue';
import {
IoTDataSpecsDataTypeEnum,
@@ -211,9 +214,9 @@ const selectedOperator = computed(() => {
* 处理选择变化事件
* @param value 选中的操作符值
*/
const handleChange = (value: string) => {
function handleChange(value: any) {
emit('change', value);
};
}
/** 监听属性类型变化 */
watch(
@@ -235,13 +238,13 @@ watch(
<template>
<div class="w-full">
<el-select
<Select
v-model="localValue"
placeholder="请选择操作符"
@change="handleChange"
class="w-full"
>
<el-option
<Select.Option
v-for="operator in availableOperators"
:key="operator.value"
:label="operator.label"
@@ -249,21 +252,21 @@ watch(
>
<div class="py-4px flex w-full items-center justify-between">
<div class="gap-8px flex items-center">
<div class="text-14px font-500 text-[var(--el-text-color-primary)]">
<div class="text-14px font-500 text-primary">
{{ operator.label }}
</div>
<div
class="text-12px px-6px py-2px rounded-4px bg-[var(--el-color-primary-light-9)] font-mono text-[var(--el-color-primary)]"
class="text-12px px-6px py-2px rounded-4px bg-primary-light-9 text-primary font-mono"
>
{{ operator.symbol }}
</div>
</div>
<div class="text-12px text-[var(--el-text-color-secondary)]">
<div class="text-12px text-secondary">
{{ operator.description }}
</div>
</div>
</el-option>
</el-select>
</Select.Option>
</Select>
</div>
</template>

View File

@@ -1,8 +1,12 @@
<!-- 产品选择器组件 -->
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { DICT_TYPE } from '@vben/constants';
import { ProductApi } from '#/api/iot/product/product';
import { Select } from 'ant-design-vue';
import { getSimpleProductList } from '#/api/iot/product/product';
/** 产品选择器组件 */
defineOptions({ name: 'ProductSelector' });
@@ -23,16 +27,16 @@ const productList = ref<any[]>([]); // 产品列表
* 处理选择变化事件
* @param value 选中的产品 ID
*/
const handleChange = (value?: number) => {
function handleChange(value?: number) {
emit('update:modelValue', value);
emit('change', value);
};
}
/** 获取产品列表 */
const getProductList = async () => {
async function getProductList() {
try {
productLoading.value = true;
const res = await ProductApi.getSimpleProductList();
const res = await getSimpleProductList();
productList.value = res || [];
} catch (error) {
console.error('获取产品列表失败:', error);
@@ -40,7 +44,7 @@ const getProductList = async () => {
} finally {
productLoading.value = false;
}
};
}
// 组件挂载时获取产品列表
onMounted(() => {
@@ -49,7 +53,7 @@ onMounted(() => {
</script>
<template>
<el-select
<Select
:model-value="modelValue"
@update:model-value="handleChange"
placeholder="请选择产品"
@@ -58,7 +62,7 @@ onMounted(() => {
class="w-full"
:loading="productLoading"
>
<el-option
<Select.Option
v-for="product in productList"
:key="product.id"
:label="product.name"
@@ -66,17 +70,15 @@ onMounted(() => {
>
<div class="py-4px flex w-full items-center justify-between">
<div class="flex-1">
<div
class="text-14px font-500 mb-2px text-[var(--el-text-color-primary)]"
>
<div class="text-14px font-500 mb-2px text-primary">
{{ product.name }}
</div>
<div class="text-12px text-[var(--el-text-color-secondary)]">
<div class="text-12px text-secondary">
{{ product.productKey }}
</div>
</div>
<dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="product.status" />
</div>
</el-option>
</el-select>
</Select.Option>
</Select>
</template>

View File

@@ -1,17 +1,20 @@
<!-- 属性选择器组件 -->
<script setup lang="ts">
import type {
IotThingModelTSLResp,
ThingModelApi,
ThingModelEvent,
ThingModelParam,
ThingModelProperty,
ThingModelService,
} from '#/api/iot/thingmodel';
import { InfoFilled } from '@element-plus/icons-vue';
import { useVModel } from '@vueuse/core';
import { computed, ref, watch } from 'vue';
import { ThingModelApi } from '#/api/iot/thingmodel';
import { IconifyIcon } from '@vben/icons';
import { useVModel } from '@vueuse/core';
import { Button, Popover, Select, Tag } from 'ant-design-vue';
import { getThingModelListByProductId } from '#/api/iot/thingmodel';
import {
getAccessModeLabel,
getDataTypeName,
@@ -61,8 +64,8 @@ interface PropertySelectorItem {
const localValue = useVModel(props, 'modelValue', emit);
const loading = ref(false); // 加载状态
const propertyList = ref<PropertySelectorItem[]>([]); // 属性列表
const thingModelTSL = ref<IotThingModelTSLResp | null>(null); // 物模型TSL数据
const propertyList = ref<ThingModelApi.Property[]>([]); // 属性列表
const thingModelTSL = ref<null | ThingModelApi.ThingModel>(null); // 物模型TSL数据
// 计算属性:属性分组
const propertyGroups = computed(() => {
@@ -107,7 +110,7 @@ const selectedProperty = computed(() => {
* 处理选择变化事件
* @param value 选中的属性标识符
*/
const handleChange = (value: string) => {
function handleChange(value: any) {
const property = propertyList.value.find((p) => p.identifier === value);
if (property) {
emit('change', {
@@ -115,12 +118,12 @@ const handleChange = (value: string) => {
config: property,
});
}
};
}
/**
* 获取物模型TSL数据
*/
const getThingModelTSL = async () => {
async function getThingModelTSL() {
if (!props.productId) {
thingModelTSL.value = null;
propertyList.value = [];
@@ -129,9 +132,7 @@ const getThingModelTSL = async () => {
loading.value = true;
try {
const tslData = await ThingModelApi.getThingModelTSLByProductId(
props.productId,
);
const tslData = await getThingModelListByProductId(props.productId);
if (tslData) {
thingModelTSL.value = tslData;
@@ -146,10 +147,10 @@ const getThingModelTSL = async () => {
} finally {
loading.value = false;
}
};
}
/** 解析物模型 TSL 数据 */
const parseThingModelData = () => {
function parseThingModelData() {
const tsl = thingModelTSL.value;
const properties: PropertySelectorItem[] = [];
@@ -210,14 +211,14 @@ const parseThingModelData = () => {
});
}
propertyList.value = properties;
};
}
/**
* 获取属性单位
* @param property 属性对象
* @returns 属性单位
*/
const getPropertyUnit = (property: any) => {
function getPropertyUnit(property: any) {
if (!property) return undefined;
// 数值型数据的单位
@@ -226,14 +227,14 @@ const getPropertyUnit = (property: any) => {
}
return undefined;
};
}
/**
* 获取属性范围描述
* @param property 属性对象
* @returns 属性范围描述
*/
const getPropertyRange = (property: any) => {
function getPropertyRange(property: any) {
if (!property) return undefined;
// 数值型数据的范围
@@ -252,7 +253,7 @@ const getPropertyRange = (property: any) => {
}
return undefined;
};
}
/** 监听产品变化 */
watch(
@@ -274,7 +275,7 @@ watch(
<template>
<div class="gap-8px flex items-center">
<el-select
<Select
v-model="localValue"
placeholder="请选择监控项"
filterable
@@ -283,39 +284,37 @@ watch(
class="!w-150px"
:loading="loading"
>
<el-option-group
<Select.OptionGroup
v-for="group in propertyGroups"
:key="group.label"
:label="group.label"
>
<el-option
<Select.Option
v-for="property in group.options"
:key="property.identifier"
:label="property.name"
:value="property.identifier"
>
<div class="py-2px flex w-full items-center justify-between">
<span
class="text-14px font-500 flex-1 truncate text-[var(--el-text-color-primary)]"
>
<span class="text-14px font-500 text-primary flex-1 truncate">
{{ property.name }}
</span>
<el-tag
<Tag
:type="getDataTypeTagType(property.dataType)"
size="small"
class="ml-8px flex-shrink-0"
>
{{ property.identifier }}
</el-tag>
</Tag>
</div>
</el-option>
</el-option-group>
</el-select>
</Select.Option>
</Select.OptionGroup>
</Select>
<!-- 属性详情弹出层 -->
<el-popover
<Popover
v-if="selectedProperty"
placement="right-start"
placement="rightTop"
:width="350"
trigger="click"
:show-arrow="true"
@@ -323,42 +322,39 @@ watch(
popper-class="property-detail-popover"
>
<template #reference>
<el-button
type="info"
:icon="InfoFilled"
<Button
type="primary"
text
circle
size="small"
class="flex-shrink-0"
title="查看属性详情"
/>
>
<IconifyIcon icon="ep:info-filled" />
</Button>
</template>
<!-- 弹出层内容 -->
<div class="property-detail-content">
<div class="gap-8px mb-12px flex items-center">
<Icon
icon="ep:info-filled"
class="text-16px text-[var(--el-color-info)]"
/>
<span class="text-14px font-500 text-[var(--el-text-color-primary)]">
<IconifyIcon icon="ep:info-filled" class="text-16px text-info" />
<span class="text-14px font-500 text-primary">
{{ selectedProperty.name }}
</span>
<el-tag
<Tag
:type="getDataTypeTagType(selectedProperty.dataType)"
size="small"
>
{{ getDataTypeName(selectedProperty.dataType) }}
</el-tag>
</Tag>
</div>
<div class="space-y-8px ml-24px">
<div class="gap-8px flex items-start">
<span
class="text-12px min-w-60px flex-shrink-0 text-[var(--el-text-color-secondary)]"
>
<span class="text-12px min-w-60px text-secondary flex-shrink-0">
标识符
</span>
<span class="text-12px flex-1 text-[var(--el-text-color-primary)]">
<span class="text-12px text-primary flex-1">
{{ selectedProperty.identifier }}
</span>
</div>
@@ -367,34 +363,28 @@ watch(
v-if="selectedProperty.description"
class="gap-8px flex items-start"
>
<span
class="text-12px min-w-60px flex-shrink-0 text-[var(--el-text-color-secondary)]"
>
<span class="text-12px min-w-60px text-secondary flex-shrink-0">
描述
</span>
<span class="text-12px flex-1 text-[var(--el-text-color-primary)]">
<span class="text-12px text-primary flex-1">
{{ selectedProperty.description }}
</span>
</div>
<div v-if="selectedProperty.unit" class="gap-8px flex items-start">
<span
class="text-12px min-w-60px flex-shrink-0 text-[var(--el-text-color-secondary)]"
>
<span class="text-12px min-w-60px text-secondary flex-shrink-0">
单位
</span>
<span class="text-12px flex-1 text-[var(--el-text-color-primary)]">
<span class="text-12px text-primary flex-1">
{{ selectedProperty.unit }}
</span>
</div>
<div v-if="selectedProperty.range" class="gap-8px flex items-start">
<span
class="text-12px min-w-60px flex-shrink-0 text-[var(--el-text-color-secondary)]"
>
<span class="text-12px min-w-60px text-secondary flex-shrink-0">
取值范围
</span>
<span class="text-12px flex-1 text-[var(--el-text-color-primary)]">
<span class="text-12px text-primary flex-1">
{{ selectedProperty.range }}
</span>
</div>
@@ -407,12 +397,10 @@ watch(
"
class="gap-8px flex items-start"
>
<span
class="text-12px min-w-60px flex-shrink-0 text-[var(--el-text-color-secondary)]"
>
<span class="text-12px min-w-60px text-secondary flex-shrink-0">
访问模式:
</span>
<span class="text-12px flex-1 text-[var(--el-text-color-primary)]">
<span class="text-12px text-primary flex-1">
{{ getAccessModeLabel(selectedProperty.accessMode) }}
</span>
</div>
@@ -424,12 +412,10 @@ watch(
"
class="gap-8px flex items-start"
>
<span
class="text-12px min-w-60px flex-shrink-0 text-[var(--el-text-color-secondary)]"
>
<span class="text-12px min-w-60px text-secondary flex-shrink-0">
事件类型:
</span>
<span class="text-12px flex-1 text-[var(--el-text-color-primary)]">
<span class="text-12px text-primary flex-1">
{{ getEventTypeLabel(selectedProperty.eventType) }}
</span>
</div>
@@ -441,18 +427,16 @@ watch(
"
class="gap-8px flex items-start"
>
<span
class="text-12px min-w-60px flex-shrink-0 text-[var(--el-text-color-secondary)]"
>
<span class="text-12px min-w-60px text-secondary flex-shrink-0">
调用类型:
</span>
<span class="text-12px flex-1 text-[var(--el-text-color-primary)]">
<span class="text-12px text-primary flex-1">
{{ getThingModelServiceCallTypeLabel(selectedProperty.callType) }}
</span>
</div>
</div>
</div>
</el-popover>
</Popover>
</div>
</template>

View File

@@ -9,7 +9,7 @@ import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteSceneRule,
getRuleScenePage,
getSceneRulePage,
updateSceneRuleStatus,
} from '#/api/iot/rule/scene';
import { $t } from '#/locales';
@@ -85,7 +85,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getRuleScenePage({
return await getSceneRulePage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,

View File

@@ -1,5 +1,9 @@
<!-- 产品的物模型表单event -->
<script lang="ts" setup>
import type { Ref } from 'vue';
import { watch } from 'vue';
import { isEmpty } from '@vben/utils';
import { useVModel } from '@vueuse/core';

View File

@@ -2,18 +2,19 @@
<script lang="ts" setup>
import type { Ref } from 'vue';
import type { ProductVO } from '#/api/iot/product/product';
import type { IotProductApi } from '#/api/iot/product/product';
import type { ThingModelData } from '#/api/iot/thingmodel';
import { inject, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { DICT_TYPE, getIntDictOptions } from '@vben/constants';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { $t } from '@vben/locales';
import { cloneDeep } from '@vben/utils';
import { message } from 'ant-design-vue';
import { cloneDeep } from 'lodash-es';
import { ThingModelApi, ThingModelFormRules } from '#/api/iot/thingmodel';
import { createThingModel, updateThingModel } from '#/api/iot/thingmodel';
import {
IOT_PROVIDE_KEY,
IoTDataSpecsDataTypeEnum,
@@ -29,9 +30,7 @@ defineOptions({ name: 'IoTThingModelForm' });
/** 提交表单 */
const emit = defineEmits(['success']);
const product = inject<Ref<ProductVO>>(IOT_PROVIDE_KEY.PRODUCT); // 注入产品信息
const { t } = useI18n(); // 国际化
const product = inject<Ref<IotProductApi.Product>>(IOT_PROVIDE_KEY.PRODUCT); // 注入产品信息
const dialogVisible = ref(false); // 弹窗的是否展示
const dialogTitle = ref(''); // 弹窗的标题
@@ -55,13 +54,13 @@ const formRef = ref(); // 表单 Ref
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
dialogVisible.value = true;
dialogTitle.value = t(`action.${type}`);
dialogTitle.value = $t(`action.${type}`);
formType.value = type;
resetForm();
if (id) {
formLoading.value = true;
try {
formData.value = await ThingModelApi.getThingModel(id);
formData.value = await getThingModel(id);
// 情况一:属性初始化
if (
!formData.value.property ||
@@ -96,7 +95,7 @@ const open = async (type: string, id?: number) => {
};
defineExpose({ open, close: () => (dialogVisible.value = false) });
const submitForm = async () => {
async function submitForm() {
await formRef.value.validate();
formLoading.value = true;
try {
@@ -105,23 +104,20 @@ const submitForm = async () => {
data.productId = product!.value.id;
data.productKey = product!.value.productKey;
fillExtraAttributes(data);
if (formType.value === 'create') {
await ThingModelApi.createThingModel(data);
message.success({ content: t('common.createSuccess') });
} else {
await ThingModelApi.updateThingModel(data);
message.success({ content: t('common.updateSuccess') });
}
await (formType.value === 'create'
? createThingModel(data)
: updateThingModel(data));
message.success($t('ui.actionMessage.operationSuccess'));
// 关闭弹窗
dialogVisible.value = false;
emit('success');
} finally {
formLoading.value = false;
}
};
}
/** 填写额外的属性(处理不同类型的情况) */
const fillExtraAttributes = (data: any) => {
function fillExtraAttributes(data: any) {
// 属性
if (data.type === IoTThingModelTypeEnum.PROPERTY) {
removeDataSpecs(data.property);
@@ -149,22 +145,22 @@ const fillExtraAttributes = (data: any) => {
delete data.property;
delete data.service;
}
};
}
/** 处理 dataSpecs 为空的情况 */
const removeDataSpecs = (val: any) => {
function removeDataSpecs(val: any) {
if (!val.dataSpecs || Object.keys(val.dataSpecs).length === 0) {
delete val.dataSpecs;
}
if (!val.dataSpecsList || val.dataSpecsList.length === 0) {
delete val.dataSpecsList;
}
};
}
/** 重置表单 */
const resetForm = () => {
function resetForm() {
formData.value = {
type: IoTThingModelTypeEnum.PROPERTY,
type: IoTThingModelTypeEnum.PROPERTY.toString(),
dataType: IoTDataSpecsDataTypeEnum.INT,
property: {
dataType: IoTDataSpecsDataTypeEnum.INT,
@@ -174,9 +170,9 @@ const resetForm = () => {
},
service: {},
event: {},
} as ThingModelData;
};
formRef.value?.resetFields();
};
}
</script>
<template>
@@ -192,7 +188,10 @@ const resetForm = () => {
<a-form-item label="功能类型" name="type">
<a-radio-group v-model:value="formData.type">
<a-radio-button
v-for="dict in getIntDictOptions(DICT_TYPE.IOT_THING_MODEL_TYPE)"
v-for="dict in getDictOptions(
DICT_TYPE.IOT_THING_MODEL_TYPE,
'number',
)"
:key="dict.value"
:value="dict.value"
>

View File

@@ -1,9 +1,14 @@
<script setup lang="ts">
import type { Ref } from 'vue';
import type { ProductVO } from '#/api/iot/product/product';
import { inject, onMounted, ref } from 'vue';
import hljs from 'highlight.js'; // 导入代码高亮文件
import json from 'highlight.js/lib/languages/json';
import { ProductVO } from '#/api/iot/product/product';
import { ThingModelApi } from '#/api/iot/thingmodel';
import { getThingModelListByProductId } from '#/api/iot/thingmodel';
import { IOT_PROVIDE_KEY } from '#/views/iot/utils/constants';
import 'highlight.js/styles/github.css'; // 导入代码高亮样式
@@ -16,18 +21,18 @@ const product = inject<Ref<ProductVO>>(IOT_PROVIDE_KEY.PRODUCT); // 注入产品
const viewMode = ref('code'); // 查看模式code-代码视图editor-编辑器视图
/** 打开弹窗 */
const open = () => {
function open() {
dialogVisible.value = true;
};
}
defineExpose({ open });
/** 获取 TSL */
const thingModelTSL = ref({});
const getTsl = async () => {
thingModelTSL.value = await ThingModelApi.getThingModelTSLByProductId(
async function getTsl() {
thingModelTSL.value = await getThingModelListByProductId(
product?.value?.id || 0,
);
};
}
/** 初始化 */
onMounted(async () => {

View File

@@ -1,5 +1,6 @@
<script lang="ts" setup>
import { ThingModelData } from '#/api/iot/thingmodel';
import type { ThingModelData } from '#/api/iot/thingmodel';
import {
getEventTypeLabel,
getThingModelServiceCallTypeLabel,
@@ -15,7 +16,7 @@ defineProps<{ data: ThingModelData }>();
<template>
<!-- 属性 -->
<template v-if="data.type === IoTThingModelTypeEnum.PROPERTY">
<template v-if="data.type === IoTThingModelTypeEnum.PROPERTY.toString()">
<!-- 非列表型数值 -->
<div
v-if="
@@ -23,16 +24,16 @@ defineProps<{ data: ThingModelData }>();
IoTDataSpecsDataTypeEnum.INT,
IoTDataSpecsDataTypeEnum.DOUBLE,
IoTDataSpecsDataTypeEnum.FLOAT,
].includes(data.property.dataType)
].includes(data.property?.dataType as any)
"
>
取值范围:{{
`${data.property.dataSpecs.min}~${data.property.dataSpecs.max}`
`${data.property?.dataSpecs.min}~${data.property?.dataSpecs.max}`
}}
</div>
<!-- 非列表型:文本 -->
<div v-if="IoTDataSpecsDataTypeEnum.TEXT === data.property.dataType">
数据长度:{{ data.property.dataSpecs.length }}
<div v-if="IoTDataSpecsDataTypeEnum.TEXT === data.property?.dataType">
数据长度:{{ data.property?.dataSpecs.length }}
</div>
<!-- 列表型: 数组、结构、时间(特殊) -->
<div
@@ -41,7 +42,7 @@ defineProps<{ data: ThingModelData }>();
IoTDataSpecsDataTypeEnum.ARRAY,
IoTDataSpecsDataTypeEnum.STRUCT,
IoTDataSpecsDataTypeEnum.DATE,
].includes(data.property.dataType)
].includes(data.property?.dataType as any)
"
>
-
@@ -50,29 +51,31 @@ defineProps<{ data: ThingModelData }>();
<div
v-if="
[IoTDataSpecsDataTypeEnum.BOOL, IoTDataSpecsDataTypeEnum.ENUM].includes(
data.property.dataType,
data.property?.dataType as any,
)
"
>
<div>
{{
IoTDataSpecsDataTypeEnum.BOOL === data.property.dataType
IoTDataSpecsDataTypeEnum.BOOL === data.property?.dataType
? '布尔值'
: '枚举值'
}}
</div>
<div v-for="item in data.property.dataSpecsList" :key="item.value">
<div v-for="item in data.property?.dataSpecsList" :key="item.value">
{{ `${item.name}-${item.value}` }}
</div>
</div>
</template>
<!-- 服务 -->
<div v-if="data.type === IoTThingModelTypeEnum.SERVICE">
调用方式:{{ getThingModelServiceCallTypeLabel(data.service!.callType) }}
<div v-if="data.type === IoTThingModelTypeEnum.SERVICE.toString()">
调用方式:{{
getThingModelServiceCallTypeLabel(data.service?.callType as any)
}}
</div>
<!-- 事件 -->
<div v-if="data.type === IoTThingModelTypeEnum.EVENT">
事件类型:{{ getEventTypeLabel(data.event!.type) }}
<div v-if="data.type === IoTThingModelTypeEnum.EVENT.toString()">
事件类型:{{ getEventTypeLabel(data.event?.type as any) }}
</div>
</template>

View File

@@ -1,6 +1,9 @@
<!-- dataTypearray 数组类型 -->
<script lang="ts" setup>
import type { Ref } from 'vue';
import { useVModel } from '@vueuse/core';
import { Form, Input, Radio } from 'ant-design-vue';
import {
getDataTypeOptions,
@@ -17,20 +20,19 @@ const emits = defineEmits(['update:modelValue']);
const dataSpecs = useVModel(props, 'modelValue', emits) as Ref<any>;
/** 元素类型改变时间。当值为 struct 时,对 dataSpecs 中的 dataSpecsList 进行初始化 */
const handleChange = (val: string) => {
function handleChange(val: any) {
if (val !== IoTDataSpecsDataTypeEnum.STRUCT) {
return;
}
dataSpecs.value.dataSpecsList = [];
};
}
</script>
<template>
<el-form-item label="元素类型" prop="property.dataSpecs.childDataType">
<el-radio-group v-model="dataSpecs.childDataType" @change="handleChange">
<Form.Item label="元素类型" prop="property.dataSpecs.childDataType">
<Radio.Group v-model="dataSpecs.childDataType" @change="handleChange">
<template v-for="item in getDataTypeOptions()" :key="item.value">
<el-radio
<Radio
v-if="
!(
[
@@ -44,18 +46,16 @@ const handleChange = (val: string) => {
class="w-1/3"
>
{{ `${item.value}(${item.label})` }}
</el-radio>
</Radio>
</template>
</el-radio-group>
</el-form-item>
<el-form-item label="元素个数" prop="property.dataSpecs.size">
<el-input v-model="dataSpecs.size" placeholder="请输入数组中的元素个数" />
</el-form-item>
</Radio.Group>
</Form.Item>
<Form.Item label="元素个数" prop="property.dataSpecs.size">
<Input v-model="dataSpecs.size" placeholder="请输入数组中的元素个数" />
</Form.Item>
<!-- Struct 型配置-->
<ThingModelStructDataSpecs
v-if="dataSpecs.childDataType === IoTDataSpecsDataTypeEnum.STRUCT"
v-model="dataSpecs.dataSpecsList"
/>
</template>
<style lang="scss" scoped></style>

View File

@@ -1,10 +1,14 @@
<!-- dataTypeenum 数组类型 -->
<script lang="ts" setup>
import type { Ref } from 'vue';
import type { DataSpecsEnumOrBoolData } from '#/api/iot/thingmodel';
import { isEmpty } from '@vben/utils';
import { useVModel } from '@vueuse/core';
import { Button, Form, Input, message } from 'ant-design-vue';
import { DataSpecsEnumOrBoolData } from '#/api/iot/thingmodel';
import { IoTDataSpecsDataTypeEnum } from '#/views/iot/utils/constants';
/** 枚举型的 dataSpecs 配置组件 */
@@ -17,30 +21,30 @@ const dataSpecsList = useVModel(props, 'modelValue', emits) as Ref<
>;
/** 添加枚举项 */
const addEnum = () => {
function addEnum() {
dataSpecsList.value.push({
dataType: IoTDataSpecsDataTypeEnum.ENUM,
name: '', // 枚举项的名称
value: undefined, // 枚举值
value: '', // 枚举值
});
};
}
/** 删除枚举项 */
const deleteEnum = (index: number) => {
function deleteEnum(index: number) {
if (dataSpecsList.value.length === 1) {
message.warning('至少需要一个枚举项');
return;
}
dataSpecsList.value.splice(index, 1);
};
}
/** 校验枚举值 */
const validateEnumValue = (_: any, value: any, callback: any) => {
function validateEnumValue(_: any, value: any, callback: any) {
if (isEmpty(value)) {
callback(new Error('枚举值不能为空'));
return;
}
if (isNaN(Number(value))) {
if (Number.isNaN(Number(value))) {
callback(new Error('枚举值必须是数字'));
return;
}
@@ -51,10 +55,10 @@ const validateEnumValue = (_: any, value: any, callback: any) => {
return;
}
callback();
};
}
/** 校验枚举描述 */
const validateEnumName = (_: any, value: string, callback: any) => {
function validateEnumName(_: any, value: string, callback: any) {
if (isEmpty(value)) {
callback(new Error('枚举描述不能为空'));
return;
@@ -75,10 +79,10 @@ const validateEnumName = (_: any, value: string, callback: any) => {
return;
}
callback();
};
}
/** 校验整个枚举列表 */
const validateEnumList = (_: any, __: any, callback: any) => {
function validateEnumList(_: any, __: any, callback: any) {
if (isEmpty(dataSpecsList.value)) {
callback(new Error('请至少添加一个枚举项'));
return;
@@ -95,7 +99,7 @@ const validateEnumList = (_: any, __: any, callback: any) => {
// 检查枚举值是否都是数字
const hasInvalidNumber = dataSpecsList.value.some((item) =>
isNaN(Number(item.value)),
Number.isNaN(Number(item.value)),
);
if (hasInvalidNumber) {
callback(new Error('存在非数字的枚举值'));
@@ -110,11 +114,11 @@ const validateEnumList = (_: any, __: any, callback: any) => {
return;
}
callback();
};
}
</script>
<template>
<el-form-item
<Form.Item
:rules="[
{ required: true, validator: validateEnumList, trigger: 'change' },
]"
@@ -130,7 +134,7 @@ const validateEnumList = (_: any, __: any, callback: any) => {
:key="index"
class="mb-5px flex items-center justify-between"
>
<el-form-item
<Form.Item
:prop="`property.dataSpecsList[${index}].value`"
:rules="[
{ required: true, message: '枚举值不能为空' },
@@ -138,10 +142,10 @@ const validateEnumList = (_: any, __: any, callback: any) => {
]"
class="mb-0 flex-1"
>
<el-input v-model="item.value" placeholder="请输入枚举值,如'0'" />
</el-form-item>
<Input v-model="item.value" placeholder="请输入枚举值,如'0'" />
</Form.Item>
<span class="mx-2">~</span>
<el-form-item
<Form.Item
:prop="`property.dataSpecsList[${index}].name`"
:rules="[
{ required: true, message: '枚举描述不能为空' },
@@ -149,20 +153,15 @@ const validateEnumList = (_: any, __: any, callback: any) => {
]"
class="mb-0 flex-1"
>
<el-input v-model="item.name" placeholder="对该枚举项的描述" />
</el-form-item>
<el-button
class="ml-10px"
link
type="primary"
@click="deleteEnum(index)"
>
<Input v-model="item.name" placeholder="对该枚举项的描述" />
</Form.Item>
<Button class="ml-10px" link type="primary" @click="deleteEnum(index)">
删除
</el-button>
</Button>
</div>
<el-button link type="primary" @click="addEnum">+添加枚举项</el-button>
<Button link type="primary" @click="addEnum">+添加枚举项</Button>
</div>
</el-form-item>
</Form.Item>
</template>
<style lang="scss" scoped>

View File

@@ -1,11 +1,14 @@
<!-- dataTypenumber 数组类型 -->
<script lang="ts" setup>
import { DICT_TYPE, getStrDictOptions } from '@vben/constants';
import type { Ref } from 'vue';
import type { DataSpecsNumberData } from '#/api/iot/thingmodel';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { useVModel } from '@vueuse/core';
import { DataSpecsNumberData } from '#/api/iot/thingmodel';
/** 数值型的 dataSpecs 配置组件 */
defineOptions({ name: 'ThingModelNumberDataSpecs' });
@@ -28,11 +31,11 @@ const unitChange = (UnitSpecs: string) => {
const validateMin = (_: any, __: any, callback: any) => {
const min = Number(dataSpecs.value.min);
const max = Number(dataSpecs.value.max);
if (isNaN(min)) {
if (Number.isNaN(min)) {
callback(new Error('请输入有效的数值'));
return;
}
if (max !== undefined && !isNaN(max) && min >= max) {
if (max !== undefined && !Number.isNaN(max) && min >= max) {
callback(new Error('最小值必须小于最大值'));
return;
}
@@ -44,11 +47,11 @@ const validateMin = (_: any, __: any, callback: any) => {
const validateMax = (_: any, __: any, callback: any) => {
const min = Number(dataSpecs.value.min);
const max = Number(dataSpecs.value.max);
if (isNaN(max)) {
if (Number.isNaN(max)) {
callback(new Error('请输入有效的数值'));
return;
}
if (min !== undefined && !isNaN(min) && max <= min) {
if (min !== undefined && !Number.isNaN(min) && max <= min) {
callback(new Error('最大值必须大于最小值'));
return;
}
@@ -59,7 +62,7 @@ const validateMax = (_: any, __: any, callback: any) => {
/** 校验步长 */
const validateStep = (_: any, __: any, callback: any) => {
const step = Number(dataSpecs.value.step);
if (isNaN(step)) {
if (Number.isNaN(step)) {
callback(new Error('请输入有效的数值'));
return;
}
@@ -69,7 +72,7 @@ const validateStep = (_: any, __: any, callback: any) => {
}
const min = Number(dataSpecs.value.min);
const max = Number(dataSpecs.value.max);
if (!isNaN(min) && !isNaN(max) && step > max - min) {
if (!Number.isNaN(min) && !Number.isNaN(max) && step > max - min) {
callback(new Error('步长不能大于最大值和最小值的差值'));
return;
}
@@ -129,8 +132,9 @@ const validateStep = (_: any, __: any, callback: any) => {
@change="unitChange"
>
<el-option
v-for="(item, index) in getStrDictOptions(
v-for="(item, index) in getDictOptions(
DICT_TYPE.IOT_THING_MODEL_UNIT,
'string',
)"
:key="index"
:label="`${item.label}-${item.value}`"

View File

@@ -1,10 +1,14 @@
<!-- dataTypestruct 数组类型 -->
<script lang="ts" setup>
import type { Ref } from 'vue';
import { nextTick, onMounted, ref, unref } from 'vue';
import { isEmpty } from '@vben/utils';
import { useVModel } from '@vueuse/core';
import { Button, Divider, Form, Input, Modal } from 'ant-design-vue';
import { ThingModelFormRules } from '#/api/iot/thingmodel';
import { IoTDataSpecsDataTypeEnum } from '#/views/iot/utils/constants';
import ThingModelProperty from '../ThingModelProperty.vue';
@@ -29,7 +33,7 @@ const formData = ref<any>({
});
/** 打开 struct 表单 */
const openStructForm = (val: any) => {
function openStructForm(val: any) {
dialogVisible.value = true;
resetForm();
if (isEmpty(val)) {
@@ -46,15 +50,15 @@ const openStructForm = (val: any) => {
dataSpecsList: val.dataSpecsList,
},
};
};
}
/** 删除 struct 项 */
const deleteStructItem = (index: number) => {
function deleteStructItem(index: number) {
dataSpecsList.value.splice(index, 1);
};
}
/** 添加参数 */
const submitForm = async () => {
async function submitForm() {
await structFormRef.value.validate();
try {
@@ -88,10 +92,10 @@ const submitForm = async () => {
} finally {
dialogVisible.value = false;
}
};
}
/** 重置表单 */
const resetForm = () => {
function resetForm() {
formData.value = {
property: {
dataType: IoTDataSpecsDataTypeEnum.INT,
@@ -101,16 +105,16 @@ const resetForm = () => {
},
};
structFormRef.value?.resetFields();
};
}
/** 校验 struct 不能为空 */
const validateList = (_: any, __: any, callback: any) => {
function validateList(_: any, __: any, callback: any) {
if (isEmpty(dataSpecsList.value)) {
callback(new Error('struct 不能为空'));
return;
}
callback();
};
}
/** 组件初始化 */
onMounted(async () => {
@@ -122,60 +126,52 @@ onMounted(async () => {
<template>
<!-- struct 数据展示 -->
<el-form-item
<Form.Item
:rules="[{ required: true, validator: validateList, trigger: 'change' }]"
label="JSON 对象"
>
<div
v-for="(item, index) in dataSpecsList"
:key="index"
class="w-1/1 struct-item px-10px mb-10px flex justify-between"
class="px-10px mb-10px flex w-full justify-between bg-gray-100"
>
<span>参数名称{{ item.name }}</span>
<div class="btn">
<el-button link type="primary" @click="openStructForm(item)">
<Button link type="primary" @click="openStructForm(item)">
编辑
</el-button>
<el-divider direction="vertical" />
<el-button link type="danger" @click="deleteStructItem(index)">
删除
</el-button>
</Button>
<Divider direction="vertical" />
<Button link danger @click="deleteStructItem(index)"> 删除 </Button>
</div>
</div>
<el-button link type="primary" @click="openStructForm(null)">
<Button link type="primary" @click="openStructForm(null)">
+新增参数
</el-button>
</el-form-item>
</Button>
</Form.Item>
<!-- struct 表单 -->
<Dialog v-model="dialogVisible" :title="dialogTitle" append-to-body>
<el-form
<Modal v-model="dialogVisible" :title="dialogTitle" append-to-body>
<Form
ref="structFormRef"
v-loading="formLoading"
:model="formData"
:rules="ThingModelFormRules"
label-width="100px"
>
<el-form-item label="参数名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入功能名称" />
</el-form-item>
<el-form-item label="标识符" prop="identifier">
<el-input v-model="formData.identifier" placeholder="请输入标识符" />
</el-form-item>
<Form.Item label="参数名称" prop="name">
<Input v-model="formData.name" placeholder="请输入功能名称" />
</Form.Item>
<Form.Item label="标识符" prop="identifier">
<Input v-model="formData.identifier" placeholder="请输入标识符" />
</Form.Item>
<!-- 属性配置 -->
<ThingModelProperty v-model="formData.property" is-struct-data-specs />
</el-form>
</Form>
<template #footer>
<el-button :disabled="formLoading" type="primary" @click="submitForm">
<Button :disabled="formLoading" type="primary" @click="submitForm">
</el-button>
<el-button @click="dialogVisible = false"> </el-button>
</Button>
<Button @click="dialogVisible = false"> </Button>
</template>
</Dialog>
</Modal>
</template>
<style lang="scss" scoped>
.struct-item {
background-color: #e4f2fd;
}
</style>

View File

@@ -13,7 +13,7 @@ export const IoTThingModelTypeEnum = {
PROPERTY: 1, // 属性
SERVICE: 2, // 服务
EVENT: 3, // 事件
} as const;
};
/** IoT 设备消息的方法枚举 */
export const IotDeviceMessageMethodEnum = {
@@ -68,7 +68,7 @@ export const IoTThingModelServiceCallTypeEnum = {
label: '同步',
value: 'sync',
},
} as const;
};
export const getThingModelServiceCallTypeLabel = (
value: string,
): string | undefined =>
@@ -90,7 +90,7 @@ export const IoTThingModelEventTypeEnum = {
label: '故障',
value: 'error',
},
} as const;
};
export const getEventTypeLabel = (value: string): string | undefined =>
Object.values(IoTThingModelEventTypeEnum).find((type) => type.value === value)
?.label;
@@ -99,7 +99,7 @@ export const getEventTypeLabel = (value: string): string | undefined =>
export const IoTThingModelParamDirectionEnum = {
INPUT: 'input', // 输入参数
OUTPUT: 'output', // 输出参数
} as const;
};
// IoT 产品物模型访问模式枚举类
export const IoTThingModelAccessModeEnum = {
@@ -115,7 +115,7 @@ export const IoTThingModelAccessModeEnum = {
label: '只写',
value: 'w',
},
} as const;
};
/** 获取访问模式标签 */
export const getAccessModeLabel = (value: string): string => {
@@ -136,7 +136,7 @@ export const IoTDataSpecsDataTypeEnum = {
DATE: 'date',
STRUCT: 'struct',
ARRAY: 'array',
} as const;
};
export const getDataTypeOptions = () => {
return [
@@ -165,7 +165,7 @@ export const getDataTypeOptionsLabel = (value: string) => {
/** 获取数据类型显示名称(用于属性选择器) */
export const getDataTypeName = (dataType: string): string => {
const typeMap = {
const typeMap: Record<string, string> = {
[IoTDataSpecsDataTypeEnum.INT]: '整数',
[IoTDataSpecsDataTypeEnum.FLOAT]: '浮点数',
[IoTDataSpecsDataTypeEnum.DOUBLE]: '双精度',
@@ -179,11 +179,14 @@ export const getDataTypeName = (dataType: string): string => {
return typeMap[dataType] || dataType;
};
/** 获取数据类型标签类型(用于 el-tag 的 type 属性) */
/** 获取数据类型标签类型(用于 tag 的 type 属性) */
export const getDataTypeTagType = (
dataType: string,
): 'danger' | 'info' | 'primary' | 'success' | 'warning' => {
const tagMap = {
const tagMap: Record<
string,
'danger' | 'info' | 'primary' | 'success' | 'warning'
> = {
[IoTDataSpecsDataTypeEnum.INT]: 'primary',
[IoTDataSpecsDataTypeEnum.FLOAT]: 'success',
[IoTDataSpecsDataTypeEnum.DOUBLE]: 'success',
@@ -193,7 +196,7 @@ export const getDataTypeTagType = (
[IoTDataSpecsDataTypeEnum.DATE]: 'primary',
[IoTDataSpecsDataTypeEnum.STRUCT]: 'info',
[IoTDataSpecsDataTypeEnum.ARRAY]: 'warning',
} as const;
};
return tagMap[dataType] || 'info';
};
@@ -202,7 +205,7 @@ export const THING_MODEL_GROUP_LABELS = {
PROPERTY: '设备属性',
EVENT: '设备事件',
SERVICE: '设备服务',
} as const;
};
// IoT OTA 任务设备范围枚举
export const IoTOtaTaskDeviceScopeEnum = {
@@ -214,7 +217,7 @@ export const IoTOtaTaskDeviceScopeEnum = {
label: '指定设备',
value: 2,
},
} as const;
};
// IoT OTA 任务状态枚举
export const IoTOtaTaskStatusEnum = {
@@ -230,7 +233,7 @@ export const IoTOtaTaskStatusEnum = {
label: '已取消',
value: 30,
},
} as const;
};
// IoT OTA 升级记录状态枚举
export const IoTOtaTaskRecordStatusEnum = {
@@ -258,7 +261,7 @@ export const IoTOtaTaskRecordStatusEnum = {
label: '升级取消',
value: 50,
},
} as const;
};
// ========== 场景联动规则相关常量 ==========
@@ -269,7 +272,7 @@ export const IotRuleSceneTriggerTypeEnum = {
DEVICE_EVENT_POST: 3, // 设备事件上报
DEVICE_SERVICE_INVOKE: 4, // 设备服务调用
TIMER: 100, // 定时触发
} as const;
};
/** 触发器类型选项配置 */
export const triggerTypeOptions = [
@@ -296,7 +299,7 @@ export const triggerTypeOptions = [
];
/** 判断是否为设备触发器类型 */
export const isDeviceTrigger = (type: number): boolean => {
export function isDeviceTrigger(type: number): boolean {
const deviceTriggerTypes = [
IotRuleSceneTriggerTypeEnum.DEVICE_STATE_UPDATE,
IotRuleSceneTriggerTypeEnum.DEVICE_PROPERTY_POST,
@@ -304,7 +307,7 @@ export const isDeviceTrigger = (type: number): boolean => {
IotRuleSceneTriggerTypeEnum.DEVICE_SERVICE_INVOKE,
] as number[];
return deviceTriggerTypes.includes(type);
};
}
// ========== 场景联动规则执行器相关常量 ==========
@@ -314,7 +317,7 @@ export const IotRuleSceneActionTypeEnum = {
DEVICE_SERVICE_INVOKE: 2, // 设备服务调用
ALERT_TRIGGER: 100, // 告警触发
ALERT_RECOVER: 101, // 告警恢复
} as const;
};
/** 执行器类型选项配置 */
export const getActionTypeOptions = () => [
@@ -356,14 +359,14 @@ export const IotRuleSceneTriggerConditionParameterOperatorEnum = {
NOT_BETWEEN: { name: '不在...之间', value: 'not between' }, // 不在...之间
LIKE: { name: '字符串匹配', value: 'like' }, // 字符串匹配
NOT_NULL: { name: '非空', value: 'not null' }, // 非空
} as const;
};
/** IoT 场景联动触发条件类型枚举 */
export const IotRuleSceneTriggerConditionTypeEnum = {
DEVICE_STATUS: 1, // 设备状态
DEVICE_PROPERTY: 2, // 设备属性
CURRENT_TIME: 3, // 当前时间
} as const;
};
/** 获取条件类型选项 */
export const getConditionTypeOptions = () => [
@@ -418,7 +421,7 @@ export const IoTDeviceStatusEnum = {
value2: 'not_activated',
tagType: 'info',
},
} as const;
};
/** 设备选择器特殊选项 */
export const DEVICE_SELECTOR_OPTIONS = {
@@ -426,7 +429,7 @@ export const DEVICE_SELECTOR_OPTIONS = {
id: 0,
deviceName: '全部设备',
},
} as const;
};
/** IoT 场景联动触发时间操作符枚举 */
export const IotRuleSceneTriggerTimeOperatorEnum = {
@@ -437,7 +440,7 @@ export const IotRuleSceneTriggerTimeOperatorEnum = {
BEFORE_TODAY: { name: '在今日之前', value: 'before_today' }, // 在今日之前
AFTER_TODAY: { name: '在今日之后', value: 'after_today' }, // 在今日之后
TODAY: { name: '在今日之间', value: 'today' }, // 在今日之间
} as const;
};
/** 获取触发器类型标签 */
export const getTriggerTypeLabel = (type: number): string => {
@@ -453,7 +456,7 @@ export const JsonParamsInputTypeEnum = {
EVENT: 'event',
PROPERTY: 'property',
CUSTOM: 'custom',
} as const;
};
/** JSON 参数输入组件类型 */
export type JsonParamsInputType =
@@ -512,7 +515,7 @@ export const JSON_PARAMS_INPUT_CONSTANTS = {
CUSTOM: '请先进行配置',
DEFAULT: '请先进行配置',
},
} as const;
};
/** JSON 参数输入组件图标常量 */
export const JSON_PARAMS_INPUT_ICONS = {
@@ -539,10 +542,10 @@ export const JSON_PARAMS_INPUT_ICONS = {
ERROR: 'ep:warning',
SUCCESS: 'ep:circle-check',
},
} as const;
};
/** JSON 参数输入组件示例值常量 */
export const JSON_PARAMS_EXAMPLE_VALUES = {
export const JSON_PARAMS_EXAMPLE_VALUES: Record<string, any> = {
[IoTDataSpecsDataTypeEnum.INT]: { display: '25', value: 25 },
[IoTDataSpecsDataTypeEnum.FLOAT]: { display: '25.5', value: 25.5 },
[IoTDataSpecsDataTypeEnum.DOUBLE]: { display: '25.5', value: 25.5 },
@@ -552,4 +555,4 @@ export const JSON_PARAMS_EXAMPLE_VALUES = {
[IoTDataSpecsDataTypeEnum.STRUCT]: { display: '{}', value: {} },
[IoTDataSpecsDataTypeEnum.ARRAY]: { display: '[]', value: [] },
DEFAULT: { display: '""', value: '' },
} as const;
};