controller.py 4.2 KB

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