mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-30 03:01:53 +08:00
ip库引入,查询工具类
This commit is contained in:
parent
7f131a3853
commit
0b5aa560b3
@ -20,12 +20,34 @@
|
||||
</description>
|
||||
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
|
||||
|
||||
<properties>
|
||||
<ip2region.version>2.6.6</ip2region.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- IP地址检索 -->
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
<version>${ip2region.version}</version>
|
||||
</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>cn.iocoder.boot</groupId>
|
||||
|
@ -1,11 +1,94 @@
|
||||
package cn.iocoder.yudao.framework.ip.core.utils;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||
import cn.iocoder.yudao.framework.ip.core.Area;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* IP 工具类
|
||||
* <p>
|
||||
* 依赖于ip2region.xdb精简版,来源于<a href="https://gitee.com/zhijiantianya/ip2region"/>
|
||||
* region精简为城市编码
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Slf4j
|
||||
public class IPUtils {
|
||||
|
||||
/**
|
||||
* 根据ip搜索
|
||||
* 启动加载到内存中
|
||||
*/
|
||||
private static Searcher SEARCHER;
|
||||
|
||||
/**
|
||||
* 初始化SEARCHER
|
||||
*/
|
||||
private final static IPUtils INSTANCE = new IPUtils();
|
||||
|
||||
|
||||
/**
|
||||
* 私有化构造
|
||||
*/
|
||||
private IPUtils() {
|
||||
String dbPath = "ip2region.xdb";
|
||||
dbPath = IPUtils.class.getClassLoader().getResource(dbPath).getPath();
|
||||
try {
|
||||
SEARCHER = Searcher.newWithBuffer(Searcher.loadContentFromFile(dbPath));
|
||||
} catch (IOException e) {
|
||||
// 加载xdb文件异常,不影响启动
|
||||
log.error("启动加载IP SEARCH异常", e);
|
||||
SEARCHER = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询IP对应的地区ID,格式应为127.0.0.1
|
||||
* @param ip ip地址
|
||||
* @return 地区id
|
||||
*/
|
||||
public static Integer getAreaId(String ip) {
|
||||
try {
|
||||
return Integer.parseInt(SEARCHER.search(ip));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException(INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询IP对应的地区ID,格式参考{@link Searcher#checkIP(String)} 的返回
|
||||
* @param ip ip地址
|
||||
* @return 地区id
|
||||
*/
|
||||
public static Integer getAreaId(long ip) {
|
||||
try {
|
||||
return Integer.parseInt(SEARCHER.search(ip));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException(INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询IP对应的地区,格式应为127.0.0.1
|
||||
* @param ip ip地址
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(String ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询IP对应的地区,格式参考{@link Searcher#checkIP(String)} 的返回
|
||||
* @param ip ip地址
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(long ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
@ -0,0 +1,55 @@
|
||||
package cn.iocoder.yudao.framework.ip.core.utils;
|
||||
|
||||
import cn.iocoder.yudao.framework.ip.core.Area;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* {@link IPUtils} 的单元测试
|
||||
*/
|
||||
class IPUtilsTest {
|
||||
|
||||
@Test
|
||||
void getAreaId() {
|
||||
// 120.202.4.0|120.202.4.255|420600
|
||||
Integer areaId = IPUtils.getAreaId("120.202.4.50");
|
||||
assertEquals(420600, areaId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAreaId() {
|
||||
// 120.203.123.0|120.203.133.255|360900
|
||||
long ip = 0L;
|
||||
try {
|
||||
ip = Searcher.checkIP("120.203.123.250");
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
Integer areaId = IPUtils.getAreaId(ip);
|
||||
assertEquals(360900, areaId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getArea() {
|
||||
// 120.202.4.0|120.202.4.255|420600
|
||||
Area area = IPUtils.getArea("120.202.4.50");
|
||||
assertEquals("襄阳市", area.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetArea() {
|
||||
// 120.203.123.0|120.203.133.255|360900
|
||||
long ip = 0L;
|
||||
try {
|
||||
ip = Searcher.checkIP("120.203.123.252");
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
Area area = IPUtils.getArea(ip);
|
||||
assertEquals("宜春市", area.getName());
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user