CommandIssuance.vue
5.77 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<template>
<div class="tabs-detail">
<div class="mt-4">
<BasicForm @register="registerForm">
<template #commandSlot>
<div class="flex">
<div ref="jsoneditorRef" style="height: 100%; width: 100%"></div>
<a-button style="margin: -5px 0" type="text" @click="handlePremitter">格式化</a-button>
<Tooltip title='{"method":"methodThingskit","params":{"pin":7,"value":1}}' class="ml-2">
<QuestionCircleOutlined style="font-size: 1rem" />
</Tooltip>
</div>
</template>
</BasicForm>
<Space class="w-full justify-end" justify="end">
<Button :disabled="disable" 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, ref, onMounted, unref, nextTick } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { CommandSchemas } from '../../config/data';
import { commandIssuanceApi } from '/@/api/device/deviceManager';
import { useMessage } from '/@/hooks/web/useMessage';
import { Button } from '/@/components/Button';
import jsoneditor from 'jsoneditor';
import 'jsoneditor/dist/jsoneditor.min.css';
import { QuestionCircleOutlined } from '@ant-design/icons-vue';
import { Space, Tooltip } from 'ant-design-vue';
import { DeviceRecord } from '/@/api/device/model/deviceModel';
import { TransportTypeEnum } from '../../../profiles/components/TransportDescript/const';
interface CommandParams {
additionalInfo: Recordable;
cmdType: string;
method: string;
params: string | Recordable;
persistent: boolean;
}
export default defineComponent({
components: { BasicForm, Button, QuestionCircleOutlined, Tooltip, Space },
props: {
deviceDetail: {
type: Object as PropType<DeviceRecord>,
required: true,
},
},
emits: ['register'],
setup(props) {
const { createMessage } = useMessage();
const jsonData = ref<CommandParams>({} as unknown as CommandParams);
const disable = ref(false);
const [registerForm, { getFieldsValue, validate, resetFields }] = useForm({
labelWidth: 120,
schemas: CommandSchemas(
props.deviceDetail.deviceProfile.transportType as TransportTypeEnum
),
labelAlign: 'right',
showSubmitButton: false,
showResetButton: false,
wrapperCol: {
span: 12,
},
});
// json 以及初始化JSON
const jsoneditorRef = ref();
const jsonValue = ref({});
const jsonInstance = ref();
onMounted(() => {
nextTick(() => {
let options = {
mode: 'code',
mainMenuBar: false,
statusBar: false,
};
let editor = new jsoneditor(jsoneditorRef.value, options);
editor.set(jsonValue.value);
jsonInstance.value = editor;
});
});
const handlePremitter = () => {
const value = unref(jsonInstance).get();
if (!value) return;
return unref(jsonInstance).set(value);
};
const handleCancel = () => {
resetFields();
unref(jsonInstance).set({});
};
const handleOk = async () => {
disable.value = true;
try {
// 验证
const valid = await validate();
if (!valid) return;
// 收集表单数据
const field = getFieldsValue();
let passStatus = false;
if (field.valueType === 'json') {
const getJson = unref(jsonInstance).get();
if (Object.prototype.isPrototypeOf(getJson) && Object.keys(getJson).length === 0) {
createMessage.error('命令内容不能为空');
passStatus = true;
}
if (getJson === '') {
passStatus = true;
createMessage.error('命令内容不能为空');
}
jsonData.value.params = getJson;
} else {
jsonData.value.params = field.commandText;
if (!jsonData.value.params) {
createMessage.error('命令内容不能为空');
passStatus = true;
}
if (
jsonData.value.params == '""' ||
jsonData.value.params == "''" ||
jsonData.value.params == '“”'
) {
createMessage.error('命令内容不能为空');
passStatus = true;
}
}
jsonData.value.persistent = true;
jsonData.value.method = 'methodThingskit';
if (passStatus) return;
commandIssuanceApi(field.commandType, props.deviceDetail.tbDeviceId, jsonData.value)
.then((res) => {
if (!res) return;
createMessage.success('命令下发成功');
disable.value = true;
// 请求
handleCancel();
})
.catch((e) => {
if (e?.message) {
createMessage.error(e?.message);
}
handleCancel();
})
.finally(() => {
setTimeout(() => {
disable.value = false;
}, 300);
});
} catch (e) {
} finally {
//这里捕获json插件的错误
disable.value = false;
}
};
return {
registerForm,
handleCancel,
handleOk,
disable,
jsonData,
jsoneditorRef,
jsonValue,
jsonInstance,
handlePremitter,
};
},
});
</script>
<style scoped>
.jsoneditor-transform {
background-position: -144px -96px;
display: none !important;
}
</style>