离线定位fix

This commit is contained in:
admin
2024-07-25 15:52:01 +08:00
parent e775faa738
commit 9ae9963ea4
7 changed files with 154 additions and 20 deletions

View File

@@ -1,9 +1,7 @@
package com.starry.common.utils.ip;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.starry.common.utils.HttpUtils;
import com.starry.common.utils.RegionUtils;
import com.starry.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
@@ -27,21 +25,20 @@ public class AddressUtils {
public static String getRealAddressByIp(String ip) {
if ("127.0.0.1".equals(ip)) {
return "内网IP";
} else {
try {
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", "GBK");
if (StringUtils.isEmpty(rspStr)) {
log.error("获取地理位置异常 {}", ip);
return UNKNOWN;
}
JSONObject obj = JSON.parseObject(rspStr);
String region = obj.getString("pro");
String city = obj.getString("city");
return String.format("%s %s", region, city);
} catch (Exception e) {
log.error("获取地理位置异常 {}", ip);
}
return UNKNOWN;
}
try {
String rspStr = RegionUtils.getRegion(ip);
if (StringUtils.isEmpty(rspStr)) {
log.error("获取地理位置异常 {}", ip);
return UNKNOWN;
}
String[] obj = rspStr.split("\\|");
String region = obj[2];
String city = obj[3];
return String.format("%s %s", region, city);
} catch (Exception e) {
log.error("获取地理位置异常", e);
}
return UNKNOWN;
}
}