Commit 3288bccc81b0f23f479d0b8930758d3da479534f
1 parent
00f647d4
feat:pc端 设备新增命令下发和引入json-editor-vue3
Showing
3 changed files
with
23 additions
and
22 deletions
... | ... | @@ -655,19 +655,7 @@ export const CommandSchemas: FormSchema[] = [ |
655 | 655 | { |
656 | 656 | field: 'commandValue', |
657 | 657 | label: '请输入命令内容', |
658 | - defaultValue: `{ | |
659 | - method: 'setGpio', | |
660 | - params: { | |
661 | - pin: 7, | |
662 | - value: 1 | |
663 | - } | |
664 | -}`, | |
658 | + slot: 'commandSlot', | |
665 | 659 | component: 'InputTextArea', |
666 | - componentProps: { | |
667 | - autoSize: { | |
668 | - maxRows: 10, | |
669 | - autoSize: true, | |
670 | - }, | |
671 | - }, | |
672 | 660 | }, |
673 | 661 | ]; | ... | ... |
1 | 1 | <template> |
2 | 2 | <div class="tabs-detail"> |
3 | 3 | <div class="mt-4"> |
4 | - <BasicForm @register="registerForm" /> | |
4 | + <BasicForm @register="registerForm"> | |
5 | + <template #commandSlot> | |
6 | + <JsonEditorVue class="editor" v-model="jsonData" @blur="jsonValidate" /> | |
7 | + </template> | |
8 | + </BasicForm> | |
5 | 9 | <div> |
6 | 10 | <Button :disabled="disable" type="primary" @click="handleOk" class="mr-2">确定</Button> |
7 | 11 | <Button type="default" @click="handleCancel" class="mr-2">重置</Button> |
... | ... | @@ -16,9 +20,10 @@ |
16 | 20 | import { commandIssuanceApi } from '/@/api/device/deviceManager'; |
17 | 21 | import { useMessage } from '/@/hooks/web/useMessage'; |
18 | 22 | import { Button } from '/@/components/Button'; |
23 | + import JsonEditorVue from 'json-editor-vue3'; | |
19 | 24 | |
20 | 25 | export default defineComponent({ |
21 | - components: { BasicForm, Button }, | |
26 | + components: { BasicForm, Button, JsonEditorVue }, | |
22 | 27 | props: { |
23 | 28 | deviceDetail: { |
24 | 29 | type: Object, |
... | ... | @@ -27,14 +32,21 @@ |
27 | 32 | }, |
28 | 33 | emits: ['register'], |
29 | 34 | setup(props) { |
30 | - const disable = ref(false); | |
31 | - const commandValueStr = JSON.stringify({ | |
35 | + const { createMessage } = useMessage(); | |
36 | + const jsonData: any = ref({ | |
32 | 37 | method: 'setGpio', |
33 | 38 | params: { |
34 | 39 | pin: 7, |
35 | 40 | value: 1, |
36 | 41 | }, |
37 | 42 | }); |
43 | + const jsonValidate = async (editor) => { | |
44 | + const res = await editor.validate(); | |
45 | + // res 是错误列表,如果是空数组,则表示检测没有错误 | |
46 | + console.log(res); | |
47 | + if (res.length !== 0) return createMessage.error('请填写正确的命令下发json数据格式'); | |
48 | + }; | |
49 | + const disable = ref(false); | |
38 | 50 | const [registerForm, { getFieldsValue, validate, resetFields }] = useForm({ |
39 | 51 | labelWidth: 100, |
40 | 52 | schemas: CommandSchemas, |
... | ... | @@ -50,18 +62,16 @@ |
50 | 62 | }; |
51 | 63 | const handleOk = async () => { |
52 | 64 | disable.value = true; |
53 | - const { createMessage } = useMessage(); | |
54 | 65 | // 验证 |
55 | 66 | const valid = await validate(); |
56 | 67 | if (!valid) return; |
57 | 68 | // 收集表单数据 |
58 | 69 | const field = getFieldsValue(); |
59 | - const commandValue = JSON.parse(commandValueStr); | |
60 | - commandValue.persistent = true; | |
61 | - commandValue.additionalInfo = { | |
70 | + jsonData.value.persistent = true; | |
71 | + jsonData.value.additionalInfo = { | |
62 | 72 | cmdType: 'API', |
63 | 73 | }; |
64 | - commandIssuanceApi(field.commandType, props.deviceDetail.tbDeviceId, commandValue) | |
74 | + commandIssuanceApi(field.commandType, props.deviceDetail.tbDeviceId, jsonData.value) | |
65 | 75 | .then((res) => { |
66 | 76 | if (!res) return; |
67 | 77 | createMessage.success('命令下发成功'); |
... | ... | @@ -85,6 +95,8 @@ |
85 | 95 | handleCancel, |
86 | 96 | handleOk, |
87 | 97 | disable, |
98 | + jsonData, | |
99 | + jsonValidate, | |
88 | 100 | }; |
89 | 101 | }, |
90 | 102 | }); | ... | ... |