From 4edb26584d1278aca3578c92ff1ddf57017cfe5f Mon Sep 17 00:00:00 2001 From: "shenjian@fangjia.com" Date: Mon, 18 Jul 2022 15:42:49 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=AB=98=E5=BE=B7key=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../temp/shenjian/util/GaoDeUtils.groovy | 120 +++++------------- 1 file changed, 29 insertions(+), 91 deletions(-) diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy index af5f5fc8b..042e2ea23 100644 --- a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy @@ -24,10 +24,6 @@ import java.util.concurrent.atomic.AtomicInteger */ class GaoDeUtils { - // 新获取key - def static GAO_DE_AK = ["4e9592208336433ee1b7aa4d1d434418", "10bcb421d2ee4e51ed9268d6dca58ad1"] - def static AtomicInteger keyUsed = new AtomicInteger(0) - def static codeNameMap = new HashMap(){{ put("150500", "地铁"); put("1507", "公交"); @@ -54,6 +50,18 @@ class GaoDeUtils { put("0601", "商场") }}; + /** + * 获取高德KEY + * @return + */ + static String getGaodeKey() { + JSONObject json = HttpRequestUtil.getJsonContent("http://192.168.0.100:38080/FangJiaDataService/getAmapKey") + if (json.status == 0 && json.result != null) { + return json.result + } + System.exit(1); + } + /** * 查找周边配套 * @param city @@ -66,7 +74,7 @@ class GaoDeUtils { */ static List searchNearByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){ def params = [:] - params.key = GAO_DE_AK[keyUsed] + params.key = this.getGaodeKey() if (types) { params.types = types @@ -91,17 +99,7 @@ class GaoDeUtils { count = json.getString("count").toInteger() result.addAll(DataUtil.toMongoDbObj(json.pois)) } else if (json && json.infocode && json.infocode == "10044") { - synchronized (this) { - for (int i = 0; i < GAO_DE_AK.size(); i++) { - if (GAO_DE_AK[i] == params.key) { - if (i + 1 >= GAO_DE_AK.size()) { - System.exit(1) - } - keyUsed = new AtomicInteger(i+1) - } - } - break; - } + System.exit(1) } else if (json && json.infocode && json.infocode == "10004") { // 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。 Thread.sleep(60000) @@ -128,7 +126,7 @@ class GaoDeUtils { */ static List searchNearDtoByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){ def params = [:] - params.key = GAO_DE_AK[keyUsed.intValue()] + params.key = this.getGaodeKey() if (types) { params.types = types @@ -158,26 +156,14 @@ class GaoDeUtils { ex.printStackTrace() } } else if (json && json.infocode && json.infocode == "10044") { - synchronized (this) { - for (int i = 0; i < GAO_DE_AK.size(); i++) { - if (GAO_DE_AK[i] == params.key) { - if (i + 1 >= GAO_DE_AK.size()) { - System.exit(1) - } - keyUsed = new AtomicInteger(i+1) - } - } - break; - } + System.exit(1) } else if (json && json.infocode && json.infocode == "10004") { // 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。 Thread.sleep(60000) break; } else if (json && json.infocode && json.infocode == "10017") { // 所请求的资源不可用 - //System.exit(1) - Thread.sleep(60000) - break; + System.exit(1) } else { break } @@ -225,7 +211,7 @@ class GaoDeUtils { */ static List getAmapById(id){ def params = [:] - params.key = GAO_DE_AK[keyUsed.intValue()] + params.key = this.getGaodeKey() params.id = id params.output = 'json' JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); @@ -240,23 +226,13 @@ class GaoDeUtils { ex.printStackTrace() } } else if (json && json.infocode && json.infocode == "10044") { - synchronized (this) { - for (int i = 0; i < GAO_DE_AK.size(); i++) { - if (GAO_DE_AK[i] == params.key) { - if (i + 1 >= GAO_DE_AK.size()) { - System.exit(1) - } - keyUsed = new AtomicInteger(i+1) - } - } - } + System.exit(1) } else if (json && json.infocode && json.infocode == "10004") { // 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。 Thread.sleep(60000) } else if (json && json.infocode && json.infocode == "10017") { // 所请求的资源不可用 - //System.exit(1) - Thread.sleep(60000) + System.exit(1) } return Collections.emptyList() } @@ -266,7 +242,7 @@ class GaoDeUtils { */ static List getAmapDetailById(id){ def params = [:] - params.key = GAO_DE_AK[keyUsed.intValue()] + params.key = this.getGaodeKey() params.id = id params.output = 'json' JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); @@ -281,23 +257,13 @@ class GaoDeUtils { ex.printStackTrace() } } else if (json && json.infocode && json.infocode == "10044") { - synchronized (this) { - for (int i = 0; i < GAO_DE_AK.size(); i++) { - if (GAO_DE_AK[i] == params.key) { - if (i + 1 >= GAO_DE_AK.size()) { - System.exit(1) - } - keyUsed = new AtomicInteger(i+1) - } - } - } + System.exit(1) } else if (json && json.infocode && json.infocode == "10004") { // 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。 Thread.sleep(60000) } else if (json && json.infocode && json.infocode == "10017") { // 所请求的资源不可用 - //System.exit(1) - Thread.sleep(60000) + System.exit(1) } return Collections.emptyList() } @@ -307,7 +273,7 @@ class GaoDeUtils { */ static List searchPoi(String city, String keywords, String region, String typecodes){ def params = [:] - params.key = GAO_DE_AK[keyUsed.intValue()] + params.key = this.getGaodeKey() params.keywords = keywords params.city = city params.region = region @@ -337,17 +303,7 @@ class GaoDeUtils { ex.printStackTrace() } } else if (json && json.infocode && json.infocode == "10044") { - synchronized (this) { - for (int i = 0; i < GAO_DE_AK.size(); i++) { - if (GAO_DE_AK[i] == params.key) { - if (i + 1 >= GAO_DE_AK.size()) { - System.exit(1) - } - keyUsed = new AtomicInteger(i+1) - } - } - break; - } + System.exit(1) } else if (json && json.infocode && (json.infocode == "10004" || json.infocode == "10021" || json.infocode == "10017")) { // 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。 Thread.sleep(60000) @@ -377,7 +333,7 @@ class GaoDeUtils { */ static def getGaoDeAddress(location) { Map params = new HashMap() - params.put("key", GAO_DE_AK[keyUsed.intValue()]) + params.put("key", this.getGaodeKey()) params.put("location", location) String content try { @@ -393,16 +349,7 @@ class GaoDeUtils { return regeocode.formatted_address; } } else if (json && json.infocode && json.infocode == "10044") { - synchronized (this) { - for (int i = 0; i < GAO_DE_AK.size(); i++) { - if (GAO_DE_AK[i] == params.key) { - if (i + 1 >= GAO_DE_AK.size()) { - System.exit(1) - } - keyUsed = new AtomicInteger(i+1) - } - } - } + System.exit(1) } else if (json && json.infocode && json.infocode == "10004") { // 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。 Thread.sleep(60000) @@ -418,7 +365,7 @@ class GaoDeUtils { */ static def parserByGaode(def city, def address) { Map params = new HashMap() - params.put("key", GAO_DE_AK[keyUsed.intValue()]) + params.put("key", this.getGaodeKey()) params.put("city", city) params.put("address", address) String content @@ -437,16 +384,7 @@ class GaoDeUtils { return ["lng": arrs[0], "lat": arrs[1]] } } else if (json && json.infocode && json.infocode == "10044") { - synchronized (this) { - for (int i = 0; i < GAO_DE_AK.size(); i++) { - if (GAO_DE_AK[i] == params.key) { - if (i + 1 >= GAO_DE_AK.size()) { - System.exit(1) - } - keyUsed = new AtomicInteger(i+1) - } - } - } + System.exit(1) } else if (json && json.infocode && json.infocode == "10004") { // 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。 Thread.sleep(60000) From 6d611ff6b6943387c1afd4280a37f6c6d46e89ca Mon Sep 17 00:00:00 2001 From: Jian Shen Date: Mon, 18 Jul 2022 15:55:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96key=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy index 042e2ea23..8a94fe7ed 100644 --- a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy @@ -55,7 +55,7 @@ class GaoDeUtils { * @return */ static String getGaodeKey() { - JSONObject json = HttpRequestUtil.getJsonContent("http://192.168.0.100:38080/FangJiaDataService/getAmapKey") + JSONObject json = HttpRequestUtil.getJsonContent("http://192.168.0.100:38080/FangJiaDataService/getAmapKey", new HashMap()) if (json.status == 0 && json.result != null) { return json.result } From b86fb651ed12834be3839a0d88c753ab308c02f9 Mon Sep 17 00:00:00 2001 From: Jian Shen Date: Mon, 18 Jul 2022 16:56:27 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96key=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../temp/shenjian/util/GaoDeUtils.groovy | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy index 8a94fe7ed..2a2972c21 100644 --- a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/GaoDeUtils.groovy @@ -54,8 +54,10 @@ class GaoDeUtils { * 获取高德KEY * @return */ - static String getGaodeKey() { - JSONObject json = HttpRequestUtil.getJsonContent("http://192.168.0.100:38080/FangJiaDataService/getAmapKey", new HashMap()) + static String getGaodeKey(String type) { + Map params = new HashMap<>(); + params.put("type", type) + JSONObject json = HttpRequestUtil.getJsonContent("http://192.168.0.100:38080/FangJiaDataService/getAmapKey", params) if (json.status == 0 && json.result != null) { return json.result } @@ -74,7 +76,7 @@ class GaoDeUtils { */ static List searchNearByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){ def params = [:] - params.key = this.getGaodeKey() + if (types) { params.types = types @@ -91,8 +93,10 @@ class GaoDeUtils { while (result.size() < count) { params.page = "${page}".toString() page++ + params.key = this.getGaodeKey("v3/place/around") JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/around", params); if (json == null) { + params.key = this.getGaodeKey("v3/place/around") json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/around", params); } if (json && json.info && json.info == "OK") { @@ -126,7 +130,6 @@ class GaoDeUtils { */ static List searchNearDtoByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){ def params = [:] - params.key = this.getGaodeKey() if (types) { params.types = types @@ -143,8 +146,10 @@ class GaoDeUtils { while (result.size() < count) { params.page = "${page}".toString() page++ + params.key = this.getGaodeKey("v5/place/around") JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/around", params); if (json == null) { + params.key = this.getGaodeKey("v5/place/around") json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/around", params); } if (json && json.info && json.info == "OK") { @@ -211,11 +216,12 @@ class GaoDeUtils { */ static List getAmapById(id){ def params = [:] - params.key = this.getGaodeKey() params.id = id params.output = 'json' + params.key = this.getGaodeKey("v3/place/detail") JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); if (json == null) { + params.key = this.getGaodeKey("v3/place/detail") json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); } if (json && json.info && json.info == "OK") { @@ -242,11 +248,12 @@ class GaoDeUtils { */ static List getAmapDetailById(id){ def params = [:] - params.key = this.getGaodeKey() params.id = id params.output = 'json' + params.key = this.getGaodeKey("v3/place/detail") JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); if (json == null) { + params.key = this.getGaodeKey("v3/place/detail") json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); } if (json && json.info && json.info == "OK") { @@ -273,7 +280,6 @@ class GaoDeUtils { */ static List searchPoi(String city, String keywords, String region, String typecodes){ def params = [:] - params.key = this.getGaodeKey() params.keywords = keywords params.city = city params.region = region @@ -290,8 +296,10 @@ class GaoDeUtils { while (result.size() < count) { params.page = "${page}".toString() page++ + params.key = this.getGaodeKey("v5/place/text") JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/text", params); if (json == null) { + params.key = this.getGaodeKey("v5/place/text") json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/text", params); } if (json && json.info && json.info == "OK") { @@ -333,12 +341,13 @@ class GaoDeUtils { */ static def getGaoDeAddress(location) { Map params = new HashMap() - params.put("key", this.getGaodeKey()) params.put("location", location) String content try { + params.put("key", this.getGaodeKey("v3/geocode/regeo")) content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8") } catch (Exception e) { + params.put("key", this.getGaodeKey("v3/geocode/regeo")) content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8") } net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(content) @@ -365,13 +374,14 @@ class GaoDeUtils { */ static def parserByGaode(def city, def address) { Map params = new HashMap() - params.put("key", this.getGaodeKey()) params.put("city", city) params.put("address", address) String content try { + params.put("key", this.getGaodeKey("v3/geocode/geo")) content = new HttpRequest().get("https://restapi.amap.com/v3/geocode/geo", params, "utf-8") } catch (Exception e) { + params.put("key", this.getGaodeKey("v3/geocode/geo")) content = new HttpRequest().get("https://restapi.amap.com/v3/geocode/geo", params, "utf-8") } net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(content) @@ -393,4 +403,8 @@ class GaoDeUtils { System.exit(1) } } + + static void main(String[] args) { + println(this.getGaodeKey("v3/geocode/geo")) + } }