You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.9 KiB

"""
系统管理API端点
"""
from fastapi import APIRouter, HTTPException, status
from app.services.system_service import SystemService
from app.schemas.system import MachineCodeResponse, SystemInfoResponse
from app.utils.api_decorators import handle_api_errors
from app.utils.structured_log import get_structured_logger
logger = get_structured_logger(__name__)
router = APIRouter()
# 创建全局system_service实例
system_service = SystemService()
@router.get("/system/machine-code",
summary="获取机器码",
description="获取当前系统的机器码,支持Windows和Linux系统",
response_model=MachineCodeResponse)
@handle_api_errors
async def get_machine_code():
"""获取机器码
Windows: 使用 wmic csproduct get uuid 命令
Linux: 使用 dmidecode -s system-serial-number 或读取系统文件
"""
try:
logger.info("API调用: 获取机器码")
result = await system_service.get_machine_code()
if result["success"]:
logger.info("机器码获取成功")
return result
else:
logger.warning("机器码获取失败")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=result["message"]
)
except HTTPException:
raise
except Exception as e:
logger.error(f"获取机器码API异常: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"获取机器码失败: {str(e)}"
)
@router.get("/system/info",
summary="获取系统信息",
description="获取当前系统的详细信息",
response_model=SystemInfoResponse)
@handle_api_errors
async def get_system_info():
"""获取系统信息"""
try:
logger.info("API调用: 获取系统信息")
result = await system_service.get_system_info()
if result["success"]:
logger.info("系统信息获取成功")
return result
else:
logger.warning("系统信息获取失败")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=result["message"]
)
except HTTPException:
raise
except Exception as e:
logger.error(f"获取系统信息API异常: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"获取系统信息失败: {str(e)}"
)
@router.get("/system/machine-code/detailed",
summary="获取详细机器码信息",
description="获取包含系统信息的详细机器码信息")
@handle_api_errors
async def get_detailed_machine_code_info():
"""获取详细的机器码信息"""
try:
logger.info("API调用: 获取详细机器码信息")
result = await system_service.get_detailed_machine_code_info()
if result["success"]:
logger.info("详细机器码信息获取成功")
return result
else:
logger.warning("详细机器码信息获取失败")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=result["message"]
)
except HTTPException:
raise
except Exception as e:
logger.error(f"获取详细机器码信息API异常: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"获取详细机器码信息失败: {str(e)}"
)
@router.get("/system/health",
summary="系统健康检查",
description="检查系统基本功能是否正常")
@handle_api_errors
async def system_health_check():
"""系统健康检查"""
try:
logger.info("API调用: 系统健康检查")
# 检查基本功能
system_type = system_service.system_utils.get_system_type()
machine_code = system_service.system_utils.get_machine_code()
health_status = {
"success": True,
"message": "系统健康检查通过",
"data": {
"system_type": system_type,
"machine_code_available": machine_code is not None,
"status": "healthy"
}
}
if not machine_code:
health_status["data"]["status"] = "warning"
health_status["data"]["warning"] = "机器码获取失败"
logger.info("系统健康检查完成")
return health_status
except Exception as e:
logger.error(f"系统健康检查异常: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"系统健康检查失败: {str(e)}"
)