From 9e6bc980543acdfc63f7b6ce80ccdacba35bc365 Mon Sep 17 00:00:00 2001 From: taojinlong Date: Wed, 24 Jul 2024 16:13:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20API=E6=95=B0=E6=8D=AE=E6=BA=90=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=B4=EF=BC=8C=E5=B8=8C=E6=9C=9B=E8=83=BD?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datasource/provider/ApiUtils.java | 33 ++++++++++++++++--- .../data/datasource/form/ApiKeyValue.vue | 6 +++- .../data/datasource/form/EditorDetail.vue | 1 - 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/core/core-backend/src/main/java/io/dataease/datasource/provider/ApiUtils.java b/core/core-backend/src/main/java/io/dataease/datasource/provider/ApiUtils.java index 9119b189ba..c713b31199 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/provider/ApiUtils.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/provider/ApiUtils.java @@ -23,6 +23,8 @@ import org.springframework.util.ObjectUtils; import java.net.URLEncoder; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; public class ApiUtils { @@ -53,12 +55,12 @@ public class ApiUtils { return tableDescs; } - public static Map getTableNamesMap(String configration) throws DEException { - Map result = new HashMap<>(); + public static Map getTableNamesMap(String configration) throws DEException { + Map result = new HashMap<>(); try { JsonNode rootNode = objectMapper.readTree(configration); for (int i = 0; i < rootNode.size(); i++) { - result.put(rootNode.get(i).get("name").asText(),rootNode.get(i).get("deTableName").asText()); + result.put(rootNode.get(i).get("name").asText(), rootNode.get(i).get("deTableName").asText()); } } catch (Exception e) { DEException.throwException(e); @@ -158,8 +160,31 @@ public class ApiUtils { } } } - } + } else if (header.get("nameType") != null && header.get("nameType").toString().equalsIgnoreCase("custom")) { + List params = new ArrayList<>(); + String regex = "\\$\\{(.*?)\\}"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(header.get("value").toString()); + while (matcher.find()) { + params.add(matcher.group(1)); + } + String result = header.get("value").toString(); + for (String param : params) { + for (ApiDefinition definition : paramsList) { + for (int i = 0; i < definition.getFields().size(); i++) { + TableField field = definition.getFields().get(i); + if (field.getOriginName().equalsIgnoreCase(param)) { + String resultStr = execHttpRequest(definition, definition.getApiQueryTimeout() == null || apiDefinition.getApiQueryTimeout() <= 0 ? 10 : apiDefinition.getApiQueryTimeout(), null); + List dataList = fetchResult(resultStr, definition); + if (dataList.size() > 0) { + result = result.replace("${" + param +"}",dataList.get(0)[i]); + } + } + } + } + } + httpClientConfig.addHeader(header.get("name").toString(), result); } else { httpClientConfig.addHeader(header.get("name").toString(), header.get("value").toString()); } diff --git a/core/core-frontend/src/views/visualized/data/datasource/form/ApiKeyValue.vue b/core/core-frontend/src/views/visualized/data/datasource/form/ApiKeyValue.vue index 641590cf85..feae13854e 100644 --- a/core/core-frontend/src/views/visualized/data/datasource/form/ApiKeyValue.vue +++ b/core/core-frontend/src/views/visualized/data/datasource/form/ApiKeyValue.vue @@ -85,6 +85,10 @@ const options = [ { label: '固定值', value: 'fixed' + }, + { + label: '自定义', + value: 'custom' } ] @@ -162,7 +166,7 @@ const options = [ v-if="!needMock && activeName === 'table' && element.nameType !== 'params'" v-model="element.value" :disabled="isReadOnly" - :placeholder="'值'" + :placeholder="element.nameType === 'fixed' ? '值' : '可用${参数名}使用用参数'" show-word-limit /> diff --git a/core/core-frontend/src/views/visualized/data/datasource/form/EditorDetail.vue b/core/core-frontend/src/views/visualized/data/datasource/form/EditorDetail.vue index 152b7bf44a..655a31d5ce 100644 --- a/core/core-frontend/src/views/visualized/data/datasource/form/EditorDetail.vue +++ b/core/core-frontend/src/views/visualized/data/datasource/form/EditorDetail.vue @@ -159,7 +159,6 @@ const authMethodList = [ ] const validateSshHost = (_: any, value: any, callback: any) => { - console.log(value) if ((value === undefined || value === null || value === '') && form.value.configuration.useSSH) { callback(new Error('SSH主机不能为空')) }