ChildDevice.vue 4.46 KB
<template>
  <div class="bg-neutral-100 dark:text-gray-300 dark:bg-dark-700">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <Authority :value="DeviceListAuthEnum.DELETE">
          <Popconfirm
            :title="t('common.batchDeleteConfirmText')"
            :ok-text="t('common.okText')"
            :cancel-text="t('common.cancelText')"
            @confirm="handleDelete()"
            :disabled="disabledDeleteFlag"
          >
            <Button danger :disabled="disabledDeleteFlag">
              {{ t('common.batchDeleteText') }}
            </Button>
          </Popconfirm>
        </Authority>
      </template>
      <template #tbDeviceName="{ record }">
        <div style="color: #619eff" class="cursor-pointer" @click="handleGetTbDeviceId(record)">
          {{ record.alias || record.tbDeviceName }}
        </div>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: t('common.delText'),
              auth: DeviceListAuthEnum.DETAIL,
              icon: 'ant-design:eye-outlined',
              popConfirm: {
                title: t('common.deleteConfirmText'),
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { Button, Popconfirm } from 'ant-design-vue';
  import { DeviceRecord, DeviceState } from '/@/api/device/model/deviceModel';
  import { getTbDeviceId } from '/@/api/device/deviceConfigApi';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { childDeviceColumns, childDeviceSchemas } from '../../config/detail.config';
  import { deleteDevice, getChildDevicePage } from '/@/api/device/deviceManager';
  import { DeviceListAuthEnum } from '../../config/device.data';
  import { Authority } from '/@/components/Authority';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';

  export default defineComponent({
    name: 'DeviceManagement',
    components: {
      BasicTable,
      Popconfirm,
      TableAction,
      Authority,
      Button,
    },
    props: {
      fromId: {
        type: String,
        required: true,
      },
    },
    emits: ['openTbDeviceDetail'],
    setup(props, { emit }) {
      const { t } = useI18n();
      const [registerTable, { getSelectRowKeys, reload, setLoading }] = useTable({
        api: getChildDevicePage,
        columns: childDeviceColumns,
        formConfig: {
          labelWidth: 100,
          schemas: childDeviceSchemas,
        },
        beforeFetch: (data) => {
          Reflect.set(data, 'fromId', props.fromId);
        },
        rowKey: 'id',
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        clickToRowSelect: false,
        actionColumn: {
          width: 200,
          title: t('common.operateTable'),
          slots: { customRender: 'action' },
          fixed: 'right',
        },

        rowSelection: {
          type: 'checkbox',
          onSelect: (_record, _flag, allSelecteds) => {
            disabledDeleteFlag.value = !allSelecteds.length;
          },
          onSelectAll: () => {
            // 全选事件
            disabledDeleteFlag.value = false;
          },
        },
      });

      const { createMessage } = useMessage();

      const disabledDeleteFlag = ref(true);

      const handleGetTbDeviceId = async (record) => {
        try {
          const res = await getTbDeviceId(record.tbDeviceId);
          if (res && res.id) {
            emit('openTbDeviceDetail', { id: res.id, tbDeviceId: res.tbDeviceId });
          }
        } catch (error) {}
      };

      const handleDelete = async (record?: DeviceRecord) => {
        let ids: string[] = [];
        if (record) {
          ids.push(record.id);
        } else {
          ids = getSelectRowKeys();
        }
        try {
          setLoading(true);
          await deleteDevice(ids);
          createMessage.success(t('common.deleteSuccessText'));
          reload();
        } catch (error) {
          throw error;
        } finally {
          setLoading(false);
        }
      };

      return {
        registerTable,
        DeviceState,
        handleGetTbDeviceId,
        DeviceListAuthEnum,
        handleDelete,
        disabledDeleteFlag,
        t,
      };
    },
  });
</script>