controller.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse, StreamingResponse
  4. from app.common.response import StreamResponse, SuccessResponse
  5. from app.common.request import PaginationService
  6. from app.core.router_class import OperationLogRoute
  7. from app.utils.common_util import bytes2file_response
  8. from app.core.base_params import PaginationQueryParam
  9. from app.core.dependencies import AuthPermission
  10. from app.core.base_schema import BatchSetAvailable
  11. from app.core.logger import log
  12. from ..auth.schema import AuthSchema
  13. from .service import RoleService
  14. from .schema import (
  15. RoleCreateSchema,
  16. RoleUpdateSchema,
  17. RolePermissionSettingSchema,
  18. RoleQueryParam
  19. )
  20. RoleRouter = APIRouter(route_class=OperationLogRoute, prefix="/role", tags=["角色管理"])
  21. @RoleRouter.get("/list", summary="查询角色", description="查询角色")
  22. async def get_obj_list_controller(
  23. page: PaginationQueryParam = Depends(),
  24. search: RoleQueryParam = Depends(),
  25. auth: AuthSchema = Depends(AuthPermission(["module_system:role:query"])),
  26. ) -> JSONResponse:
  27. """
  28. 查询角色
  29. 参数:
  30. - page (PaginationQueryParam): 分页查询参数模型
  31. - search (RoleQueryParam): 查询参数模型
  32. - auth (AuthSchema): 认证信息模型
  33. 返回:
  34. - JSONResponse: 分页查询结果JSON响应
  35. """
  36. order_by = [{"order": "asc"}]
  37. if page.order_by:
  38. order_by = page.order_by
  39. result_dict_list = await RoleService.get_role_list_service(search=search, auth=auth, order_by=order_by)
  40. result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
  41. log.info(f"查询角色成功")
  42. return SuccessResponse(data=result_dict, msg="查询角色成功")
  43. @RoleRouter.get("/detail/{id}", summary="查询角色详情", description="查询角色详情")
  44. async def get_obj_detail_controller(
  45. id: int = Path(..., description="角色ID"),
  46. auth: AuthSchema = Depends(AuthPermission(["module_system:role:query"])),
  47. ) -> JSONResponse:
  48. """
  49. 查询角色详情
  50. 参数:
  51. - id (int): 角色ID
  52. - auth (AuthSchema): 认证信息模型
  53. 返回:
  54. - JSONResponse: 角色详情JSON响应
  55. """
  56. result_dict = await RoleService.get_role_detail_service(id=id, auth=auth)
  57. log.info(f"获取角色详情成功 {id}")
  58. return SuccessResponse(data=result_dict, msg="获取角色详情成功")
  59. @RoleRouter.post("/create", summary="创建角色", description="创建角色")
  60. async def create_obj_controller(
  61. data: RoleCreateSchema,
  62. auth: AuthSchema = Depends(AuthPermission(["module_system:role:create"])),
  63. ) -> JSONResponse:
  64. """
  65. 创建角色
  66. 参数:
  67. - data (RoleCreateSchema): 创建角色模型
  68. - auth (AuthSchema): 认证信息模型
  69. 返回:
  70. - JSONResponse: 创建角色JSON响应
  71. """
  72. result_dict = await RoleService.create_role_service(data=data, auth=auth)
  73. log.info(f"创建角色成功: {result_dict}")
  74. return SuccessResponse(data=result_dict, msg="创建角色成功")
  75. @RoleRouter.put("/update/{id}", summary="修改角色", description="修改角色")
  76. async def update_obj_controller(
  77. data: RoleUpdateSchema,
  78. id: int = Path(..., description="角色ID"),
  79. auth: AuthSchema = Depends(AuthPermission(["module_system:role:update"])),
  80. ) -> JSONResponse:
  81. """
  82. 修改角色
  83. 参数:
  84. - data (RoleUpdateSchema): 修改角色模型
  85. - id (int): 角色ID
  86. - auth (AuthSchema): 认证信息模型
  87. 返回:
  88. - JSONResponse: 修改角色JSON响应
  89. """
  90. result_dict = await RoleService.update_role_service(id=id, data=data, auth=auth)
  91. log.info(f"修改角色成功: {result_dict}")
  92. return SuccessResponse(data=result_dict, msg="修改角色成功")
  93. @RoleRouter.delete("/delete", summary="删除角色", description="删除角色")
  94. async def delete_obj_controller(
  95. ids: list[int] = Body(..., description="ID列表"),
  96. auth: AuthSchema = Depends(AuthPermission(["module_system:role:delete"])),
  97. ) -> JSONResponse:
  98. """
  99. 删除角色
  100. 参数:
  101. - ids (list[int]): ID列表
  102. - auth (AuthSchema): 认证信息模型
  103. 返回:
  104. - JSONResponse: 删除角色JSON响应
  105. """
  106. await RoleService.delete_role_service(ids=ids, auth=auth)
  107. log.info(f"删除角色成功: {ids}")
  108. return SuccessResponse(msg="删除角色成功")
  109. @RoleRouter.patch("/available/setting", summary="批量修改角色状态", description="批量修改角色状态")
  110. async def batch_set_available_obj_controller(
  111. data: BatchSetAvailable,
  112. auth: AuthSchema = Depends(AuthPermission(["module_system:role:patch"])),
  113. ) -> JSONResponse:
  114. """
  115. 批量修改角色状态
  116. 参数:
  117. - data (BatchSetAvailable): 批量修改角色状态模型
  118. - auth (AuthSchema): 认证信息模型
  119. 返回:
  120. - JSONResponse: 批量修改角色状态JSON响应
  121. """
  122. await RoleService.set_role_available_service(data=data, auth=auth)
  123. log.info(f"批量修改角色状态成功: {data.ids}")
  124. return SuccessResponse(msg="批量修改角色状态成功")
  125. @RoleRouter.patch("/permission/setting", summary="角色授权", description="角色授权")
  126. async def set_role_permission_controller(
  127. data: RolePermissionSettingSchema,
  128. auth: AuthSchema = Depends(AuthPermission(["module_system:role:permission"])),
  129. ) -> JSONResponse:
  130. """
  131. 角色授权
  132. 参数:
  133. - data (RolePermissionSettingSchema): 角色授权模型
  134. - auth (AuthSchema): 认证信息模型
  135. 返回:
  136. - JSONResponse: 角色授权JSON响应
  137. """
  138. await RoleService.set_role_permission_service(data=data, auth=auth)
  139. log.info(f"设置角色权限成功: {data}")
  140. return SuccessResponse(msg="授权角色成功")
  141. @RoleRouter.post('/export', summary="导出角色", description="导出角色")
  142. async def export_obj_list_controller(
  143. search: RoleQueryParam = Depends(),
  144. auth: AuthSchema = Depends(AuthPermission(["module_system:role:export"])),
  145. ) -> StreamingResponse:
  146. """
  147. 导出角色
  148. 参数:
  149. - search (RoleQueryParam): 查询参数模型
  150. - auth (AuthSchema): 认证信息模型
  151. 返回:
  152. - StreamingResponse: 导出角色流响应
  153. """
  154. role_query_result = await RoleService.get_role_list_service(search=search, auth=auth)
  155. role_export_result = await RoleService.export_role_list_service(role_list=role_query_result)
  156. log.info('导出角色成功')
  157. return StreamResponse(
  158. data=bytes2file_response(role_export_result),
  159. media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  160. headers = {
  161. 'Content-Disposition': 'attachment; filename=role.xlsx'
  162. }
  163. )