CommandIssuance.vue
4.75 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
<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>
<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';
interface CommandParams {
additionalInfo: Recordable;
cmdType: string;
method: string;
params: string | Recordable;
persistent: boolean;
}
export default defineComponent({
components: { BasicForm, Button, QuestionCircleOutlined, Tooltip },
props: {
deviceDetail: {
type: Object,
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: 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 handlePremitter = () => {
const value = unref(jsonInstance).get();
if (!value) return;
return unref(jsonInstance).set(value);
};
const handleCancel = () => {
resetFields();
unref(jsonInstance).set({});
};
const handleOk = async () => {
try {
disable.value = true;
// 验证
const valid = await validate();
if (!valid) return;
// 收集表单数据
const field = getFieldsValue();
if (field.valueType === 'json') {
const getJson = unref(jsonInstance).get();
jsonData.value.params = getJson;
} else {
jsonData.value.params = field.commandText;
}
jsonData.value.persistent = true;
jsonData.value.additionalInfo = {
cmdType: 'API',
};
jsonData.value.method = 'methodThingskit';
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) {
//这里捕获json插件的错误
createMessage.error('请填写正确的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>