list.vue 6.02 KB
<template>
  <div>
    <BasicTable @register="registerTable" :row-selection="rowSelection">
      <template #content="{ record }">
        <a-button type="link" class="ml-2" @click="handleRecordContent(record)"> 查看 </a-button>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleCreateOrEdit(null)"> 新增公共接口 </a-button>
        <Popconfirm
          title="您确定要批量删除数据"
          ok-text="确定"
          cancel-text="取消"
          @confirm="handleDeleteOrBatchDelete(null)"
        >
          <a-button color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
        </Popconfirm>
        <Popconfirm
          title="您确定要批量发布"
          ok-text="确定"
          cancel-text="取消"
          @confirm="handleBatchPublish('batchPublish')"
        >
          <a-button color="error" :disabled="hasBatchPublish"> 批量发布 </a-button>
        </Popconfirm>
        <!-- <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',
              onClick: handlePublish.bind(null, 'publish', record),
              ifShow: () => {
                return record.state === 0;
              },
            },
            {
              label: '取消发布',
              icon: 'ant-design:node-collapse-outlined',
              onClick: handlePublish.bind(null, 'canelPublish', record),
              ifShow: () => {
                return record.state === 1;
              },
            },
            {
              label: '修改',
              icon: 'clarity:note-edit-line',
              onClick: handleCreateOrEdit.bind(null, record),
              ifShow: () => {
                return record.state === 0;
              },
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              ifShow: () => {
                return record.state === 0;
              },
              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';
  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';

  const [registerTable, { reload, clearSelectedRowKeys }] = useTable({
    api: getDataViewInterfacePage,
    columns,
    showIndexColumn: false,
    clickToRowSelect: false,
    showTableSetting: true,
    bordered: true,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    actionColumn: {
      width: 150,
      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.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, { data: JSON.parse(record.requestParams) }),
    });
  };

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

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

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