From 334b59a8ab033c5bdd5fb2110572f2ba9b7127ac Mon Sep 17 00:00:00 2001 From: zhangtao <9480807882@qq.com> Date: Thu, 27 Nov 2025 00:00:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(base=5Fcrud):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E6=9F=A5=E8=AF=A2=E8=BF=94=E5=9B=9E=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将PageResultSchema的实例化改为直接返回字典结构,简化代码并提高性能 ``` ```msg fix(module_generator): 修正模板字段命名问题 将模板中的python_field统一改为column_name,保持字段命名一致性 --- .../gencode/templates/ts/api.ts.j2 | 12 ++++++------ backend/app/core/base_crud.py | 16 +++++++--------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/backend/app/api/v1/module_generator/gencode/templates/ts/api.ts.j2 b/backend/app/api/v1/module_generator/gencode/templates/ts/api.ts.j2 index 54f3cffa..06ce9411 100644 --- a/backend/app/api/v1/module_generator/gencode/templates/ts/api.ts.j2 +++ b/backend/app/api/v1/module_generator/gencode/templates/ts/api.ts.j2 @@ -107,12 +107,12 @@ export interface {{ class_name }}PageQuery extends PageQuery { end_time?: string; } -// 列表查询结果 +// 列表展示项 export interface {{ class_name }}Table { index?: number; {% for column in columns %} - {{ column.python_field }}?: {{ - 'boolean' if ('status' in (column.python_field|lower)) or (column.html_type == 'radio') + {{ column.column_name }}?: {{ + 'boolean' if ('status' in (column.column_name|lower)) or (column.html_type == 'radio') else 'number' if column.is_pk == 1 else 'string' }}; @@ -123,9 +123,9 @@ export interface {{ class_name }}Table { // 新增/修改/详情表单参数 export interface {{ class_name }}Form { {% for column in columns %} - {% if (column.is_insert == 1 or column.is_edit == 1) and column.python_field not in ['creatorId', 'createdAt', 'updatedAt'] %} - {{ column.python_field }}?: {{ - 'boolean' if ('status' in (column.python_field|lower)) or (column.html_type == 'radio') + {% if (column.is_insert == 1 or column.is_edit == 1) and column.column_name not in ['creator_id', 'created_at', 'updated_at'] %} + {{ column.column_name }}?: {{ + 'boolean' if ('status' in (column.column_name|lower)) or (column.html_type == 'radio') else 'number' if column.is_pk == 1 else 'string' }}; diff --git a/backend/app/core/base_crud.py b/backend/app/core/base_crud.py index 9969ca01..313474f7 100644 --- a/backend/app/core/base_crud.py +++ b/backend/app/core/base_crud.py @@ -179,15 +179,13 @@ class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]): result: Result = await self.db.execute(sql.offset(offset).limit(limit)) objs = result.scalars().all() - data = PageResultSchema( - items=[out_schema.model_validate(obj).model_dump() for obj in objs], - total=total, - page_no=offset // limit + 1 if limit else 1, - page_size=limit, - has_next=offset + limit < total, - ).model_dump() - - return data + return { + "page_no": offset // limit + 1 if limit else 1, + "page_size": limit if limit else 10, + "total": total, + "has_next": offset + limit < total, + "items": [out_schema.model_validate(obj).model_dump() for obj in objs] + } except Exception as e: raise CustomException(msg=f"分页查询失败: {str(e)}")