fix: iot
This commit is contained in:
@@ -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"
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user