andy 1 жил өмнө
parent
commit
6816c651a0

+ 56 - 0
src/main/java/com/scbfkj/uni/library/script/HttpScriptUtil.java

@@ -0,0 +1,56 @@
+package com.scbfkj.uni.library.script;
+
+import com.scbfkj.uni.library.DataFormatUtil;
+import com.scbfkj.uni.process.DataBase;
+import com.scbfkj.uni.process.Web;
+import com.scbfkj.uni.system.Config;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class HttpScriptUtil {
+
+    private static final DataBase DATA_BASE = new DataBase();
+
+    public static Map<String, Object> execByDataSourceId(String dataSourceId, Map<String, Object> data) throws Exception {
+
+            Map<String, Object> connection = queryConnectionStr(dataSourceId);
+            if(data.containsKey("url")){
+                connection.put("url",data.get("url"));
+            }
+            if(data.containsKey("headers")){
+                connection.put("headers",data.get("headers"));
+            }
+            if(data.containsKey("body")){
+                connection.put("headers",data.get("headers"));
+            }
+                connection.put("method",data.get("method"));
+
+        return new Web().execWebApi(connection.get("headers"), connection.getOrDefault("method", "post").toString(), connection.get("body"), DataFormatUtil.toString(connection));
+    }
+    public static Map<String, Object> exec(Object header, String method, Object defaultBody, String url) throws Exception {
+
+
+        return new Web().execWebApi(header, method, defaultBody, url);
+    }
+
+
+    private static Map<String, Object> queryConnectionStr(String datasourceId) throws Exception {
+        List<Map<String, Object>> result = DATA_BASE.query(Config.getCenterConnectionStr(), """
+                select host,httpheaders,httpbody
+                from datasource
+                where datasourceid = ?""", datasourceId);
+        if (result.isEmpty()) {
+            throw new RuntimeException("数据源错误:没有找到数据源");
+        }
+        return result.stream().findFirst().map(it -> {
+            HashMap<String, Object> hashMap = new HashMap<>();
+            hashMap.put("url", it.get("host"));
+            hashMap.put("headers", it.get("httpheaders"));
+            hashMap.put("body", it.get("httpbody"));
+            return hashMap;
+        }).get();
+    }
+
+}

+ 13 - 37
src/main/java/com/scbfkj/uni/process/Web.java

@@ -31,7 +31,7 @@ public class Web {
             return new DefaultPooledObject<>(web);
         }
     });
-    private final ObjectPool<WebClient> clientPool = new GenericObjectPool<>(new BasePooledObjectFactory<>() {
+    private final static ObjectPool<WebClient> clientPool = new GenericObjectPool<>(new BasePooledObjectFactory<>() {
 
         @Override
         public WebClient create() throws Exception {
@@ -55,29 +55,15 @@ public class Web {
 
     private Map<String, Object> baseHeader = new HashMap<>();
 
-    /**
-     * webapi 执行入口函数
-     */
-    public Map<String, Object> exec(Object header, String method, Object defaultBody, String connectConfigStr) throws Exception {
-        WebClient webClient = clientPool.borrowObject();
-        HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
-        Map<?, ?> connectConfig = DataFormatUtil.toMap(connectConfigStr);
-        String url = Objects.nonNull(connectConfig.get("url")) ? connectConfig.get("url").toString() : null;
-        if (Objects.isNull(url)) {
-            return UniReturnUtil.fail("webapi请求地址为空");
-        }
-        return UniReturnUtil.fail("");
-    }
 
-    public Map<String, Object> execWebApi(Object header, String method, Object defaultBody, String connectConfig) {
-        Map<String, Object> connectConfigMaps = (Map<String, Object>) DataFormatUtil.toMap(connectConfig);
-        Map<String, Object> restTemplateResult = initWebApiParams(connectConfigMaps); // 通过连接配置 获取restTemplate
+    public Map<String, Object> execWebApi(Object headers, String method, Object defaultBody, String url) {
+        Map<String, Object> restTemplateResult = initWebApiParams(headers,method,url); // 通过连接配置 获取restTemplate
 
         if (!"0".equals(restTemplateResult.get("code"))) {
             return UniReturnUtil.fail(restTemplateResult.get("message").toString());
         }
-        if (header instanceof Map) {
-            Map<String, Object> temeMap = (Map) header;
+        if (headers instanceof Map) {
+            Map<String, Object> temeMap = (Map) headers;
             if (!temeMap.isEmpty()) {
                 temeMap.putAll(baseHeader);
                 webApiHeader.clear();
@@ -86,17 +72,11 @@ public class Web {
                 }
             }
         }
-        if (StringUtils.hasLength(method)) webapiMethod = method.toLowerCase();
         try {
-            List<Map<String, Object>> returnList = new ArrayList<>();
-            if (defaultBody instanceof List<?> tempList) {
-                for (Object o : tempList) {
-                    returnList.add(sendWebApiRequest(o));
-                }
-            } else {
-                returnList.add(sendWebApiRequest(defaultBody));
-            }
-            return (defaultBody instanceof List<?>) ? UniReturnUtil.success(returnList) : returnList.get(0);
+
+            Map<String, Object> result = sendWebApiRequest(defaultBody);
+
+            return UniReturnUtil.success(result) ;
         } catch (Exception e) {
             return UniReturnUtil.fail("restTemplate执行异常" + UniReturnUtil.getMessage(e));
         }
@@ -125,19 +105,15 @@ public class Web {
         }
     }
 
-    private Map<String, Object> initWebApiParams(Map<String, Object> connectConfig) {
+    private Map<String, Object> initWebApiParams(Object headers,String method,String url) {
         try {
-            String configStr = DataFormatUtil.toString(connectConfig);
 
             if (restTemplate == null) {
-                if (Objects.isNull(connectConfig)) {
-                    return UniReturnUtil.fail("webapi请求配置参数为空");
-                }
-                webapiURL = Objects.nonNull(connectConfig.get("url")) ? connectConfig.get("url").toString() : null;
+                webapiURL = url;
                 if (Objects.isNull(webapiURL)) {
                     return UniReturnUtil.fail("webapi请求地址为空");
                 }
-                Object tempHeaders = connectConfig.get("headers"); //需确定返回的是一个map对象还是字符串  headers: {ContentType:"JSON"}
+                Object tempHeaders = headers; //需确定返回的是一个map对象还是字符串  headers: {ContentType:"JSON"}
                 webApiHeader = new HttpHeaders();
                 if (Objects.isNull(tempHeaders)) {
                     webApiHeader.setContentType(MediaType.APPLICATION_JSON);
@@ -150,7 +126,7 @@ public class Web {
                         }
                     }
                 }
-                Object webMethod = connectConfig.get("method"); //需确定返回的是一个map对象还是字符串  headers: {ContentType:"JSON"}
+                Object webMethod = method; //需确定返回的是一个map对象还是字符串  headers: {ContentType:"JSON"}
                 if (Objects.nonNull(webMethod)) {
                     webapiMethod = webMethod.toString().toLowerCase();
                 } else {