Selaa lähdekoodia

:space_invader:feat: 更换三方地图API逆编码

Ritchie 1 vuosi sitten
vanhempi
commit
526c1ac5ce

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

@@ -52,6 +52,8 @@ import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
+import static com.szwl.util.geoCoderUtil.getLocByLonLatVer;
+
 
 @Api(value = "/indexController", tags = {"设备信息上传接口"})
 @RestController
@@ -243,9 +245,17 @@ public class IndexController {
         double lat = Double.parseDouble(latitude);
 
         // 经度范围在73至135之间,纬度范围在3至53之间,即国内区域
-        if (!(lon >= 73) || !(lon <= 135) || !(lat >= 3) || !(lat <= 53)) {
-            // 根据ip校验位置信息
-            String addr = WhoIsUtil.getLocByIp(ip);
+        // 经度:73~124,纬度17~47,经度排除韩国,纬度排除俄罗斯,海南以南
+        if (!(lon >= 73) || !(lon <= 124) || !(lat >= 17) || !(lat <= 47)) {
+            // 根据 ip 校验位置信息
+//            String addr = WhoIsUtil.getLocByIp(ip);
+            // 根据 longitude, latitude 校验位置信息
+            String addr = null;
+            try {
+                addr = getLocByLonLatVer(longitude, latitude);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
             locationCheck.setIp(ip);
             locationCheck.setAddr(addr);
             locationCheckService.saveOrUpdate(locationCheck);

+ 12 - 1
src/main/java/com/szwl/controller/ScheduledService.java

@@ -27,6 +27,8 @@ import java.util.Calendar;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import static com.szwl.util.geoCoderUtil.getLocByLonLatVer;
+
 
 @Configuration //1.主要用于标记配置类,兼备Component的效果。
 @Component
@@ -58,7 +60,16 @@ public class ScheduledService {
             for (TLocationCheck locationChc : locationChecks) {
                 String clientId = locationChc.getClientId();
                 String ip = locationChc.getIp();
-                String addr = WhoIsUtil.getLocByIp(ip);
+                String longitude = locationChc.getLongitude();
+                String latitude = locationChc.getLatitude();
+
+//                String addr = WhoIsUtil.getLocByIp(ip);
+                String addr = null;
+                try {
+                    addr = getLocByLonLatVer(longitude, latitude);
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
                 // 这里不需要更新ip,只需要校验有ip的设备位置对不对
 //                locationCheckService.locCheckMsg(locationChc, clientId, addr);
                 String check = locationCheckService.schLocCheck(locationChc, clientId, addr);

+ 8 - 0
src/main/java/com/szwl/service/impl/TLocationCheckServiceImpl.java

@@ -92,9 +92,13 @@ public class TLocationCheckServiceImpl extends ServiceImpl<TLocationCheckMapper,
         }
 
         String phone = "18620242721";
+        String phone1 = "13631231970"; // 基哥
+        String phone2 = "15018460394"; // 龙
+
         String messages = "【申泽部门】设备编号:" + clientId + ",所在ip与设定位置不符,请及时查看。";
         if (!containsUserInput) { // 不包含,发短信通知,并拒绝审核
             YunPianSms.sendSms(appid, messages, phone);
+            YunPianSms.sendSms(appid, messages, phone2);
             return "fail";
         }
         return "success";
@@ -136,8 +140,12 @@ public class TLocationCheckServiceImpl extends ServiceImpl<TLocationCheckMapper,
     @Override
     public void schSendMsg(String locErrorEq) throws IOException {
         String phone = "18620242721";
+        String phone1 = "13631231970"; // 基哥
+        String phone2 = "15018460394"; // 龙
         String messages = "【申泽部门】设备编号:" + locErrorEq + ",所在ip与设定位置不符,请及时查看。";
         YunPianSms.sendSms(appid, messages, phone);
+        YunPianSms.sendSms(appid, messages, phone1);
+        YunPianSms.sendSms(appid, messages, phone2);
     }
 
 

+ 54 - 0
src/main/java/com/szwl/util/geoCoderUtil.java

@@ -0,0 +1,54 @@
+package com.szwl.util;
+
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpUtil;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.sun.org.apache.xpath.internal.SourceTree;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+public class geoCoderUtil {
+    // 地理位置逆编码
+    private static final String TD_KEY = "26bac63d19fac57c16bc7759455431d8";
+
+    private static final String TD_URL = "http://api.tianditu.gov.cn/geocoder";
+    // 请求:http://api.tianditu.gov.cn/geocoder?postStr={'lon':116.37304,'lat':39.92594,'ver':1}&type=geocode&tk=您的密钥
+
+
+
+    public static String getLocByLonLatVer(String lon, String lat) throws Exception {
+        String ver = "1"; // 版本号
+        String reqUrl = TD_URL + "?postStr={'lon':" + lon + ",'lat':" + lat + ",'ver':" + ver + "}&type=geocode&tk=" + URLEncoder.encode(TD_KEY, "UTF-8");
+        String s = HttpUtil.get(reqUrl);
+        return getNotionAddr(s);
+
+    }
+
+    public static String getNotionAddr(String result) {
+        ObjectMapper mapper = new ObjectMapper();
+
+        try {
+            JsonNode node = mapper.readTree(result);
+
+            String nation = node.at("/result/addressComponent/nation").asText();
+            String address = node.at("/result/addressComponent/address").asText();
+
+//            System.out.println("Nation + Address:" + nation + address);
+            return nation + address;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+//    public static void main(String[] args) throws Exception {
+//        String lon = "127.0013";
+//        String lat = "37.5015";
+//
+//        String response = getLocByLonLatVer(lon, lat);
+//        System.out.println(response);
+//    }
+}
+