From bda3e6a4c7bef9726c2a40ed4d3a2a6381496413 Mon Sep 17 00:00:00 2001 From: XieYongHong <18010623010@163.com> Date: Thu, 17 Oct 2024 15:22:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=80=90Api=E9=85=8D=E7=BD=AE=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dapi=E5=93=8D=E5=BA=94=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=B1=95=E7=A4=BA=E9=94=99=E8=AF=AF=EF=BC=8C?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3=E6=B7=BB=E5=8A=A0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Platforms/Api/components/ApiDoes.vue | 121 ++++--- .../Platforms/Api/components/ApiTest.vue | 52 ++- .../Platforms/Api/components/ChooseApi.vue | 1 + .../Platforms/Api/components/monaco.vue | 63 ++++ src/views/system/Platforms/Api/index.vue | 310 ++++++++++-------- src/views/system/Platforms/Api/typing.d.ts | 7 +- src/views/system/Platforms/Api/utils.ts | 130 +++++--- 7 files changed, 456 insertions(+), 228 deletions(-) create mode 100644 src/views/system/Platforms/Api/components/monaco.vue diff --git a/src/views/system/Platforms/Api/components/ApiDoes.vue b/src/views/system/Platforms/Api/components/ApiDoes.vue index c6611a24..417cd251 100644 --- a/src/views/system/Platforms/Api/components/ApiDoes.vue +++ b/src/views/system/Platforms/Api/components/ApiDoes.vue @@ -24,7 +24,12 @@
请求示例
- + +
请求参数
@@ -37,10 +42,7 @@ size="small" > -
@@ -80,18 +82,23 @@ - + + - diff --git a/src/views/system/Platforms/Api/components/ApiTest.vue b/src/views/system/Platforms/Api/components/ApiTest.vue index 294fe526..a5a7aa19 100644 --- a/src/views/system/Platforms/Api/components/ApiTest.vue +++ b/src/views/system/Platforms/Api/components/ApiTest.vue @@ -5,7 +5,7 @@
- 发送 + 发送
@@ -107,14 +107,31 @@ 新增 - + Json + Text + + +
@@ -145,6 +162,8 @@ const editorRef = ref(); const formRef = ref(); const method = ref() const showRequestBody = ref(!!props.selectApi?.requestBody) +const bodyType = ref('text'); +const loading = ref(false); const requestBody = reactive({ tableColumns: [ { @@ -208,6 +227,11 @@ const init = () => { }; init(); +const handleChangeBodyType = () => { + console.log(editorRef.value) + editorRef.value?.setModelLanguage(editorRef.value.getModel(), bodyType.value); +} + const send = () => { if (paramsTable.value.length) formRef.value && @@ -240,16 +264,22 @@ const _send = () => { ...urlParams, }; }else{ - params = JSON.parse(requestBody.code || '{}') + if(bodyType.value == 'text') { + params = requestBody.code + } else { + params = JSON.parse(requestBody.code || '{}') + } } - - server[methodObj[methodName]](url, params).then((resp: any) => { + loading.value = true; + server[methodObj[methodName]](url, params, {}, bodyType.value === 'text' ? {headers: {'Content-Type': 'text/plain'}} : {}).then((resp: any) => { // 如果用户没填写参数且有body的情况下,给用户展示请求示例 if (Object.keys(params).length === 0 && refStr.value) { requestBody.code = JSON.stringify(getDefaultParams()); editorRef.value?.editorFormat(); } responsesContent.value = resp; + }).finally(() => { + loading.value = false; }); }; diff --git a/src/views/system/Platforms/Api/components/ChooseApi.vue b/src/views/system/Platforms/Api/components/ChooseApi.vue index e91a0723..b09e864b 100644 --- a/src/views/system/Platforms/Api/components/ChooseApi.vue +++ b/src/views/system/Platforms/Api/components/ChooseApi.vue @@ -5,6 +5,7 @@ :columns="columns" :dataSource="_tableData" :rowSelection="props.mode !== 'home' ? rowSelection : undefined" + :style="{padding: 0}" noPagination model="TABLE" > diff --git a/src/views/system/Platforms/Api/components/monaco.vue b/src/views/system/Platforms/Api/components/monaco.vue new file mode 100644 index 00000000..6ca6a33b --- /dev/null +++ b/src/views/system/Platforms/Api/components/monaco.vue @@ -0,0 +1,63 @@ + + + + + diff --git a/src/views/system/Platforms/Api/index.vue b/src/views/system/Platforms/Api/index.vue index 6f2db80d..80cd269f 100644 --- a/src/views/system/Platforms/Api/index.vue +++ b/src/views/system/Platforms/Api/index.vue @@ -1,76 +1,77 @@ diff --git a/src/views/system/Platforms/Api/typing.d.ts b/src/views/system/Platforms/Api/typing.d.ts index a21f36fd..cd2ab37d 100644 --- a/src/views/system/Platforms/Api/typing.d.ts +++ b/src/views/system/Platforms/Api/typing.d.ts @@ -34,6 +34,11 @@ export type modeType = 'api'| 'appManger' | 'home' export type schemaObjType = { paramsName: string; paramsType: string; + id: string; + type?: string; + schema?: string; + + required?: boolean desc?: string; children?: schemaObjType[]; -}; \ No newline at end of file +}; diff --git a/src/views/system/Platforms/Api/utils.ts b/src/views/system/Platforms/Api/utils.ts index 0db68e83..76d31783 100644 --- a/src/views/system/Platforms/Api/utils.ts +++ b/src/views/system/Platforms/Api/utils.ts @@ -1,4 +1,5 @@ import { schemaObjType } from "./typing"; +import {randomString} from "@/utils/utils"; /** @@ -8,28 +9,38 @@ import { schemaObjType } from "./typing"; */ export function findData(schemas: object, schemaName: string , paths:string[]=[]) { const basicType = ['string', 'integer', 'boolean','number']; - + if (!schemaName || !schemas[schemaName]) { return []; } const result: schemaObjType[] = []; const schema = schemas[schemaName]; + const required = schema.required || [] + Object.entries(schema.properties).forEach((item: [string, any]) => { + const [paramsName, extra] = item + const paramsType = - (item[1].$ref && item[1].$ref.split('/').pop()) || - (item[1].items?.$ref && item[1].items.$ref.split('/').pop()) || - item[1].item?.type || - item[1].type || + (extra.$ref && extra.$ref.split('/').pop()) || + (extra.items?.$ref && extra.items.$ref.split('/').pop()) || + extra.item?.type || + extra.type || ''; + const schema = extra.format ? `${paramsType}(${extra.format})` : '' + const schemaObj: schemaObjType = { - paramsName: item[0], + paramsName: paramsName, paramsType, - desc: item[1].description || '', + required: required.includes(paramsName), + desc: extra.description || '', + schema: schema, + type: !basicType.includes(paramsType) ? paramsType : '', + id: randomString() }; - - if (!basicType.includes(paramsType) && paths.filter(path=>path === schemaName).length >=2 ){ + + if (!basicType.includes(paramsType) && (paths.filter(path=>path === schemaName).length <=2) ){ paths.push(schemaName) - schemaObj.children = findData(schemas, paramsType); + schemaObj.children = findData(schemas, paramsType, paths); } result.push(schemaObj); }); @@ -47,54 +58,89 @@ export function getCodeText( schemas: object, arr: schemaObjType[], level: number, -): object { - const result = {}; + paths: string[] = [] +): any { + const tips: Array = [] + let result = {} arr.forEach((item) => { + let value: any = "" + tips.push(item.desc) switch (item.paramsType) { case 'string': - result[item.paramsName] = ''; + value = '' break; case 'integer': - result[item.paramsName] = 0; + case 'number': + value = 0 break; case 'boolean': - result[item.paramsName] = true; + value = true break; case 'array': - result[item.paramsName] = []; + value = [] break; case 'object': - result[item.paramsName] = ''; - break; - case 'number': - result[item.paramsName] = 0; + value = {} break; default: { - const properties = schemas[item.paramsType]?.properties as object || {}; - const newArr = Object.entries(properties).map( - (item: [string, any]) => { - return{ - paramsName: item[0], - paramsType: level - ? (item[1].$ref && item[1].$ref.split('/').pop()) || - (item[1].items?.$ref && - item[1].items.$ref.split('/').pop()) || - item[1].item?.type || - item[1].type || - '' - : item[1].type, - }}, - ); - result[item.paramsName] = getCodeText( - schemas, - newArr, - level - 1, - ); + if (item.children) { + if (paths.filter(path=> path === item.paramsName).length >=2) { + break + } + paths.push(item.paramsName) + const _result = getCodeText( + schemas, + item.children, + level - 1, + paths + ) + value = _result.codeText + tips.push(..._result.codeTips) + tips.push(undefined) + } else { + if (paths.filter(path=> path === item.paramsName).length >=2) { + break + } + paths.push(item.paramsName) + const properties = schemas[item.paramsType]?.properties as object || {}; + const newArr = Object.entries(properties).map( + (item: [string, any]) => { + const [paramsName, extra] = item + + const paramsType = + (extra.$ref && extra.$ref.split('/').pop()) || + (extra.items?.$ref && extra.items.$ref.split('/').pop()) || + extra.item?.type || + extra.type || + ''; + + return{ + paramsName: paramsName, + desc: extra.description || '', + paramsType: paramsType, + schema: extra.format ? `${paramsType}(${extra.format})` : '' + }}, + ); + + const _result = getCodeText( + schemas, + newArr, + level - 1, + paths + ) + value = _result.codeText + tips.push(..._result.codeTips) + tips.push(undefined) + } } } + result[item.paramsName] = value }); + return { + codeText: result, + codeTips: tips - return result; + }; } /** @@ -124,4 +170,4 @@ export function dealNoRef(type:string,schema?:any):any{ return; } return result -} \ No newline at end of file +}