upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Body, Depends
|
||||
from fastapi.responses import JSONResponse
|
||||
from redis.asyncio.client import Redis
|
||||
|
||||
from app.common.request import PaginationService
|
||||
from app.common.response import SuccessResponse,ErrorResponse
|
||||
from app.core.dependencies import AuthPermission, redis_getter
|
||||
from app.core.base_params import PaginationQueryParam
|
||||
from app.core.router_class import OperationLogRoute
|
||||
from app.core.logger import log
|
||||
|
||||
from .schema import OnlineQueryParam
|
||||
from .service import OnlineService
|
||||
|
||||
|
||||
OnlineRouter = APIRouter(route_class=OperationLogRoute, prefix="/online", tags=["在线用户"])
|
||||
|
||||
|
||||
@OnlineRouter.get(
|
||||
'/list',
|
||||
dependencies=[Depends(AuthPermission(['module_monitor:online:query']))],
|
||||
summary="获取在线用户列表",
|
||||
description="获取在线用户列表"
|
||||
)
|
||||
async def get_online_list_controller(
|
||||
redis: Redis = Depends(redis_getter),
|
||||
paging_query: PaginationQueryParam = Depends(),
|
||||
search: OnlineQueryParam = Depends()
|
||||
)->JSONResponse:
|
||||
"""
|
||||
获取在线用户列表
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis异步客户端实例。
|
||||
- paging_query (PaginationQueryParam): 分页查询参数模型。
|
||||
- search (OnlineQueryParam): 查询参数模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含在线用户列表的JSON响应。
|
||||
"""
|
||||
result_dict_list = await OnlineService.get_online_list_service(redis=redis, search=search)
|
||||
result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= paging_query.page_no, page_size = paging_query.page_size)
|
||||
log.info('获取成功')
|
||||
|
||||
return SuccessResponse(data=result_dict,msg='获取成功')
|
||||
|
||||
|
||||
@OnlineRouter.delete(
|
||||
'/delete',
|
||||
dependencies=[Depends(AuthPermission(['module_monitor:online:delete']))],
|
||||
summary="强制下线",
|
||||
description="强制下线"
|
||||
)
|
||||
async def delete_online_controller(
|
||||
session_id: str = Body(..., description="会话编号"),
|
||||
redis: Redis = Depends(redis_getter),
|
||||
)->JSONResponse:
|
||||
"""
|
||||
强制下线指定在线用户
|
||||
|
||||
参数:
|
||||
- session_id (str): 在线用户会话ID。
|
||||
- redis (Redis): Redis异步客户端实例。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含操作结果的JSON响应。
|
||||
"""
|
||||
is_ok = await OnlineService.delete_online_service(redis=redis, session_id=session_id)
|
||||
if is_ok:
|
||||
log.info("强制下线成功")
|
||||
return SuccessResponse(msg="强制下线成功")
|
||||
else:
|
||||
log.info("强制下线失败")
|
||||
return ErrorResponse(msg="强制下线失败")
|
||||
|
||||
@OnlineRouter.delete(
|
||||
'/clear',
|
||||
dependencies=[Depends(AuthPermission(['module_monitor:online:delete']))],
|
||||
summary="清除所有在线用户",
|
||||
description="清除所有在线用户"
|
||||
)
|
||||
async def clear_online_controller(
|
||||
redis: Redis = Depends(redis_getter),
|
||||
)->JSONResponse:
|
||||
"""
|
||||
清除所有在线用户
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis异步客户端实例。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含操作结果的JSON响应。
|
||||
"""
|
||||
is_ok = await OnlineService.clear_online_service(redis=redis)
|
||||
if is_ok:
|
||||
log.info("清除所有在线用户成功")
|
||||
return SuccessResponse(msg="清除所有在线用户成功")
|
||||
else:
|
||||
log.info("清除所有在线用户失败")
|
||||
return ErrorResponse(msg="清除所有在线用户失败")
|
||||
@@ -0,0 +1,41 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from fastapi import Query
|
||||
|
||||
from app.core.validator import DateTimeStr
|
||||
|
||||
|
||||
class OnlineOutSchema(BaseModel):
|
||||
"""
|
||||
在线用户对应pydantic模型
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
name: str = Field(..., description='用户名称')
|
||||
session_id: str = Field(..., description='会话编号')
|
||||
user_id: int = Field(..., description='用户ID')
|
||||
user_name: str = Field(..., description='用户名')
|
||||
ipaddr: str | None = Field(default=None, description='登陆IP地址')
|
||||
login_location: str | None = Field(default=None, description='登录所属地')
|
||||
os: str | None = Field(default=None, description='操作系统')
|
||||
browser: str | None = Field(default=None, description='浏览器')
|
||||
login_time: DateTimeStr | None = Field(default=None, description='登录时间')
|
||||
login_type: str | None = Field(default=None, description='登录类型 PC端 | 移动端')
|
||||
|
||||
|
||||
class OnlineQueryParam:
|
||||
"""在线用户查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str | None = Query(None, description="登录名称"),
|
||||
ipaddr: str | None = Query(None, description="登陆IP地址"),
|
||||
login_location: str | None = Query(None, description="登录所属地"),
|
||||
) -> None:
|
||||
|
||||
# 模糊查询字段
|
||||
self.name = ("like", f"%{name}%") if name else None
|
||||
self.login_location = ("like", f"%{login_location}%") if login_location else None
|
||||
self.ipaddr = ("like", f"%{ipaddr}%") if ipaddr else None
|
||||
@@ -0,0 +1,120 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
from redis.asyncio.client import Redis
|
||||
|
||||
from app.common.enums import RedisInitKeyConfig
|
||||
from app.core.redis_crud import RedisCURD
|
||||
from app.core.security import decode_access_token
|
||||
from app.core.logger import log
|
||||
|
||||
from .schema import OnlineQueryParam
|
||||
|
||||
|
||||
class OnlineService:
|
||||
"""在线用户管理模块服务层"""
|
||||
|
||||
@classmethod
|
||||
async def get_online_list_service(cls, redis: Redis, search: OnlineQueryParam | None = None) -> list[dict]:
|
||||
"""
|
||||
获取在线用户列表信息(支持分页和搜索)
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis异步客户端实例。
|
||||
- search (OnlineQueryParam | None): 查询参数模型。
|
||||
|
||||
返回:
|
||||
- list[dict]: 在线用户详情字典列表。
|
||||
"""
|
||||
|
||||
keys = await RedisCURD(redis).get_keys(f"{RedisInitKeyConfig.ACCESS_TOKEN.key}:*")
|
||||
tokens = await RedisCURD(redis).mget(keys)
|
||||
|
||||
online_users = []
|
||||
for token in tokens:
|
||||
if not token:
|
||||
continue
|
||||
try:
|
||||
payload = decode_access_token(token=token)
|
||||
session_info = json.loads(payload.sub)
|
||||
if cls._match_search_conditions(session_info, search):
|
||||
online_users.append(session_info)
|
||||
except Exception as e:
|
||||
log.error(f"解析在线用户数据失败: {e}")
|
||||
continue
|
||||
# 按照 login_time 倒序排序
|
||||
online_users.sort(key=lambda x: x.get('login_time', ''), reverse=True)
|
||||
|
||||
return online_users
|
||||
|
||||
|
||||
@classmethod
|
||||
async def delete_online_service(cls, redis: Redis, session_id: str) -> bool:
|
||||
"""
|
||||
强制下线指定在线用户
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis异步客户端实例。
|
||||
- session_id (str): 在线用户会话ID。
|
||||
|
||||
返回:
|
||||
- bool: 如果操作成功则返回True,否则返回False。
|
||||
"""
|
||||
# 删除 token
|
||||
await RedisCURD(redis).delete(f"{RedisInitKeyConfig.ACCESS_TOKEN.key}:{session_id}")
|
||||
await RedisCURD(redis).delete(f"{RedisInitKeyConfig.REFRESH_TOKEN.key}:{session_id}")
|
||||
|
||||
|
||||
log.info(f"强制下线用户会话: {session_id}")
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def clear_online_service(cls, redis: Redis) -> bool:
|
||||
"""
|
||||
强制下线所有在线用户
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis异步客户端实例。
|
||||
|
||||
返回:
|
||||
- bool: 如果操作成功则返回True,否则返回False。
|
||||
"""
|
||||
# 删除 token
|
||||
await RedisCURD(redis).clear(f"{RedisInitKeyConfig.ACCESS_TOKEN.key}:*")
|
||||
await RedisCURD(redis).clear(f"{RedisInitKeyConfig.REFRESH_TOKEN.key}:*")
|
||||
|
||||
log.info(f"清除所有在线用户会话成功")
|
||||
return True
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _match_search_conditions(online_info: dict, search: OnlineQueryParam | None = None) -> bool:
|
||||
"""
|
||||
检查是否匹配搜索条件
|
||||
|
||||
参数:
|
||||
- online_info (dict): 在线用户信息字典。
|
||||
- search (OnlineQueryParam | None): 查询参数模型。
|
||||
|
||||
返回:
|
||||
- bool: 如果匹配则返回True,否则返回False。
|
||||
"""
|
||||
if not search:
|
||||
return True
|
||||
|
||||
if search.name and search.name[1]:
|
||||
keyword = search.name[1].strip('%')
|
||||
if keyword.lower() not in online_info.get("name", "").lower():
|
||||
return False
|
||||
|
||||
if search.ipaddr and search.ipaddr[1]:
|
||||
keyword = search.ipaddr[1].strip('%')
|
||||
if keyword not in online_info.get("ipaddr", ""):
|
||||
return False
|
||||
|
||||
if search.login_location and search.login_location[1]:
|
||||
keyword = search.login_location[1].strip('%')
|
||||
if keyword.lower() not in online_info.get("login_location", "").lower():
|
||||
return False
|
||||
|
||||
return True
|
||||
Reference in New Issue
Block a user