admin-vben5/apps/web-antd/src/views/tool/gen/index.vue

231 lines
6.1 KiB
Vue
Raw Normal View History

2024-08-07 08:57:56 +08:00
<script setup lang="ts">
2024-09-23 22:11:31 +08:00
import type { Recordable } from '@vben/types';
2024-10-05 23:51:38 +08:00
import { ref } from 'vue';
2024-09-23 22:11:31 +08:00
import { useRouter } from 'vue-router';
2024-10-05 14:48:23 +08:00
import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
2024-09-13 12:00:55 +08:00
2024-10-05 14:48:23 +08:00
import { message, Modal, Popconfirm, Space } from 'ant-design-vue';
import dayjs from 'dayjs';
2024-09-23 22:11:31 +08:00
2024-10-05 14:48:23 +08:00
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
2024-09-23 22:11:31 +08:00
import { batchGenCode, generatedList, genRemove, syncDb } from '#/api/tool/gen';
import { downloadByData } from '#/utils/file/download';
2024-09-13 12:00:55 +08:00
import codePreviewModal from './code-preview-modal.vue';
2024-10-05 14:48:23 +08:00
import { columns, querySchema } from './data';
2024-10-05 18:01:17 +08:00
import tableImportModal from './table-import-modal.vue';
2024-09-13 12:00:55 +08:00
2024-10-05 14:48:23 +08:00
const formOptions: VbenFormProps = {
schema: querySchema(),
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};
const gridOptions: VxeGridProps = {
checkboxConfig: {
// 高亮
highlight: true,
// 翻页时保留选中状态
reserve: true,
// 点击行选中
trigger: 'row',
2024-09-23 22:11:31 +08:00
},
2024-10-05 14:48:23 +08:00
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
// 区间选择器处理
if (formValues?.createTime) {
formValues.params = {
beginTime: dayjs(formValues.createTime[0]).format(
'YYYY-MM-DD 00:00:00',
),
endTime: dayjs(formValues.createTime[1]).format(
'YYYY-MM-DD 23:59:59',
),
};
Reflect.deleteProperty(formValues, 'createTime');
} else {
Reflect.deleteProperty(formValues, 'params');
}
return await generatedList({
pageNum: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
2024-09-23 22:11:31 +08:00
},
2024-10-05 14:48:23 +08:00
rowConfig: {
isHover: true,
keyField: 'tableId',
2024-09-23 22:11:31 +08:00
},
2024-10-05 14:48:23 +08:00
round: true,
align: 'center',
showOverflow: true,
};
2024-10-05 23:51:38 +08:00
const checked = ref(false);
const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents: {
checkboxChange: (e: any) => {
checked.value = e.records.length > 0;
},
checkboxAll: (e: any) => {
checked.value = e.records.length > 0;
},
},
});
2024-09-23 22:11:31 +08:00
2024-10-05 14:48:23 +08:00
const [CodePreviewModal, previewModalApi] = useVbenModal({
connectedComponent: codePreviewModal,
2024-09-23 22:11:31 +08:00
});
function handlePreview(record: Recordable<any>) {
previewModalApi.setData({ tableId: record.tableId });
2024-09-13 12:00:55 +08:00
previewModalApi.open();
}
2024-09-23 22:11:31 +08:00
const router = useRouter();
function handleEdit(record: Recordable<any>) {
router.push(`/code-gen/edit/${record.tableId}`);
}
async function handleSync(record: Recordable<any>) {
await syncDb(record.tableId);
2024-10-05 23:51:38 +08:00
await tableApi.query();
2024-09-24 11:38:51 +08:00
}
/**
* 批量生成代码
*/
async function handleBatchGen() {
2024-10-05 14:48:23 +08:00
const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: any) => row.tableId);
if (ids.length === 0) {
2024-09-24 11:38:51 +08:00
message.info('请选择需要生成代码的表');
return;
}
const hideLoading = message.loading('下载中...');
try {
2024-10-05 14:48:23 +08:00
const params = ids.join(',');
2024-09-24 11:38:51 +08:00
const data = await batchGenCode(params);
const timestamp = Date.now();
downloadByData(data, `批量代码生成_${timestamp}.zip`);
} finally {
hideLoading();
}
}
2024-09-23 22:11:31 +08:00
async function handleDownload(record: Recordable<any>) {
const hideLoading = message.loading('下载中...');
try {
const blob = await batchGenCode(record.tableId);
const filename = `代码生成_${record.tableName}_${new Date().toLocaleString()}.zip`;
downloadByData(blob, filename);
} catch (error) {
console.error(error);
} finally {
hideLoading();
}
}
2024-09-24 11:38:51 +08:00
/**
* 删除
* @param record
*/
2024-09-23 22:11:31 +08:00
async function handleDelete(record: Recordable<any>) {
await genRemove(record.tableId);
2024-10-05 23:51:38 +08:00
await tableApi.query();
2024-09-23 22:11:31 +08:00
}
2024-09-24 11:23:02 +08:00
2024-10-05 14:48:23 +08:00
function handleMultiDelete() {
const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: any) => row.tableId);
Modal.confirm({
title: '提示',
okType: 'danger',
content: `确认删除选中的${ids.length}条记录吗?`,
onOk: async () => {
await genRemove(ids);
2024-10-05 23:51:38 +08:00
await tableApi.query();
checked.value = false;
2024-09-24 11:23:02 +08:00
},
2024-10-05 14:48:23 +08:00
});
}
2024-10-05 18:01:17 +08:00
const [TableImportModal, tableImportModalApi] = useVbenModal({
connectedComponent: tableImportModal,
});
function handleImport() {
tableImportModalApi.open();
}
2024-08-07 08:57:56 +08:00
</script>
<template>
2024-10-05 14:48:23 +08:00
<Page :auto-content-height="true">
<BasicTable>
<template #toolbar-actions>
<span class="pl-[7px] text-[16px]">代码生成列表</span>
</template>
<template #toolbar-tools>
<Space>
2024-10-05 23:51:38 +08:00
<a-button
:disabled="!checked"
danger
type="primary"
@click="handleMultiDelete"
>
2024-10-05 14:48:23 +08:00
{{ $t('pages.common.delete') }}
2024-09-23 22:11:31 +08:00
</a-button>
2024-10-05 23:51:38 +08:00
<a-button :disabled="!checked" @click="handleBatchGen">
2024-09-24 11:38:51 +08:00
{{ $t('pages.common.generate') }}
</a-button>
2024-10-05 18:01:17 +08:00
<a-button type="primary" @click="handleImport">
2024-09-24 11:38:51 +08:00
{{ $t('pages.common.import') }}
</a-button>
2024-10-05 14:48:23 +08:00
</Space>
</template>
<template #action="{ row }">
<a-button size="small" type="link" @click.stop="handlePreview(row)">
{{ $t('pages.common.preview') }}
</a-button>
<a-button size="small" type="link" @click.stop="handleEdit(row)">
{{ $t('pages.common.edit') }}
</a-button>
<Popconfirm
:title="`确认同步[${row.tableName}]?`"
placement="left"
@confirm="handleSync(row)"
>
<a-button size="small" type="link" @click.stop="">
{{ $t('pages.common.sync') }}
</a-button>
</Popconfirm>
<a-button size="small" type="link" @click.stop="handleDownload(row)">
生成代码
</a-button>
<Popconfirm
:title="`确认删除[${row.tableName}]?`"
placement="left"
@confirm="handleDelete(row)"
>
<a-button danger size="small" type="link" @click.stop="">
{{ $t('pages.common.delete') }}
</a-button>
</Popconfirm>
</template>
</BasicTable>
2024-09-13 12:00:55 +08:00
<CodePreviewModal />
2024-10-05 23:51:38 +08:00
<TableImportModal @reload="tableApi.query()" />
2024-09-13 12:00:55 +08:00
</Page>
2024-08-07 08:57:56 +08:00
</template>