add post 自动生成CURD generate_crud 脚本
This commit is contained in:
76
backend/system/management/commands/tpl/frontend_api.ts.tpl
Normal file
76
backend/system/management/commands/tpl/frontend_api.ts.tpl
Normal file
@@ -0,0 +1,76 @@
|
||||
import { request } from '#/utils/request';
|
||||
|
||||
export namespace ${model_name}Api {
|
||||
export interface ${model_name} {
|
||||
id: number;
|
||||
name: string;
|
||||
create_time: string;
|
||||
update_time: string;
|
||||
// 根据实际字段添加
|
||||
}
|
||||
|
||||
export interface ${model_name}ListParams {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
name?: string;
|
||||
// 根据实际字段添加
|
||||
}
|
||||
|
||||
export interface ${model_name}ListResult {
|
||||
total: number;
|
||||
items: ${model_name}[];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取${verbose_name}列表
|
||||
*/
|
||||
export function get${model_name}List(params: ${model_name}Api.${model_name}ListParams) {
|
||||
return request<${model_name}Api.${model_name}ListResult>({
|
||||
url: '/$app_name/${model_name_lower}/',
|
||||
method: 'GET',
|
||||
params,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取${verbose_name}详情
|
||||
*/
|
||||
export function get${model_name}Detail(id: number) {
|
||||
return request<${model_name}Api.${model_name}>({
|
||||
url: `/$app_name/${model_name_lower}/${id}/`,
|
||||
method: 'GET',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建${verbose_name}
|
||||
*/
|
||||
export function create${model_name}(data: Partial<${model_name}Api.${model_name}>) {
|
||||
return request<${model_name}Api.${model_name}>({
|
||||
url: '/$app_name/${model_name_lower}/',
|
||||
method: 'POST',
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新${verbose_name}
|
||||
*/
|
||||
export function update${model_name}(id: number, data: Partial<${model_name}Api.${model_name}>) {
|
||||
return request<${model_name}Api.${model_name}>({
|
||||
url: `/$app_name/${model_name_lower}/${id}/`,
|
||||
method: 'PUT',
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除${verbose_name}
|
||||
*/
|
||||
export function delete${model_name}(id: number) {
|
||||
return request({
|
||||
url: `/$app_name/${model_name_lower}/${id}/`,
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
78
backend/system/management/commands/tpl/frontend_data.ts.tpl
Normal file
78
backend/system/management/commands/tpl/frontend_data.ts.tpl
Normal file
@@ -0,0 +1,78 @@
|
||||
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
|
||||
|
||||
import type { VbenFormSchema } from '#/adapter/form';
|
||||
import type { OnActionClickFn } from '#/adapter/vxe-table';
|
||||
import type { ${app_name_camel}${model_name}Api } from '#/models/$app_name/${model_name_snake}';
|
||||
|
||||
import { z } from '#/adapter/form';
|
||||
import { $$t } from '#/locales';
|
||||
import { format_datetime } from '#/utils/date';
|
||||
|
||||
/**
|
||||
* 获取编辑表单的字段配置
|
||||
*/
|
||||
export function useSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
$form_fields
|
||||
];
|
||||
}
|
||||
|
||||
export function useColumns(
|
||||
onActionClick?: OnActionClickFn<${app_name_camel}${model_name}Api.${app_name_camel}${model_name}>,
|
||||
): VxeTableGridOptions<${app_name_camel}${model_name}Api.${app_name_camel}${model_name}>['columns'] {
|
||||
return [
|
||||
{
|
||||
align: 'left',
|
||||
field: 'id',
|
||||
},
|
||||
{
|
||||
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('${app_name}.{model_name_snake}.name'),
|
||||
onClick: onActionClick,
|
||||
},
|
||||
name: 'CellOperation',
|
||||
options: [
|
||||
'edit', // 默认的编辑按钮
|
||||
{
|
||||
code: 'view', // 新增查看详情按钮(可自定义code)
|
||||
text: '数据', // 按钮文本(国际化)
|
||||
},
|
||||
{
|
||||
code: 'delete', // 默认的删除按钮
|
||||
},
|
||||
],
|
||||
},
|
||||
field: 'operation',
|
||||
fixed: 'right',
|
||||
headerAlign: 'center',
|
||||
showOverflow: false,
|
||||
title: '操作',
|
||||
width: 200,
|
||||
},
|
||||
];
|
||||
}
|
||||
79
backend/system/management/commands/tpl/frontend_form.vue.tpl
Normal file
79
backend/system/management/commands/tpl/frontend_form.vue.tpl
Normal file
@@ -0,0 +1,79 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ${app_name_camel}${model_name}Api } from '#/models/$app_name/${model_name_snake}';
|
||||
|
||||
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 { ${app_name_camel}${model_name}Model } from '#/models/${app_name}/${model_name_snake}';
|
||||
|
||||
import { useSchema } from '../data';
|
||||
|
||||
const emit = defineEmits(['success']);
|
||||
|
||||
const formModel = new ${app_name_camel}${model_name}Model();
|
||||
|
||||
const formData = ref<${app_name_camel}${model_name}Api.${app_name_camel}${model_name}>();
|
||||
const getTitle = computed(() => {
|
||||
return formData.value?.id
|
||||
? $$t('ui.actionTitle.edit', [$$t('${app_name}.${model_name_lower}.name')])
|
||||
: $$t('ui.actionTitle.create', [$$t('${app_name}.${model_name_lower}.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<${app_name_camel}${model_name}Api.${app_name_camel}${model_name}>();
|
||||
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>
|
||||
132
backend/system/management/commands/tpl/frontend_list.vue.tpl
Normal file
132
backend/system/management/commands/tpl/frontend_list.vue.tpl
Normal file
@@ -0,0 +1,132 @@
|
||||
<script lang="ts" setup>
|
||||
import type {
|
||||
OnActionClickParams,
|
||||
VxeTableGridOptions,
|
||||
} from '#/adapter/vxe-table';
|
||||
import type { ${app_name_camel}${model_name}Api } from '#/models/$app_name/${model_name_snake}';
|
||||
|
||||
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 { ${app_name_camel}${model_name}Model } from '#/models/${app_name}/${model_name_snake}';
|
||||
|
||||
import { useColumns } from './data';
|
||||
import Form from './modules/form.vue';
|
||||
|
||||
const formModel = new ${app_name_camel}${model_name}Model();
|
||||
|
||||
const [FormModal, formModalApi] = useVbenModal({
|
||||
connectedComponent: Form,
|
||||
destroyOnClose: true,
|
||||
});
|
||||
|
||||
/**
|
||||
* 编辑${verbose_name}
|
||||
*/
|
||||
function onEdit(row: ${app_name_camel}${model_name}Api.${app_name_camel}${model_name}) {
|
||||
formModalApi.setData(row).open();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新${verbose_name}
|
||||
*/
|
||||
function onCreate() {
|
||||
formModalApi.setData(null).open();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除${verbose_name}
|
||||
*/
|
||||
function onDelete(row: ${app_name_camel}${model_name}Api.${app_name_camel}${model_name}) {
|
||||
const hideLoading = message.loading({
|
||||
content: '删除${verbose_name}',
|
||||
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<${app_name_camel}${model_name}Api.${app_name_camel}${model_name}>) {
|
||||
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="${verbose_name}">
|
||||
<template #toolbar-tools>
|
||||
<Button type="primary" @click="onCreate">
|
||||
<Plus class="size-5" />
|
||||
{{ $$t('ui.actionTitle.create', [$$t('${app_name}.${model_name_snake}.name')]) }}
|
||||
</Button>
|
||||
</template>
|
||||
</Grid>
|
||||
</Page>
|
||||
</template>
|
||||
13
backend/system/management/commands/tpl/frontend_model.ts.tpl
Normal file
13
backend/system/management/commands/tpl/frontend_model.ts.tpl
Normal file
@@ -0,0 +1,13 @@
|
||||
import { BaseModel } from '#/models/base';
|
||||
|
||||
export namespace ${app_name_camel}${model_name}Api {
|
||||
export interface ${app_name_camel}${model_name} {
|
||||
$interface_fields
|
||||
}
|
||||
}
|
||||
|
||||
export class ${app_name_camel}${model_name}Model extends BaseModel<${app_name_camel}${model_name}Api.${app_name_camel}${model_name}> {
|
||||
constructor() {
|
||||
super('/$app_name/${model_name_lower}/');
|
||||
}
|
||||
}
|
||||
27
backend/system/management/commands/tpl/viewset.py.tpl
Normal file
27
backend/system/management/commands/tpl/viewset.py.tpl
Normal file
@@ -0,0 +1,27 @@
|
||||
from $app_name.models import $model_name
|
||||
from utils.serializers import CustomModelSerializer
|
||||
from utils.custom_model_viewSet import CustomModelViewSet
|
||||
|
||||
class ${model_name}Serializer(CustomModelSerializer):
|
||||
"""
|
||||
$verbose_name 序列化器
|
||||
"""
|
||||
class Meta:
|
||||
model = $model_name
|
||||
fields = '__all__'
|
||||
read_only_fields = ['id', 'create_time', 'update_time']
|
||||
|
||||
|
||||
class ${model_name}ViewSet(CustomModelViewSet):
|
||||
"""
|
||||
$verbose_name 视图集
|
||||
"""
|
||||
queryset = $model_name.objects.filter(is_deleted=False).order_by('-id')
|
||||
serializer_class = ${model_name}Serializer
|
||||
filterset_fields = [$filterset_fields]
|
||||
search_fields = ['name'] # 根据实际字段调整
|
||||
ordering_fields = ['create_time', 'id']
|
||||
ordering = ['-create_time']
|
||||
|
||||
# 移入urls中
|
||||
# router.register(r'${model_name_snake}', views.${model_name}ViewSet)
|
||||
Reference in New Issue
Block a user