feat: 代码生成

This commit is contained in:
dap 2024-10-05 14:48:23 +08:00
parent a77886a675
commit 34c2117aa0
2 changed files with 152 additions and 136 deletions

View File

@ -1,4 +1,4 @@
import type { FormSchemaGetter } from '#/adapter'; import type { FormSchemaGetter, VxeGridProps } from '#/adapter';
export const querySchema: FormSchemaGetter = () => [ export const querySchema: FormSchemaGetter = () => [
{ {
@ -23,3 +23,34 @@ export const querySchema: FormSchemaGetter = () => [
label: '创建时间', label: '创建时间',
}, },
]; ];
export const columns: VxeGridProps['columns'] = [
{ type: 'checkbox', width: 60 },
{
field: 'tableName',
title: '表名称',
},
{
field: 'tableComment',
title: '表描述',
},
{
field: 'className',
title: '实体类',
},
{
field: 'createTime',
title: '创建时间',
},
{
field: 'updateTime',
title: '更新时间',
},
{
field: 'action',
fixed: 'right',
slots: { default: 'action' },
title: '操作',
width: 300,
},
];

View File

@ -1,60 +1,79 @@
<script setup lang="ts"> <script setup lang="ts">
import type { Recordable } from '@vben/types'; import type { Recordable } from '@vben/types';
import type { ColumnsType } from 'ant-design-vue/es/table';
import type { Key } from 'ant-design-vue/es/table/interface';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import { Page, useVbenModal } from '@vben/common-ui'; import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
import { Card, message, Popconfirm, Table } from 'ant-design-vue'; import { message, Modal, Popconfirm, Space } from 'ant-design-vue';
import dayjs from 'dayjs';
import { useVbenForm } from '#/adapter'; import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
import { batchGenCode, generatedList, genRemove, syncDb } from '#/api/tool/gen'; import { batchGenCode, generatedList, genRemove, syncDb } from '#/api/tool/gen';
import { downloadByData } from '#/utils/file/download'; import { downloadByData } from '#/utils/file/download';
import codePreviewModal from './code-preview-modal.vue'; import codePreviewModal from './code-preview-modal.vue';
import { querySchema } from './data'; import { columns, querySchema } from './data';
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',
},
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,
});
},
},
},
rowConfig: {
isHover: true,
keyField: 'tableId',
},
round: true,
align: 'center',
showOverflow: true,
};
const [BasicTable, tableApi] = useVbenVxeGrid({ formOptions, gridOptions });
const [CodePreviewModal, previewModalApi] = useVbenModal({ const [CodePreviewModal, previewModalApi] = useVbenModal({
connectedComponent: codePreviewModal, connectedComponent: codePreviewModal,
}); });
const columns: ColumnsType = [
{
dataIndex: 'tableName',
title: '表名称',
},
{
dataIndex: 'tableComment',
title: '表描述',
},
{
dataIndex: 'className',
title: '实体类',
},
{
dataIndex: 'createTime',
title: '创建时间',
},
{
dataIndex: 'updateTime',
title: '更新时间',
},
{
align: 'center',
dataIndex: 'action',
title: '操作',
},
];
const dataSource = ref([]);
onMounted(async () => {
const resp = await generatedList({});
dataSource.value = resp.rows;
});
function handlePreview(record: Recordable<any>) { function handlePreview(record: Recordable<any>) {
previewModalApi.setData({ tableId: record.tableId }); previewModalApi.setData({ tableId: record.tableId });
previewModalApi.open(); previewModalApi.open();
@ -67,25 +86,22 @@ function handleEdit(record: Recordable<any>) {
async function handleSync(record: Recordable<any>) { async function handleSync(record: Recordable<any>) {
await syncDb(record.tableId); await syncDb(record.tableId);
// reload await tableApi.reload();
}
const selectedRowKeys = ref<string[]>([]);
function handleSelectChange(selectedKeys: Key[]) {
selectedRowKeys.value = selectedKeys as string[];
} }
/** /**
* 批量生成代码 * 批量生成代码
*/ */
async function handleBatchGen() { async function handleBatchGen() {
if (selectedRowKeys.value.length === 0) { const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: any) => row.tableId);
if (ids.length === 0) {
message.info('请选择需要生成代码的表'); message.info('请选择需要生成代码的表');
return; return;
} }
const hideLoading = message.loading('下载中...'); const hideLoading = message.loading('下载中...');
try { try {
const params = selectedRowKeys.value.join(','); const params = ids.join(',');
const data = await batchGenCode(params); const data = await batchGenCode(params);
const timestamp = Date.now(); const timestamp = Date.now();
downloadByData(data, `批量代码生成_${timestamp}.zip`); downloadByData(data, `批量代码生成_${timestamp}.zip`);
@ -113,104 +129,73 @@ async function handleDownload(record: Recordable<any>) {
*/ */
async function handleDelete(record: Recordable<any>) { async function handleDelete(record: Recordable<any>) {
await genRemove(record.tableId); await genRemove(record.tableId);
// reload await tableApi.reload();
} }
const [QueryForm] = useVbenForm({ function handleMultiDelete() {
// const rows = tableApi.grid.getCheckboxRecords();
collapsed: false, const ids = rows.map((row: any) => row.tableId);
// Modal.confirm({
commonConfig: { title: '提示',
// okType: 'danger',
componentProps: { content: `确认删除选中的${ids.length}条记录吗?`,
class: 'w-full', onOk: async () => {
await genRemove(ids);
await tableApi.reload();
}, },
}, });
schema: querySchema(), }
//
showCollapseButton: true,
submitButtonOptions: {
text: '查询',
},
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
});
</script> </script>
<template> <template>
<Page content-class="flex flex-col gap-4"> <Page :auto-content-height="true">
<Card> <BasicTable>
<QueryForm /> <template #toolbar-actions>
</Card> <span class="pl-[7px] text-[16px]">代码生成列表</span>
<Card </template>
:body-style="{ <template #toolbar-tools>
padding: '12px', <Space>
display: 'flex', <a-button danger type="primary" @click="handleMultiDelete">
flexDirection: 'column', {{ $t('pages.common.delete') }}
gap: '12px',
}"
>
<div class="flex justify-between">
<span class="text-lg font-bold">代码生成列表</span>
<div class="flex gap-[8px]">
<a-button
:disabled="selectedRowKeys.length === 0"
danger
type="primary"
>
删除
</a-button> </a-button>
<a-button <a-button @click="handleBatchGen">
:disabled="selectedRowKeys.length === 0"
@click="handleBatchGen"
>
{{ $t('pages.common.generate') }} {{ $t('pages.common.generate') }}
</a-button> </a-button>
<a-button type="primary"> <a-button type="primary">
{{ $t('pages.common.import') }} {{ $t('pages.common.import') }}
</a-button> </a-button>
</div> </Space>
</div> </template>
<Table <template #action="{ row }">
:columns="columns" <a-button size="small" type="link" @click.stop="handlePreview(row)">
:data-source="dataSource" {{ $t('pages.common.preview') }}
:row-selection="{ selectedRowKeys, onChange: handleSelectChange }" </a-button>
row-key="tableId" <a-button size="small" type="link" @click.stop="handleEdit(row)">
size="middle" {{ $t('pages.common.edit') }}
> </a-button>
<template #bodyCell="{ record, column }"> <Popconfirm
<template v-if="column.dataIndex === 'action'"> :title="`确认同步[${row.tableName}]?`"
<a-button size="small" type="link" @click="handlePreview(record)"> placement="left"
{{ $t('pages.common.preview') }} @confirm="handleSync(row)"
</a-button> >
<a-button size="small" type="link" @click="handleEdit(record)"> <a-button size="small" type="link" @click.stop="">
{{ $t('pages.common.edit') }} {{ $t('pages.common.sync') }}
</a-button> </a-button>
<Popconfirm </Popconfirm>
:title="`确认同步[${record.tableName}]?`" <a-button size="small" type="link" @click.stop="handleDownload(row)">
placement="left" 生成代码
@confirm="handleSync(record)" </a-button>
> <Popconfirm
<a-button size="small" type="link"> :title="`确认删除[${row.tableName}]?`"
{{ $t('pages.common.sync') }} placement="left"
</a-button> @confirm="handleDelete(row)"
</Popconfirm> >
<a-button size="small" type="link" @click="handleDownload(record)"> <a-button danger size="small" type="link" @click.stop="">
生成代码 {{ $t('pages.common.delete') }}
</a-button> </a-button>
<Popconfirm </Popconfirm>
:title="`确认删除[${record.tableName}]?`" </template>
placement="left" </BasicTable>
@confirm="handleDelete(record)"
>
<a-button danger size="small" type="link">
{{ $t('pages.common.delete') }}
</a-button>
</Popconfirm>
</template>
</template>
</Table>
</Card>
<CodePreviewModal /> <CodePreviewModal />
</Page> </Page>
</template> </template>