package com.gkhy.hazmat.common.utils;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.gkhy.hazmat.common.config.SsasConfig;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Slf4j
|
public class AddressUtils {
|
|
public static final String IP_URL="http://whois.pconline.com.cn/ipJson.jsp";
|
|
|
public static String getRealAddressByIP(String ip){
|
if(IpUtils.internalIp(ip)){
|
return "内网IP";
|
}
|
|
if(SsasConfig.isAddressEnabled()){
|
try {
|
Map<String, Object> queryParams = new HashMap<>();
|
queryParams.put("ip", ip);
|
queryParams.put("json", "true");
|
String rspStr = HttpUtils.sendGet(IP_URL, queryParams);
|
if (StringUtils.isBlank(rspStr)) {
|
log.error("获取地理位置异常 {}", ip);
|
return "XX XX";
|
}
|
JSONObject jsonObject = JSON.parseObject(rspStr);
|
String region = jsonObject.getString("pro");
|
String city = jsonObject.getString("city");
|
return String.format("%s %s", region, city);
|
}catch (Exception e){
|
log.error("获取地理位置异常 {}", ip);
|
}
|
}
|
return "XX XX";
|
}
|
}
|