29
29
</a-button >
30
30
</Title >
31
31
<div class =" mb-sm" >
32
- <div
33
- v-if =" detail.paramType === 'formdata' || detail.paramType === 'route'"
34
- class =" param-box"
35
- >
32
+ <div v-if =" detail.paramType === 'formdata'" class =" param-box" >
36
33
<Table
37
34
:columns =" headersColumns"
38
35
size =" small"
@@ -313,7 +310,7 @@ export default defineComponent({
313
310
watchEffect (() => {
314
311
if (props .detail .param ) {
315
312
const paramFormData = handleParamData (props .detail .param );
316
- if (props .detail .paramType === " formdata" || props . detail . paramType === " route " ) {
313
+ if (props .detail .paramType === " formdata" ) {
317
314
state .paramFormData = paramFormData ;
318
315
} else {
319
316
const json = renderCodeJsonByParams (paramFormData , true );
@@ -377,8 +374,27 @@ export default defineComponent({
377
374
state .paramCode = code ;
378
375
}
379
376
377
+ function replaceRouteParam(url : string , key : string , value : any ) {
378
+ const placeholderKeys = [` :${key } ` , ` <${key }> ` , ` <${key }?> ` , ` [:${key }] ` , ` {${key }} ` ];
379
+ let isReplace = false ;
380
+ for (let i = 0 ; i < placeholderKeys .length ; i ++ ) {
381
+ const key = placeholderKeys [i ];
382
+ if (url .indexOf (key ) > - 1 ) {
383
+ const reg = new RegExp (key , " g" );
384
+ url = url .replace (reg , value );
385
+ isReplace = true ;
386
+ }
387
+ }
388
+
389
+ return {
390
+ url ,
391
+ isReplace ,
392
+ };
393
+ }
394
+
380
395
function excute() {
381
396
let url = props .detail .url as string ;
397
+
382
398
if (props .detail .paramType == " formdata" ) {
383
399
const formData = new FormData ();
384
400
state .paramFormData .forEach ((item ) => {
@@ -396,41 +412,28 @@ export default defineComponent({
396
412
}
397
413
}
398
414
} else {
399
- const value: any = item .default ;
400
- formData .append (item .name , value );
401
- }
402
- });
403
- sendRequest (url , formData );
404
- } else if (props .detail .paramType == " route" ) {
405
- // 路由参数,将参数拼接到url中
406
- let params: any = {};
407
- state .paramFormData .forEach ((item ) => {
408
- const placeholderKeys = [
409
- ` :${item .name } ` ,
410
- ` <${item .name }> ` ,
411
- ` <${item .name }?> ` ,
412
- ` [:${item .name }] ` ,
413
- ` {${item .name }} ` ,
414
- ];
415
- let isReplace = false ;
416
- for (let i = 0 ; i < placeholderKeys .length ; i ++ ) {
417
- const key = placeholderKeys [i ];
418
- if (url .indexOf (key ) > - 1 ) {
419
- const reg = new RegExp (key , " g" );
415
+ const routeData = replaceRouteParam (url , item .name , item .default );
416
+ url = routeData .url ;
417
+ if (! routeData .isReplace ) {
420
418
const value: any = item .default ;
421
- url = url .replace (reg , value );
422
- isReplace = true ;
419
+ formData .append (item .name , value );
423
420
}
424
421
}
425
- if (! isReplace ) {
426
- params [item .name ] = item .default ;
427
- }
428
422
});
429
- sendRequest (url , params );
423
+ sendRequest (url , formData );
430
424
} else if (state .paramCode as string ) {
431
425
try {
432
426
const paramJson = eval (" (" + state .paramCode + " )" );
433
- sendRequest (url , paramJson );
427
+ let json: any = {};
428
+ for (const key in paramJson ) {
429
+ const value = paramJson [key ];
430
+ const routeData = replaceRouteParam (url , key , value );
431
+ url = routeData .url ;
432
+ if (! routeData .isReplace ) {
433
+ json [key ] = value ;
434
+ }
435
+ }
436
+ sendRequest (url , json );
434
437
} catch (error ) {
435
438
state .loading = false ;
436
439
message .error (t (" apiPage.json.formatError" ));
0 commit comments