Files
FastapiAdmin/backend/app/utils/ip_local_util.py
T
zhangtao 93cbe5d987 feat(operation_log): 新增登录位置和处理时间字段
在操作日志中新增登录位置和处理时间字段,以提供更详细的日志信息。同时优化了IP地址归属地获取逻辑,并调整了日志展示界面的列宽。
2025-05-18 18:51:41 +08:00

71 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
import re
import requests
from app.core.logger import logger
class IpLocalUtil:
"""
获取IP归属地工具类
"""
@classmethod
def is_valid_ip(cls, ip: str) -> bool:
"""
校验IP格式是否合法
:param ip: IP地址
:return: 是否合法
"""
ip_pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
return bool(re.match(ip_pattern, ip))
@classmethod
def get_ip_location(cls, ip: str) -> str:
"""
获取IP归属地信息
:param ip: IP地址
:return: IP归属地信息
"""
# 校验IP格式
if not cls.is_valid_ip(ip):
logger.error(f"IP格式不合法: {ip}")
return "未知"
# 内网IP直接返回
if ip == '127.0.0.1' or ip == 'localhost':
return '内网IP'
try:
# 优先使用百度API
ip_result = requests.get(
f'https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}',
timeout=5
)
if ip_result.status_code == 200:
data = ip_result.json().get('data', {})
# "continent": "亚洲",
# "country": "中国",
# "zipcode": "710061",
# "owner": "中国移动",
# "isp": "中国移动",
# "adcode": "610113",
# "prov": "陕西省",
# "city": "西安市",
# "district": "雁塔区"
country = data.get('country', '未知国家')
owner = data.get('owner', '未知运营商')
prov = data.get('prov', '未知省份')
city = data.get('city', '未知城市')
district = data.get('district', '未知地区')
return f'【{owner}】-{country}-{prov}-{city}-{district}'
except Exception as e:
logger.error(f"获取IP归属地失败: {e}")
return "未知"