|
|
@@ -2,29 +2,36 @@ package com.jzg.quote.huanong.crawler.component;
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.jzg.commons.exception.SystemException;
|
|
|
import com.jzg.quotation.commons.component.RequestComponent;
|
|
|
import com.jzg.quote.huanong.crawler.constant.enums.RequestUrl;
|
|
|
import com.jzg.quote.huanong.crawler.entity.request.*;
|
|
|
import com.jzg.quote.huanong.crawler.entity.response.*;
|
|
|
import com.jzg.quote.huanong.crawler.properties.HuaNongCrawlerProperties;
|
|
|
import com.jzg.quote.huanong.crawler.utils.RequestEncryptor;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
-import org.springframework.web.reactive.function.BodyInserters;
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
|
|
+import java.net.URI;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* @description: 华农请求基类
|
|
|
* @author: wenks
|
|
|
* @date: 2025/3/20 12:01
|
|
|
**/
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
@RequiredArgsConstructor
|
|
|
public class HuaNongCrawlerRequestComponent {
|
|
|
@@ -136,6 +143,7 @@ public class HuaNongCrawlerRequestComponent {
|
|
|
|
|
|
/**
|
|
|
* 跳转页面链接
|
|
|
+ *
|
|
|
* @param request 跳转页面链接请求
|
|
|
* @param headers 请求头
|
|
|
* @return 跳转页面需要的data
|
|
|
@@ -145,24 +153,70 @@ public class HuaNongCrawlerRequestComponent {
|
|
|
return response.getData();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取 accessToken
|
|
|
+ *
|
|
|
+ * @param data data数据
|
|
|
+ * @param headers 请求头
|
|
|
+ * @return accessToken
|
|
|
+ */
|
|
|
public String bizUpload(String data, HttpHeaders headers) {
|
|
|
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
|
|
|
parts.add("data", data);
|
|
|
- ResponseEntity<String> response = webClient.post()
|
|
|
- .uri(properties.getUploadUrl() + RequestUrl.BIZ_UPLOAD)
|
|
|
- .headers(RequestComponent.setHeaders(headers))
|
|
|
- .contentType(MediaType.MULTIPART_FORM_DATA)
|
|
|
- .body(BodyInserters.fromMultipartData(parts))
|
|
|
- .retrieve()
|
|
|
- .toEntity(String.class)
|
|
|
- .block();
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
+ String url = properties.getUploadUrl() + RequestUrl.BIZ_UPLOAD;
|
|
|
+ ResponseEntity<String> response = requestComponent.formData(url, parts, String.class, headers);
|
|
|
+ String fragment = Optional.ofNullable(response)
|
|
|
+ .map(ResponseEntity::getHeaders)
|
|
|
+ .map(HttpHeaders::getLocation)
|
|
|
+ .map(URI::getFragment)
|
|
|
+ .orElse("");
|
|
|
|
|
|
+ // 解析片段中的查询参数
|
|
|
+ String[] s = fragment.split("\\?");
|
|
|
+ if (s.length > 1) {
|
|
|
+ Map<String, String> params = parseQueryParams(s[1]);
|
|
|
+ return params.get("accessToken");
|
|
|
+ }
|
|
|
+ throw new SystemException("");
|
|
|
+ }
|
|
|
|
|
|
+ private static Map<String, String> parseQueryParams(String query) {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ for (String pair : query.split("&")) {
|
|
|
+ int idx = pair.indexOf("=");
|
|
|
+ if (idx > 0) {
|
|
|
+ params.put(pair.substring(0, idx), pair.substring(idx + 1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return params;
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传影像
|
|
|
+ *
|
|
|
+ * @param headers 请求头
|
|
|
+ * @return 上传影像响应
|
|
|
+ */
|
|
|
+ public HuaNongCrawlerFileUploadResponse fileUpload(HttpHeaders headers) {
|
|
|
+ MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
|
|
|
+ String url = properties.getUploadUrl() + RequestUrl.FILE_UPLOAD;
|
|
|
+ ResponseEntity<HuaNongCrawlerFileUploadResponse> responseEntity = requestComponent.formData(url, parts, HuaNongCrawlerFileUploadResponse.class, headers);
|
|
|
+ return responseEntity.getBody();
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询上传目录
|
|
|
+ *
|
|
|
+ * @param request 上传目录请求
|
|
|
+ * @param headers 请求头
|
|
|
+ * @return 上传目录响应
|
|
|
+ */
|
|
|
+ public HuaNongCrawlerCatalogQueryResponse catalogQuery(HttpServletRequest request, HttpHeaders headers) {
|
|
|
+ MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
|
|
|
+ String url = properties.getUploadUrl() + RequestUrl.CATALOG_QUERY;
|
|
|
+ ResponseEntity<HuaNongCrawlerCatalogQueryResponse> responseEntity = requestComponent.formData(url, parts, HuaNongCrawlerCatalogQueryResponse.class, headers);
|
|
|
+ return responseEntity.getBody();
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 提交核保
|
|
|
@@ -189,6 +243,7 @@ public class HuaNongCrawlerRequestComponent {
|
|
|
|
|
|
/**
|
|
|
* 非车保单下载
|
|
|
+ *
|
|
|
* @param request 非车保单请求
|
|
|
* @param headers 请求头
|
|
|
* @return 非车保单下载链接
|