Browse Source

:new: 增加一个地图接口

Ritchie 8 months ago
parent
commit
e25ccf46bb

+ 11 - 3
src/main/java/com/szwl/controller/IndexController.java

@@ -28,6 +28,7 @@ import com.szwl.service.*;
 import com.szwl.service.es.EsTEquipmentService;
 import com.szwl.util.IDGenerator;
 import com.szwl.util.WechatSendUtil;
+import com.szwl.util.WhoIsUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.io.IOUtils;
@@ -278,11 +279,18 @@ public class IndexController {
             // 根据 ip 校验位置信息
 //            String addr = WhoIsUtil.getLocByIp(ip);
             // 根据 longitude, latitude 校验位置信息
-            String addr = null;
+            String addr;
+            String ipApiAddr = WhoIsUtil.getLocByIp2(ip);
             try {
-                addr = getLocByLonLatVer(longitude, latitude);
+                String lonLatAddr = getLocByLonLatVer(longitude, latitude);
+                if (lonLatAddr!= null &&!lonLatAddr.isEmpty()) {
+                    addr = lonLatAddr + ipApiAddr;
+                } else {
+                    addr = ipApiAddr;
+                }
             } catch (Exception e) {
-                throw new RuntimeException(e);
+                e.printStackTrace();
+                addr = "Failed to get geolocation";
             }
             locationCheck.setIp(ip);
             locationCheck.setLongitude(longitude);

+ 146 - 144
src/main/java/com/szwl/controller/TLocationCheckController.java

@@ -4,6 +4,7 @@ package com.szwl.controller;
 import cn.hutool.core.util.IdUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.szwl.model.bo.R;
 import com.szwl.model.bo.ResponseModel;
 import com.szwl.model.entity.TLocationCheck;
@@ -28,49 +29,50 @@ import java.util.*;
 @RequestMapping("/tLocationCheck")
 public class TLocationCheckController {
 
-    TLocationCheckService locationCheckService;
-    TAdminService adminService;
+  TLocationCheckService locationCheckService;
+  TAdminService adminService;
 
-    public TLocationCheckController(TLocationCheckService locationCheckService, TAdminService adminService) {
-        this.locationCheckService = locationCheckService;
-        this.adminService = adminService;
-    }
+  public TLocationCheckController(TLocationCheckService locationCheckService,
+      TAdminService adminService) {
+    this.locationCheckService = locationCheckService;
+    this.adminService = adminService;
+  }
 
-    @ApiOperation(value = "获取定位信息")
-    @GetMapping("/getLocInfo")
-    public ResponseModel<?> getLocInfo(String clientId) {
-        if (StringUtils.isEmpty(clientId)) {
-            return R.fail("设备id不能为空");
-        }
-        // 根据设备id查询位置信息
-        LambdaQueryWrapper<TLocationCheck> lqw = Wrappers.lambdaQuery();
-        lqw.eq(TLocationCheck::getClientId, clientId);
-        TLocationCheck locationCheck = locationCheckService.getOne(lqw);
-        if (Objects.nonNull(locationCheck)) {
-            HashMap<String, String> locationMap = new HashMap<>();
-            String country = locationCheck.getCountry();
-            String username = locationCheck.getUsername();
-            String name = locationCheck.getName();
-            String phone = locationCheck.getPhone();
-            String longitude = locationCheck.getLongitude();
-            String latitude = locationCheck.getLatitude();
-            String addr = locationCheck.getAddr();
-            String modUsername = locationCheck.getModUsername();
-            String modName = locationCheck.getModName();
-            String modPhone = locationCheck.getModPhone();
-            String message = locationCheck.getMessage();
-
-            locationMap.put("country", country); // 录入国家
-            locationMap.put("username", username); // 账号用户名
-            locationMap.put("name", name); // 录入人姓名
-            locationMap.put("phone", phone); // 录入人电话
-            locationMap.put("longitude", longitude); // 经度
-            locationMap.put("latitude", latitude); // 纬度
-            locationMap.put("address", addr); // 反编译的地址
-            locationMap.put("modUsername", modUsername); // 修改人账号
-            locationMap.put("modName", modName); // 修改人姓名
-            locationMap.put("modPhone", modPhone); // 修改人电话
-            locationMap.put("message", message); // 修改原因
+  @ApiOperation(value = "获取定位信息")
+  @GetMapping("/getLocInfo")
+  public ResponseModel<?> getLocInfo(String clientId) {
+    if (StringUtils.isEmpty(clientId)) {
+      return R.fail("设备id不能为空");
+    }
+    // 根据设备id查询位置信息
+    LambdaQueryWrapper<TLocationCheck> lqw = Wrappers.lambdaQuery();
+    lqw.eq(TLocationCheck::getClientId, clientId);
+    TLocationCheck locationCheck = locationCheckService.getOne(lqw);
+    if (Objects.nonNull(locationCheck)) {
+      HashMap<String, String> locationMap = new HashMap<>();
+      String country = locationCheck.getCountry();
+      String username = locationCheck.getUsername();
+      String name = locationCheck.getName();
+      String phone = locationCheck.getPhone();
+      String longitude = locationCheck.getLongitude();
+      String latitude = locationCheck.getLatitude();
+      String addr = locationCheck.getAddr();
+      String modUsername = locationCheck.getModUsername();
+      String modName = locationCheck.getModName();
+      String modPhone = locationCheck.getModPhone();
+      String message = locationCheck.getMessage();
+
+      locationMap.put("country", country); // 录入国家
+      locationMap.put("username", username); // 账号用户名
+      locationMap.put("name", name); // 录入人姓名
+      locationMap.put("phone", phone); // 录入人电话
+      locationMap.put("longitude", longitude); // 经度
+      locationMap.put("latitude", latitude); // 纬度
+      locationMap.put("address", addr); // 反编译的地址
+      locationMap.put("modUsername", modUsername); // 修改人账号
+      locationMap.put("modName", modName); // 修改人姓名
+      locationMap.put("modPhone", modPhone); // 修改人电话
+      locationMap.put("message", message); // 修改原因
 
 //            if (country.equals("中国")) {
 //                String location = locationCheck.getLocation();
@@ -78,133 +80,133 @@ public class TLocationCheckController {
 //            } else {
 //                return R.ok(country);
 //            }
-            return R.ok(locationMap);
-        }
-        return R.fail("地区信息未录入");
+      return R.ok(locationMap);
+    }
+    return R.fail("地区信息未录入");
+  }
+
+  @ApiOperation(value = "根据ip校验clientId设备定位")
+  @PostMapping("/ipCheck")
+  public ResponseModel<?> ipCheck(@RequestParam String clientId, @RequestParam String ip){
+    if (StringUtils.isBlank(clientId) || StringUtils.isBlank(ip)) {
+      return R.fail("参数不能为空");
     }
+    // 根据 clientId 查设备的 location,country
+    LambdaQueryWrapper<TLocationCheck> lqw = Wrappers.lambdaQuery();
+    lqw.eq(TLocationCheck::getClientId, clientId);
+    TLocationCheck locationCheck = locationCheckService.getOne(lqw);
+    if (Objects.isNull(locationCheck)) {
+      return R.fail("生产部未录入改设备的位置信息");
+    }
+
+    locationCheck.setIp(ip);
+    locationCheck.setModifyTime(new Date());
 
-    @ApiOperation(value = "根据ip校验clientId设备定位")
-    @PostMapping("/ipCheck")
-    public ResponseModel<?> ipCheck(@RequestParam String clientId, @RequestParam String ip) {
-        if (StringUtils.isBlank(clientId) || StringUtils.isBlank(ip)) {
-            return R.fail("参数不能为空");
-        }
-        // 根据 clientId 查设备的 location,country
-        LambdaQueryWrapper<TLocationCheck> lqw = Wrappers.lambdaQuery();
-        lqw.eq(TLocationCheck::getClientId, clientId);
-        TLocationCheck locationCheck = locationCheckService.getOne(lqw);
-        if (Objects.isNull(locationCheck)) {
-            return R.fail("生产部未录入改设备的位置信息");
-        }
-
-        locationCheck.setIp(ip);
-        locationCheck.setModifyTime(new Date());
-
-        String addr = WhoIsUtil.getLocByIp(ip);
+    String addr = WhoIsUtil.getLocByIp(ip);
 
 //        String result = locationCheckService.locCheckMsg(locationCheck, clientId, addr);
-        String result = locationCheckService.locCheckNoMsg(locationCheck, clientId, addr);
+    String result = locationCheckService.locCheckNoMsg(locationCheck, clientId, addr);
 
-        if ("fail".equals(result)) {
-            locationCheck.setErr("不匹配");
-        }
-        locationCheckService.updateById(locationCheck);
-        return R.ok(result);
+    if ("fail".equals(result)) {
+      locationCheck.setErr("不匹配");
+    }
+    locationCheckService.updateById(locationCheck);
+    return R.ok(result);
+  }
+
+  @ApiOperation(value = "修改定位信息")
+  @PostMapping("/updateLocInfo")
+  public ResponseModel<?> updateLocInfo(@RequestBody TLocationCheck locationCheck) {
+    if (Objects.isNull(locationCheck)) {
+      return R.fail("参数不能为空");
     }
 
-    @ApiOperation(value = "修改定位信息")
-    @PostMapping("/updateLocInfo")
-    public ResponseModel<?> updateLocInfo(@RequestBody TLocationCheck locationCheck) {
-        if (Objects.isNull(locationCheck)) {
-            return R.fail("参数不能为空");
-        }
-
-        // 售后部同事修改的信息:姓名,电话,username,clientId,国家
-        String modUsername = locationCheck.getModUsername();
-
-        Set<String> allowedUsernames = new HashSet<>(Arrays.asList("shouhoumi", "sysMgtAcc", "jiang123", "ethan"));
-        if (!allowedUsernames.contains(modUsername)) {
-            return R.fail("该账号无权修改");
-        }
-        String modName = locationCheck.getModName();
-        String modPhone = locationCheck.getModPhone();
-        String clientId = locationCheck.getClientId();
-//        String location = locationCheck.getLocation();
-        String country = locationCheck.getCountry();
-        String message = locationCheck.getMessage();
+    // 售后部同事修改的信息:姓名,电话,username,clientId,国家
+    String modUsername = locationCheck.getModUsername();
 
-        if (Objects.isNull(clientId)) {
-            return R.fail("设备唯一码不能为空");
-        }
+    Set<String> allowedUsernames = new HashSet<>(
+        Arrays.asList("shouhoumi", "sysMgtAcc", "jiang123", "ethan"));
+    if (!allowedUsernames.contains(modUsername)) {
+      return R.fail("该账号无权修改");
+    }
+    String modName = locationCheck.getModName();
+    String modPhone = locationCheck.getModPhone();
+    String clientId = locationCheck.getClientId();
+//        String location = locationCheck.getLocation();
+    String country = locationCheck.getCountry();
+    String message = locationCheck.getMessage();
 
-        LambdaQueryWrapper<TLocationCheck> wrapper = Wrappers.lambdaQuery();
-        wrapper.eq(TLocationCheck::getClientId, clientId);
-        List<TLocationCheck> checks = locationCheckService.list(wrapper);
-        TLocationCheck tLocationCheck = checks.get(0);
+    if (Objects.isNull(clientId)) {
+      return R.fail("设备唯一码不能为空");
+    }
 
+    LambdaQueryWrapper<TLocationCheck> wrapper = Wrappers.lambdaQuery();
+    wrapper.eq(TLocationCheck::getClientId, clientId);
+    List<TLocationCheck> checks = locationCheckService.list(wrapper);
+    TLocationCheck tLocationCheck = checks.get(0);
 
-        tLocationCheck.setModifyTime(new Date());
-        tLocationCheck.setModName(modName);
+    tLocationCheck.setModifyTime(new Date());
+    tLocationCheck.setModName(modName);
 //        tLocationCheck.setModAdminId(modAdminId);
-        tLocationCheck.setModPhone(modPhone);
-        tLocationCheck.setModUsername(modUsername);
+    tLocationCheck.setModPhone(modPhone);
+    tLocationCheck.setModUsername(modUsername);
 //        tLocationCheck.setLocation(location);
-        tLocationCheck.setClientId(clientId);
-        tLocationCheck.setCountry(country);
-        tLocationCheck.setMessage(message);
+    tLocationCheck.setClientId(clientId);
+    tLocationCheck.setCountry(country);
+    tLocationCheck.setMessage(message);
+
+    locationCheckService.updateById(tLocationCheck);
 
-        locationCheckService.updateById(tLocationCheck);
+    return R.ok("修改成功");
+  }
 
 
-        return R.ok("修改成功");
+  @ApiOperation(value = "录入定位信息")
+  @PostMapping("/inputLocInfo")
+  public ResponseModel<?> inputLocInfo(@RequestBody TLocationCheck locationCheck) {
+    if (Objects.isNull(locationCheck)) {
+      return R.fail("参数不能为空");
     }
 
+    String name = locationCheck.getName();
+    String phone = locationCheck.getPhone();
+    String username = locationCheck.getUsername();
+    String clientId = locationCheck.getClientId();
+    String country = locationCheck.getCountry(); // 国家
+//        String location = locationCheck.getLocation(); // 地区
 
-    @ApiOperation(value = "录入定位信息")
-    @PostMapping("/inputLocInfo")
-    public ResponseModel<?> inputLocInfo(@RequestBody TLocationCheck locationCheck) {
-        if (Objects.isNull(locationCheck)) {
-            return R.fail("参数不能为空");
-        }
+    if (Objects.isNull(clientId)) {
+      return R.fail("设备唯一码不能为空");
+    }
+    LambdaQueryWrapper<TLocationCheck> wrapper = Wrappers.lambdaQuery();
+    wrapper.eq(TLocationCheck::getClientId, clientId);
+    List<TLocationCheck> checks = locationCheckService.list(wrapper);
+    if (checks.size() > 0) {
+      return R.fail("此设备信息已录入");
+    }
 
-        String name = locationCheck.getName();
-        String phone = locationCheck.getPhone();
-        String username = locationCheck.getUsername();
-        String clientId = locationCheck.getClientId();
-        String country = locationCheck.getCountry(); // 国家
-//        String location = locationCheck.getLocation(); // 地区
+    Set<String> allowedUsers = new HashSet<>(
+        Arrays.asList("sysMgtAcc", "jiang123", "shouhou121", "shouhou369", "shouhou397",
+            "shouhoumi", "zhl123", "ethan"));
+    if (!allowedUsers.contains(username)) {
+      return R.fail("该账号无权操作");
+    }
 
-        if (Objects.isNull(clientId)) {
-            return R.fail("设备唯一码不能为空");
-        }
-        LambdaQueryWrapper<TLocationCheck> wrapper = Wrappers.lambdaQuery();
-        wrapper.eq(TLocationCheck::getClientId, clientId);
-        List<TLocationCheck> checks = locationCheckService.list(wrapper);
-        if (checks.size() > 0) {
-            return R.fail("此设备信息已录入");
-        }
-
-        Set<String> allowedUsers = new HashSet<>(Arrays.asList("sysMgtAcc", "jiang123", "shouhou121", "shouhou369", "shouhou397", "shouhoumi", "zhl123", "ethan"));
-        if (!allowedUsers.contains(username)) {
-            return R.fail("该账号无权操作");
-        }
-
-
-        TLocationCheck tLocationCheck = new TLocationCheck();
-        String uuid = IdUtil.simpleUUID();
-        tLocationCheck.setId(uuid);
-        tLocationCheck.setCreateTime(new Date());
-        tLocationCheck.setName(name);
-        tLocationCheck.setPhone(phone);
-        tLocationCheck.setUsername(username);
-        tLocationCheck.setClientId(clientId);
-        tLocationCheck.setCountry(country);
+    TLocationCheck tLocationCheck = new TLocationCheck();
+    String uuid = IdUtil.simpleUUID();
+    tLocationCheck.setId(uuid);
+    tLocationCheck.setCreateTime(new Date());
+    tLocationCheck.setName(name);
+    tLocationCheck.setPhone(phone);
+    tLocationCheck.setUsername(username);
+    tLocationCheck.setClientId(clientId);
+    tLocationCheck.setCountry(country);
 //        tLocationCheck.setLocation(location);
 
-        locationCheckService.save(tLocationCheck);
+    locationCheckService.save(tLocationCheck);
 
-        return R.ok("设备出厂信息录入成功");
-    }
+    return R.ok("设备出厂信息录入成功");
+  }
 
 }
 

+ 24 - 4
src/main/java/com/szwl/util/WhoIsUtil.java

@@ -1,15 +1,35 @@
 package com.szwl.util;
 
 import cn.hutool.http.HttpUtil;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 public class WhoIsUtil {
 
-    public static String getLocByIp(String ip) {
-        // 根据 ip 获取位置
+  public static String getLocByIp(String ip) {
+    // 根据 ip 获取位置
 //        String reqUrl = "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip;
-        String reqUrl = "http://whois.pconline.com.cn/ip.jsp?ip=" + ip;
+    String reqUrl = "http://whois.pconline.com.cn/ip.jsp?ip=" + ip;
 
 //        HttpRequest.get(reqUrl)
-        return HttpUtil.get(reqUrl);
+    return HttpUtil.get(reqUrl);
+  }
+
+  public static String getLocByIp2(String ip) throws JsonProcessingException {
+    String reqUrl = "http://ip-api.com/json/" + ip + "?lang=zh-CN";
+    String result = HttpUtil.get(reqUrl);
+
+    ObjectMapper objectMapper = new ObjectMapper();
+    JsonNode jsonNode = objectMapper.readTree(result);
+
+    if ("fail".equals(jsonNode.get("status").asText())) {
+      return "";
     }
+
+    String country = jsonNode.get("country").asText("");
+    String city = jsonNode.get("city").asText("");
+    return country + city;
+  }
+
 }

+ 14 - 0
src/test/java/com/szwl/util/WhoIsUtilTest.java

@@ -0,0 +1,14 @@
+package com.szwl.util;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+class WhoIsUtilTest {
+
+  public static void main(String[] args) throws JsonProcessingException {
+    String s = WhoIsUtil.getLocByIp2("41.246.128.129");
+
+    System.out.println("位置"+s);
+  }
+}