controller.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Body, Depends
  3. from fastapi.responses import JSONResponse
  4. from redis.asyncio.client import Redis
  5. from app.common.request import PaginationService
  6. from app.common.response import SuccessResponse,ErrorResponse
  7. from app.core.dependencies import AuthPermission, redis_getter
  8. from app.core.base_params import PaginationQueryParam
  9. from app.core.router_class import OperationLogRoute
  10. from app.core.logger import log
  11. from .schema import OnlineQueryParam
  12. from .service import OnlineService
  13. OnlineRouter = APIRouter(route_class=OperationLogRoute, prefix="/online", tags=["在线用户"])
  14. @OnlineRouter.get(
  15. '/list',
  16. dependencies=[Depends(AuthPermission(['module_monitor:online:query']))],
  17. summary="获取在线用户列表",
  18. description="获取在线用户列表"
  19. )
  20. async def get_online_list_controller(
  21. redis: Redis = Depends(redis_getter),
  22. paging_query: PaginationQueryParam = Depends(),
  23. search: OnlineQueryParam = Depends()
  24. )->JSONResponse:
  25. """
  26. 获取在线用户列表
  27. 参数:
  28. - redis (Redis): Redis异步客户端实例。
  29. - paging_query (PaginationQueryParam): 分页查询参数模型。
  30. - search (OnlineQueryParam): 查询参数模型。
  31. 返回:
  32. - JSONResponse: 包含在线用户列表的JSON响应。
  33. """
  34. result_dict_list = await OnlineService.get_online_list_service(redis=redis, search=search)
  35. result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= paging_query.page_no, page_size = paging_query.page_size)
  36. log.info('获取成功')
  37. return SuccessResponse(data=result_dict,msg='获取成功')
  38. @OnlineRouter.delete(
  39. '/delete',
  40. dependencies=[Depends(AuthPermission(['module_monitor:online:delete']))],
  41. summary="强制下线",
  42. description="强制下线"
  43. )
  44. async def delete_online_controller(
  45. session_id: str = Body(..., description="会话编号"),
  46. redis: Redis = Depends(redis_getter),
  47. )->JSONResponse:
  48. """
  49. 强制下线指定在线用户
  50. 参数:
  51. - session_id (str): 在线用户会话ID。
  52. - redis (Redis): Redis异步客户端实例。
  53. 返回:
  54. - JSONResponse: 包含操作结果的JSON响应。
  55. """
  56. is_ok = await OnlineService.delete_online_service(redis=redis, session_id=session_id)
  57. if is_ok:
  58. log.info("强制下线成功")
  59. return SuccessResponse(msg="强制下线成功")
  60. else:
  61. log.info("强制下线失败")
  62. return ErrorResponse(msg="强制下线失败")
  63. @OnlineRouter.delete(
  64. '/clear',
  65. dependencies=[Depends(AuthPermission(['module_monitor:online:delete']))],
  66. summary="清除所有在线用户",
  67. description="清除所有在线用户"
  68. )
  69. async def clear_online_controller(
  70. redis: Redis = Depends(redis_getter),
  71. )->JSONResponse:
  72. """
  73. 清除所有在线用户
  74. 参数:
  75. - redis (Redis): Redis异步客户端实例。
  76. 返回:
  77. - JSONResponse: 包含操作结果的JSON响应。
  78. """
  79. is_ok = await OnlineService.clear_online_service(redis=redis)
  80. if is_ok:
  81. log.info("清除所有在线用户成功")
  82. return SuccessResponse(msg="清除所有在线用户成功")
  83. else:
  84. log.info("清除所有在线用户失败")
  85. return ErrorResponse(msg="清除所有在线用户失败")