From b37275dba6b782bf3bb3817c4504f6cdef1bef7c Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 18 三月 2021 18:38:51 +0800
Subject: [PATCH] APP首页顶部标签兼容

---
 src/main/java/com/yeshi/buwan/util/IPUtil.java |   91 +++++++++++++++++++++++++++++----------------
 1 files changed, 59 insertions(+), 32 deletions(-)

diff --git a/src/main/java/com/yeshi/buwan/util/IPUtil.java b/src/main/java/com/yeshi/buwan/util/IPUtil.java
index cd8c277..d482d82 100644
--- a/src/main/java/com/yeshi/buwan/util/IPUtil.java
+++ b/src/main/java/com/yeshi/buwan/util/IPUtil.java
@@ -261,42 +261,28 @@
         return ip;
     }
 
-    public static Map<String, String> getIPInfo(String ip) {
-        Map<String, String> map = new HashMap<String, String>();
-        HttpClient client = new HttpClient();
-        GetMethod method = new GetMethod("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
-        try {
-            client.executeMethod(method);
-            String result = method.getResponseBodyAsString();
-            JSONObject object = JSONObject.fromObject(result);
-            if (object.optInt("code") == 0) {
-                map.put("city", object.optJSONObject("data").optString("city"));
-                map.put("country", object.optJSONObject("data").optString("country"));
-                return map;
-            }
 
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return map;
+    /**
+     * 鑾峰彇杩滅▼绔彛
+     *
+     * @param request
+     * @return
+     */
+    public static int getRemotePort(HttpServletRequest request) {
+        return request.getRemotePort();
     }
 
     public static String getIPContry(String ip) {
-        String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563")
-                .addTextPara("ip", ip)
-                .post();
-        JSONObject json = JSONObject.fromObject(res);
-        if (json.optInt("showapi_res_code") == 0) {
-            JSONObject body = json.optJSONObject("showapi_res_body");
-            if (body != null)
-                return body.optString("country");
-        }
-        return "";
+        IPInfo ipInfo = getIPInfo(ip);
+        return ipInfo == null ? "" : ipInfo.getCountry();
     }
 
     public static String getIPProvince(String ip) {
+        IPInfo ipInfo = getIPInfo(ip);
+        return ipInfo == null ? "" : ipInfo.getProvince();
+    }
+
+    public static IPInfo getIPInfo(String ip) {
         try {
             String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563")
                     .addTextPara("ip", ip)
@@ -304,13 +290,54 @@
             JSONObject json = JSONObject.fromObject(res);
             if (json.optInt("showapi_res_code") == 0) {
                 JSONObject body = json.optJSONObject("showapi_res_body");
-                if (body != null)
-                    return body.optString("state");
+                if (body != null) {
+                    String country = body.optString("country");
+                    String state = body.optString("state");
+                    String city = body.optString("city");
+                    return new IPInfo(country, state, city);
+                }
             }
         } catch (Exception e) {
 
         }
-        return "";
+        return null;
+    }
+
+
+    public static class IPInfo {
+        private String province;
+        private String city;
+        private String country;
+
+        public IPInfo(String country, String province, String city) {
+            this.province = province;
+            this.city = city;
+            this.country = country;
+        }
+
+        public String getProvince() {
+            return province;
+        }
+
+        public void setProvince(String province) {
+            this.province = province;
+        }
+
+        public String getCity() {
+            return city;
+        }
+
+        public void setCity(String city) {
+            this.city = city;
+        }
+
+        public String getCountry() {
+            return country;
+        }
+
+        public void setCountry(String country) {
+            this.country = country;
+        }
     }
 
 }

--
Gitblit v1.8.0