diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddrSvcController.java b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddrSvcController.java index e7dc05ae8..a12b7d38d 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddrSvcController.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddrSvcController.java @@ -14,11 +14,30 @@ import com.fangjia.open.util.CommonDtoUtils; import com.fangjia.service.bo.DistrictStatBo; import com.fangjia.service.datastore.DistrictService; import com.fangjia.service.district.DistrictStatService; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import sun.misc.BASE64Encoder; +import java.io.IOException; +import java.net.Authenticator; +import java.net.InetSocketAddress; +import java.net.PasswordAuthentication; +import java.net.Proxy; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; +import java.util.Stack; /** * 地址治理 @@ -358,8 +377,22 @@ public class AddrSvcController { return mav; } + private static final Map areaMap = new HashMap() { + { + // 添加键值对到 Map 中 + areaMap.put("111", "主城区"); + areaMap.put("112", "城乡结合区"); + areaMap.put("121", "镇中心区"); + areaMap.put("122", "镇乡结合区"); + areaMap.put("123", "特殊区域"); + areaMap.put("210", "村庄"); + } + }; + + + /** - * 涉农识别-新规则 + * 涉农识别-新规则-南京银行 * * @param model * @return @@ -372,6 +405,175 @@ public class AddrSvcController { return mav; } + HttpRequest request = new HttpRequest(); + JSONObject json = null; + Map params = new HashMap(); + params.put("city", StringUtils.isNotBlank(model.getCity()) ? model.getCity() : null); + String address = model.getAddress(); + params.put("address", address); + String type = StringUtils.isNotBlank(model.getType()) ? model.getType() : null; + params.put("type", type); + params.put("showFullValue", StringUtils.isNotBlank(model.getShowFullValue()) ? model.getShowFullValue() : null); + params.put("showTestValue", StringUtils.isNotBlank(model.getShowTestValue()) ? model.getShowTestValue() : null); + Integer radius = model.getRadius(); + params.put("radius", (radius != null && radius != 0) ? radius.toString() : null); + try { + json = request.getJsonContent("http://192.168.0.100:32080/FangJiaAddressService/villageIdentify", params, "UTF-8"); + } catch (Exception e) { + try { + json = request.getJsonContent("http://192.168.0.100:32080/FangJiaAddressService/villageIdentify", params, "UTF-8"); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + if (json != null) { + int code = json.getInt("msgCode"); + String msg = json.getString("msg"); + BasicModel basicModel = new BasicModel(code, msg); + if (json.containsKey("result")) { + //我们的结果 + JSONObject result = json.getJSONObject("result"); + try { + //如果是江苏省或者浙江省 调用齐梁的结果 + if ("江苏省".equals(result.getString("province")) || "浙江省".equals(result.getString("province"))) { + + //结果比较,如果涉农结果不一致 + //调用齐梁 type :1 对公 0 对私 + //我们 type :1 对私 2 对共 + String qltype = "2".equals(type) ? "1" : "0"; + Map map = senRequest(address, qltype); + // 如果涉农结果不一样,用齐梁结果 + if (!("0".equals(map.get("farmer")) && "非农".equals(result.get("verifyFlag")))||!("1".equals(map.get("farmer")) && "农".equals(result.get("verifyFlag")))){ + map.put("addrVerify", result.getString("addrVerify")); + map.put("typeName",areaMap.get(map.get("typeCode"))); + } + basicModel.setResult(map); + + }else { + basicModel.setResult(result); + + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + mav.setModel(basicModel); + } else { + mav.setModel(BasicModel.buildServerErrorModel().setMsg("服务升级中...")); + } + return mav; + } + + private static Map senRequest(String address, String type) throws IOException { + String targetURL = "http://146.56.217.44:9010/uams/farmer/all/v3/sign"; + String proxyHost = "tunnel2.qg.net"; + int proxyPort = 14186; + final String proxyUsername = "113BBC06"; + final String proxyPassword = "3B2FBD05B0AF"; + String jsonData = "{\"address\":\"" + address + "\",\"type\":\"" + type + "\"}"; + String authKey = proxyUsername; + String password = proxyPassword; + String auth = authKey + ":" + password; + String authHeader = "Basic " + new BASE64Encoder().encode(auth.getBytes(Charset.forName("UTF-8"))); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(targetURL); + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); + Authenticator.setDefault(new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray()); + } + }); + httpPost.setHeader("Content-Type", "application/json"); + httpPost.setHeader("Accept", "*/*"); + httpPost.setHeader("Host", "146.56.217.44:9010"); + httpPost.setHeader("Proxy-Authorization", authHeader); + StringEntity stringEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON); + httpPost.setEntity(stringEntity); + + CloseableHttpResponse httpResponse = null; + int retryCount = 2; // 设置重试次数 + int retryDelay = 10; // 设置重试延迟时间(毫秒) + boolean success = false; + for (int i = 0; i < retryCount; i++) { + try { + httpResponse = httpClient.execute(httpPost); + success = true; + break; + } catch (IOException e) { + System.err.println("代理请求失败,进行重试..."); + try { + Thread.sleep(retryDelay); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + } + + if (success) { + HttpEntity responseEntity = httpResponse.getEntity(); + String responseString = EntityUtils.toString(responseEntity); + + // 使用 Gson 解析 JSON 字符串 + Gson gson = new Gson(); + JsonObject jsonObject = gson.fromJson(responseString, JsonObject.class); + + + // 创建 Map 存储各个值 + Map dataMap = new HashMap(); + + // 获取各个值并放入 Map +// dataMap.put("code", jsonObject.get("code").getAsString()); +// dataMap.put("msg", jsonObject.get("msg").getAsString()); + JsonObject data = jsonObject.getAsJsonObject("data"); +// dataMap.put("version", data.get("version").getAsString()); + dataMap.put("province", data.get("province").getAsString()); + dataMap.put("city", data.get("city").getAsString()); + dataMap.put("district", data.get("district").getAsString()); + dataMap.put("town", data.get("town").getAsString()); + dataMap.put("committee", data.get("village").getAsString()); + dataMap.put("provinceCode", data.get("provinceCode").getAsString()); + dataMap.put("cityCode", data.get("cityCode").getAsString()); + dataMap.put("districtCode", data.get("districtCode").getAsString()); + dataMap.put("townCode", data.get("townCode").getAsString()); + dataMap.put("committeeCode", data.get("villageCode").getAsString()); + dataMap.put("typeCode", data.get("areaCode").getAsString()); + dataMap.put("verifyFlag", "0".equals(data.get("farmer").getAsString())?"非农":"农"); + dataMap.put("countyGov", "0".equals(data.get("isGovt").getAsString())?"否":"是"); +// dataMap.put("govtAddr", data.get("govtAddr").isJsonNull() ? null : data.get("govtAddr").getAsString()); // 对于可能为 null 的值,需要判断是否为空 + JsonArray indusParkList = data.get("indusParkList").isJsonNull() ? null : data.get("indusParkList").getAsJsonArray(); + if (indusParkList != null) { + JsonObject jsonObject1 = gson.fromJson(indusParkList.get(0), JsonObject.class); + dataMap.put("devDistrictName", jsonObject1.get("indusParkName").getAsString()); + dataMap.put("devDistrict", "是"); + } + + httpResponse.close(); + return dataMap; + } else { + System.err.println("重试次数已用完,请求失败。"); + } + + httpClient.close(); + return null; + } + + /** + * 涉农识别-新规则-标准版 + * + * @param model + * @return + */ + public Object villageIdentifyStandardAction(AddressModel model) { + + RestfulModelAndView mav = new RestfulModelAndView(); + if (StringUtils.isBlank(model.getAddress())) { + mav.setModel(BasicModel.buildBadRequestModel("地址不能为空")); + return mav; + } + HttpRequest request = new HttpRequest(); JSONObject json = null; Map params = new HashMap(); diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/Test.java b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/Test.java new file mode 100644 index 000000000..e03484f3e --- /dev/null +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/Test.java @@ -0,0 +1,190 @@ +//package com.fangjia.open.controllers; +// +//import com.google.gson.Gson; +//import com.google.gson.JsonArray; +//import com.google.gson.JsonObject; +//import org.apache.http.HttpEntity; +//import org.apache.http.client.methods.CloseableHttpResponse; +//import org.apache.http.client.methods.HttpPost; +//import org.apache.http.entity.ContentType; +//import org.apache.http.entity.StringEntity; +//import org.apache.http.impl.client.CloseableHttpClient; +//import org.apache.http.impl.client.HttpClients; +//import org.apache.http.util.EntityUtils; +//import sun.misc.BASE64Encoder; +// +//import java.io.IOException; +//import java.net.Authenticator; +//import java.net.InetSocketAddress; +//import java.net.PasswordAuthentication; +//import java.net.Proxy; +//import java.nio.charset.Charset; +//import java.nio.charset.StandardCharsets; +//import java.util.Base64; +//import java.util.HashMap; +//import java.util.Map; +// +////public class Test { +//// public static void main(String[] args) throws IOException { +//// // 目标URL +//// String targetURL = "http://146.56.217.44:9010/uams/farmer/all/v3/sign"; +//// +//// // 设置代理 +//// String proxyHost = "tunnel2.qg.net"; +//// int proxyPort = 14186; +//// +//// // 设置代理用户名和密码 +//// final String proxyUsername = "113BBC06"; +//// final String proxyPassword = "3B2FBD05B0AF"; +//// +//// // 设置请求的JSON数据 +//// String address = "江苏省扬州市宝应县安宜镇宝应大道88号"; +//// String type = "1"; +//// String jsonData = "{\"address\":\"" + address + "\",\"type\":\"" + type + "\"}"; +//// +//// // 设置HTTP基本认证 +//// String authKey = proxyUsername; +//// String password = proxyPassword; +//// String auth = authKey + ":" + password; +//// String authHeader = "Basic " + new BASE64Encoder().encode(auth.getBytes(Charset.forName("UTF-8"))); +//// +//// // 创建HTTP客户端 +//// CloseableHttpClient httpClient = HttpClients.createDefault(); +//// HttpPost httpPost = new HttpPost(targetURL); +//// +//// // 设置代理 +//// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); +//// +//// // 设置代理认证 +//// Authenticator.setDefault(new Authenticator() { +//// protected PasswordAuthentication getPasswordAuthentication() { +//// return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray()); +//// } +//// }); +//// +//// // 设置请求头 +//// httpPost.setHeader("Content-Type", "application/json"); +//// httpPost.setHeader("Accept", "*/*"); +//// httpPost.setHeader("Host", "146.56.217.44:9010"); +//// httpPost.setHeader("Proxy-Authorization", authHeader); +//// +//// // 设置请求体 +//// StringEntity stringEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON); +//// httpPost.setEntity(stringEntity); +//// +//// // 发送POST请求 +//// CloseableHttpResponse httpResponse = httpClient.execute(httpPost); +//// +//// // 获取响应 +//// HttpEntity responseEntity = httpResponse.getEntity(); +//// String responseString = EntityUtils.toString(responseEntity); +//// System.out.println(responseString); +//// +//// // 关闭连接 +//// httpResponse.close(); +//// httpClient.close(); +//// } +////} +// +//public class Test { +// public static void main(String[] args) throws IOException { +// Map map = senRequest("江苏省泰州市靖江市通江路37号", "1"); +// System.out.println(map); +// +// } +// +// private static Map senRequest(String address, String type) throws IOException { +// String targetURL = "http://146.56.217.44:9010/uams/farmer/all/v3/sign"; +// String proxyHost = "tunnel2.qg.net"; +// int proxyPort = 14186; +// final String proxyUsername = "113BBC06"; +// final String proxyPassword = "3B2FBD05B0AF"; +// String jsonData = "{\"address\":\"" + address + "\",\"type\":\"" + type + "\"}"; +// String authKey = proxyUsername; +// String password = proxyPassword; +// String auth = authKey + ":" + password; +// String authHeader = "Basic " + new BASE64Encoder().encode(auth.getBytes(Charset.forName("UTF-8"))); +// +// CloseableHttpClient httpClient = HttpClients.createDefault(); +// HttpPost httpPost = new HttpPost(targetURL); +// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); +// Authenticator.setDefault(new Authenticator() { +// protected PasswordAuthentication getPasswordAuthentication() { +// return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray()); +// } +// }); +// httpPost.setHeader("Content-Type", "application/json"); +// httpPost.setHeader("Accept", "*/*"); +// httpPost.setHeader("Host", "146.56.217.44:9010"); +// httpPost.setHeader("Proxy-Authorization", authHeader); +// StringEntity stringEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON); +// httpPost.setEntity(stringEntity); +// +// CloseableHttpResponse httpResponse = null; +// int retryCount = 2; // 设置重试次数 +// int retryDelay = 10; // 设置重试延迟时间(毫秒) +// boolean success = false; +// for (int i = 0; i < retryCount; i++) { +// try { +// httpResponse = httpClient.execute(httpPost); +// success = true; +// break; +// } catch (IOException e) { +// System.err.println("代理请求失败,进行重试..."); +// try { +// Thread.sleep(retryDelay); +// } catch (InterruptedException ex) { +// Thread.currentThread().interrupt(); +// } +// } +// } +// +// if (success) { +// HttpEntity responseEntity = httpResponse.getEntity(); +// String responseString = EntityUtils.toString(responseEntity); +// +// // 使用 Gson 解析 JSON 字符串 +// Gson gson = new Gson(); +// JsonObject jsonObject = gson.fromJson(responseString, JsonObject.class); +// +// +// // 创建 Map 存储各个值 +// Map dataMap = new HashMap(); +// +// // 获取各个值并放入 Map +// dataMap.put("code", jsonObject.get("code").getAsString()); +// dataMap.put("msg", jsonObject.get("msg").getAsString()); +// JsonObject data = jsonObject.getAsJsonObject("data"); +// dataMap.put("version", data.get("version").getAsString()); +// dataMap.put("province", data.get("province").getAsString()); +// dataMap.put("city", data.get("city").getAsString()); +// dataMap.put("district", data.get("district").getAsString()); +// dataMap.put("town", data.get("town").getAsString()); +// dataMap.put("village", data.get("village").getAsString()); +// dataMap.put("provinceCode", data.get("provinceCode").getAsString()); +// dataMap.put("cityCode", data.get("cityCode").getAsString()); +// dataMap.put("districtCode", data.get("districtCode").getAsString()); +// dataMap.put("townCode", data.get("townCode").getAsString()); +// dataMap.put("villageCode", data.get("villageCode").getAsString()); +// dataMap.put("areaCode", data.get("areaCode").getAsString()); +// dataMap.put("farmer", data.get("farmer").getAsString()); +// dataMap.put("isGovt", data.get("isGovt").getAsString()); +// dataMap.put("govtAddr", data.get("govtAddr").isJsonNull() ? null : data.get("govtAddr").getAsString()); // 对于可能为 null 的值,需要判断是否为空 +// JsonArray indusParkList = data.get("indusParkList").isJsonNull() ? null : data.get("indusParkList").getAsJsonArray(); +// if (indusParkList != null) { +// JsonObject jsonObject1 = gson.fromJson(indusParkList.get(0), JsonObject.class); +// dataMap.put("devName",jsonObject1.get("indusParkName").getAsString()); +// dataMap.put("devLevel",jsonObject1.get("indusParkLevel").getAsString()); +// } +// +// httpResponse.close(); +// return dataMap; +// } else { +// System.err.println("重试次数已用完,请求失败。"); +// } +// +// httpClient.close(); +// return null; +// } +//} +//