CommandIssuance.vue
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<template>
<div class="tabs-detail">
<div>
<BasicForm @register="registerForm" />
<Space class="w-full justify-end py-2" justify="end">
<Button :loading="loading" type="primary" @click="handleOk" class="mr-2">确定</Button>
<Button type="default" @click="handleCancel" class="mr-2">重置</Button>
</Space>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, nextTick, ref } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { CommandFieldsEnum, CommandSchemas, CommandType, ValueType } from '../../config/data';
import { commandIssuanceApi } from '/@/api/device/deviceManager';
import { useMessage } from '/@/hooks/web/useMessage';
import { Button } from '/@/components/Button';
import { Space } from 'ant-design-vue';
import { DeviceRecord } from '/@/api/device/model/deviceModel';
import { TransportTypeEnum } from '../../../profiles/components/TransportDescript/const';
import { parseStringToJSON } from '/@/components/CodeEditor/src/JSONEditor';
import { CommandDeliveryWayEnum } from '/@/enums/toolEnum';
export default defineComponent({
components: { BasicForm, Button, Space },
props: {
deviceDetail: {
type: Object as PropType<DeviceRecord>,
required: true,
},
},
emits: ['register'],
setup(props) {
const { createMessage } = useMessage();
const loading = ref(false);
const [registerForm, { getFieldsValue, validate, resetFields, clearValidate }] = useForm({
labelWidth: 120,
schemas: CommandSchemas(
props.deviceDetail.deviceProfile.transportType as TransportTypeEnum,
props.deviceDetail.deviceProfileId
),
baseColProps: { span: 20 },
labelAlign: 'right',
showSubmitButton: false,
showResetButton: false,
});
const handleCancel = async () => {
await resetFields();
await nextTick();
await clearValidate();
};
const handleOk = async () => {
loading.value = true;
try {
// 验证
const valid = await validate();
if (!valid) return;
// 收集表单数据
const field = getFieldsValue();
let command: Recordable = {
persistent: true,
method: 'methodThingskit',
params: field[CommandFieldsEnum.COMMAND_TEXT],
};
if (field[CommandFieldsEnum.COMMAND_TYPE] === CommandType.CUSTOM) {
if (field[CommandFieldsEnum.VALUE_TYPE] === ValueType.JSON) {
const { json } = parseStringToJSON(field.commandValue);
command.params = json;
}
} else {
const { transportType } = props.deviceDetail.deviceProfile;
command.params =
transportType === TransportTypeEnum.TCP
? field[CommandFieldsEnum.TCP_SERVICE]
: {
[field[CommandFieldsEnum.SERVICE]]: field[CommandFieldsEnum.MODEL_INPUT],
};
command.additionalInfo = { cmdType: 1 };
}
commandIssuanceApi(
field[CommandFieldsEnum.CUSTOM_TYPE] as CommandDeliveryWayEnum,
props.deviceDetail.tbDeviceId,
command
)
.then((res) => {
if (!res) return;
createMessage.success('命令下发成功');
loading.value = true;
// 请求
handleCancel();
})
.catch((e) => {
if (e?.message) {
createMessage.error(e?.message);
}
handleCancel();
})
.finally(() => {
setTimeout(() => {
loading.value = false;
}, 300);
});
} catch (e) {
throw e;
} finally {
loading.value = false;
}
};
return {
registerForm,
handleCancel,
handleOk,
loading,
};
},
});
</script>
<style scoped lang="less">
.jsoneditor-transform {
background-position: -144px -96px;
display: none !important;
}
.tabs-detail:deep(.object-model-validate-form) {
> .ant-row {
@apply w-full;
}
}
</style>