CommandIssuance.vue
3.2 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
<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, ref } 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 { 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';
  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 }] = useForm({
        labelWidth: 120,
        schemas: CommandSchemas(
          props.deviceDetail.deviceProfile.transportType as TransportTypeEnum
        ),
        labelAlign: 'right',
        showSubmitButton: false,
        showResetButton: false,
      });
      const handleCancel = () => {
        resetFields();
      };
      const handleOk = async () => {
        loading.value = true;
        try {
          // 验证
          const valid = await validate();
          if (!valid) return;
          // 收集表单数据
          const field = getFieldsValue();
          let command = {
            persistent: true,
            method: 'methodThingskit',
            params: field.commandText,
          };
          if (field.valueType === 'json') {
            const { json } = parseStringToJSON(field.commandValue);
            command.params = json;
          }
          commandIssuanceApi(field.commandType, 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>
  .jsoneditor-transform {
    background-position: -144px -96px;
    display: none !important;
  }
</style>