mirror of
https://gitee.com/novel_dev_team/novel-plus
synced 2026-07-20 17:54:59 +08:00
fix: 修复中国部分 IP 无省份信息导致地理位置显示为“0”的问题
部分中国 IP(如 46.248.24.0-46.248.25.255)缺少省份信息,返回值为“0”。 现优化为直接显示国家名称“中国”。
This commit is contained in:
@@ -26,6 +26,7 @@ public class IpLocationServiceImpl implements IpLocationService {
|
|||||||
try {
|
try {
|
||||||
// 示例返回:"中国|0|湖北省|武汉市|电信"
|
// 示例返回:"中国|0|湖北省|武汉市|电信"
|
||||||
String region = searcher.search(ip);
|
String region = searcher.search(ip);
|
||||||
|
log.info("IP:{},区域:{}", ip, region);
|
||||||
String[] regions = region.split("\\|");
|
String[] regions = region.split("\\|");
|
||||||
if (regions.length > 0) {
|
if (regions.length > 0) {
|
||||||
// 国家
|
// 国家
|
||||||
@@ -38,9 +39,17 @@ public class IpLocationServiceImpl implements IpLocationService {
|
|||||||
return getLocation(publicIp);
|
return getLocation(publicIp);
|
||||||
}
|
}
|
||||||
} else if ("中国".equals(country)) {
|
} else if ("中国".equals(country)) {
|
||||||
// 是中国,则返回省份(第三个字段)
|
// 若为中国,则尝试返回省份(regions[2])
|
||||||
String province = regions.length > 2 ? regions[2] : "未知地区";
|
if (regions.length <= 2) {
|
||||||
// 去掉最后一个“省”字
|
// 数据不足,直接返回国家
|
||||||
|
return country;
|
||||||
|
}
|
||||||
|
String province = regions[2];
|
||||||
|
if (!StringUtils.hasText(province) || "0".equals(province)) {
|
||||||
|
// 省份字段为空或未知,返回国家
|
||||||
|
return country;
|
||||||
|
}
|
||||||
|
// 去掉末尾的“省”字
|
||||||
return province.endsWith("省") ? province.substring(0, province.length() - 1) : province;
|
return province.endsWith("省") ? province.substring(0, province.length() - 1) : province;
|
||||||
} else {
|
} else {
|
||||||
// 非中国,返回国家名
|
// 非中国,返回国家名
|
||||||
|
|||||||
Reference in New Issue
Block a user