index.vue 6.79 KB
<template>
  <div>
    <BasicTable
      @register="registerTable"
      :loading="loading"
      :rowSelection="{ type: 'checkbox' }"
      :clickToRowSelect="false"
    >
      <template #toolbar>
        <Authority :value="PermissionDataFlowEnum.PERMISSION_POST">
          <a-button
            type="primary"
            @click="handleBussinessModal(BusinessDataFlowTextEnum.BUSINESS_MODAL_ADD_TEXT, null)"
          >
            {{ t('rule.dataflow.index.add') }}
          </a-button>
        </Authority>
        <Authority :value="PermissionDataFlowEnum.PERMISSION_DELETE">
          <Popconfirm
            :title="t('rule.dataflow.index.sureBatchDelete')"
            :ok-text="t('common.okText')"
            :cancel-text="t('common.cancelText')"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button type="primary" color="error" :disabled="hasBatchDelete">
              <span :style="{ color: hasBatchDelete ? 'grey' : 'white' }">{{
                t('rule.dataflow.index.batchDelete')
              }}</span>
            </a-button>
          </Popconfirm>
        </Authority>
        <a-button
          :disabled="hasBatchDelete"
          @click="handleBatchEnable"
          :type="hasBatchDelete ? 'default' : 'primary'"
        >
          <span :style="{ color: hasBatchDelete ? 'grey' : 'white' }">{{
            t('rule.dataflow.index.batchEnable')
          }}</span>
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: t('rule.dataflow.index.view'),
              auth: PermissionDataFlowEnum.PERMISSION_GET,
              icon: 'ant-design:eye-outlined',
              onClick: handleBussinessModal.bind(
                null,
                BusinessDataFlowTextEnum.BUSINESS_MODAL_VIEW_TEXT,
                record
              ),
              ifShow: record.status == 1,
            },
            {
              label: t('rule.dataflow.index.edit'),
              auth: PermissionDataFlowEnum.PERMISSION_UPDATE,
              icon: 'clarity:note-edit-line',
              onClick: handleBussinessModal.bind(
                null,
                BusinessDataFlowTextEnum.BUSINESS_MODAL_EDIT_TEXT,
                record
              ),
              ifShow: record.status === 0,
            },
            {
              label: t('rule.dataflow.index.singleDelete'),
              auth: PermissionDataFlowEnum.PERMISSION_DELETE,
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: t('common.isDelete'),
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
              ifShow: record.status === 0,
            },
          ]"
        />
      </template>
      <template #status="{ record }">
        <Authority :value="PermissionDataFlowEnum.PERMISSION_UPDATE">
          <Switch
            :checked="record.status === 1"
            :loading="record.pendingStatus"
            :checkedChildren="t('rule.dataflow.index.enable')"
            :unCheckedChildren="t('rule.dataflow.index.disable')"
            @change="(checked:boolean)=>hanldeSwitch(checked,record)"
          />
        </Authority>
        <Tag
          v-if="!hasPermission(PermissionDataFlowEnum.PERMISSION_UPDATE)"
          :color="record.status ? 'green' : 'red'"
        >
          {{ record.status ? t('rule.dataflow.index.enable') : t('rule.dataflow.index.disable') }}
        </Tag>
      </template>
    </BasicTable>
    <DataFlowModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="dataFlowIndex">
  import { ref, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import {
    getConvertApi,
    isEnableOrDisableApi,
    deleteConvertApi,
  } from '/@/api/datamanager/dataManagerApi';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Authority } from '/@/components/Authority';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { Switch, Popconfirm, Tag } from 'ant-design-vue';
  import { PermissionDataFlowEnum, BusinessDataFlowTextEnum } from './enum';
  import { DataFlowModal } from './components/dataflowmodal';
  import { defaultTableAttribute } from './config';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n(); // 加载国际化
  const { createMessage } = useMessage();

  const loading = ref(true);

  const { hasPermission } = usePermission();

  const handleSuccess = () => {
    reload();
  };

  const [registerModal, { openModal }] = useModal();

  const [registerTable, { setProps, reload, getSelectRowKeys, clearSelectedRowKeys }] = useTable({
    api: getConvertApi,
    ...defaultTableAttribute,
  });

  const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
    useBatchDelete(deleteConvertApi, handleSuccess, setProps) as any;
  selectionOptions.rowSelection.getCheckboxProps = (record: Recordable) => {
    //业务 status为1则禁用
    if (record.status === 1) return { disabled: true };
    else {
      return { disabled: false };
    }
  };

  nextTick(() => {
    setProps(selectionOptions);
  });

  //业务 弹窗
  const handleBussinessModal = (text, record) => {
    const modalParams = {
      text,
      record,
    };
    openModal(true, modalParams);
  };

  const handleBatchEnable = async () => {
    const params: Record<string, any> = {
      convertIds: getSelectRowKeys(),
      status: 1,
    };
    const res = await isEnableOrDisableApi(params);
    if (!res) createMessage.error(t('rule.dataflow.index.multipleErrorMessage'));
    else {
      createMessage.success(t('rule.dataflow.index.multipleSuccessMessage'));
    }
    setPropsLoading(false);
    handleSuccess();
  };

  const setPropsLoading = (loading: boolean) => {
    setProps({
      loading,
    });
    clearSelectedRowKeys();
    resetSelectedRowKeys();
  };

  const hanldeSwitch = async (checked, record) => {
    try {
      setPropsLoading(true);
      const status = checked ? 1 : 0;
      const params: Record<string, any> = {
        convertIds: [record.id],
        nodeType: 1,
        status,
      };
      const resp = await isEnableOrDisableApi(params);
      if (!resp)
        createMessage.error(
          `${resp && status ? t('common.enableText') : t('common.disableText')}${t(
            'common.failText'
          )}`
        );
      else {
        createMessage.success(
          `${resp && status ? t('common.enableText') : t('common.disableText')}${t(
            'common.successText'
          )}`
        );
      }
    } finally {
      setPropsLoading(false);
      handleSuccess();
    }
  };
</script>