添加用户管理
This commit is contained in:
@@ -28,7 +28,7 @@ def camel_to_snake(name):
|
||||
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
|
||||
|
||||
def ensure_view_dirs(app_name, model_name_snake):
|
||||
base_dir = f'web/apps/web-antd/src/views/{app_name.lower()}'
|
||||
base_dir = f'../web/apps/web-antd/src/views/{app_name.lower()}'
|
||||
model_dir = os.path.join(base_dir, model_name_snake)
|
||||
if not os.path.exists(base_dir):
|
||||
os.makedirs(model_dir, exist_ok=True)
|
||||
@@ -77,7 +77,7 @@ class Command(BaseCommand):
|
||||
raise CommandError(f'模型 {app_name}.{model_name} 不存在')
|
||||
|
||||
# 生成后端代码
|
||||
self.generate_backend_code(app_name, model_name, model, model_name_snake)
|
||||
# self.generate_backend_code(app_name, model_name, model, model_name_snake)
|
||||
|
||||
if generate_frontend:
|
||||
# 生成前端代码
|
||||
@@ -148,15 +148,29 @@ class Command(BaseCommand):
|
||||
self.stdout.write(f'生成前端列表页面: {list_path}')
|
||||
|
||||
def generate_frontend_data(self, app_name, model_name, model, model_name_snake):
|
||||
form_fields = []
|
||||
CORE_FIELDS = ['create_time', 'update_time', 'creator', 'modifier', 'is_deleted', 'remark']
|
||||
business_fields = []
|
||||
core_fields = []
|
||||
for field in model._meta.fields:
|
||||
if field.name in ['id', 'create_time', 'update_time', 'creator', 'modifier']:
|
||||
continue
|
||||
if field.name in CORE_FIELDS:
|
||||
core_fields.append(field)
|
||||
else:
|
||||
business_fields.append(field)
|
||||
# 生成 useSchema
|
||||
form_fields = []
|
||||
for field in business_fields + core_fields:
|
||||
if field.name in ['id', 'create_time', 'update_time', 'creator', 'modifier', 'is_deleted']:
|
||||
continue # 这些一般不在表单里
|
||||
field_config = self.generate_form_field(field)
|
||||
if field_config:
|
||||
form_fields.append(field_config)
|
||||
# 生成 useColumns
|
||||
columns = []
|
||||
for field in business_fields + core_fields:
|
||||
columns.append(f" {{\n field: '{field.name}',\n title: '{getattr(field, 'verbose_name', field.name)}',\n }},")
|
||||
context = get_context(app_name, model_name, model, model_name_snake)
|
||||
context['form_fields'] = '\n'.join(form_fields)
|
||||
context['columns'] = '\n'.join(columns)
|
||||
data_path = f'../web/apps/web-antd/src/views/{app_name.lower()}/{model_name_snake}/data.ts'
|
||||
data_code = render_tpl('frontend_data.ts.tpl', context)
|
||||
with open(data_path, 'w', encoding='utf-8') as f:
|
||||
@@ -196,55 +210,14 @@ class Command(BaseCommand):
|
||||
"""生成表单字段配置"""
|
||||
field_name = field.name
|
||||
field_label = getattr(field, 'verbose_name', field_name)
|
||||
|
||||
col_props = ",\n colProps: { span: 12 }"
|
||||
if isinstance(field, models.CharField):
|
||||
return f''' {{
|
||||
component: 'Input',
|
||||
fieldName: '{field_name}',
|
||||
label: '{field_label}',
|
||||
rules: z
|
||||
.string()
|
||||
.min(1, $t('ui.formRules.required', ['{field_label}']))
|
||||
.max(100, $t('ui.formRules.maxLength', ['{field_label}', 100])),
|
||||
}},'''
|
||||
return f''' {{\n component: 'Input',\n fieldName: '{field_name}',\n label: '{field_label}',{col_props}\n rules: z\n .string()\n .min(1, $t('ui.formRules.required', ['{field_label}']))\n .max(100, $t('ui.formRules.maxLength', ['{field_label}', 100])),\n }},'''
|
||||
elif isinstance(field, models.TextField):
|
||||
return f''' {{
|
||||
component: 'Input',
|
||||
componentProps: {{
|
||||
rows: 3,
|
||||
showCount: true,
|
||||
}},
|
||||
fieldName: '{field_name}',
|
||||
label: '{field_label}',
|
||||
rules: z
|
||||
.string()
|
||||
.max(500, $t('ui.formRules.maxLength', ['{field_label}', 500]))
|
||||
.optional(),
|
||||
}},'''
|
||||
return f''' {{\n component: 'Input',\n componentProps: {{\n rows: 3,\n showCount: true,\n }},\n fieldName: '{field_name}',\n label: '{field_label}',{col_props}\n rules: z\n .string()\n .max(500, $t('ui.formRules.maxLength', ['{field_label}', 500]))\n .optional(),\n }},'''
|
||||
elif isinstance(field, models.IntegerField):
|
||||
return f''' {{
|
||||
component: 'InputNumber',
|
||||
fieldName: '{field_name}',
|
||||
label: '{field_label}',
|
||||
}},'''
|
||||
return f''' {{\n component: 'InputNumber',\n fieldName: '{field_name}',\n label: '{field_label}',{col_props}\n }},'''
|
||||
elif isinstance(field, models.BooleanField):
|
||||
return f''' {{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {{
|
||||
buttonStyle: 'solid',
|
||||
options: [
|
||||
{{ label: '开启', value: true }},
|
||||
{{ label: '关闭', value: false }},
|
||||
],
|
||||
optionType: 'button',
|
||||
}},
|
||||
defaultValue: true,
|
||||
fieldName: '{field_name}',
|
||||
label: '{field_label}',
|
||||
}},'''
|
||||
return f''' {{\n component: 'RadioGroup',\n componentProps: {{\n buttonStyle: 'solid',\n options: [\n {{ label: '开启', value: 1 }},\n {{ label: '关闭', value: 0 }},\n ],\n optionType: 'button',\n }},{col_props}\n defaultValue: 1,\n fieldName: '{field_name}',\n label: '{field_label}',\n }},'''
|
||||
else:
|
||||
return f''' {{
|
||||
component: 'Input',
|
||||
fieldName: '{field_name}',
|
||||
label: '{field_label}',
|
||||
}},'''
|
||||
return f''' {{\n component: 'Input',\n fieldName: '{field_name}',\n label: '{field_label}',{col_props}\n }},'''
|
||||
@@ -2,7 +2,7 @@ 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 type { ${app_name_camel}${model_name}Api } from '#/models/${app_name}/${model_name_snake}';
|
||||
|
||||
import { z } from '#/adapter/form';
|
||||
import { $$t } from '#/locales';
|
||||
@@ -13,66 +13,35 @@ import { format_datetime } from '#/utils/date';
|
||||
*/
|
||||
export function useSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
$form_fields
|
||||
${form_fields}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表格列配置
|
||||
* @description 使用函数的形式返回列数据而不是直接export一个Array常量,是为了响应语言切换时重新翻译表头
|
||||
* @param onActionClick 表格操作按钮点击事件
|
||||
*/
|
||||
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 [
|
||||
${columns}
|
||||
{
|
||||
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',
|
||||
align: 'center',
|
||||
cellRender: {
|
||||
attrs: {
|
||||
nameField: 'name',
|
||||
nameTitle: $$t('${app_name}.{model_name_snake}.name'),
|
||||
nameTitle: $t('${app_name}.${model_name_snake}.name'),
|
||||
onClick: onActionClick,
|
||||
},
|
||||
name: 'CellOperation',
|
||||
options: [
|
||||
'edit', // 默认的编辑按钮
|
||||
{
|
||||
code: 'view', // 新增查看详情按钮(可自定义code)
|
||||
text: '数据', // 按钮文本(国际化)
|
||||
},
|
||||
{
|
||||
code: 'delete', // 默认的删除按钮
|
||||
},
|
||||
],
|
||||
options: ['edit', 'delete'],
|
||||
},
|
||||
field: 'operation',
|
||||
field: 'action',
|
||||
fixed: 'right',
|
||||
headerAlign: 'center',
|
||||
showOverflow: false,
|
||||
title: '操作',
|
||||
width: 200,
|
||||
width: 120,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ function refreshGrid() {
|
||||
<template #toolbar-tools>
|
||||
<Button type="primary" @click="onCreate">
|
||||
<Plus class="size-5" />
|
||||
{{ $$t('ui.actionTitle.create', [$$t('${app_name}.${model_name_snake}.name')]) }}
|
||||
{{ $$t('ui.actionTitle.create', [$$t('${app_name}.${model_name_snake}.name')]) }}
|
||||
</Button>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
Reference in New Issue
Block a user