index.vue 4.52 KB
<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增设备 </a-button>
      </template>
      <template #config="{ record }">
        <a-button type="link" class="ml-2" @click="showData(record)"> 查看配置 </a-button>
      </template>
      <template #deviceProfile="{ record }">
        <a-button type="link" class="ml-2" @click="goDeviceProfile">
          {{ record.deviceProfile.name }}
        </a-button>
      </template>
      <template #deviceType="{ record }">
        <Tag color="success" class="ml-2">
          {{
            record.deviceType == DeviceTypeEnum.GATEWAY
              ? '网关设备'
              : record.deviceType == DeviceTypeEnum.DIRECT_CONNECTION
              ? '直连设备'
              : '网关子设备'
          }}
        </Tag>
      </template>
      <template #deviceState="{ record }">
        <Tag
          :color="
            record.deviceState == DeviceState.INACTIVE
              ? 'warning'
              : record.deviceState == DeviceState.ONLINE
              ? 'success'
              : 'error'
          "
          class="ml-2"
        >
          {{
            record.deviceState == DeviceState.INACTIVE
              ? '待激活'
              : record.deviceState == DeviceState.ONLINE
              ? '在线'
              : '离线'
          }}
        </Tag>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <RuleEngineDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, h } from 'vue';
  import { DeviceState, DeviceTypeEnum } from '/@/api/device/model/deviceModel';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import RuleEngineDrawer from './RuleEngineDrawer.vue';
  import { columns } from './config.data';
  import { Modal, Tag } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { deleteDevice, devicePage } from '/@/api/device/deviceManager';
  import { PageEnum } from '/@/enums/pageEnum';
  import { useGo } from '/@/hooks/web/usePage';

  export default defineComponent({
    name: 'DeviceManagement',
    components: { BasicTable, RuleEngineDrawer, TableAction, Tag },
    setup() {
      const [registerDrawer, { openDrawer }] = useDrawer();
      const { createMessage } = useMessage();
      const go = useGo();
      const [registerTable, { reload }] = useTable({
        title: '设备列表',
        api: devicePage,
        columns,
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        let ids = [record.id];
        deleteDevice(ids).then(() => {
          createMessage.success('删除设备成功');
          handleSuccess();
        });
      }

      function handleSuccess() {
        reload();
      }
      function showData(record: Recordable) {
        Modal.info({
          title: '当前配置',
          width: 480,
          content: h(JsonPreview, { data: JSON.parse(JSON.stringify(record.deviceInfo)) }),
        });
      }
      function goDeviceProfile() {
        go(PageEnum.DEVICE_PROFILE);
      }
      return {
        registerTable,
        registerDrawer,
        showData,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        goDeviceProfile,
        DeviceTypeEnum,
        DeviceState,
      };
    },
  });
</script>