refactor: 移除IP地址转换字符串的冗余操作

在多个文件中,`request.client.host` 已经是字符串类型,无需再使用 `str()` 进行转换。此次修改简化了代码,避免了不必要的类型转换操作。
This commit is contained in:
zhangtao
2025-05-18 19:37:14 +08:00
parent cc2c1931da
commit dcfdc052d0
3 changed files with 4 additions and 3 deletions
@@ -94,7 +94,7 @@ class LoginService:
name=user.name, name=user.name,
user_name=user.username, user_name=user.username,
ipaddr=request.client.host, ipaddr=request.client.host,
login_location=IpLocalUtil.get_ip_location(str(request.client.host)), login_location=IpLocalUtil.get_ip_location(request.client.host),
os=user_agent.os.family, os=user_agent.os.family,
browser = user_agent.browser.family, browser = user_agent.browser.family,
login_time=user.last_login login_time=user.last_login
+1 -1
View File
@@ -68,7 +68,7 @@ class OperationLogRoute(APIRoute):
current_user_id = request.scope.get("user_id") current_user_id = request.scope.get("user_id")
if request.url.path == '/api/v1/system/auth/login': if request.url.path == '/api/v1/system/auth/login':
# 只有登录的才会获取登录地址 # 只有登录的才会获取登录地址
login_location = IpLocalUtil.get_ip_location(str(request.client.host)) login_location = IpLocalUtil.get_ip_location(request.client.host)
await OperationLogService.create_log_service(data=OperationLogCreateSchema( await OperationLogService.create_log_service(data=OperationLogCreateSchema(
request_path = request.url.path, request_path = request.url.path,
+2 -1
View File
@@ -35,7 +35,7 @@ class IpLocalUtil:
logger.error(f"IP格式不合法: {ip}") logger.error(f"IP格式不合法: {ip}")
return "未知" return "未知"
logger.info(f"获取IP归属地: {ip}") logger.info(f"获取IP归属地: {ip}, 类型: {type(ip)}")
# 内网IP直接返回 # 内网IP直接返回
if ip == '127.0.0.1' or ip == 'localhost': if ip == '127.0.0.1' or ip == 'localhost':
@@ -50,6 +50,7 @@ class IpLocalUtil:
if ip_result.status_code == 200: if ip_result.status_code == 200:
data = ip_result.json().get('data', {}) data = ip_result.json().get('data', {})
logger.info(f"获取IP归属地成功: {data}")
# "continent": "亚洲", # "continent": "亚洲",
# "country": "中国", # "country": "中国",
# "zipcode": "710061", # "zipcode": "710061",