Procházet zdrojové kódy

Merge branch 'master' of http://39.98.38.2:13000/dumingliang/sh-model-platform

nanjingliujinyu před 3 měsíci
rodič
revize
b9b4161ccb

+ 13 - 6
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/controller/PtServiceController.java

@@ -192,24 +192,31 @@ public class PtServiceController extends BaseController {
         Date nowDate = DateUtils.getNowDate();
         ptServiceLog.setRunTm(nowDate);
         ptServiceLog.setCreateBy(getUsername());
-        String s = "";
+        HashMap<String, String> map = null;
         try {
 
-            s = ptServiceService.testRun(ptService);
+            map = ptServiceService.testRun(ptService);
         } catch (Exception e) {
             ptServiceLog.setSenState("0");
             String errorMessage = ExceptionUtil.getRootErrorMessage(e);
             errorMessage = errorMessage.length() > 490 ? errorMessage.substring(0, 490) : errorMessage;
             ptServiceLog.setErrorMessage(errorMessage);
-            return new AjaxResult(HttpStatus.HTTP_ERROR, s);
+            serviceRunLogService.insertPtServiceRunLog(ptServiceLog);
+            return new AjaxResult(HttpStatus.HTTP_ERROR, "测试出错,请检查参数与路由");
 
         }
-        ptServiceLog.setSenState("1");
         ptServiceLog.setExecTm(
                 DateUtils.getNowDate().getTime() - nowDate.getTime());
-        ptServiceLog.setReturnData(s);
+        ptServiceLog.setReturnData(map.get("result"));
+        if (null == map.get("status") || !map.get("status").equals("200")) {
+            ptServiceLog.setSenState("0");
+            serviceRunLogService.insertPtServiceRunLog(ptServiceLog);
+            return new AjaxResult(HttpStatus.HTTP_ERROR, "测试出错,请检查参数与路由");
+        }
+
+        ptServiceLog.setSenState("1");
         serviceRunLogService.insertPtServiceRunLog(ptServiceLog);
-        return success(s);
+        return success(map.get("result"));
     }
 
     @Autowired

+ 2 - 1
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/service/PtServiceService.java

@@ -6,6 +6,7 @@ import com.ruoyi.interfaces.domain.PtService;
 import com.ruoyi.interfaces.domain.vo.MdModelInfoVo;
 
 import java.io.IOException;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -41,7 +42,7 @@ public interface PtServiceService extends IService<PtService> {
 
     int put(PtService ptService);
 
-    String testRun(PtService ptService) throws IOException;
+    HashMap<String,String> testRun(PtService ptService) throws IOException;
 
     int audit(PtService service);
 

+ 4 - 3
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/service/impl/PtServiceServiceImpl.java

@@ -129,9 +129,10 @@ public class PtServiceServiceImpl extends ServiceImpl<PtServiceMapper, PtService
 
 
     @Override
-    public String testRun(PtService ptService) throws IOException {
+    public HashMap<String,String> testRun(PtService ptService) throws IOException {
 
-        String url = RuoYiConfig.getGatewayUrl() + ptService.getProxyPath() + ptService.getUrl();
+        //String url = RuoYiConfig.getGatewayUrl() + ptService.getProxyPath() + ptService.getUrl();
+        String url =  ptService.getUrl();
         HashMap<String, String> headers = new HashMap<>();
         List<PtServiceParam> params = ptService.getParams();
         HttpUtils.setGatewayHeaders(headers);
@@ -148,7 +149,7 @@ public class PtServiceServiceImpl extends ServiceImpl<PtServiceMapper, PtService
                 for (PtServiceParam param : params) {
                     paramString+=(param.getParamCode()+"="+param.getParamValue())+"&";
                 }
-                return HttpUtils.sendGet(url,paramString,headers);
+                return HttpUtils.sendHttpGet(url,paramString,headers);
         }
         return null;
     }

+ 6 - 2
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/tinyflow/node/ServiceNode.java

@@ -52,7 +52,7 @@ public class ServiceNode extends BaseNode {
                     }
                 }
                 try {
-                    map.putAll(JsonUtils.jsonToPojo(HttpUtils.sendBodyPost(url, paramMap, headers), Map.class));
+                    map.putAll(JsonUtils.jsonToPojo(HttpUtils.sendBodyPost(url, paramMap, headers).get("result"), Map.class));
                 } catch (IOException e) {
                     throw new RuntimeException(e);
                 }
@@ -65,7 +65,11 @@ public class ServiceNode extends BaseNode {
                         paramString += (serviceParam.getParamCode() + "=" + paramValue) + "&";
                     }
                 }
-                map.putAll(JsonUtils.jsonToPojo(HttpUtils.sendGet(url, paramString, headers), Map.class));
+                try {
+                    map.putAll(JsonUtils.jsonToPojo(HttpUtils.sendHttpGet(url, paramString, headers).get("result"), Map.class));
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
                 break;
         }
         /**

+ 62 - 29
ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java

@@ -81,9 +81,9 @@ public class HttpUtils {
         return sendGet(url, toQueryString(param));
     }
 
-    public static String sendGet(String url, Object param,HashMap<String, String> headers) {
+    public static String sendGet(String url, Object param, HashMap<String, String> headers) throws IOException {
 
-        return sendGet(url, toQueryString(param),headers);
+        return sendGet(url, toQueryString(param), headers);
     }
 
     /**
@@ -133,8 +133,9 @@ public class HttpUtils {
         return result.toString();
     }
 
-    public static String sendGet(String url, String param, HashMap<String, String> headers) {
+    public static String sendGet(String url, String param, HashMap<String, String> headers) throws IOException {
 
+        HashMap<String, String> hashMap = new HashMap<>();
         StringBuilder result = new StringBuilder();
         BufferedReader in = null;
         try {
@@ -152,19 +153,12 @@ public class HttpUtils {
 
             connection.connect();
             in = new BufferedReader(new InputStreamReader(connection.getInputStream(), Constants.UTF8));
+
             String line;
             while ((line = in.readLine()) != null) {
                 result.append(line);
             }
             log.info("recv - {}", result);
-        } catch (ConnectException e) {
-            log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
-        } catch (SocketTimeoutException e) {
-            log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
-        } catch (IOException e) {
-            log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
-        } catch (Exception e) {
-            log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
         } finally {
             try {
                 if (in != null) {
@@ -177,6 +171,52 @@ public class HttpUtils {
         return result.toString();
     }
 
+
+    public static HashMap<String, String> sendHttpGet(String url, String param, HashMap<String, String> headers) throws IOException {
+        String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
+
+        HashMap<String, String> hashMap = new HashMap<>();
+        StringBuilder result = new StringBuilder();
+
+
+        HttpURLConnection conn = null;
+        try {
+            URL realUrl = new URL(urlNameString);
+            conn = (HttpURLConnection) realUrl.openConnection();
+            conn.setRequestMethod("GET");
+
+
+            // 设置通用请求头
+            conn.setRequestProperty("Accept", "application/json");
+            if (StringUtils.isNotEmpty(headers)) {
+                //headers.remove("Content-Type"); // 防御性移除外部可能传入的冲突头
+                headers.forEach(conn::setRequestProperty);
+            }
+            conn.setDoOutput(false);
+            conn.setDoInput(true);
+            conn.setConnectTimeout(5000);    // 设置超时
+            conn.setReadTimeout(10000);
+
+// 检查 HTTP 状态码
+            int status = conn.getResponseCode();
+            hashMap.put("status", status + "");
+            // 读取响应(显式 UTF-8 解码)
+            try (BufferedReader in = new BufferedReader(
+                    new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
+                String line;
+                while ((line = in.readLine()) != null) {
+                    result.append(line);
+                }
+            }
+        }  finally {
+            if (conn != null) {
+                conn.disconnect();
+            }
+        }
+        hashMap.put("result", result.toString());
+        return hashMap;
+    }
+
     /**
      * 向指定 URL 发送POST方法的请求
      *
@@ -284,16 +324,16 @@ public class HttpUtils {
         return result.toString();
     }
 
-    public static String sendBodyPost(String url, Object body, HashMap<String, String> headers) throws IOException {
+    public static HashMap<String, String> sendBodyPost(String url, Object body, HashMap<String, String> headers) throws IOException {
         String bodyJson = JsonUtils.objectToJson(body);
-        System.out.println(bodyJson);
-        return sendBodyPostTest( url,  bodyJson, headers);
+        return sendBodyPostTest(url, bodyJson, headers);
     }
 
-    public static String sendBodyPostTest(String url, String param, HashMap<String, String> headers) throws IOException {
-        if (url == null || param == null) {
-            throw new IllegalArgumentException("URL and parameters cannot be null");
+    public static HashMap<String, String> sendBodyPostTest(String url, String param, HashMap<String, String> headers) throws IOException {
+        if (url == null) {
+            throw new IllegalArgumentException("网址为空");
         }
+        HashMap<String, String> hashMap = new HashMap<>();
 
         HttpURLConnection conn = null;
         StringBuilder result = new StringBuilder();
@@ -325,10 +365,7 @@ public class HttpUtils {
 
             // 检查 HTTP 状态码
             int status = conn.getResponseCode();
-            if (status != HttpURLConnection.HTTP_OK) {
-                throw new IOException("HTTP error code: " + status);
-            }
-
+            hashMap.put("status", status + "");
             // 读取响应(显式 UTF-8 解码)
             try (BufferedReader in = new BufferedReader(
                     new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
@@ -339,18 +376,13 @@ public class HttpUtils {
             }
 
             log.info("Request succeeded, response: {}", result);
-        } catch (ConnectException | SocketTimeoutException e) {
-            log.error("Connection error: {}", url, e);
-            throw e;
-        } catch (IOException e) {
-            log.error("I/O error: {}", url, e);
-            throw e;
         } finally {
             if (conn != null) {
                 conn.disconnect();
             }
         }
-        return result.toString();
+        hashMap.put("result", result.toString());
+        return hashMap;
     }
 
 
@@ -584,7 +616,8 @@ public class HttpUtils {
 
         return URLEncodedUtils.format(params, StandardCharsets.UTF_8);
     }
-    public static void setGatewayHeaders(HashMap<String, String> hashMap){
+
+    public static void setGatewayHeaders(HashMap<String, String> hashMap) {
         hashMap.put("Authorization", "1478702065085063169");
     }
 }