list.vue 6.98 KB
<template>
  <div>
    <BasicTable
      @register="registerTable"
      :row-selection="rowSelection"
      class="bg-neutral-100 dark:bg-dark-700"
    >
      <template #content="{ record }">
        <a-button type="link" class="ml-2" @click="handleRecordContent(record)"> 查看 </a-button>
      </template>
      <template #toolbar>
        <Authority :value="PublicInterface.CREATE">
          <a-button type="primary" @click="handleCreateOrEdit(null)"> 新增公共接口 </a-button>
        </Authority>

        <Authority :value="PublicInterface.DELETE">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
          </Popconfirm>
        </Authority>
        <Authority :value="PublicInterface.PUBLISH">
          <Popconfirm
            title="您确定要批量发布"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleBatchPublish('batchPublish')"
          >
            <a-button color="primary" :disabled="hasBatchPublish"> 批量发布 </a-button>
          </Popconfirm>
        </Authority>
        <!-- <Popconfirm
          title="您确定要批量取消发布"
          ok-text="确定"
          cancel-text="取消"
          @confirm="handleBatchPublish('cancelBatchPublish')"
        >
          <a-button color="error" :disabled="hasBatchPublish"> 批量取消发布 </a-button>
        </Popconfirm> -->
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '发布',
              icon: 'ant-design:node-expand-outlined',
              auth: PublicInterface.PUBLISH,
              onClick: handlePublish.bind(null, 'publish', record),
              ifShow: () => {
                return record.state === 0 && record.creator === userId;
              },
            },
            {
              label: '取消发布',
              icon: 'ant-design:node-collapse-outlined',
              auth: PublicInterface.CANCEL_PUBLISH,
              onClick: handlePublish.bind(null, 'canelPublish', record),
              ifShow: () => {
                return record.state === 1 && record.creator === userId;
              },
            },
            {
              label: '修改',
              icon: 'clarity:note-edit-line',
              auth: PublicInterface.UPDATE,
              onClick: handleCreateOrEdit.bind(null, record),
              ifShow: () => {
                return record.state === 0 && record.creator === userId;
              },
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              auth: PublicInterface.DELETE,
              color: 'error',
              ifShow: () => {
                return record.state === 0 && record.creator === userId;
              },
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
  </div>
  <PublicApiForm @register="registerDrawer" @success="handleSuccess" />
</template>
<script lang="ts" setup name="list">
  import { h, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { columns, searchFormSchema } from './config/config';
  import { PublicApiForm } from './index';
  import {
    getDataViewInterfacePage,
    deleteBigViewInterface,
    getPublish,
    getCancelPublish,
    putPublishOrCancelPublish,
  } from '/@/api/bigscreen/center/bigscreenCenter';
  import { Popconfirm, Modal } from 'ant-design-vue';
  import { JsonPreview } from '/@/components/CodeEditor';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { USER_INFO_KEY } from '/@/enums/cacheEnum';
  import { getAuthCache } from '/@/utils/auth';
  import { PublicInterface } from '../config';
  import { Authority } from '/@/components/Authority';

  const userInfo = getAuthCache(USER_INFO_KEY) as any;

  const userId = ref(userInfo?.userId);

  const [registerTable, { reload, clearSelectedRowKeys }] = useTable({
    api: getDataViewInterfacePage,
    columns,
    showIndexColumn: false,
    clickToRowSelect: false,
    showTableSetting: true,
    bordered: true,
    formConfig: {
      labelWidth: 80,
      schemas: searchFormSchema,
      baseColProps: { span: 9 },
      actionColOptions: { span: 6 },
    },
    useSearchForm: true,
    actionColumn: {
      width: 180,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  const hasBatchPublish = ref(true);

  const hasBatchDelete = ref(true);

  const batchDeleteIds = ref([]);

  const { createMessage } = useMessage();

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

  const setStatusIsFalse = () => {
    (hasBatchDelete.value = false), (hasBatchPublish.value = false);
  };

  const setStatusIsTrue = () => {
    (hasBatchDelete.value = true), (hasBatchPublish.value = true);
  };

  const rowSelection = {
    onChange: (_, selectedRows: any[]) => {
      batchDeleteIds.value = selectedRows.map((it) => it.id) as never as any;
      if (batchDeleteIds.value.length > 0) setStatusIsFalse();
      else setStatusIsTrue();
    },
    getCheckboxProps: (record) => ({
      disabled: record.creator !== userId.value || record.state === 1,
    }),
  };

  const handleCreateOrEdit = (record) => {
    const isUpdate = record === null ? false : true;
    const recordContent = record === null ? null : record;
    openDrawer(true, {
      isUpdate,
      record: recordContent,
    });
  };

  const handleRecordContent = (record) => {
    Modal.info({
      title: '接口内容',
      width: 600,
      content: h(JsonPreview, { virtual: true, data: JSON.parse(record.requestParams) }),
    });
  };

  const handlePublish = async (type, record) => {
    try {
      type === 'publish' ? await getPublish(record.id) : await getCancelPublish(record.id);
      createMessage.success(`${type === 'publish' ? '发布' : '取消发布'}成功`);
    } finally {
      handleSuccess();
    }
  };

  const handleBatchPublish = async (type) => {
    try {
      await putPublishOrCancelPublish(type, batchDeleteIds.value);
      createMessage.success(`${type === 'batchPublish' ? '批量发布' : '批量取消发布'}成功`);
    } finally {
      handleSuccess();
    }
  };

  const handleDeleteOrBatchDelete = async (record) => {
    try {
      const ids = record === null ? batchDeleteIds.value : [record.id];
      await deleteBigViewInterface(ids);
      createMessage.success(`${record === null ? '批量删除' : '删除'}成功`);
    } finally {
      handleSuccess();
    }
  };
</script>