Merge branch '20260204'
# Conflicts: # .idea/compiler.xml # .idea/vcs.xml # lidee-admin/src/main/resources/application-local.yaml # lidee-admin/target/classes/application-local.yaml # lidee-admin/target/lidee-admin.jar # lidee-admin/target/lidee-admin.jar.original # lidee-admin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-core/src/main/java/com/lideeyunji/core/framework/entity/ReportFieldEntity.java # lidee-core/src/main/java/com/lideeyunji/core/framework/service/impl/ReportServiceImpl.java # lidee-core/target/classes/com/lideeyunji/core/framework/entity/ReportFieldEntity.class # lidee-core/target/classes/com/lideeyunji/core/framework/service/impl/ReportServiceImpl.class # lidee-core/target/lidee-core-2.2.4.jar # lidee-core/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-module/lidee-module-api/target/lidee-module-api-2.2.4.jar # lidee-module/lidee-module-api/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-module/lidee-module-biz/target/lidee-module-biz-2.2.4.jar # lidee-module/lidee-module-biz/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-service/lidee-service-infra-api/target/lidee-service-infra-api-2.2.4.jar # lidee-service/lidee-service-infra-biz/target/lidee-service-infra-biz-2.2.4.jar # lidee-service/lidee-service-system-api/target/lidee-service-system-api-2.2.4.jar # lidee-service/lidee-service-system-biz/target/lidee-service-system-biz-2.2.4.jar # lidee-tool/tool-common/target/tool-common-2.2.4.jar # lidee-tool/tool-spring-boot-starter-ai/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-tool/tool-spring-boot-starter-ai/target/tool-spring-boot-starter-ai-2.2.4.jar # lidee-tool/tool-spring-boot-starter-captcha/target/tool-spring-boot-starter-captcha-2.2.4.jar # lidee-tool/tool-spring-boot-starter-dict/target/tool-spring-boot-starter-dict-2.2.4.jar # lidee-tool/tool-spring-boot-starter-excel/target/tool-spring-boot-starter-excel-2.2.4.jar # lidee-tool/tool-spring-boot-starter-exception/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-tool/tool-spring-boot-starter-exception/target/tool-spring-boot-starter-exception-2.2.4.jar # lidee-tool/tool-spring-boot-starter-file/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-tool/tool-spring-boot-starter-file/target/tool-spring-boot-starter-file-2.2.4.jar # lidee-tool/tool-spring-boot-starter-flowable/target/tool-spring-boot-starter-flowable-2.2.4.jar # lidee-tool/tool-spring-boot-starter-ip/target/tool-spring-boot-starter-ip-2.2.4.jar # lidee-tool/tool-spring-boot-starter-job/target/tool-spring-boot-starter-job-2.2.4.jar # lidee-tool/tool-spring-boot-starter-monitor/target/tool-spring-boot-starter-monitor-2.2.4.jar # lidee-tool/tool-spring-boot-starter-mybatis/target/tool-spring-boot-starter-mybatis-2.2.4.jar # lidee-tool/tool-spring-boot-starter-operatelog/target/tool-spring-boot-starter-operatelog-2.2.4.jar # lidee-tool/tool-spring-boot-starter-permission/target/tool-spring-boot-starter-permission-2.2.4.jar # lidee-tool/tool-spring-boot-starter-protection/target/tool-spring-boot-starter-protection-2.2.4.jar # lidee-tool/tool-spring-boot-starter-redis/target/tool-spring-boot-starter-redis-2.2.4.jar # lidee-tool/tool-spring-boot-starter-security/target/tool-spring-boot-starter-security-2.2.4.jar # lidee-tool/tool-spring-boot-starter-sql/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-tool/tool-spring-boot-starter-sql/target/tool-spring-boot-starter-sql-2.2.4.jar # lidee-tool/tool-spring-boot-starter-tenant/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-tool/tool-spring-boot-starter-tenant/target/tool-spring-boot-starter-tenant-2.2.4.jar # lidee-tool/tool-spring-boot-starter-test/target/tool-spring-boot-starter-test-2.2.4.jar # lidee-tool/tool-spring-boot-starter-web/target/tool-spring-boot-starter-web-2.2.4.jar # lidee-tool/tool-spring-boot-starter-websocket/target/tool-spring-boot-starter-websocket-2.2.4.jar # lidee-tool/tool-spring-boot-starter-yunji/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst # lidee-tool/tool-spring-boot-starter-yunji/target/tool-spring-boot-starter-yunji-2.2.4.jar # logs/lideeyunji-error.log # logs/lideeyunji-info.log
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.lideeyunji</groupId>
|
||||
<artifactId>lidee-tool</artifactId>
|
||||
<version>2.2.4</version>
|
||||
</parent>
|
||||
<groupId>com.lideeyunji</groupId>
|
||||
<artifactId>tool-spring-boot-starter-ip</artifactId>
|
||||
<version>2.2.4</version>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>IP 拓展,支持如下功能:
|
||||
1. IP 功能:查询 IP 对应的城市信息
|
||||
基于 https://gitee.com/lionsoul/ip2region 实现
|
||||
2. 城市功能:查询城市编码对应的城市信息
|
||||
基于 https://github.com/modood/Administrative-divisions-of-China 实现</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.lideeyunji</groupId>
|
||||
<artifactId>tool-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lideeyunji</groupId>
|
||||
<artifactId>tool-spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,54 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>lidee-tool</artifactId>
|
||||
<groupId>com.lideeyunji</groupId>
|
||||
<version>${lidee.version}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>tool-spring-boot-starter-ip</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>IP 拓展,支持如下功能:
|
||||
1. IP 功能:查询 IP 对应的城市信息
|
||||
基于 https://gitee.com/lionsoul/ip2region 实现
|
||||
2. 城市功能:查询城市编码对应的城市信息
|
||||
基于 https://github.com/modood/Administrative-divisions-of-China 实现
|
||||
</description>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.lideeyunji</groupId>
|
||||
<artifactId>tool-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- IP地址检索 -->
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<scope>provided</scope> <!-- 设置为 provided,只有工具类需要使用到 -->
|
||||
</dependency>
|
||||
|
||||
<!-- Test 测试相关 -->
|
||||
<dependency>
|
||||
<groupId>com.lideeyunji</groupId>
|
||||
<artifactId>tool-spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.lideeyunji.tool.framework.ip.core;
|
||||
|
||||
import com.lideeyunji.tool.framework.ip.core.enums.AreaTypeEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 区域节点,包括国家、省份、城市、地区等信息
|
||||
*
|
||||
* 数据可见 resources/area.csv 文件
|
||||
*
|
||||
* @author 金灯剑客
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Area {
|
||||
|
||||
/**
|
||||
* 编号 - 全球,即根目录
|
||||
*/
|
||||
public static final Integer ID_GLOBAL = 0;
|
||||
/**
|
||||
* 编号 - 中国
|
||||
*/
|
||||
public static final Integer ID_CHINA = 1;
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 类型
|
||||
*
|
||||
* 枚举 {@link AreaTypeEnum}
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 父节点
|
||||
*/
|
||||
private Area parent;
|
||||
/**
|
||||
* 子节点
|
||||
*/
|
||||
private List<Area> children;
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.lideeyunji.tool.framework.ip.core.enums;
|
||||
|
||||
import com.lideeyunji.tool.framework.common.core.IntArrayValuable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 区域类型枚举
|
||||
*
|
||||
* @author 金灯剑客
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum AreaTypeEnum implements IntArrayValuable {
|
||||
|
||||
COUNTRY(1, "国家"),
|
||||
PROVINCE(2, "省份"),
|
||||
CITY(3, "城市"),
|
||||
DISTRICT(4, "地区"), // 县、镇、区等
|
||||
;
|
||||
|
||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AreaTypeEnum::getType).toArray();
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final Integer type;
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return ARRAYS;
|
||||
}
|
||||
}
|
||||
@@ -1,265 +0,0 @@
|
||||
package com.lideeyunji.tool.framework.ip.core.utils;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.csv.CsvRow;
|
||||
import cn.hutool.core.text.csv.CsvUtil;
|
||||
import com.lideeyunji.tool.framework.common.util.object.ObjectUtils;
|
||||
import com.lideeyunji.tool.framework.ip.core.Area;
|
||||
import com.lideeyunji.tool.framework.ip.core.enums.AreaTypeEnum;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static com.lideeyunji.tool.framework.common.util.collection.CollectionUtils.convertList;
|
||||
|
||||
/**
|
||||
* 区域工具类
|
||||
*
|
||||
* @author 金灯剑客
|
||||
*/
|
||||
@Slf4j
|
||||
public class AreaUtils {
|
||||
|
||||
/**
|
||||
* 初始化 SEARCHER
|
||||
*/
|
||||
@SuppressWarnings("InstantiationOfUtilityClass")
|
||||
private final static AreaUtils INSTANCE = new AreaUtils();
|
||||
|
||||
/**
|
||||
* Area 内存缓存,提升访问速度
|
||||
*/
|
||||
private static Map<Integer, Area> areas;
|
||||
private static Map<String, String> fullNameIdMap;
|
||||
|
||||
private AreaUtils() {
|
||||
long now = System.currentTimeMillis();
|
||||
areas = new HashMap<>();
|
||||
areas.put(Area.ID_GLOBAL, new Area(Area.ID_GLOBAL, "全球", 0,
|
||||
null, new ArrayList<>()));
|
||||
// 从 csv 中加载数据
|
||||
List<CsvRow> rows = CsvUtil.getReader().read(ResourceUtil.getUtf8Reader("area.csv")).getRows();
|
||||
rows.remove(0); // 删除 header
|
||||
for (CsvRow row : rows) {
|
||||
// 创建 Area 对象
|
||||
Area area = new Area(Integer.valueOf(row.get(0)), row.get(1), Integer.valueOf(row.get(2)),
|
||||
null, new ArrayList<>());
|
||||
// 添加到 areas 中
|
||||
areas.put(area.getId(), area);
|
||||
}
|
||||
|
||||
// 构建父子关系:因为 Area 中没有 parentId 字段,所以需要重复读取
|
||||
for (CsvRow row : rows) {
|
||||
Area area = areas.get(Integer.valueOf(row.get(0))); // 自己
|
||||
Area parent = areas.get(Integer.valueOf(row.get(3))); // 父
|
||||
Assert.isTrue(area != parent, "{}:父子节点相同", area.getName());
|
||||
area.setParent(parent);
|
||||
parent.getChildren().add(area);
|
||||
}
|
||||
log.info("启动加载 AreaUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定编号对应的区域
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @return 区域
|
||||
*/
|
||||
public static Area getArea(Integer id) {
|
||||
return areas.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化区域
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @return 格式化后的区域
|
||||
*/
|
||||
public static String format(Integer id) {
|
||||
return format(id, " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化区域
|
||||
*
|
||||
* 例如说:
|
||||
* 1. id = “静安区”时:上海 上海市 静安区
|
||||
* 2. id = “上海市”时:上海 上海市
|
||||
* 3. id = “上海”时:上海
|
||||
* 4. id = “美国”时:美国
|
||||
* 当区域在中国时,默认不显示中国
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @param separator 分隔符
|
||||
* @return 格式化后的区域
|
||||
*/
|
||||
public static String format(Integer id, String separator) {
|
||||
// 获得区域
|
||||
Area area = areas.get(id);
|
||||
if (area == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 格式化
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < AreaTypeEnum.values().length; i++) { // 避免死循环
|
||||
sb.insert(0, area.getName());
|
||||
// “递归”父节点
|
||||
area = area.getParent();
|
||||
if (area == null
|
||||
|| ObjectUtils.equalsAny(area.getId(), Area.ID_GLOBAL, Area.ID_CHINA)) { // 跳过父节点为中国的情况
|
||||
break;
|
||||
}
|
||||
sb.insert(0, separator);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的区域列表
|
||||
*
|
||||
* @param type 区域类型
|
||||
* @param func 转换函数
|
||||
* @param <T> 结果类型
|
||||
* @return 区域列表
|
||||
*/
|
||||
public static <T> List<T> getByType(AreaTypeEnum type, Function<Area, T> func) {
|
||||
return convertList(areas.values(), func, area -> type.getType().equals(area.getType()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据区域编号、上级区域类型,获取上级区域编号
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @param type 区域类型
|
||||
* @return 上级区域编号
|
||||
*/
|
||||
public static Integer getParentIdByType(Integer id, @NonNull AreaTypeEnum type) {
|
||||
for (int i = 0; i < Byte.MAX_VALUE; i++) {
|
||||
Area area = AreaUtils.getArea(id);
|
||||
if (area == null) {
|
||||
return null;
|
||||
}
|
||||
// 情况一:匹配到,返回它
|
||||
if (type.getType().equals(area.getType())) {
|
||||
return area.getId();
|
||||
}
|
||||
// 情况二:找到根节点,返回空
|
||||
if (area.getParent() == null || area.getParent().getId() == null) {
|
||||
return null;
|
||||
}
|
||||
// 其它:继续向上查找
|
||||
id = area.getParent().getId();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//获取所有上级
|
||||
public static List<Area> getAllParentList(Integer id){
|
||||
List<Area> areaList=new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < Byte.MAX_VALUE; i++) {
|
||||
Area area = AreaUtils.getArea(id);
|
||||
if (area == null) {
|
||||
return areaList;
|
||||
}
|
||||
Area myArea=new Area();
|
||||
myArea.setId(area.getId());
|
||||
myArea.setName(area.getName());
|
||||
myArea.setType(area.getType());
|
||||
myArea.setParent(area.getParent());
|
||||
areaList.add(myArea);//添加
|
||||
|
||||
// 情况二:找到根节点,返回空
|
||||
if (area.getParent() == null || area.getParent().getId() == null) {
|
||||
return areaList;
|
||||
}
|
||||
// 其它:继续向上查找
|
||||
id = area.getParent().getId();
|
||||
}
|
||||
return areaList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归收集区域信息为 Map<路径名称, ID路径>
|
||||
*
|
||||
* @return Map<String, String> key=路径名称,value=ID路径,例如:"中国/北京/东城", "110000,110100,110101"
|
||||
*/
|
||||
public static Map<String, String> getChinaProvinceCityDistrictMap() {//省市区
|
||||
Area rootArea = AreaUtils.getArea(Area.ID_CHINA);
|
||||
Map<String, String> result = new LinkedHashMap<>(); // 保持插入顺序
|
||||
collectProvinceCityDistrict(rootArea, new ArrayList<>(), new ArrayList<>(), result);
|
||||
return result;
|
||||
}
|
||||
public static Map<String, String> getChinaProvinceCityMap() {//省市
|
||||
Area rootArea = AreaUtils.getArea(Area.ID_CHINA);
|
||||
Map<String, String> result = new LinkedHashMap<>(); // 保持插入顺序
|
||||
collectProvinceCity(rootArea, new ArrayList<>(), new ArrayList<>(), result);
|
||||
return result;
|
||||
}
|
||||
public static Map<String, String> getChinaProvinceMap() {//省市
|
||||
Area rootArea = AreaUtils.getArea(Area.ID_CHINA);
|
||||
Map<String, String> result = new LinkedHashMap<>(); // 保持插入顺序
|
||||
rootArea.getChildren();
|
||||
rootArea.getChildren().forEach(area -> {
|
||||
if(area.getType()== AreaTypeEnum.PROVINCE.getType()){
|
||||
result.put(area.getName(),area.getId().toString());
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
//省市
|
||||
private static void collectProvinceCity(Area area,
|
||||
List<String> namePath,
|
||||
List<String> idPath,
|
||||
Map<String, String> result) {
|
||||
// 添加当前节点到路径中
|
||||
if (!"中国".equals(area.getName())) {
|
||||
namePath.add(area.getName());
|
||||
idPath.add(area.getId().toString());
|
||||
}
|
||||
|
||||
// 如果是市级节点,写入结果并结束递归
|
||||
if (area.getType() == AreaTypeEnum.CITY.getType()) {
|
||||
String pathName = String.join("/", namePath);
|
||||
String pathId = String.join(",", idPath);
|
||||
result.put(pathName, pathId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果有子节点,继续递归(适用于省级节点)
|
||||
if (area.getChildren() != null && !area.getChildren().isEmpty()) {
|
||||
for (Area child : area.getChildren()) {
|
||||
collectProvinceCity(child, new ArrayList<>(namePath), new ArrayList<>(idPath), result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//省市区
|
||||
private static void collectProvinceCityDistrict(Area area,
|
||||
List<String> namePath,
|
||||
List<String> idPath,
|
||||
Map<String, String> result) {
|
||||
// 添加当前节点到路径中
|
||||
if(!"中国".equals(area.getName())){
|
||||
namePath.add(area.getName());
|
||||
idPath.add(area.getId().toString());
|
||||
}
|
||||
|
||||
// 如果有子节点,继续递归
|
||||
if (area.getChildren() != null && !area.getChildren().isEmpty()) {
|
||||
for (Area child : area.getChildren()) {
|
||||
collectProvinceCityDistrict(child, new ArrayList<>(namePath), new ArrayList<>(idPath), result);
|
||||
}
|
||||
} else {
|
||||
// 没有子节点了,保存当前路径
|
||||
String pathName = String.join("/", namePath);
|
||||
String pathId = String.join(",", idPath);
|
||||
result.put(pathName, pathId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package com.lideeyunji.tool.framework.ip.core.utils;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import com.lideeyunji.tool.framework.ip.core.Area;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* IP 工具类
|
||||
*
|
||||
* IP 数据源来自 ip2region.xdb 精简版,基于 <a href="https://gitee.com/zhijiantianya/ip2region"/> 项目
|
||||
*
|
||||
* @author wanglhup
|
||||
*/
|
||||
@Slf4j
|
||||
public class IPUtils {
|
||||
|
||||
/**
|
||||
* 初始化 SEARCHER
|
||||
*/
|
||||
@SuppressWarnings("InstantiationOfUtilityClass")
|
||||
private final static IPUtils INSTANCE = new IPUtils();
|
||||
|
||||
/**
|
||||
* IP 查询器,启动加载到内存中
|
||||
*/
|
||||
private static Searcher SEARCHER;
|
||||
|
||||
/**
|
||||
* 私有化构造
|
||||
*/
|
||||
private IPUtils() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
byte[] bytes = ResourceUtil.readBytes("ip2region.xdb");
|
||||
SEARCHER = Searcher.newWithBuffer(bytes);
|
||||
log.info("启动加载 IPUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
|
||||
} catch (IOException e) {
|
||||
log.error("启动加载 IPUtils 失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区编号
|
||||
*
|
||||
* @param ip IP 地址,格式为 127.0.0.1
|
||||
* @return 地区id
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static Integer getAreaId(String ip) {
|
||||
return Integer.parseInt(SEARCHER.search(ip.trim()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区编号
|
||||
*
|
||||
* @param ip IP 地址的时间戳,格式参考{@link Searcher#checkIP(String)} 的返回
|
||||
* @return 地区编号
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static Integer getAreaId(long ip) {
|
||||
return Integer.parseInt(SEARCHER.search(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区
|
||||
*
|
||||
* @param ip IP 地址,格式为 127.0.0.1
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(String ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区
|
||||
*
|
||||
* @param ip IP 地址的时间戳,格式参考{@link Searcher#checkIP(String)} 的返回
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(long ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
/**
|
||||
* IP 拓展,支持如下功能:
|
||||
*
|
||||
* 1. IP 功能:查询 IP 对应的城市信息
|
||||
* 基于 https://gitee.com/lionsoul/ip2region 实现
|
||||
* 2. 城市功能:查询城市编码对应的城市信息
|
||||
* 基于 https://github.com/modood/Administrative-divisions-of-China 实现
|
||||
*
|
||||
* @author 金灯剑客
|
||||
*/
|
||||
package com.lideeyunji.tool.framework.ip;
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Reference in New Issue
Block a user