From 395ebd37e8acdb83a862766d977e3455c6bef62e Mon Sep 17 00:00:00 2001 From: chenhc <2369838784@qq.com> Date: Tue, 11 Feb 2025 17:20:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E5=81=A5=E5=BA=B7=E5=B9=B3=E5=8F=B0=E8=BD=AC=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/sync/SyncController.java | 67 +++++++++++++++++-- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java b/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java index 5996a1b..7f6eb6d 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/controller/sync/SyncController.java @@ -2,7 +2,9 @@ package com.glxp.sale.admin.controller.sync; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONException; import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.glxp.sale.admin.constant.BasicProcessStatus; @@ -21,9 +23,7 @@ import com.glxp.sale.common.util.ResultVOUtils; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; +import org.springframework.http.*; import org.springframework.scheduling.annotation.Async; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -37,8 +37,14 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.PushBuilder; import java.io.BufferedReader; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 中继服务接口 直连 @@ -182,13 +188,17 @@ public class SyncController { String isTopService = systemParamConfigService.selectValueByParamKey("is_top_aliService"); if (StrUtil.isNotEmpty(isTopService) && isTopService.equals("1")) { uri = uri.replace("syncToAliapi/", ""); + uri = uri.replace("syncToAliapi", ""); } String upperServerHost = systemParamConfigService.selectValueByParamKey("aliApi_server_ip"); if (StrUtil.isNotEmpty(upperServerHost)) { - uri = upperServerHost + "/" + uri; + if (StrUtil.isNotEmpty(uri)){ + uri = upperServerHost + "/" + uri; + }else { + uri = upperServerHost; + } } - uri = changeUri(uri); - JSONObject jsonObject = redirect(request, uri); + JSONObject jsonObject = redirectAliApi(request, uri); return jsonObject; } @@ -250,6 +260,51 @@ public class SyncController { return result; } + + private JSONObject redirectAliApi(HttpServletRequest request, String uri) { + // 创建HTTP头,可以从原始请求中复制一些头信息 + HttpHeaders headers = new HttpHeaders(); + // 例如,复制Content-Type头(如果需要) + String contentType = request.getContentType() != null ? request.getContentType().toString() : "application/json"; + headers.setContentType(MediaType.parseMediaType(contentType)); + // 如果需要,还可以添加其他头信息,如Authorization等 + + // 创建HttpEntity,包含头和可能的请求体(这里假设是GET请求,所以没有请求体) + HttpEntity entity = new HttpEntity<>(headers); + RestTemplate restTemplate = new RestTemplate(); + String queryString = request.getQueryString(); + Map queryMap = null; + try { + queryMap = parseQueryString(queryString); + } catch (URISyntaxException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + if (request.getMethod().equals("POST")) { + String json = HttpUtil.post(uri,queryMap); + return JSONObject.parseObject(json); + } else if (request.getMethod().equals("GET")) { + String json = HttpUtil.get(uri,queryMap); + return JSONObject.parseObject(json); + } + + return new JSONObject(); + } + + public static Map parseQueryString(String queryString) throws URISyntaxException, UnsupportedEncodingException { + Map queryParams = new HashMap<>(); + String[] pairs = queryString.split("&"); + for (String pair : pairs) { + int idx = pair.indexOf("="); + String key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8.toString()); + String value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8.toString()); + queryParams.put(key, value); + } + return queryParams; + } + private JSONObject redirect(HttpServletRequest request, String uri, String idDatas, String type) { RestTemplate restTemplate = new RestTemplate(); HttpEntity httpEntity = uriUtils.buildHeader(request);