diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/controller/MgDocumentController.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/controller/MgDocumentController.java index ebc28a8e..f29f809e 100644 --- a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/controller/MgDocumentController.java +++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/controller/MgDocumentController.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSON; import com.zyplayer.doc.core.annotation.AuthMan; import com.zyplayer.doc.core.json.DocResponseJson; import com.zyplayer.doc.core.json.ResponseJson; +import com.zyplayer.doc.swagger.controller.vo.GlobalParamVo; import com.zyplayer.doc.swagger.controller.vo.LocationListVo; import com.zyplayer.doc.swagger.controller.vo.SwaggerLocationVo; import com.zyplayer.doc.swagger.controller.vo.SwaggerResourcesInfoVo; @@ -103,13 +104,17 @@ public class MgDocumentController { } } } + List globalParamList = this.getGlobalParamList(); if (resourcesSet.size() > 0) { List locationListStorage = this.getLocationSet(); for (SwaggerResourcesInfoVo resourcesInfoVo : resourcesSet) { List resourceList = null; String resourcesUrl = resourcesInfoVo.getUrl(); try { - String resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body(); + String resourcesStr = HttpRequest.get(resourcesUrl) + .form(this.getGlobalParamObjMap(globalParamList, "form")) + .addHeaders(this.getGlobalParamMap(globalParamList, "header")) + .timeout(3000).execute().body(); resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class); } catch (Exception e) { logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage()); @@ -136,7 +141,10 @@ public class MgDocumentController { List swaggerResourceStrList = new LinkedList<>(); for (LocationListVo location : locationList) { try { - String resourceStr = HttpRequest.get(location.getLocation()).timeout(3000).execute().body(); + String resourceStr = HttpRequest.get(location.getLocation()) + .form(this.getGlobalParamObjMap(globalParamList, "form")) + .addHeaders(this.getGlobalParamMap(globalParamList, "header")) + .timeout(3000).execute().body(); String resourcesUrl = location.getLocation(); int indexV2 = location.getLocation().indexOf("/v2"); if (indexV2 >= 0) { @@ -297,14 +305,19 @@ public class MgDocumentController { resourcesList = JSON.parseArray(swaggerResourcesStr, SwaggerResourcesInfoVo.class); // 如果是编辑,把之前的删除掉,再在后面添加 if (StringUtils.isNotBlank(oldUrl)) { - final String tempStr = oldUrl; + String tempStr = oldUrl; resourcesList = resourcesList.stream().filter(val -> !Objects.equals(val.getUrl(), tempStr)).collect(Collectors.toList()); } } try { + List globalParamList = this.getGlobalParamList(); oldUrl = this.encodeUrlParam(oldUrl); resourcesUrl = this.encodeUrlParam(resourcesUrl); - String resourcesStr = HttpRequest.get(resourcesUrl).header("Accept", "application/json, text/javascript, */*; q=0.01").timeout(3000).execute().body(); + String resourcesStr = HttpRequest.get(resourcesUrl) + .form(this.getGlobalParamObjMap(globalParamList, "form")) + .addHeaders(this.getGlobalParamMap(globalParamList, "header")) + .header("Accept", "application/json, text/javascript, */*; q=0.01") + .timeout(3000).execute().body(); boolean isLocation = this.addSwaggerLocationList(resourcesStr, rewriteDomainUrl, resourcesUrl, oldUrl, openVisit); if (!isLocation) { List resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class); @@ -349,7 +362,11 @@ public class MgDocumentController { resourcesSet.addAll(resourcesList); } try { - String resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body(); + List globalParamList = this.getGlobalParamList(); + String resourcesStr = HttpRequest.get(resourcesUrl) + .form(this.getGlobalParamObjMap(globalParamList, "form")) + .addHeaders(this.getGlobalParamMap(globalParamList, "header")) + .timeout(3000).execute().body(); List resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class); if (resourceList == null || resourceList.isEmpty()) { return DocResponseJson.warn("该地址未找到文档"); @@ -500,6 +517,38 @@ public class MgDocumentController { return resourcesDomain + location; } + /** + * 全局参数 + */ + private List getGlobalParamList() { + String globalParamList = storageService.get(StorageKeys.GLOBAL_PARAM_LIST); + // paramIn key value + List resultList = new LinkedList<>(); + if (StringUtils.isBlank(globalParamList)) { + return resultList; + } + resultList = JSON.parseArray(globalParamList, GlobalParamVo.class); + return resultList; + } + + /** + * 获取指定类型的全局参数 + */ + private Map getGlobalParamMap(List globalParamList, String paramIn) { + Map paramMap = globalParamList.stream().filter(val -> Objects.equals(val.getParamIn(), paramIn)) + .collect(Collectors.toMap(GlobalParamVo::getKey, GlobalParamVo::getValue)); + return paramMap; + } + + /** + * 获取指定类型的全局参数 + */ + private Map getGlobalParamObjMap(List globalParamList, String paramIn) { + Map paramMap = globalParamList.stream().filter(val -> Objects.equals(val.getParamIn(), paramIn)) + .collect(Collectors.toMap(GlobalParamVo::getKey, GlobalParamVo::getValue)); + return paramMap; + } + /** * url编码参数 */ diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/controller/vo/GlobalParamVo.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/controller/vo/GlobalParamVo.java new file mode 100644 index 00000000..5c55e5af --- /dev/null +++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/controller/vo/GlobalParamVo.java @@ -0,0 +1,31 @@ +package com.zyplayer.doc.swagger.controller.vo; + +public class GlobalParamVo { + private String paramIn; + private String key; + private String value; + + public String getParamIn() { + return paramIn; + } + + public void setParamIn(String paramIn) { + this.paramIn = paramIn; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/constant/StorageKeys.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/constant/StorageKeys.java index b609f9ae..45ea5d4f 100644 --- a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/constant/StorageKeys.java +++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/constant/StorageKeys.java @@ -17,4 +17,6 @@ public class StorageKeys { public static final String SWAGGER_OFFLINE_DOC_START = "swagger-offline-doc-"; // 自增ID的key public static final String SWAGGER_ID_WORKER = "swagger-id-worker"; + // 全局参数 + public static final String GLOBAL_PARAM_LIST = "zyplayer-doc-global-param-list"; } diff --git a/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui-cache-keys.js b/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui-cache-keys.js index fa666b89..c8fe027e 100644 --- a/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui-cache-keys.js +++ b/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui-cache-keys.js @@ -8,4 +8,4 @@ var cacheKeys = { globalParamList: 'zyplayer-doc-global-param-list', pRequestObjStart: 'p-request-obj-', pSimulationResponse: 'p-simulation-response', -} \ No newline at end of file +}; \ No newline at end of file