add post 自动生成CURD generate_crud 脚本

This commit is contained in:
xie7654
2025-07-01 11:58:13 +08:00
parent 05554eb821
commit 3bc50e79e8
24 changed files with 1663 additions and 23 deletions

View File

@@ -6,7 +6,7 @@ import type { SystemDictTypeApi } from '#/api/system/dict_type';
import { z } from '#/adapter/form';
import { $t } from '#/locales';
import {format_datetime} from "#/utils/date";
import { format_datetime } from '#/utils/date';
/**
* 获取编辑表单的字段配置。如果没有使用多语言可以直接export一个数组常量

View File

@@ -0,0 +1,124 @@
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SystemPostApi } from '#/models/system/post';
import { z } from '#/adapter/form';
import { $t } from '#/locales';
import { format_datetime } from '#/utils/date';
/**
* 获取编辑表单的字段配置
*/
export function useSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'name',
label: '名称',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['名称']))
.max(100, $t('ui.formRules.maxLength', ['名称', 100])),
},
{
component: 'Input',
fieldName: 'code',
label: '编码',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['编码']))
.max(100, $t('ui.formRules.maxLength', ['编码', 100])),
},
{
component: 'InputNumber',
fieldName: 'sort',
label: '排序',
rules: z.number(),
},
{
component: 'RadioGroup',
componentProps: {
buttonStyle: 'solid',
options: [
{ label: '开启', value: true },
{ label: '关闭', value: false },
],
optionType: 'button',
},
defaultValue: true,
fieldName: 'status',
label: '是否启用',
},
{
component: 'Input',
fieldName: 'remark',
label: '备注',
},
];
}
export function useColumns(
onActionClick?: OnActionClickFn<SystemPostApi.SystemPost>,
): VxeTableGridOptions<SystemPostApi.SystemPost>['columns'] {
return [
{
field: 'id',
title: 'id',
},
{
field: 'name',
title: '岗位名称',
},
{
field: 'code',
title: '岗位编码',
},
{
cellRender: {
name: 'CellTag',
options: [
{ label: $t('common.enabled'), value: true },
{ label: $t('common.disabled'), value: false },
],
},
field: 'status',
title: '状态',
width: 100,
},
{
field: 'remark',
title: '备注',
},
{
field: 'create_time',
title: '创建时间',
width: 180,
formatter: ({ cellValue }) => format_datetime(cellValue),
},
{
align: 'right',
cellRender: {
attrs: {
nameField: 'name',
nameTitle: $t('system.{model_name_snake}.name'),
onClick: onActionClick,
},
name: 'CellOperation',
options: [
'edit', // 默认的编辑按钮
{
code: 'delete', // 默认的删除按钮
},
],
},
field: 'operation',
fixed: 'right',
headerAlign: 'center',
showOverflow: false,
title: '操作',
width: 200,
},
];
}

View File

@@ -0,0 +1,132 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SystemPostApi } from '#/models/system/post';
import { Page, useVbenModal } from '@vben/common-ui';
import { Plus } from '@vben/icons';
import { Button, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { $t } from '#/locales';
import { SystemPostModel } from '#/models/system/post';
import { useColumns } from './data';
import Form from './modules/form.vue';
const formModel = new SystemPostModel();
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
/**
* 编辑岗位信息表
*/
function onEdit(row: SystemPostApi.SystemPost) {
formModalApi.setData(row).open();
}
/**
* 创建新岗位信息表
*/
function onCreate() {
formModalApi.setData(null).open();
}
/**
* 删除岗位信息表
*/
function onDelete(row: SystemPostApi.SystemPost) {
const hideLoading = message.loading({
content: '删除岗位信息表',
duration: 0,
key: 'action_process_msg',
});
formModel
.delete(row.id)
.then(() => {
message.success({
content: '删除成功',
key: 'action_process_msg',
});
refreshGrid();
})
.catch(() => {
hideLoading();
});
}
/**
* 表格操作按钮的回调函数
*/
function onActionClick({
code,
row,
}: OnActionClickParams<SystemPostApi.SystemPost>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({
gridEvents: {},
gridOptions: {
columns: useColumns(onActionClick),
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: true,
},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await formModel.list({
page: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
toolbarConfig: {
custom: true,
export: false,
refresh: { code: 'query' },
zoom: true,
},
} as VxeTableGridOptions,
});
/**
* 刷新表格
*/
function refreshGrid() {
gridApi.query();
}
</script>
<template>
<Page auto-content-height>
<FormModal @success="refreshGrid" />
<Grid table-title="岗位信息表">
<template #toolbar-tools>
<Button type="primary" @click="onCreate">
<Plus class="size-5" />
{{ $t('ui.actionTitle.create', [$t('system.post.name')]) }}
</Button>
</template>
</Grid>
</Page>
</template>

View File

@@ -0,0 +1,79 @@
<script lang="ts" setup>
import type { SystemPostApi } from '#/models/system/post';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { Button } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { $t } from '#/locales';
import { SystemPostModel } from '#/models/system/post';
import { useSchema } from '../data';
const emit = defineEmits(['success']);
const formModel = new SystemPostModel();
const formData = ref<SystemPostApi.SystemPost>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', [$t('system.post.name')])
: $t('ui.actionTitle.create', [$t('system.post.name')]);
});
const [Form, formApi] = useVbenForm({
layout: 'horizontal',
schema: useSchema(),
showDefaultActions: false,
});
function resetForm() {
formApi.resetForm();
formApi.setValues(formData.value || {});
}
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (valid) {
modalApi.lock();
const data = await formApi.getValues();
try {
await (formData.value?.id
? formModel.update(formData.value.id, data)
: formModel.create(data));
await modalApi.close();
emit('success');
} finally {
modalApi.lock(false);
}
}
},
onOpenChange(isOpen) {
if (isOpen) {
const data = modalApi.getData<SystemPostApi.SystemPost>();
if (data) {
formData.value = data;
formApi.setValues(formData.value);
}
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form />
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
</div>
</template>
</Modal>
</template>
<style lang="css" scoped></style>