forked from github/dataease
feat: API数据源支持时间参数 #8378
This commit is contained in:
parent
fce03fb058
commit
1abddd7874
@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -178,13 +179,21 @@ public class ApiUtils {
|
||||
String resultStr = execHttpRequest(definition, definition.getApiQueryTimeout() == null || apiDefinition.getApiQueryTimeout() <= 0 ? 10 : apiDefinition.getApiQueryTimeout(), null);
|
||||
List<String[]> dataList = fetchResult(resultStr, definition);
|
||||
if (dataList.size() > 0) {
|
||||
result = result.replace("${" + param +"}",dataList.get(0)[i]);
|
||||
result = result.replace("${" + param + "}", dataList.get(0)[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
httpClientConfig.addHeader(header.get("name").toString(), result);
|
||||
} else if (header.get("nameType") != null && header.get("nameType").toString().equalsIgnoreCase("timeFun")) {
|
||||
String timeFormat = header.get("value").toString();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Date date = calendar.getTime();
|
||||
if (StringUtils.isNotEmpty(timeFormat) && timeFormat.split(" ")[0].equalsIgnoreCase("currentDay")) {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat.split(" ")[1]);
|
||||
httpClientConfig.addHeader(header.get("name").toString(), simpleDateFormat.format(date));
|
||||
}
|
||||
} else {
|
||||
httpClientConfig.addHeader(header.get("name").toString(), header.get("value").toString());
|
||||
}
|
||||
@ -203,7 +212,55 @@ public class ApiUtils {
|
||||
List<String> params = new ArrayList<>();
|
||||
for (Map<String, String> argument : apiDefinition.getRequest().getArguments()) {
|
||||
if (StringUtils.isNotEmpty(argument.get("name")) && StringUtils.isNotEmpty(argument.get("value"))) {
|
||||
params.add(argument.get("name") + "=" + URLEncoder.encode(argument.get("value")));
|
||||
if (argument.get("nameType") != null && argument.get("nameType").toString().equalsIgnoreCase("params")) {
|
||||
String param = argument.get("value").toString();
|
||||
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<String[]> dataList = fetchResult(resultStr, definition);
|
||||
if (dataList.size() > 0) {
|
||||
params.add(argument.get("name") + "=" + dataList.get(0)[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (argument.get("nameType") != null && argument.get("nameType").toString().equalsIgnoreCase("custom")) {
|
||||
List<String> arrayList = new ArrayList<>();
|
||||
String regex = "\\$\\{(.*?)\\}";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(argument.get("value").toString());
|
||||
while (matcher.find()) {
|
||||
arrayList.add(matcher.group(1));
|
||||
}
|
||||
String result = argument.get("value").toString();
|
||||
for (String param : arrayList) {
|
||||
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<String[]> dataList = fetchResult(resultStr, definition);
|
||||
if (dataList.size() > 0) {
|
||||
result = result.replace("${" + param + "}", dataList.get(0)[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
params.add(argument.get("name") + "=" + result);
|
||||
} else if (argument.get("nameType") != null && argument.get("nameType").toString().equalsIgnoreCase("timeFun")) {
|
||||
String timeFormat = argument.get("value").toString();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Date date = calendar.getTime();
|
||||
if (StringUtils.isNotEmpty(timeFormat) && timeFormat.split(" ")[0].equalsIgnoreCase("currentDay")) {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat.split(" ")[1]);
|
||||
params.add(argument.get("name") + "=" + simpleDateFormat.format(date));
|
||||
}
|
||||
} else {
|
||||
params.add(argument.get("name") + "=" + URLEncoder.encode(argument.get("value")));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(params)) {
|
||||
|
@ -157,6 +157,7 @@ const emits = defineEmits(['changeId'])
|
||||
:is-read-only="isReadOnly"
|
||||
:isShowEnable="isShowEnable"
|
||||
:parameters="apiRequest.arguments"
|
||||
:value-list="valueList"
|
||||
/>
|
||||
</el-tab-pane>
|
||||
|
||||
|
@ -86,11 +86,26 @@ const options = [
|
||||
label: '固定值',
|
||||
value: 'fixed'
|
||||
},
|
||||
{
|
||||
label: '时间函数',
|
||||
value: 'timeFun'
|
||||
},
|
||||
{
|
||||
label: '自定义',
|
||||
value: 'custom'
|
||||
}
|
||||
]
|
||||
|
||||
const timeFunLists = [
|
||||
{
|
||||
label: '当天(yyyy-MM-DD)',
|
||||
value: 'currentDay yyyy-MM-DD'
|
||||
},
|
||||
{
|
||||
label: '当天(yyyy/MM/DD)',
|
||||
value: 'currentDay yyyy/MM/DD'
|
||||
}
|
||||
]
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@ -161,9 +176,25 @@ const options = [
|
||||
:value="item.originName"
|
||||
/>
|
||||
</el-select>
|
||||
<el-select
|
||||
v-model="element.value"
|
||||
v-if="!needMock && activeName === 'table' && element.nameType === 'timeFun'"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in timeFunLists"
|
||||
:key="item.originName"
|
||||
:label="item.label"
|
||||
:value="item.value"
|
||||
/>
|
||||
</el-select>
|
||||
|
||||
<el-input
|
||||
v-if="!needMock && activeName === 'table' && element.nameType !== 'params'"
|
||||
v-if="
|
||||
!needMock &&
|
||||
activeName === 'table' &&
|
||||
element.nameType !== 'params' &&
|
||||
element.nameType !== 'timeFun'
|
||||
"
|
||||
v-model="element.value"
|
||||
:disabled="isReadOnly"
|
||||
:placeholder="element.nameType === 'fixed' ? '值' : '可用${参数名},使用参数'"
|
||||
|
@ -22,6 +22,10 @@ const props = defineProps({
|
||||
type: Array as PropType<Item[]>,
|
||||
default: () => []
|
||||
},
|
||||
valueList: {
|
||||
type: Array as PropType<Item[]>,
|
||||
default: () => []
|
||||
},
|
||||
suggestions: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
@ -43,6 +47,7 @@ onBeforeMount(() => {
|
||||
parameters.value.push(
|
||||
new KeyValue({
|
||||
type: 'text',
|
||||
nameType: 'fixed',
|
||||
enable: true,
|
||||
required: true,
|
||||
uuid: guid(),
|
||||
@ -91,7 +96,36 @@ const createFilter = (queryString: string) => {
|
||||
return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
|
||||
}
|
||||
}
|
||||
|
||||
const activeName = inject('api-active-name')
|
||||
const options = [
|
||||
{
|
||||
label: '参数',
|
||||
value: 'params'
|
||||
},
|
||||
{
|
||||
label: '固定值',
|
||||
value: 'fixed'
|
||||
},
|
||||
{
|
||||
label: '时间函数',
|
||||
value: 'timeFun'
|
||||
},
|
||||
{
|
||||
label: '自定义',
|
||||
value: 'custom'
|
||||
}
|
||||
]
|
||||
const timeFunLists = [
|
||||
{
|
||||
label: '当天(yyyy-MM-DD)',
|
||||
value: 'currentDay yyyy-MM-DD'
|
||||
},
|
||||
{
|
||||
label: '当天(yyyy/MM/DD)',
|
||||
value: 'currentDay yyyy/MM/DD'
|
||||
}
|
||||
]
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@ -138,9 +172,55 @@ const activeName = inject('api-active-name')
|
||||
show-word-limit
|
||||
/>
|
||||
</el-col>
|
||||
|
||||
<el-col :span="3" v-if="activeName === 'table'">
|
||||
<el-select v-model="element.nameType">
|
||||
<el-option
|
||||
v-for="item in options"
|
||||
:key="item.value"
|
||||
:label="item.label"
|
||||
:value="item.value"
|
||||
/>
|
||||
</el-select>
|
||||
</el-col>
|
||||
<el-col v-if="element.type !== 'file'" :span="6">
|
||||
<el-input
|
||||
v-if="activeName === 'params'"
|
||||
v-model="element.value"
|
||||
:disabled="isReadOnly"
|
||||
class="input-with-autocomplete"
|
||||
:placeholder="valueText"
|
||||
value-key="name"
|
||||
highlight-first-item
|
||||
/>
|
||||
|
||||
<el-select
|
||||
v-model="element.value"
|
||||
v-if="!needMock && activeName === 'table' && element.nameType === 'params'"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in valueList"
|
||||
:key="item.originName"
|
||||
:label="item.name"
|
||||
:value="item.originName"
|
||||
/>
|
||||
</el-select>
|
||||
<el-select
|
||||
v-model="element.value"
|
||||
v-if="!needMock && activeName === 'table' && element.nameType === 'timeFun'"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in timeFunLists"
|
||||
:key="item.originName"
|
||||
:label="item.label"
|
||||
:value="item.value"
|
||||
/>
|
||||
</el-select>
|
||||
<el-input
|
||||
v-if="
|
||||
activeName === 'table' &&
|
||||
element.nameType !== 'params' &&
|
||||
element.nameType !== 'timeFun'
|
||||
"
|
||||
v-model="element.value"
|
||||
:disabled="isReadOnly"
|
||||
class="input-with-autocomplete"
|
||||
@ -150,7 +230,7 @@ const activeName = inject('api-active-name')
|
||||
/>
|
||||
</el-col>
|
||||
|
||||
<el-col :span="10">
|
||||
<el-col :span="activeName === 'params' ? 10 : 7">
|
||||
<el-input
|
||||
v-model="element.description"
|
||||
maxlength="200"
|
||||
|
Loading…
Reference in New Issue
Block a user