apiTest.vue
9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<route lang="yaml">
name: apiTest
</route>
<script lang="ts" setup name="apiTest">
import { ref } from 'vue';
import {
getValidApi,
testApiData,
getApiDetail
} from "@/api/modules/dataService";
import { TableColumnWidth } from '@/utils/enum';
import { useValidator } from '@/hooks/useValidator';
import { useRouter } from "vue-router";
const { required } = useValidator();
const router = useRouter();
const { proxy } = getCurrentInstance() as any;
/** API下拉列表。已发布的启用的API名称,选择后自动带出路径和请求参数。 */
const validApiList = ref([]);
const apiFormRef = ref();
/** 选择要测试的API. */
const apiFormItems = ref([
{
type: 'select',
label: 'API名称',
field: 'apiGuid',
default: '',
placeholder: '请选择',
options: validApiList.value,
col: 'path-w30',
props: {
label: "apiName",
children: "children",
value: 'guid',
isLeaf: 'isLeaf'
},
filterable: true,
clearable: true,
required: true
},
// {
// type: 'input',
// label: 'API路径',
// field: 'path',
// default: '',
// maxlength: 50,
// col: 'path-w30',
// disabled: true,
// required: false
// },
// {
// type: 'input',
// label: '场景名称',
// field: 'sceneName',
// default: '',
// maxlength: 50,
// col: 'path-w20',
// disabled: true,
// required: false,
// },{
// type: 'input',
// label: '场景名称',
// field: 'sceneGuid',
// default: '',
// maxlength: 50,
// col: 'path-w20',
// disabled: true,
// required: false,
// visible: false,
// },{
// type: 'input',
// label: '加工单号',
// field: 'processOrderNo',
// default: '',
// maxlength: 50,
// col: 'path-w20',
// disabled: true,
// required: false,
// },
]);
const apiFormRules = ref({
apiGuid: [required('请选择API')]
});
const requestParamsTableInfo = ref({
id: "query-params-table",
height: '214px',
fields: [
{ label: "序号", type: "index", width: TableColumnWidth.INDEX, align: "center" },
{ label: "参数名", field: "paramName", width: 140 },
{ label: "参数位置", field: "paramPositionName", width: 100 },
{ label: "参数类型", field: "dataTypeName", width: 120 },
{
label: "是否必填", field: "isRequired", width: 100, getName: (scope) => {
return scope.row.isRequired == 'Y' ? '是' : '否'
}
},
{
label: "是否多值", field: "isManyValue", width: 100, getName: (scope) => {
return scope.row.isManyValue == 'Y' ? '是' : '否'
}
},
{
label: "值", field: "introductionValue", width: 240, columClass: 'edit-colum', type: 'edit', dataTypeName: 'paramType', getVisible: (scope) => {
return !scope.row.isConst;
}
},
{ label: "描述", field: "description", width: 120 },
],
editInfo: {
introductionValue: {
label: '',
type: 'input',
field: 'introductionValue',
default: '',
placeholder: '请输入',
clearable: true,
}
},
STATUS: 'edit',
data: <Array<Object>>[],
showPage: false,
actionInfo: {
show: false
},
loading: false
});
const defaultParamsData: any = ref([{
paramName: 'pageSize',
dataType: '整型',
isRequired: '是',
value: 50
}, {
paramName: 'pageIndex',
dataType: '整型',
isRequired: '是',
value: 1
}]);
/** default参数表格配置 */
const defaultParamsTableInfo = ref({
id: "default-params-table",
height: 'auto',
minHeight: '50px',
minPanelHeight: '50px',
fields: [
{ label: "序号", type: "index", width: TableColumnWidth.INDEX, align: "center" },
{ label: "参数名", field: "paramName", width: 140 },
{ label: "参数类型", field: "dataType", width: 100 },
{ label: "是否必填", field: "isRequired", width: 100 },
{ label: "值", field: "value", width: 240, columClass: 'edit-colum', type: 'edit' },
],
editInfo: {
value: { //只能输入大于0的整数。
label: '',
type: 'input',
field: 'value',
default: '',
maxlength: 50,
min: 1,
regexp: /\D/g,
placeholder: '请输入',
clearable: false,
}
},
STATUS: 'edit',
data: defaultParamsData.value,
showPage: false,
actionInfo: {
show: false
},
loading: false
});
/** api测试结果详情表单 */
const resultFormItems = ref([{
label: '请求详情',
type: 'textarea',
placeholder: '输出请求详情',
field: 'requestUrlDetail',
default: '',
rows: 10,
readonly: true,
maxlength: "",
clearable: true,
required: false,
}, {
label: '返回内容',
type: 'textarea',
placeholder: '输出返回内容',
field: 'resultInfo',
default: '',
rows: 10,
readonly: true,
maxlength: "",
clearable: true,
required: false,
}]);
const getValidLabelPromise: any = ref(null);
onBeforeMount(() => {
getValidApi().then((res: any) => {
if (res.code == proxy.$passCode) {
validApiList.value = res.data || [];
apiFormItems.value[0].options = validApiList.value;
} else {
proxy.$ElMessage.error(res.msg);
}
})
})
const getApiDetailPromise: any = ref(null);
const selectApiDetailInfo: any = ref({});////选择数据产品后带出来的信息
const apiSelectChange = (val) => {
if (!val) {//清空值
apiFormItems.value[0].default = val;
// apiFormItems.value[1].default = '';
requestParamsTableInfo.value.data = [];
resultFormItems.value[0].default = '';
resultFormItems.value[1].default = '';
} else {
resultFormItems.value[0].default = '';
resultFormItems.value[1].default = '';
requestParamsTableInfo.value.loading = true;
getApiDetail(val).then((res: any) => {
requestParamsTableInfo.value.loading = false;
requestParamsTableInfo.value.data = [];
if (res.code == proxy.$passCode) {
selectApiDetailInfo.value = res.data || {};
apiFormItems.value[0].default = val;
let apiConfigAccessRSVOS = selectApiDetailInfo.value.apiConfigAccessRSVOS || [];
apiConfigAccessRSVOS.forEach(vo => {
if (vo.paramType == 'REQ') {
requestParamsTableInfo.value.data.push({ ...vo, introductionValue: vo.defaultValue });
} else if (vo.paramType == 'CONSTANT') {
requestParamsTableInfo.value.data.push({ ...vo, isConst: true, introductionValue: vo.defaultValue });
}
})
defaultParamsTableInfo.value.data = defaultParamsData.value;
}
else {
proxy.$ElMessage.error(res.msg);
}
})
}
}
const startTestApiBtnLoading = ref(false);
const startTestApi = () => {
apiFormRef.value?.ruleFormRef?.validate((valid, errorItem) => {
if (valid) {
let requestParamsData: any[] = requestParamsTableInfo.value.data;
if (requestParamsData.length) {
let dataIndex = 1;
for (const d of requestParamsData) {
if (d.isRequired == 'Y' && d.introductionValue !== 0 && !d.introductionValue) {
proxy.$ElMessage.error(`第 ${dataIndex} 个请求参数为必填则值不能为空`);
return;
}
dataIndex++;
}
}
let defaultParamsDataInfo = defaultParamsTableInfo.value.data;
if (defaultParamsDataInfo.length == 3 && !defaultParamsDataInfo[2].value) {
proxy.$ElMessage.error(`请填写参数appKey`);
return;
}
resultFormItems.value[0].default = '';
resultFormItems.value[1].default = '';
startTestApiBtnLoading.value = true;
testApiData({
pageSize: defaultParamsDataInfo[0].value,
pageIndex: defaultParamsDataInfo[1].value,
appKey: defaultParamsDataInfo.length == 3 ? defaultParamsDataInfo[2].value : '',
guid: apiFormRef.value.formInline.apiGuid,
apiConfigAccessDTOS: requestParamsData
}).then((res: any) => {
startTestApiBtnLoading.value = false;
if (res.code == proxy.$passCode) {
proxy.$ElMessage.success('测试API调用成功');
let info = res.data;
resultFormItems.value[0].default = JSON.stringify(info.apiInvokeReqVO, null, 4);
resultFormItems.value[1].default = JSON.stringify(info.apiInvokeRespVO, null, 4);
} else {
proxy.$ElMessage.error(res.msg);
}
}).catch(() => {
startTestApiBtnLoading.value = false;
});
} else {
var obj = Object.keys(errorItem);
apiFormRef.value.ruleFormRef.scrollToField(obj[0])
}
});
}
</script>
<template>
<div class="container_wrap full">
<ContentWrap id="id-requestParams" title="请求参数" description="">
<div>
<Form ref="apiFormRef" :itemList="apiFormItems" formId="table-base-form" :rules="apiFormRules" col="col3"
@select-change="apiSelectChange" />
</div>
<Table :tableInfo="requestParamsTableInfo" class="mb10 mt8" />
</ContentWrap>
<ContentWrap id="id-defaultParams" title="DEFAULT" description="" class="mt16">
<Table :tableInfo="defaultParamsTableInfo" class="mb10" />
</ContentWrap>
<ContentWrap id="id-test" title="测试API" description="" class="mt16">
<el-button :disabled="requestParamsTableInfo.loading" :loading="startTestApiBtnLoading" @click="startTestApi"
class="mb10">开始测试</el-button>
<Form ref="resultInfoFormRef" :itemList="resultFormItems" formId="result-base-form" col="col2" />
</ContentWrap>
</div>
</template>
<style lang="scss" scoped>
.container_wrap.full {
overflow-y: auto;
padding: 16px;
}
:deep(.el-form) {
.el-form-item.path-w50 {
width: calc(60% - 6px) !important;
max-width: 720px;
}
.el-form-item.path-w30 {
width: calc(30% - 6px) !important;
max-width: 540px;
}
.el-form-item.path-w20 {
width: calc(18% - 6px) !important;
max-width: 540px;
margin-right: 8px !important;
}
.el-textarea .el-textarea__inner {
overflow-wrap: break-word;
white-space: pre-wrap;
word-break: break-all;
}
}
.mt8 {
margin-top: 8px;
}
.mt16 {
margin-top: 16px;
}
.mb10 {
margin-bottom: 10px;
}
.value {
color: #212121;
}
</style>