CommandIssuance.vue
3.72 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
<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>
<Tooltip title='{"method":"setGpio","params":{"pin":7,"value":1}}' class="ml-2">
<QuestionCircleOutlined style="font-size: 1rem" />
</Tooltip>
</div>
</template>
</BasicForm>
<div>
<Button :disabled="disable" type="primary" @click="handleOk" class="mr-2">确定</Button>
<Button type="default" @click="handleCancel" class="mr-2">重置</Button>
</div>
</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 { Tooltip } from 'ant-design-vue';
export default defineComponent({
components: { BasicForm, Button, QuestionCircleOutlined, Tooltip },
props: {
deviceDetail: {
type: Object,
required: true,
},
},
emits: ['register'],
setup(props) {
const { createMessage } = useMessage();
const jsonData: any = ref({});
const disable = ref(false);
const [registerForm, { getFieldsValue, validate, resetFields }] = useForm({
labelWidth: 100,
schemas: CommandSchemas,
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 handleCancel = () => {
resetFields();
};
const handleOk = async () => {
disable.value = true;
// 验证
const valid = await validate();
if (!valid) return;
// 收集表单数据
const field = getFieldsValue();
jsonData.value = unref(jsonInstance).get();
jsonData.value.persistent = true;
jsonData.value.additionalInfo = {
cmdType: 'API',
};
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);
}
})
.finally(() => {
setTimeout(() => {
disable.value = false;
}, 300);
});
};
return {
registerForm,
handleCancel,
handleOk,
disable,
jsonData,
jsoneditorRef,
jsonValue,
jsonInstance,
};
},
});
</script>
<style scoped>
.jsoneditor-transform {
background-position: -144px -96px;
display: none !important;
}
</style>