controller.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. from typing import Annotated
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.common.response import ResponseSchema, SuccessResponse
  6. from app.core.base_params import PaginationQueryParam
  7. from app.core.base_schema import BatchSetAvailable
  8. from app.core.dependencies import AuthPermission
  9. from app.core.logger import log
  10. from app.core.router_class import OperationLogRoute
  11. from .schema import (
  12. ApplicationCreateSchema,
  13. ApplicationOutSchema,
  14. ApplicationQueryParam,
  15. ApplicationUpdateSchema,
  16. PluginInfoOut,
  17. )
  18. from .service import ApplicationService
  19. PortalRouter = APIRouter(route_class=OperationLogRoute, prefix="/portal", tags=["应用管理"])
  20. @PortalRouter.get(
  21. "/detail/{id}",
  22. summary="获取应用详情",
  23. description="获取应用详情",
  24. response_model=ResponseSchema[ApplicationOutSchema],
  25. )
  26. async def get_obj_detail_controller(
  27. id: Annotated[int, Path(description="应用ID")],
  28. auth: Annotated[
  29. AuthSchema,
  30. Depends(AuthPermission(["module_application:portal:detail"])),
  31. ],
  32. ) -> JSONResponse:
  33. """
  34. 获取应用详情
  35. 参数:
  36. - id (int): 应用ID
  37. - auth (AuthSchema): 认证信息模型
  38. 返回:
  39. - JSONResponse: 包含应用详情的JSON响应
  40. """
  41. result_dict = await ApplicationService.detail_service(id=id, auth=auth)
  42. log.info(f"获取应用详情成功 {id}")
  43. return SuccessResponse(data=result_dict, msg="获取应用详情成功")
  44. @PortalRouter.get(
  45. "/list",
  46. summary="查询应用列表",
  47. description="查询应用列表",
  48. response_model=ResponseSchema[list[ApplicationOutSchema]],
  49. )
  50. async def get_obj_list_controller(
  51. page: Annotated[PaginationQueryParam, Depends()],
  52. search: Annotated[ApplicationQueryParam, Depends()],
  53. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:portal:query"]))],
  54. ) -> JSONResponse:
  55. """
  56. 查询应用列表
  57. 参数:
  58. - page (PaginationQueryParam): 分页参数模型
  59. - search (ApplicationQueryParam): 查询参数模型
  60. - auth (AuthSchema): 认证信息模型
  61. 返回:
  62. - JSONResponse: 包含应用列表的JSON响应
  63. """
  64. result_dict = await ApplicationService.page_service(
  65. auth=auth,
  66. page_no=page.page_no,
  67. page_size=page.page_size,
  68. search=search,
  69. order_by=page.order_by,
  70. )
  71. log.info("查询应用列表成功")
  72. return SuccessResponse(data=result_dict, msg="查询应用列表成功")
  73. @PortalRouter.get(
  74. "/plugins",
  75. summary="列出插件元数据",
  76. description="扫描 app/plugin 下 module_* 目录及可选 plugin.toml,供管理端展示。",
  77. response_model=ResponseSchema[list[PluginInfoOut]],
  78. )
  79. async def list_plugins_controller(
  80. auth: Annotated[
  81. AuthSchema,
  82. Depends(AuthPermission(["module_application:portal:query"])),
  83. ],
  84. ) -> JSONResponse:
  85. """
  86. 列出插件与子系统元数据(不涉及动态安装依赖)。
  87. 参数:
  88. - auth (AuthSchema): 认证信息模型
  89. 返回:
  90. - JSONResponse: 插件信息列表
  91. """
  92. data = await ApplicationService.list_plugins_service()
  93. log.info("查询插件元数据列表成功")
  94. return SuccessResponse(data=data, msg="查询插件元数据成功")
  95. @PortalRouter.post(
  96. "/create",
  97. summary="创建应用",
  98. description="创建应用",
  99. response_model=ResponseSchema[ApplicationOutSchema],
  100. )
  101. async def create_obj_controller(
  102. data: ApplicationCreateSchema,
  103. auth: Annotated[
  104. AuthSchema,
  105. Depends(AuthPermission(["module_application:portal:create"])),
  106. ],
  107. ) -> JSONResponse:
  108. """
  109. 创建应用
  110. 参数:
  111. - data (ApplicationCreateSchema): 应用创建模型
  112. - auth (AuthSchema): 认证信息模型
  113. 返回:
  114. - JSONResponse: 包含创建应用详情的JSON响应
  115. """
  116. result_dict = await ApplicationService.create_service(auth=auth, data=data)
  117. log.info(f"创建应用成功: {result_dict}")
  118. return SuccessResponse(data=result_dict, msg="创建应用成功")
  119. @PortalRouter.put(
  120. "/update/{id}",
  121. summary="修改应用",
  122. description="修改应用",
  123. response_model=ResponseSchema[ApplicationOutSchema],
  124. )
  125. async def update_obj_controller(
  126. data: ApplicationUpdateSchema,
  127. id: Annotated[int, Path(description="应用ID")],
  128. auth: Annotated[
  129. AuthSchema,
  130. Depends(AuthPermission(["module_application:portal:update"])),
  131. ],
  132. ) -> JSONResponse:
  133. """
  134. 修改应用
  135. 参数:
  136. - data (ApplicationUpdateSchema): 应用更新模型
  137. - id (int): 应用ID
  138. - auth (AuthSchema): 认证信息模型
  139. 返回:
  140. - JSONResponse: 包含修改应用详情的JSON响应
  141. """
  142. result_dict = await ApplicationService.update_service(auth=auth, id=id, data=data)
  143. log.info(f"修改应用成功: {result_dict}")
  144. return SuccessResponse(data=result_dict, msg="修改应用成功")
  145. @PortalRouter.delete(
  146. "/delete",
  147. summary="删除应用",
  148. description="删除应用",
  149. response_model=ResponseSchema[None],
  150. )
  151. async def delete_obj_controller(
  152. ids: Annotated[list[int], Body(description="ID列表")],
  153. auth: Annotated[
  154. AuthSchema,
  155. Depends(AuthPermission(["module_application:portal:delete"])),
  156. ],
  157. ) -> JSONResponse:
  158. """
  159. 删除应用
  160. 参数:
  161. - ids (list[int]): 应用ID列表
  162. - auth (AuthSchema): 认证信息模型
  163. 返回:
  164. - JSONResponse: 包含删除应用详情的JSON响应
  165. """
  166. await ApplicationService.delete_service(auth=auth, ids=ids)
  167. log.info(f"删除应用成功: {ids}")
  168. return SuccessResponse(msg="删除应用成功")
  169. @PortalRouter.patch(
  170. "/available/setting",
  171. summary="批量修改应用状态",
  172. description="批量修改应用状态",
  173. response_model=ResponseSchema[None],
  174. )
  175. async def batch_set_available_obj_controller(
  176. data: BatchSetAvailable,
  177. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:portal:patch"]))],
  178. ) -> JSONResponse:
  179. """
  180. 批量修改应用状态
  181. 参数:
  182. - data (BatchSetAvailable): 批量修改应用状态模型
  183. - auth (AuthSchema): 认证信息模型
  184. 返回:
  185. - JSONResponse: 批量修改应用状态成功
  186. """
  187. await ApplicationService.set_available_service(auth=auth, data=data)
  188. log.info(f"批量修改应用状态成功: {data.ids}")
  189. return SuccessResponse(msg="批量修改应用状态成功")