小么熊🐻 2 éve
szülő
commit
80a3cab349

+ 26 - 2
nbjk-admin/src/main/java/com/miaxis/app/controller/exam/ExamInfoController.java

@@ -1,5 +1,8 @@
 package com.miaxis.app.controller.exam;
 
+import com.maxmind.geoip2.DatabaseReader;
+import com.maxmind.geoip2.exception.GeoIp2Exception;
+import com.maxmind.geoip2.model.CityResponse;
 import com.miaxis.common.constant.Constants;
 import com.miaxis.common.core.controller.BaseController;
 import com.miaxis.common.core.domain.Response;
@@ -12,10 +15,16 @@ import com.miaxis.exam.vo.ExamInfoCityVo;
 import com.miaxis.exam.vo.ExamInfoProviceVo;
 import io.swagger.annotations.*;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.File;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * 【考场信息】Controller
@@ -83,12 +92,27 @@ public class ExamInfoController extends BaseController{
 
     @GetMapping(value = "/ip")
     @ApiOperation("获取访问IP")
-    public Response<String> exampleMethod() {
+    public Response<Map<String,Object>> exampleMethod() throws IOException, GeoIp2Exception {
 
         String ipAddr = IpUtils.getIpAddr(ServletUtils.getRequest());
         System.out.println(ipAddr);
 
-        return Response.success(ipAddr);
+        File database = new ClassPathResource("geolite/GeoLite2-City.mmdb").getFile();
+
+        // 初始化 GeoIP 数据库
+        DatabaseReader reader = new DatabaseReader.Builder(database).build();
+        // 替换成你要查询的 IP 地址
+        InetAddress ipAddress = InetAddress.getByName(ipAddr);
+        // 获取 IP 地址的位置信息
+        CityResponse response = reader.city(ipAddress);
+        // 输出位置信息
+        System.out.println(response.getCountry().getNames().get("zh-CN"));
+        System.out.println(response.getMostSpecificSubdivision().getNames().get("zh-CN"));
+        System.out.println(response.getCity().getNames().get("zh-CN"));
+        Map<String,Object> resultMap = new HashMap<String,Object>();
+        resultMap.put("provice",response.getMostSpecificSubdivision().getNames().get("zh-CN"));
+        resultMap.put("city",response.getCity().getNames().get("zh-CN"));
+        return Response.success(resultMap);
     }