index.vue 6.46 KB
<template>
  <div>
    <BasicTable
      @selection-change="useSelectionChange"
      @register="registerTable"
      :rowSelection="{ type: 'checkbox' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="handleAdd"> 添加转换 </a-button>
        <a-button
          @click="handleDelete"
          style="background-color: rgba(237, 111, 111, 1)"
          type="default"
        >
          <span style="color: white">批量删除</span>
        </a-button>
        <a-button
          @click="handleMutiuteDisable"
          style="background-color: rgba(128, 128, 128.2)"
          type="default"
        >
          <span style="color: white">批量禁用</span>
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              icon: 'clarity:note-edit-line',
              color: 'success',
              onClick: handleEdit.bind(null, record),
              ifShow: (_action) => {
                return record.status == 0;
              },
            },

            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleSingleDelete.bind(null, record),
              },
              ifShow: (_action) => {
                return record.status == 0;
              },
            },
            {
              label: '启用',
              icon: 'ant-design:check-circle-outlined',
              color: 'warning',
              popConfirm: {
                title: '是否启用?',
                confirm: handleEnableOrDisable.bind(null, record),
              },
              ifShow: (_action) => {
                return record.status == 0;
              },
            },
            {
              label: '禁用',
              icon: 'ant-design:check-circle-outlined',
              color: 'warning',
              popConfirm: {
                title: '是否禁用?',
                confirm: handleDisable.bind(null, record),
              },
              ifShow: (_action) => {
                return record.status == 1;
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <div>
      <DataTransferDrawer @register="registerModal" @success="handleSuccess" />
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { columns, searchFormSchema } from './config';
  import { useModal } from '/@/components/Modal';
  import DataTransferDrawer from './addDataTransferDrawer.vue';
  import {
    getConvertApi,
    isEnableOrDisableApi,
    deleteConvertApi,
  } from '/@/api/datamanager/dataManagerApi';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'Index',
    components: { BasicTable, TableAction, DataTransferDrawer },
    setup() {
      const enableObj = reactive({
        convertIds: [],
        status: 0,
      });
      const { createMessage } = useMessage();
      let selectedRowKeys: any = ref([]);
      let getSelectRowsArr: any = ref([]);
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, getSelectRowKeys, getSelectRows }] = useTable({
        title: '数据转换列表',
        clickToRowSelect: false,
        columns,
        api: getConvertApi,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        rowKey: 'id',
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      //新增
      const handleAdd = () => {
        setTimeout(() => {
          openModal(true, {
            isUpdate: false,
          });
        }, 10);
      };

      const handleSuccess = () => {
        reload();
      };
      const handleEdit = (record: Recordable) => {
        setTimeout(() => {
          openModal(true, {
            record,
            isUpdate: true,
          });
        }, 10);
      };
      const handleEnableOrDisable = async (record: Recordable) => {
        enableObj.convertIds.length = 0;
        enableObj.status = record.status;
        enableObj.convertIds.push(record.id as never);
        if (enableObj.status == 0) {
          enableObj.status = 1;
        }
        await isEnableOrDisableApi(enableObj as never);
        createMessage.success('转换配置启用成功');
        reload();
      };
      const handleDisable = async (record: Recordable) => {
        enableObj.convertIds.length = 0;
        enableObj.status = record.status;
        enableObj.convertIds.push(record.id as never);
        if (enableObj.status == 1) {
          enableObj.status = 0;
        }
        await isEnableOrDisableApi(enableObj as never);
        createMessage.success('转换配置禁用成功');
        reload();
      };
      const handleSingleDelete = async (record: Recordable) => {
        try {
          let ids = [record.id];
          await deleteConvertApi(ids);
          createMessage.success('删除成功');
          reload();
        } catch (e) {
          return e;
        }
      };
      const useSelectionChange = () => {
        selectedRowKeys.value = getSelectRowKeys();
      };

      const handleDelete = async () => {
        await deleteConvertApi(selectedRowKeys.value);
        createMessage.success('删除成功');
        reload();
      };
      const handleMutiuteDisable = async () => {
        getSelectRowsArr.value = getSelectRows();
        getSelectRowsArr.value.forEach((f) => {
          if (f.id) {
            enableObj.status = 0;
            enableObj.convertIds.push(f.id as never);
          }
        });
        await isEnableOrDisableApi(enableObj as never);
        createMessage.success('转换配置多项禁用成功');
        reload();
      };

      return {
        registerTable,
        handleAdd,
        handleDelete,
        registerModal,
        handleSuccess,
        handleEdit,
        handleEnableOrDisable,
        handleSingleDelete,
        useSelectionChange,
        handleMutiuteDisable,
        handleDisable,
      };
    },
  });
</script>

<style lang="less" scoped></style>