controller.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from typing import Annotated
  2. from fastapi import APIRouter, Depends, Path
  3. from fastapi.responses import JSONResponse
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.core.router_class import OperationLogRoute
  6. from app.plugin.module_payment.apikey.schema import (
  7. TenantApiKeyCreate,
  8. TenantApiKeyResponse,
  9. TenantApiKeyListResponse,
  10. TenantApiKeyUpdate,
  11. ApiKeyQueryParam,
  12. )
  13. from app.plugin.module_payment.apikey.service import TenantApiKeyService
  14. from app.common.response import ResponseSchema, SuccessResponse, ErrorResponse
  15. from app.core.base_params import PaginationQueryParam
  16. from app.core.dependencies import AuthPermission
  17. from app.core.logger import log
  18. ApiKeyRouter = APIRouter(route_class=OperationLogRoute, prefix="/api-key", tags=["API Key管理"])
  19. @ApiKeyRouter.post(
  20. "",
  21. summary="创建API Key",
  22. description="为租户创建新的API Key",
  23. response_model=ResponseSchema[TenantApiKeyResponse],
  24. )
  25. async def create_api_key_controller(
  26. data: TenantApiKeyCreate,
  27. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:tenant:api-key:create"]))],
  28. ) -> JSONResponse:
  29. tenant_id = auth.tenant_id
  30. api_key_obj = await TenantApiKeyService.create_api_key(
  31. auth=auth,
  32. tenant_id=tenant_id,
  33. expired_days=data.expired_days,
  34. description=data.description,
  35. )
  36. log.info(f"创建API Key成功: 租户ID={tenant_id}")
  37. return SuccessResponse(
  38. data=TenantApiKeyResponse(
  39. id=api_key_obj.id,
  40. api_key=api_key_obj.api_key,
  41. api_secret=api_key_obj.api_secret,
  42. status=api_key_obj.status,
  43. expired_at=api_key_obj.expired_at,
  44. created_time=api_key_obj.created_time,
  45. ),
  46. msg="创建APIKey成功"
  47. )
  48. @ApiKeyRouter.get(
  49. "/list",
  50. summary="查询API Key",
  51. description="查询API Key",
  52. response_model=ResponseSchema[list[TenantApiKeyListResponse]],
  53. )
  54. async def get_api_key_list_controller(
  55. page: Annotated[PaginationQueryParam, Depends()],
  56. search: Annotated[ApiKeyQueryParam, Depends()],
  57. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:tenant:api-key:query"]))],
  58. ) -> JSONResponse:
  59. result_dict = await TenantApiKeyService.get_api_key_page_service(
  60. auth=auth,
  61. page_no=page.page_no,
  62. page_size=page.page_size,
  63. search=search
  64. )
  65. return SuccessResponse(data=result_dict)
  66. @ApiKeyRouter.put(
  67. "/{api_key_id}",
  68. summary="更新API Key状态",
  69. description="更新API Key的状态",
  70. response_model=ResponseSchema[TenantApiKeyListResponse],
  71. )
  72. async def update_api_key_status_controller(
  73. api_key_id: Annotated[int, Path(description="API Key ID")],
  74. data: TenantApiKeyUpdate,
  75. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:tenant:api-key:update"]))],
  76. ) -> JSONResponse:
  77. return ErrorResponse(msg="暂不支持操作")
  78. @ApiKeyRouter.delete(
  79. "/{api_key_id}",
  80. summary="删除API Key",
  81. description="删除API Key",
  82. response_model=ResponseSchema[dict],
  83. )
  84. async def delete_api_key_controller(
  85. api_key_id: Annotated[int, Path(description="API Key ID")],
  86. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:tenant:api-key:delete"]))],
  87. ) -> JSONResponse:
  88. await TenantApiKeyService.delete_api_key(auth=auth, api_key_id=api_key_id)
  89. log.info(f"删除API Key成功: ID={api_key_id}")
  90. return SuccessResponse(msg="删除APIKey成功")