controller.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse, StreamingResponse
  4. from app.common.request import PaginationService
  5. from app.common.response import SuccessResponse, StreamResponse
  6. from app.core.router_class import OperationLogRoute
  7. from app.utils.common_util import bytes2file_response
  8. from app.core.dependencies import AuthPermission
  9. from app.core.base_params import PaginationQueryParam
  10. from app.core.logger import log
  11. from ..auth.schema import AuthSchema
  12. from .schema import OperationLogQueryParam
  13. from .service import OperationLogService
  14. LogRouter = APIRouter(route_class=OperationLogRoute, prefix="/log", tags=["日志管理"])
  15. @LogRouter.get("/list", summary="查询日志", description="查询日志")
  16. async def get_obj_list_controller(
  17. page: PaginationQueryParam = Depends(),
  18. search: OperationLogQueryParam = Depends(),
  19. auth: AuthSchema = Depends(AuthPermission(["module_system:log:query"]))
  20. ) -> JSONResponse:
  21. """
  22. 查询日志
  23. 参数:
  24. - page (PaginationQueryParam): 分页查询参数模型
  25. - search (OperationLogQueryParam): 日志查询参数模型
  26. - auth (AuthSchema): 认证信息模型
  27. 返回:
  28. - JSONResponse: 包含分页日志详情的 JSON 响应模型
  29. """
  30. order_by = [{"created_time": "desc"}]
  31. if page.order_by:
  32. order_by = page.order_by
  33. result_dict_list = await OperationLogService.get_log_list_service(search=search, auth=auth, order_by=order_by)
  34. result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
  35. log.info(f"查询日志成功")
  36. return SuccessResponse(data=result_dict, msg="查询日志成功")
  37. @LogRouter.get("/detail/{id}", summary="日志详情", description="日志详情")
  38. async def get_obj_detail_controller(
  39. id: int = Path(..., description="操作日志ID"),
  40. auth: AuthSchema = Depends(AuthPermission(["module_system:log:query"]))
  41. ) -> JSONResponse:
  42. """
  43. 获取日志详情
  44. 参数:
  45. - id (int): 操作日志ID
  46. - auth (AuthSchema): 认证信息模型
  47. 返回:
  48. - JSONResponse: 包含日志详情的 JSON 响应模型
  49. """
  50. result_dict = await OperationLogService.get_log_detail_service(id=id, auth=auth)
  51. log.info(f"查询日志成功 {id}")
  52. return SuccessResponse(data=result_dict, msg="获取日志详情成功")
  53. @LogRouter.delete("/delete", summary="删除日志", description="删除日志")
  54. async def delete_obj_log_controller(
  55. ids: list[int] = Body(..., description="ID列表"),
  56. auth: AuthSchema = Depends(AuthPermission(["module_system:log:delete"]))
  57. ) -> JSONResponse:
  58. """
  59. 删除日志
  60. 参数:
  61. - ids (list[int]): 日志 ID 列表
  62. - auth (AuthSchema): 认证信息模型
  63. 返回:
  64. - JSONResponse: 包含删除结果的 JSON 响应模型
  65. """
  66. await OperationLogService.delete_log_service(ids=ids, auth=auth)
  67. log.info(f"删除日志成功 {ids}")
  68. return SuccessResponse(msg="删除日志成功")
  69. @LogRouter.post("/export", summary="导出日志", description="导出日志")
  70. async def export_obj_list_controller(
  71. search: OperationLogQueryParam = Depends(),
  72. auth: AuthSchema = Depends(AuthPermission(["module_system:log:export"]))
  73. ) -> StreamingResponse:
  74. """
  75. 导出日志
  76. 参数:
  77. - search (OperationLogQueryParam): 日志查询参数模型
  78. - auth (AuthSchema): 认证信息模型
  79. 返回:
  80. - StreamingResponse: 包含导出日志的流式响应模型
  81. """
  82. operation_log_list = await OperationLogService.get_log_list_service(search=search, auth=auth)
  83. operation_log_export_result = await OperationLogService.export_log_list_service(operation_log_list=operation_log_list)
  84. log.info('导出日志成功')
  85. return StreamResponse(
  86. data=bytes2file_response(operation_log_export_result),
  87. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  88. headers = {
  89. 'Content-Disposition': 'attachment; filename=log.xlsx'
  90. }
  91. )