Browse Source

提交保存

xusonglin 3 years ago
parent
commit
483e1abcfb

+ 10 - 2
src/main/java/com/jkcredit/info/query/controller/InfoQueryController.java

@@ -24,11 +24,19 @@ public class InfoQueryController {
 
     @PostMapping("/query")
     public CommonResponseObject infoQuery(@RequestBody JSONObject body) {
-        log.info("query");
-        System.out.println("query");
         return informationQueryService.query(body.getString("keyWord"), body.getString("type"));
     }
 
+    @PostMapping("/queryInfoList")
+    public CommonResponseObject getInfoList(@RequestBody JSONObject body) {
+        return informationQueryService.queryList(body.getString("keyWord"), body.getString("type"));
+    }
+
+    @PostMapping("/queryInfoDetail")
+    public CommonResponseObject queryDetail(@RequestBody JSONObject body) {
+        return informationQueryService.queryDetail(body.getString("path"));
+    }
+
     @PostMapping("/loadCookie")
     public CommonResponseObject loadCookie() {
         return informationQueryService.loadCookie();

+ 36 - 0
src/main/java/com/jkcredit/info/query/handler/GlobalExceptionHandler.java

@@ -0,0 +1,36 @@
+package com.jkcredit.info.query.handler;
+
+import com.jkcredit.common.model.ResultObject;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletRequest;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.UUID;
+
+/**
+ * @author xusonglin
+ * @version V1.0
+ **/
+@RestControllerAdvice(annotations = RestController.class)
+@Slf4j
+public class GlobalExceptionHandler {
+    @ExceptionHandler(value = Exception.class)
+    @ResponseBody
+    public ResultObject errorHandler(HttpServletRequest req, Exception ex) {
+        String requestId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())
+                + UUID.randomUUID().toString();
+        ResultObject resultObject = new ResultObject();
+        resultObject.setCode(500);
+        resultObject.setMessage("failed");
+        resultObject.setRequestId(requestId);
+        resultObject.setResult(new ArrayList<>());
+        log.error("requestId:{}, exception:{}", requestId, ex.getMessage());
+        return resultObject;
+    }
+}

+ 4 - 0
src/main/java/com/jkcredit/info/query/service/InformationQueryService.java

@@ -13,4 +13,8 @@ public interface InformationQueryService {
     CommonResponseObject loadCookie();
 
     CommonResponseObject getNewCookie();
+
+    CommonResponseObject queryList(String keyWord, String type);
+
+    CommonResponseObject queryDetail(String path);
 }

+ 132 - 0
src/main/java/com/jkcredit/info/query/service/impl/InformationQueryServiceImpl.java

@@ -114,6 +114,138 @@ public class InformationQueryServiceImpl implements InformationQueryService {
         }
     }
 
+    @Override
+    public CommonResponseObject queryList(String keyWord, String type) {
+        String requestId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())
+                + UUID.randomUUID().toString();
+        log.info("keyWord:{}, type:{}, requestId:{}", keyWord, type, requestId);
+        long startTime = System.currentTimeMillis();
+        String cookie = CookieUtil.getAuthenticationCookie();
+        if (StringUtils.isBlank(cookie)) {
+            CommonResponseObject result = CommonResponseObject.failed("查询失败",
+                    CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
+            log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
+                    , keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
+            return result;
+        }
+        Map<String, JSONObject> catalogIdMap = CatalogConcurrentHashMapUtil.getInstance();
+        if (catalogIdMap.size() == 0) {
+            // 查询节点id
+            FormBody formBody = new FormBody.Builder()
+                    .add("pinyin", "false")
+                    .add("keyWord", keyWord)
+                    .build();
+            String catalogContent = HttpUtil.doPost(catalogUrl, formBody, cookie);
+            catalogIdMap = getCatalogIds(catalogContent);
+            CatalogConcurrentHashMapUtil.getInstance().putAll(catalogIdMap);
+        }
+
+        String id = "";
+        String queryId = "";
+        for (Map.Entry<String, JSONObject> entry : catalogIdMap.entrySet()) {
+            String catalogName = entry.getKey();
+            if (type.equals(catalogName)) {
+                JSONObject catalogIdObject = entry.getValue();
+                queryId = catalogIdObject.getString("queryId");
+                id = catalogIdObject.getString("id");
+            } else {
+                continue;
+            }
+        }
+        String encodeKeyWord = "";
+        try {
+            encodeKeyWord = URLEncoder.encode(keyWord, "utf-8");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        StringBuffer stringBuffer = new StringBuffer(queryResultUrl);
+        stringBuffer.append("?pinyin=false");
+        stringBuffer.append("&keyWord=").append(encodeKeyWord);
+        stringBuffer.append("&reportQuery.id=").append(queryId);
+        stringBuffer.append("&resource.id=").append(id);
+        String resultContent = HttpUtil.doGet(stringBuffer.toString(), cookie);
+        List<Map<String, String>> listInfo = getListInfo(type, resultContent);
+
+        if (listInfo.size() != 0) {
+            CommonResponseObject result = CommonResponseObject.success(listInfo, getLocalHost(), requestId);
+            log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
+                    , keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
+            return result;
+        } else {
+            CommonResponseObject result = CommonResponseObject.noResult(getLocalHost(), requestId);
+            log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
+                    , keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
+            return result;
+        }
+    }
+
+    @Override
+    public CommonResponseObject queryDetail(String path) {
+        long startTime = System.currentTimeMillis();
+        String requestId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())
+                + UUID.randomUUID().toString();
+        log.info("path:{}, requestId:{}", path, requestId);
+        if (!path.contains("../")) {
+            CommonResponseObject result = CommonResponseObject.noResult(getLocalHost(), requestId);
+            log.info("path:{}, requestId:{}, result:{}, costTime:{}"
+                    , path, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
+            return result;
+        }
+        List<Map<String, String>> results = getDetail(path);
+        if (results.size() != 0) {
+            CommonResponseObject result = CommonResponseObject.success(results, getLocalHost(), requestId);
+            log.info("path:{}, requestId:{}, result:{}, costTime:{}"
+                    , path, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
+            return result;
+        } else {
+            CommonResponseObject result = CommonResponseObject.noResult(getLocalHost(), requestId);
+            log.info("path:{}, requestId:{}, result:{}, costTime:{}"
+                    , path, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
+            return result;
+        }
+    }
+
+    private List<Map<String, String>> getListInfo(String type, String content) {
+        List<Map<String, String>> results = new ArrayList<>();
+
+        Document doc = Jsoup.parse(content);
+        Elements elementList = doc.select("td").select(".column");
+        for (Element element : elementList) {
+            Elements tdList = element.select(".kyfuse-form-value");
+            Map<String, String> tdMap = new HashMap<>();
+            for (Element td : tdList) {
+                String dataName = td.attr("data-name");
+                String dataValue = td.attr("data-value");
+                tdMap.put(dataName, dataValue);
+            }
+            results.add(tdMap);
+            if (type.equals("stolenVehicleInfo")) {
+                tdMap.put("detailPath", element.select("a").eachAttr("url").get(1));
+            } else {
+                tdMap.put("detailPath", element.select("a").attr("url"));
+            }
+        }
+        return results;
+    }
+
+    private List<Map<String, String>> getDetail(String path) {
+        List<Map<String, String>> results = new ArrayList<>();
+        String url = path.replace("../", hostUrl).replace("amp;", "");
+        String detailInfoContent = HttpUtil.doGet(url, CookieUtil.getAuthenticationCookie());
+        Document detailDoc = Jsoup.parse(detailInfoContent);
+        Elements elements = detailDoc.select(".detail-info-list");
+        Map<String, String> detailMap = new HashMap<>();
+        for (int i = 0; i < elements.size(); i++) {
+            String dataName = detailDoc.select(".detail-info-list").get(i).select("span").get(1).attr("data-name");
+            String dataValue = detailDoc.select(".detail-info-list").get(i).select("span").get(1).text();
+            detailMap.put(dataName, dataValue);
+        }
+        if (detailMap.size() != 0) {
+            results.add(detailMap);
+        }
+        return results;
+    }
+
     // 处理列表多条返回的情况
     private List<Map<String, String>> getDetailInfo(String content) {
         List<Map<String, String>> results = new ArrayList<>();

+ 31 - 2
src/main/java/com/jkcredit/info/query/task/LoadCookieTask.java

@@ -1,10 +1,17 @@
 package com.jkcredit.info.query.task;
 
+import com.jkcredit.info.query.util.CookieTaskConcurrentHashMapUtil;
 import com.jkcredit.info.query.util.CookieUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+import java.util.Random;
+
 /**
  * @author xusonglin
  * @version V1.0
@@ -12,8 +19,30 @@ import org.springframework.stereotype.Component;
 @Slf4j
 @Component
 public class LoadCookieTask {
-    @Scheduled(cron = "0 0/30 * * * ?", zone = "Asia/Shanghai")
+    @Scheduled(cron = "0 0/3 * * * ?", zone = "Asia/Shanghai")
     public void loadCookie() {
-        CookieUtil.getNewCookie();
+        long baseTime = 1080000;
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Map<String, Long> cookieTaskMap = CookieTaskConcurrentHashMapUtil.getInstance();
+        if (cookieTaskMap.size() == 0) {
+            String cookie = CookieUtil.getNewCookie();
+            if (StringUtils.isNotBlank(cookie)) {
+                int randomNum = new Random().nextInt(600000);
+                CookieTaskConcurrentHashMapUtil.getInstance().put("cookie", System.currentTimeMillis() + baseTime + randomNum);
+                log.info("LoadCookieTask.LoadCookie:{}", simpleDateFormat.format(new Date(System.currentTimeMillis() + baseTime + randomNum)));
+            }
+        } else {
+            long cookieTime = CookieTaskConcurrentHashMapUtil.getInstance().get("cookie");
+            long nowTime = System.currentTimeMillis();
+            if (cookieTime - nowTime <= 180000) {
+                // 立即执行
+                String cookie = CookieUtil.getNewCookie();
+                if (StringUtils.isNotBlank(cookie)) {
+                    int randomNum = new Random().nextInt(600000);
+                    CookieTaskConcurrentHashMapUtil.getInstance().put("cookie", System.currentTimeMillis() + baseTime + randomNum);
+                    log.info("LoadCookieTask.LoadCookie:{}", simpleDateFormat.format(new Date(System.currentTimeMillis() + baseTime + randomNum)));
+                }
+            }
+        }
     }
 }

+ 20 - 0
src/main/java/com/jkcredit/info/query/util/CookieTaskConcurrentHashMapUtil.java

@@ -0,0 +1,20 @@
+package com.jkcredit.info.query.util;
+
+import okhttp3.Cookie;
+
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author xusonglin
+ * @version V1.0
+ **/
+public enum CookieTaskConcurrentHashMapUtil {
+    INSTANCE;
+
+    public static ConcurrentHashMap<String, Long> concurrentHashMap = new ConcurrentHashMap<>();
+
+    public static ConcurrentHashMap<String, Long> getInstance() {
+        return concurrentHashMap;
+    }
+}

+ 0 - 1
src/main/java/com/jkcredit/info/query/util/CookieUtil.java

@@ -40,7 +40,6 @@ public class CookieUtil {
 
     public static String getNewCookie() {
         String responseContext = HttpUtil.doGetLoadCookie(cookieUrl);
-        log.info("getNewCookie:{}", responseContext);
         if (StringUtils.isNotBlank(responseContext)) {
             HttpUtil.doGet(authenticationCookieUrl, getCookie());
             return getCookie();

+ 10 - 6
src/main/java/com/jkcredit/info/query/util/HttpUtil.java

@@ -26,8 +26,8 @@ public class HttpUtil {
                 .build();
         OkHttpClient client = okHttpClient.newBuilder()
                 .connectTimeout(3000, TimeUnit.MILLISECONDS)
-                .readTimeout(3000, TimeUnit.MILLISECONDS)
-                .writeTimeout(3000, TimeUnit.MILLISECONDS)
+                .readTimeout(6000, TimeUnit.MILLISECONDS)
+                .writeTimeout(6000, TimeUnit.MILLISECONDS)
                 .sslSocketFactory(SSLSocketClient.getSocketFactory(), SSLSocketClient.getX509TrustManager())
                 .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                 .build();
@@ -40,6 +40,8 @@ public class HttpUtil {
             response.close();
         } catch (IOException ioe) {
             log.error("doPost.IOException:", ioe);
+        } catch (Exception e) {
+            log.error("doPost.Exception:", e);
         }
         return responseContext;
     }
@@ -51,8 +53,8 @@ public class HttpUtil {
 
         OkHttpClient client = okHttpClient.newBuilder()
                 .connectTimeout(3000, TimeUnit.MILLISECONDS)
-                .readTimeout(3000, TimeUnit.MILLISECONDS)
-                .writeTimeout(3000, TimeUnit.MILLISECONDS)
+                .readTimeout(6000, TimeUnit.MILLISECONDS)
+                .writeTimeout(6000, TimeUnit.MILLISECONDS)
                 .sslSocketFactory(SSLSocketClient.getSocketFactory(), SSLSocketClient.getX509TrustManager())
                 .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                 .build();
@@ -65,6 +67,8 @@ public class HttpUtil {
             response.close();
         } catch (IOException ioe) {
             log.error("doGet.IOException:", ioe);
+        } catch (Exception e) {
+            log.error("doGet.Exception:", e);
         }
         return responseContext;
     }
@@ -76,8 +80,8 @@ public class HttpUtil {
 
         OkHttpClient client = okHttpClient.newBuilder()
                 .connectTimeout(3000, TimeUnit.MILLISECONDS)
-                .readTimeout(3000, TimeUnit.MILLISECONDS)
-                .writeTimeout(3000, TimeUnit.MILLISECONDS)
+                .readTimeout(6000, TimeUnit.MILLISECONDS)
+                .writeTimeout(6000, TimeUnit.MILLISECONDS)
                 .cookieJar(new CookieJar() {
                     @Override
                     public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {

+ 6 - 6
src/main/resources/application.yml

@@ -17,12 +17,12 @@ supplier:
   queryResultUrl: https://jwzhcx.ga/cloudSearch/ajaxQueryResult.action
   hostUrl: https://jwzhcx.ga/
 #supplier:
-#  cookieUrl: http://51.1.0.61:28087/sevenRule.action
-#  authenticationCookieUrl: http://51.1.0.61:28087/pkiLogin.action
-#  mainPageRequestUrl: http://51.1.0.61:28087/cloudSearch/goSearch.action
-#  catalogUrl: http://51.1.0.61:28087/cloudSearch/goSearch.action
-#  queryResultUrl: http://51.1.0.61:28087/cloudSearch/ajaxQueryResult.action
-#  hostUrl: http://51.1.0.61:28087/
+#  cookieUrl: http://127.0.0.1:28087/sevenRule.action
+#  authenticationCookieUrl: http://127.0.0.1:28087/pkiLogin.action
+#  mainPageRequestUrl: http://127.0.0.1:28087/cloudSearch/goSearch.action
+#  catalogUrl: http://127.0.0.1:28087/cloudSearch/goSearch.action
+#  queryResultUrl: http://127.0.0.1:28087/cloudSearch/ajaxQueryResult.action
+#  hostUrl: http://127.0.0.1:28087/
 thread:
   pool:
     coreSize: 10