Files
FastapiAdmin/backend/app/api/v1/module_system/log/controller.py
T
zhangtao 837d8a5aa7 feat: 优化代码生成器功能并修复多个问题
- 前端表格列添加溢出提示工具
- 后端登录IP归属地查询与设置统一处理
- 分页服务明确仅用于非SQL数据源场景
- 代码生成器添加字段筛选和帮助面板
- 修复菜单高亮和布局切换问题
- 更新文档中的BUG状态和解决方案
- 优化AI聊天界面样式和交互
- 添加数据库分页支持到多个模块
- 代码生成器表单校验和流程优化
- 修复PostgreSQL日期类型问题
2026-03-30 00:37:16 +08:00

137 lines
4.3 KiB
Python

from typing import Annotated
from fastapi import APIRouter, Body, Depends, Path
from fastapi.responses import JSONResponse, StreamingResponse
from app.api.v1.module_system.auth.schema import AuthSchema
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
from app.core.base_params import PaginationQueryParam
from app.core.dependencies import AuthPermission
from app.core.logger import log
from app.core.router_class import OperationLogRoute
from app.utils.common_util import bytes2file_response
from .schema import OperationLogOutSchema, OperationLogQueryParam
from .service import OperationLogService
LogRouter = APIRouter(route_class=OperationLogRoute, prefix="/log", tags=["日志管理"])
@LogRouter.get(
"/list",
summary="查询日志",
description="查询日志",
response_model=ResponseSchema[list[OperationLogOutSchema]],
)
async def get_obj_list_controller(
page: Annotated[PaginationQueryParam, Depends()],
search: Annotated[OperationLogQueryParam, Depends()],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:log:query"]))],
) -> JSONResponse:
"""
查询日志
参数:
- page (PaginationQueryParam): 分页查询参数模型
- search (OperationLogQueryParam): 日志查询参数模型
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含分页日志详情的 JSON 响应模型
"""
order_by = [{"created_time": "desc"}]
if page.order_by:
order_by = page.order_by
result_dict = await OperationLogService.get_log_page_service(
auth=auth,
page_no=page.page_no,
page_size=page.page_size,
search=search,
order_by=order_by,
)
log.info("查询日志成功")
return SuccessResponse(data=result_dict, msg="查询日志成功")
@LogRouter.get(
"/detail/{id}",
summary="日志详情",
description="日志详情",
response_model=ResponseSchema[OperationLogOutSchema],
)
async def get_obj_detail_controller(
id: Annotated[int, Path(description="操作日志ID")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:log:detail"]))],
) -> JSONResponse:
"""
获取日志详情
参数:
- id (int): 操作日志ID
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含日志详情的 JSON 响应模型
"""
result_dict = await OperationLogService.get_log_detail_service(id=id, auth=auth)
log.info(f"查询日志成功 {id}")
return SuccessResponse(data=result_dict, msg="获取日志详情成功")
@LogRouter.delete(
"/delete",
summary="删除日志",
description="删除日志",
response_model=ResponseSchema[None],
)
async def delete_obj_log_controller(
ids: Annotated[list[int], Body(description="ID列表")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:log:delete"]))],
) -> JSONResponse:
"""
删除日志
参数:
- ids (list[int]): 日志 ID 列表
- auth (AuthSchema): 认证信息模型
返回:
- JSONResponse: 包含删除结果的 JSON 响应模型
"""
await OperationLogService.delete_log_service(ids=ids, auth=auth)
log.info(f"删除日志成功 {ids}")
return SuccessResponse(msg="删除日志成功")
@LogRouter.post(
"/export",
summary="导出日志",
description="导出日志",
response_model=ResponseSchema[None],
)
async def export_obj_list_controller(
search: Annotated[OperationLogQueryParam, Depends()],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:log:export"]))],
) -> StreamingResponse:
"""
导出日志
参数:
- search (OperationLogQueryParam): 日志查询参数模型
- auth (AuthSchema): 认证信息模型
返回:
- StreamingResponse: 包含导出日志的流式响应模型
"""
operation_log_list = await OperationLogService.get_log_list_service(search=search, auth=auth)
operation_log_export_result = await OperationLogService.export_log_list_service(
operation_log_list=operation_log_list
)
log.info("导出日志成功")
return StreamResponse(
data=bytes2file_response(operation_log_export_result),
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={"Content-Disposition": "attachment; filename=log.xlsx"},
)