index.vue 7.82 KB
<template>
  <div>
    <PageWrapper dense contentFullHeight contentClass="flex">
      <OrganizationIdTree
        class="w-1/6 xl:w-1/5"
        @select="handleSelect"
        ref="organizationIdTreeRef"
      />
      <BasicTable @register="registerTable" class="w-5/6 xl:w-4/5">
        <template #toolbar>
          <a-button type="primary" @click="handleCreate" v-if="authBtn(role)"> 新增设备 </a-button>
        </template>
        <template #deviceProfile="{ record }">
          <a-button type="link" class="ml-2" @click="goDeviceProfile(record.deviceProfile.name)">
            {{ 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="[
              record.customerId
                ? {
                    label: '取消分配',
                    icon: 'mdi:account-arrow-left',
                    ifShow: authBtn(role),
                    popConfirm: {
                      title: '是否取消分配客户',
                      confirm: handleCancelDispatchCustomer.bind(null, record),
                    },
                  }
                : {
                    label: '分配客户',
                    icon: 'mdi:account-arrow-right',
                    ifShow: authBtn(role),
                    onClick: handleDispatchCustomer.bind(null, record),
                  },

              {
                label: '详情',
                icon: 'ant-design:eye-outlined',
                onClick: handleDetail.bind(null, record),
              },
              {
                label: '编辑',
                icon: 'clarity:note-edit-line',
                ifShow: authBtn(role),
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                icon: 'ant-design:delete-outlined',
                ifShow: authBtn(role),
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </BasicTable>
      <DeviceDetailDrawer @register="registerDetailDrawer" />
      <DeviceModal @register="registerModal" @success="handleSuccess" @reload="handleSuccess" />
      <CustomerModal @register="registerCustomerModal" @reload="handleSuccess" />
    </PageWrapper>
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive } from 'vue';
  import { DeviceState, DeviceTypeEnum } from '/@/api/device/model/deviceModel';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { columns, searchFormSchema } from './config/device.data';
  import { Tag } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {
    deleteDevice,
    devicePage,
    cancelDispatchCustomer,
    getGATEWAY,
  } from '/@/api/device/deviceManager';
  import { PageEnum } from '/@/enums/pageEnum';
  import { useGo } from '/@/hooks/web/usePage';
  import { PageWrapper } from '/@/components/Page';
  import { useModal } from '/@/components/Modal';
  import { OrganizationIdTree, useResetOrganizationTree } from '/@/views/common/organizationIdTree';

  import DeviceModal from './cpns/modal/DeviceModal.vue';
  import { useDrawer } from '/@/components/Drawer';
  import DeviceDetailDrawer from './cpns/modal/DeviceDetailDrawer.vue';
  import CustomerModal from './cpns/modal/CustomerModal.vue';

  import { USER_INFO_KEY } from '/@/enums/cacheEnum';
  import { getAuthCache } from '/@/utils/auth';
  import { authBtn } from '/@/enums/roleEnum';
  export default defineComponent({
    name: 'DeviceManagement',
    components: {
      BasicTable,
      PageWrapper,
      TableAction,
      OrganizationIdTree,
      Tag,
      DeviceModal,
      DeviceDetailDrawer,
      CustomerModal,
    },
    setup(_) {
      const { createMessage } = useMessage();
      const go = useGo();

      const searchInfo = reactive<Recordable>({});
      const { organizationIdTreeRef, resetFn } = useResetOrganizationTree(searchInfo);
      const [registerModal, { openModal }] = useModal();
      const [registerCustomerModal, { openModal: openCustomerModal }] = useModal();
      const [registerDetailDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '设备列表',
        api: devicePage,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          resetFunc: resetFn,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        searchInfo: searchInfo,
        actionColumn: {
          width: 300,
          title: '操作',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      const userInfo: any = getAuthCache(USER_INFO_KEY);
      const role: string = userInfo.roles[0];

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }
      // 分配客户
      function handleDispatchCustomer(record: Recordable) {
        openCustomerModal(true, record);
      }
      // 取消分配客户
      async function handleCancelDispatchCustomer(record: Recordable) {
        console.log('record', record);
        await cancelDispatchCustomer(record);
        handleSuccess();
      }

      function handleDetail(record: Recordable) {
        const { id, tbDeviceId } = record;
        openDrawer(true, {
          id,
          tbDeviceId,
        });
      }

      async function handleEdit(record: Recordable) {
        if (record.deviceType === 'SENSOR') {
          const res = await getGATEWAY(record.tbDeviceId);
          Reflect.set(record, 'gateWayDeviceId', res.id);
        }
        openModal(true, {
          isUpdate: true,
          record,
        });
      }

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

      function handleSuccess() {
        reload();
      }
      function handleSelect(organization) {
        searchInfo.organizationId = organization;
        handleSuccess();
      }
      function goDeviceProfile(e) {
        go(PageEnum.DEVICE_PROFILE + '?name=' + String(e));
      }

      return {
        registerTable,
        handleCreate,
        handleDetail,
        handleEdit,
        handleDelete,
        handleSuccess,
        goDeviceProfile,
        handleSelect,
        registerModal,
        registerDetailDrawer,
        DeviceTypeEnum,
        DeviceState,
        searchInfo,
        organizationIdTreeRef,
        handleDispatchCustomer,
        handleCancelDispatchCustomer,
        registerCustomerModal,
        authBtn,
        role,
      };
    },
  });
</script>