优化文档存储,展示文档选择
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
|
||||
package com.zyplayer.doc.manage.framework.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.google.common.base.Predicates;
|
||||
import com.zyplayer.doc.swagger.framework.configuration.EnableSwaggerMgUi;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
@@ -15,7 +13,6 @@ import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
/**
|
||||
* 不需要管理本项目的文档,只需要开启@EnableSwaggerMgUi即可
|
||||
@@ -26,8 +23,8 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
//@EnableSwagger2
|
||||
@EnableSwaggerMgUi(
|
||||
selfDoc = false,// 是否开启自身的文档
|
||||
defaultResources = {// 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
|
||||
//"http://localhost:8080/swagger-resources"
|
||||
defaultLocation = {// 启动后第一次访问没有数据情况下需要加载进来的/v2/doc地址
|
||||
//"http://localhost:8080/v2/doc"
|
||||
}
|
||||
)
|
||||
public class SwaggerConfiguration {
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.swagger.controller.vo.LocationListVo;
|
||||
import com.zyplayer.doc.swagger.controller.vo.SwaggerLocationVo;
|
||||
import com.zyplayer.doc.swagger.controller.vo.SwaggerResourcesInfoVo;
|
||||
import com.zyplayer.doc.swagger.framework.configuration.EnableSwaggerMgUi;
|
||||
import com.zyplayer.doc.swagger.framework.configuration.SpringContextUtil;
|
||||
@@ -14,13 +16,13 @@ import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLEncoder;
|
||||
@@ -37,10 +39,9 @@ import java.util.stream.Collectors;
|
||||
@RestController
|
||||
@RequestMapping("/swagger-mg-ui/document")
|
||||
public class MgDocumentController {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(MgDocumentController.class);
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private MgStorageService storageService;
|
||||
|
||||
/**
|
||||
@@ -71,48 +72,33 @@ public class MgDocumentController {
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/docs")
|
||||
public void docs(HttpServletRequest request, HttpServletResponse response) {
|
||||
boolean needRestorage = true;
|
||||
String choiceDocList = request.getParameter("choiseDocList");
|
||||
// 转成set,防止重复
|
||||
String choiceLocationList = request.getParameter("choiceLocationList");
|
||||
List<SwaggerResourcesInfoVo> resourcesSet = new LinkedList<>();
|
||||
Set<String> swaggerDocsDeleteSet = new HashSet<>();
|
||||
List<LocationListVo> locationList = this.getLocationSet();
|
||||
|
||||
List<SwaggerResourcesInfoVo> resourcesList = new LinkedList<>();
|
||||
String swaggerResourcesStr = storageService.get(StorageKeys.SWAGGER_RESOURCES_LIST);
|
||||
String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
|
||||
if (StringUtils.isNotBlank(swaggerResourcesStr)) {
|
||||
resourcesList = JSON.parseArray(swaggerResourcesStr, SwaggerResourcesInfoVo.class);
|
||||
if (StringUtils.isNotBlank(choiceLocationList)) {
|
||||
// 当前choiceLocationList只有一个,可以这样搞
|
||||
locationList = locationList.stream().filter(val -> Objects.equals(val.getLocation(), choiceLocationList)).collect(Collectors.toList());
|
||||
}
|
||||
if (StringUtils.isNotBlank(choiceDocList)) {
|
||||
needRestorage = false;// 选择的则不再存入
|
||||
Map<String, SwaggerResourcesInfoVo> infoVoMap = resourcesList.stream().collect(Collectors.toMap(SwaggerResourcesInfoVo::getUrl, val -> val));
|
||||
for (String url : choiceDocList.split(",")) {
|
||||
SwaggerResourcesInfoVo resourcesInfoVo = infoVoMap.get(url);
|
||||
if (resourcesInfoVo != null) {
|
||||
resourcesSet.add(resourcesInfoVo);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (resourcesList.size() > 0) {
|
||||
resourcesSet.addAll(resourcesList);
|
||||
} else {
|
||||
// 默认加上自身的文档
|
||||
String serverPath = "http://" + request.getServerName() // 服务器地址
|
||||
+ ":" + request.getServerPort() // 端口号
|
||||
+ request.getContextPath();
|
||||
// 是否加入自身的文档
|
||||
Object object = SpringContextUtil.getBeanWithAnnotation(EnableSwaggerMgUi.class);
|
||||
EnableSwaggerMgUi swaggerMgUi = object.getClass().getAnnotation(EnableSwaggerMgUi.class);
|
||||
if (locationList.size() <= 0) {
|
||||
// 默认加上自身的文档
|
||||
String serverPath = "http://" + request.getServerName() // 服务器地址
|
||||
+ ":" + request.getServerPort() // 端口号
|
||||
+ request.getContextPath();
|
||||
// 是否加入自身的文档
|
||||
Object enableSwaggerMgUi = SpringContextUtil.getBeanWithAnnotation(EnableSwaggerMgUi.class);
|
||||
if (enableSwaggerMgUi != null) {
|
||||
EnableSwaggerMgUi swaggerMgUi = enableSwaggerMgUi.getClass().getAnnotation(EnableSwaggerMgUi.class);
|
||||
if (swaggerMgUi == null) {
|
||||
// 直接通过superclass去找
|
||||
Class<?> superclass = object.getClass().getSuperclass();
|
||||
Class<?> superclass = enableSwaggerMgUi.getClass().getSuperclass();
|
||||
if (superclass != null) {
|
||||
swaggerMgUi = superclass.getAnnotation(EnableSwaggerMgUi.class);
|
||||
}
|
||||
}
|
||||
if (swaggerMgUi == null) {
|
||||
// 再通过AopUtils去找
|
||||
Class<?> targetClass = AopUtils.getTargetClass(object);
|
||||
Class<?> targetClass = AopUtils.getTargetClass(enableSwaggerMgUi);
|
||||
if (targetClass != null) {
|
||||
swaggerMgUi = targetClass.getAnnotation(EnableSwaggerMgUi.class);
|
||||
}
|
||||
@@ -125,82 +111,194 @@ public class MgDocumentController {
|
||||
}
|
||||
// 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
|
||||
String[] defaultResources = swaggerMgUi.defaultResources();
|
||||
if (defaultResources != null && defaultResources.length > 0) {
|
||||
for (String url : defaultResources) {
|
||||
resourcesSet.add(new SwaggerResourcesInfoVo(url));
|
||||
}
|
||||
for (String url : defaultResources) {
|
||||
resourcesSet.add(new SwaggerResourcesInfoVo(url));
|
||||
}
|
||||
String[] defaultLocation = swaggerMgUi.defaultLocation();
|
||||
for (String url : defaultLocation) {
|
||||
locationList.add(new LocationListVo(url, ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
|
||||
List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
|
||||
swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
|
||||
}
|
||||
}
|
||||
List<Map<String, Object>> swaggerResourceList = new LinkedList<>();
|
||||
List<String> swaggerResourceStrList = new LinkedList<>();
|
||||
for (SwaggerResourcesInfoVo resourcesInfoVo : resourcesSet) {
|
||||
List<SwaggerResource> resourceList = null;
|
||||
String resourcesUrl = resourcesInfoVo.getUrl();
|
||||
try {
|
||||
String resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body();
|
||||
resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
}
|
||||
if (resourceList == null || resourceList.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
resourcesInfoVo.setResourceList(resourceList);
|
||||
resourcesUrl = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
for (SwaggerResource resource : resourceList) {
|
||||
String location = resource.getLocation();
|
||||
// 最后一个斜杠在resourcesUrl中已经加上,替换掉后面的防止两根斜杠
|
||||
location = location.startsWith("/") ? location.replaceFirst("/", "") : location;
|
||||
if (location.indexOf("?") >= 0) {
|
||||
try {
|
||||
String encode = URLEncoder.encode(resource.getName(), "utf-8");
|
||||
location = location.substring(0, location.lastIndexOf("?")) + "?group=" + encode;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (resourcesSet.size() > 0) {
|
||||
List<LocationListVo> locationListStorage = this.getLocationSet();
|
||||
for (SwaggerResourcesInfoVo resourcesInfoVo : resourcesSet) {
|
||||
List<SwaggerResource> resourceList = null;
|
||||
String resourcesUrl = resourcesInfoVo.getUrl();
|
||||
try {
|
||||
String resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body();
|
||||
resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
}
|
||||
location = resourcesUrl + location;
|
||||
// 已删除的则不处理
|
||||
if (swaggerDocsDeleteSet.contains(location)) {
|
||||
if (resourceList == null || resourceList.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
String resourceStr = HttpRequest.get(location).timeout(3000).execute().body();
|
||||
Map<String, Object> jsonObject = JSON.parseObject(resourceStr, new TypeReference<HashMap<String, Object>>(){});
|
||||
if (jsonObject == null || jsonObject.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
jsonObject.put("fullUrl", location);
|
||||
swaggerResourceList.add(jsonObject);
|
||||
// 本来想转对象之后赋值,但是在此转成JSON字符串之后格式就不是之前的了,所有不能转。。。
|
||||
// 直接字符串拼接,坑真多~
|
||||
String rewriteDomainUrl = Optional.ofNullable(resourcesInfoVo.getRewriteDomainUrl()).orElse("");
|
||||
resourceStr = resourceStr.substring(1);
|
||||
resourceStr = "{\"fullUrl\":\"" + location + "\","
|
||||
+ "\"domainUrl\":\"" + resourcesUrl + "\","
|
||||
+ "\"rewriteDomainUrl\":\"" + rewriteDomainUrl + "\","
|
||||
+ resourceStr;
|
||||
swaggerResourceStrList.add(resourceStr);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", location, e.getMessage());
|
||||
resourcesInfoVo.setResourceList(resourceList);
|
||||
String resourcesDomain = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
for (SwaggerResource swaggerResource : resourceList) {
|
||||
String location = this.getLocationUrl(resourcesDomain, swaggerResource.getLocation(), swaggerResource.getName());
|
||||
locationListStorage.add(new LocationListVo(location, resourcesUrl));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (needRestorage) {
|
||||
locationList.addAll(locationListStorage);
|
||||
this.storageSwaggerLocationList(locationListStorage);
|
||||
|
||||
AtomicInteger idIndex = new AtomicInteger(1);
|
||||
resourcesSet.forEach(val -> val.setId(idIndex.getAndIncrement()));
|
||||
storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesSet));
|
||||
}
|
||||
List<String> swaggerResourceStrList = new LinkedList<>();
|
||||
for (LocationListVo location : locationList) {
|
||||
try {
|
||||
String resourceStr = HttpRequest.get(location.getLocation()).timeout(3000).execute().body();
|
||||
Map<String, Object> jsonObject = JSON.parseObject(resourceStr, new TypeReference<HashMap<String, Object>>(){});
|
||||
if (jsonObject == null || jsonObject.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
jsonObject.put("fullUrl", location);
|
||||
|
||||
String resourcesUrl = location.getLocation();
|
||||
int indexV2 = location.getLocation().indexOf("/v2");
|
||||
if (indexV2 >= 0) {
|
||||
resourcesUrl = location.getLocation().substring(0, indexV2);
|
||||
}
|
||||
// 本来想转对象之后赋值,但是在此转成JSON字符串之后格式就不是之前的了,所有不能转。。。
|
||||
// 直接字符串拼接,坑真多~
|
||||
String rewriteDomainUrl = Optional.ofNullable(location.getRewriteDomainUrl()).orElse("");
|
||||
resourceStr = resourceStr.substring(1);
|
||||
resourceStr = "{\"fullUrl\":\"" + location.getLocation() + "\","
|
||||
+ "\"domainUrl\":\"" + resourcesUrl + "\","
|
||||
+ "\"rewriteDomainUrl\":\"" + rewriteDomainUrl + "\","
|
||||
+ resourceStr;
|
||||
swaggerResourceStrList.add(resourceStr);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", location, e.getMessage());
|
||||
}
|
||||
}
|
||||
// 用默认的json解析要内存溢出,解析不了JSONObject、、就只有这样写了~
|
||||
DocResponseJson.ok(swaggerResourceStrList).send(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的文档
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @param request request
|
||||
* @param response response
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/docsOld")
|
||||
public void docsOld(HttpServletRequest request, HttpServletResponse response) {
|
||||
// boolean needStorage = false;
|
||||
// String choiceDocList = request.getParameter("choiceDocList");
|
||||
// // 转成set,防止重复
|
||||
// List<SwaggerResourcesInfoVo> resourcesSet = new LinkedList<>();
|
||||
//
|
||||
// List<SwaggerResourcesInfoVo> resourcesList = new LinkedList<>();
|
||||
// String swaggerResourcesStr = storageService.get(StorageKeys.SWAGGER_RESOURCES_LIST);
|
||||
// if (StringUtils.isNotBlank(swaggerResourcesStr)) {
|
||||
// resourcesList = JSON.parseArray(swaggerResourcesStr, SwaggerResourcesInfoVo.class);
|
||||
// }
|
||||
// if (StringUtils.isNotBlank(choiceDocList)) {
|
||||
// Map<String, SwaggerResourcesInfoVo> infoVoMap = resourcesList.stream().collect(Collectors.toMap(SwaggerResourcesInfoVo::getUrl, val -> val));
|
||||
// for (String url : choiceDocList.split(",")) {
|
||||
// SwaggerResourcesInfoVo resourcesInfoVo = infoVoMap.get(url);
|
||||
// if (resourcesInfoVo != null) {
|
||||
// resourcesSet.add(resourcesInfoVo);
|
||||
// }
|
||||
// }
|
||||
// } else if (resourcesList.size() > 0) {
|
||||
// resourcesSet.addAll(resourcesList);
|
||||
// } else {
|
||||
// needStorage = true;
|
||||
// // 默认加上自身的文档
|
||||
// String serverPath = "http://" + request.getServerName() // 服务器地址
|
||||
// + ":" + request.getServerPort() // 端口号
|
||||
// + request.getContextPath();
|
||||
// // 是否加入自身的文档
|
||||
// Object enableSwaggerMgUi = SpringContextUtil.getBeanWithAnnotation(EnableSwaggerMgUi.class);
|
||||
// if (enableSwaggerMgUi != null) {
|
||||
// EnableSwaggerMgUi swaggerMgUi = enableSwaggerMgUi.getClass().getAnnotation(EnableSwaggerMgUi.class);
|
||||
// if (swaggerMgUi == null) {
|
||||
// // 直接通过superclass去找
|
||||
// Class<?> superclass = enableSwaggerMgUi.getClass().getSuperclass();
|
||||
// if (superclass != null) {
|
||||
// swaggerMgUi = superclass.getAnnotation(EnableSwaggerMgUi.class);
|
||||
// }
|
||||
// }
|
||||
// if (swaggerMgUi == null) {
|
||||
// // 再通过AopUtils去找
|
||||
// Class<?> targetClass = AopUtils.getTargetClass(enableSwaggerMgUi);
|
||||
// if (targetClass != null) {
|
||||
// swaggerMgUi = targetClass.getAnnotation(EnableSwaggerMgUi.class);
|
||||
// }
|
||||
// }
|
||||
// if (swaggerMgUi == null) {
|
||||
// resourcesSet.add(new SwaggerResourcesInfoVo(serverPath + "/swagger-resources"));
|
||||
// } else {
|
||||
// if (swaggerMgUi.selfDoc()) {
|
||||
// resourcesSet.add(new SwaggerResourcesInfoVo(serverPath + "/swagger-resources"));
|
||||
// }
|
||||
// // 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
|
||||
// String[] defaultResources = swaggerMgUi.defaultResources();
|
||||
// for (String url : defaultResources) {
|
||||
// resourcesSet.add(new SwaggerResourcesInfoVo(url));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// List<String> swaggerResourceStrList = new LinkedList<>();
|
||||
// for (SwaggerResourcesInfoVo resourcesInfoVo : resourcesSet) {
|
||||
// List<SwaggerResource> resourceList = null;
|
||||
// String resourcesUrl = resourcesInfoVo.getUrl();
|
||||
// try {
|
||||
// String resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body();
|
||||
// resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
// }
|
||||
// if (resourceList == null || resourceList.isEmpty()) {
|
||||
// continue;
|
||||
// }
|
||||
// resourcesInfoVo.setResourceList(resourceList);
|
||||
// resourcesUrl = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
// for (SwaggerResource resource : resourceList) {
|
||||
// String location = this.getLocationUrl(resourcesUrl, resource.getLocation(), resource.getName());
|
||||
// try {
|
||||
// String resourceStr = HttpRequest.get(location).timeout(3000).execute().body();
|
||||
// Map<String, Object> jsonObject = JSON.parseObject(resourceStr, new TypeReference<HashMap<String, Object>>(){});
|
||||
// if (jsonObject == null || jsonObject.isEmpty()) {
|
||||
// continue;
|
||||
// }
|
||||
// jsonObject.put("fullUrl", location);
|
||||
// // 本来想转对象之后赋值,但是在此转成JSON字符串之后格式就不是之前的了,所有不能转。。。
|
||||
// // 直接字符串拼接,坑真多~
|
||||
// String rewriteDomainUrl = Optional.ofNullable(resourcesInfoVo.getRewriteDomainUrl()).orElse("");
|
||||
// resourceStr = resourceStr.substring(1);
|
||||
// resourceStr = "{\"fullUrl\":\"" + location + "\","
|
||||
// + "\"domainUrl\":\"" + resourcesUrl + "\","
|
||||
// + "\"rewriteDomainUrl\":\"" + rewriteDomainUrl + "\","
|
||||
// + resourceStr;
|
||||
// swaggerResourceStrList.add(resourceStr);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("获取文档失败:{},{}", location, e.getMessage());
|
||||
// }
|
||||
// }
|
||||
// if (needStorage) {
|
||||
// this.addSwaggerLocationList(resourceList, resourcesUrl);
|
||||
// }
|
||||
// }
|
||||
// if (needStorage && resourcesSet.size() > 0) {
|
||||
// AtomicInteger idIndex = new AtomicInteger(1);
|
||||
// resourcesSet.forEach(val -> val.setId(idIndex.getAndIncrement()));
|
||||
// storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesSet));
|
||||
// }
|
||||
// // 用默认的json解析要内存溢出,解析不了JSONObject、、就只有这样写了~
|
||||
// DocResponseJson.ok(swaggerResourceStrList).send(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加/swagger-resources地址
|
||||
*
|
||||
@@ -212,71 +310,38 @@ public class MgDocumentController {
|
||||
@PostMapping(value = "/addSwaggerResources")
|
||||
public ResponseJson<Object> addSwaggerResources(String resourcesUrl, String rewriteDomainUrl, String oldUrl) {
|
||||
String swaggerResourcesStr = storageService.get(StorageKeys.SWAGGER_RESOURCES_LIST);
|
||||
String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
|
||||
Set<String> swaggerDocsDeleteSet = new HashSet<>();
|
||||
if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
|
||||
List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
|
||||
swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
|
||||
}
|
||||
// 转成set,防止重复
|
||||
List<SwaggerResourcesInfoVo> resourcesList = new LinkedList<>();
|
||||
if (StringUtils.isNotBlank(swaggerResourcesStr)) {
|
||||
resourcesList = JSON.parseArray(swaggerResourcesStr, SwaggerResourcesInfoVo.class);
|
||||
// 如果是编辑,把之前的删除掉,再在后面添加
|
||||
if (StringUtils.isNotBlank(oldUrl)) {
|
||||
resourcesList = resourcesList.stream().filter(val -> !Objects.equals(val.getUrl(), oldUrl)).collect(Collectors.toList());
|
||||
final String tempStr = oldUrl;
|
||||
resourcesList = resourcesList.stream().filter(val -> !Objects.equals(val.getUrl(), tempStr)).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
try {
|
||||
oldUrl = this.encodeUrlParam(oldUrl);
|
||||
resourcesUrl = this.encodeUrlParam(resourcesUrl);
|
||||
String resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body();
|
||||
List<SwaggerResource> resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class);
|
||||
if (resourceList == null || resourceList.isEmpty()) {
|
||||
return DocResponseJson.warn("该地址未找到文档");
|
||||
}
|
||||
// 重新加入的时候把之前的已删除的回恢复
|
||||
String resourcesDomain = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
for (SwaggerResource swaggerResource : resourceList) {
|
||||
String location = swaggerResource.getLocation();
|
||||
// 最后一个斜杠在resourcesUrl中已经加上,替换掉后面的防止两根斜杠
|
||||
location = location.startsWith("/") ? location.replaceFirst("/", "") : location;
|
||||
if (location.indexOf("?") >= 0) {
|
||||
try {
|
||||
String encode = URLEncoder.encode(swaggerResource.getName(), "utf-8");
|
||||
location = location.substring(0, location.lastIndexOf("?")) + "?group=" + encode;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
boolean isLocation = this.addSwaggerLocationList(resourcesStr, rewriteDomainUrl, resourcesUrl, oldUrl);
|
||||
if (!isLocation) {
|
||||
List<SwaggerResource> resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class);
|
||||
if (resourceList == null || resourceList.isEmpty()) {
|
||||
return DocResponseJson.warn("该地址未找到文档");
|
||||
}
|
||||
location = resourcesDomain + location;
|
||||
swaggerDocsDeleteSet.remove(location);
|
||||
this.addSwaggerLocationList(resourceList, resourcesUrl);
|
||||
SwaggerResourcesInfoVo resourcesInfoVo = new SwaggerResourcesInfoVo(resourcesUrl, resourceList);
|
||||
resourcesInfoVo.setRewriteDomainUrl(rewriteDomainUrl);
|
||||
resourcesList.add(resourcesInfoVo);
|
||||
AtomicInteger idIndex = new AtomicInteger(1);
|
||||
resourcesList.forEach(val -> val.setId(idIndex.getAndIncrement()));
|
||||
}
|
||||
SwaggerResourcesInfoVo resourcesInfoVo = new SwaggerResourcesInfoVo(resourcesUrl, resourceList);
|
||||
resourcesInfoVo.setRewriteDomainUrl(rewriteDomainUrl);
|
||||
resourcesList.add(resourcesInfoVo);
|
||||
AtomicInteger idIndex = new AtomicInteger(1);
|
||||
resourcesList.forEach(val -> val.setId(idIndex.getAndIncrement()));
|
||||
} catch (Exception e) {
|
||||
// 暂不想支持直接添加地址
|
||||
// try {
|
||||
// SwaggerLocationVo swaggerLocationVo = JSON.parseObject(resourcesStr, SwaggerLocationVo.class);
|
||||
// if (StringUtils.isNotBlank(swaggerLocationVo.getSwagger())) {
|
||||
// Set<String> locationSet = new HashSet<>();
|
||||
// if (StringUtils.isNotBlank(swaggerLocationListStr)) {
|
||||
// String swaggerLocationListStr = storageService.get(StorageKeys.SWAGGER_LOCATION_LIST);
|
||||
// List<String> locationList = JSON.parseArray(swaggerLocationListStr, String.class);
|
||||
// locationSet.addAll(locationList);
|
||||
// storageService.put(StorageKeys.SWAGGER_LOCATION_LIST, JSON.toJSONString(locationSet));
|
||||
// }
|
||||
// } else {
|
||||
// return DocResponseJson.warn("该地址查找文档失败");
|
||||
// }
|
||||
// } catch (Exception e2) {
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
return DocResponseJson.warn("该地址查找文档失败");
|
||||
// }
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
return DocResponseJson.warn("该地址查找文档失败");
|
||||
}
|
||||
storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesList));
|
||||
storageService.put(StorageKeys.SWAGGER_DOCS_DELETE_LIST, JSON.toJSONString(swaggerDocsDeleteSet));
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
@@ -303,22 +368,6 @@ public class MgDocumentController {
|
||||
if (resourceList == null || resourceList.isEmpty()) {
|
||||
return DocResponseJson.warn("该地址未找到文档");
|
||||
}
|
||||
// 重新加入的时候把之前的已删除的回恢复
|
||||
String resourcesDomain = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
for (SwaggerResource swaggerResource : resourceList) {
|
||||
String location = swaggerResource.getLocation();
|
||||
// 最后一个斜杠在resourcesUrl中已经加上,替换掉后面的防止两根斜杠
|
||||
location = location.startsWith("/") ? location.replaceFirst("/", "") : location;
|
||||
if (location.indexOf("?") >= 0) {
|
||||
try {
|
||||
String encode = URLEncoder.encode(swaggerResource.getName(), "utf-8");
|
||||
location = location.substring(0, location.lastIndexOf("?")) + "?group=" + encode;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
location = resourcesDomain + location;
|
||||
}
|
||||
resourcesSet.add(new SwaggerResourcesInfoVo(resourcesUrl));
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
@@ -336,16 +385,136 @@ public class MgDocumentController {
|
||||
* @param docUrl 文档地址
|
||||
* @return 删除结果
|
||||
*/
|
||||
@Deprecated
|
||||
@PostMapping(value = "/deleteSwaggerDoc")
|
||||
public ResponseJson<Object> deleteSwaggerDoc(String docUrl) {
|
||||
String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
|
||||
Set<String> swaggerDocsDeleteSet = new HashSet<>();
|
||||
if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
|
||||
List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
|
||||
swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
|
||||
}
|
||||
swaggerDocsDeleteSet.add(docUrl);
|
||||
storageService.put(StorageKeys.SWAGGER_DOCS_DELETE_LIST, JSON.toJSONString(swaggerDocsDeleteSet));
|
||||
// String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
|
||||
// Set<String> swaggerDocsDeleteSet = new HashSet<>();
|
||||
// if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
|
||||
// List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
|
||||
// swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
|
||||
// }
|
||||
// swaggerDocsDeleteSet.add(docUrl);
|
||||
// storageService.put(StorageKeys.SWAGGER_DOCS_DELETE_LIST, JSON.toJSONString(swaggerDocsDeleteSet));
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取swaggerLocation列表
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
@PostMapping(value = "/getLocationList")
|
||||
public ResponseJson<List<LocationListVo>> getLocationList() {
|
||||
List<LocationListVo> locationSet = this.getLocationSet();
|
||||
return DocResponseJson.ok(locationSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接添加地址
|
||||
*/
|
||||
private boolean addSwaggerLocationList(String resourcesStr, String rewriteDomainUrl, String locationUrl, String oldUrl) {
|
||||
try {
|
||||
SwaggerLocationVo swaggerLocationVo = JSON.parseObject(resourcesStr, SwaggerLocationVo.class);
|
||||
if (swaggerLocationVo != null && StringUtils.isNotBlank(swaggerLocationVo.getSwagger())) {
|
||||
List<LocationListVo> locationList = this.getLocationSet();
|
||||
locationList = locationList.stream().filter(val -> !Objects.equals(val.getLocation(), oldUrl)).collect(Collectors.toList());
|
||||
LocationListVo locationListVo = new LocationListVo(locationUrl, "");
|
||||
locationListVo.setRewriteDomainUrl(rewriteDomainUrl);
|
||||
locationList.add(locationListVo);
|
||||
this.storageSwaggerLocationList(locationList);
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接添加地址
|
||||
*/
|
||||
private void addSwaggerLocationList(List<SwaggerResource> resourceList, String resourcesUrl) {
|
||||
List<LocationListVo> locationList = this.getLocationSet();
|
||||
// 加入到location列表
|
||||
String resourcesDomain = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
for (SwaggerResource swaggerResource : resourceList) {
|
||||
String location = this.getLocationUrl(resourcesDomain, swaggerResource.getLocation(), swaggerResource.getName());
|
||||
locationList.add(new LocationListVo(location, resourcesUrl));
|
||||
}
|
||||
this.storageSwaggerLocationList(locationList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存location列表
|
||||
*/
|
||||
private void storageSwaggerLocationList(List<LocationListVo> locationSet) {
|
||||
AtomicInteger idIndex = new AtomicInteger(1);
|
||||
locationSet = locationSet.stream().distinct().collect(Collectors.toList());
|
||||
locationSet.forEach(val -> val.setId(idIndex.getAndIncrement()));
|
||||
storageService.put(StorageKeys.SWAGGER_LOCATION_LIST, JSON.toJSONString(locationSet));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取swaggerLocation列表
|
||||
*/
|
||||
private List<LocationListVo> getLocationSet() {
|
||||
List<LocationListVo> locationList = new LinkedList<>();
|
||||
String swaggerLocationListStr = storageService.get(StorageKeys.SWAGGER_LOCATION_LIST);
|
||||
if (StringUtils.isNotBlank(swaggerLocationListStr)) {
|
||||
locationList = JSON.parseArray(swaggerLocationListStr, LocationListVo.class);
|
||||
}
|
||||
return locationList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取拼接好的location
|
||||
*/
|
||||
private String getLocationUrl(String resourcesDomain, String location, String resourceName) {
|
||||
// 最后一个斜杠在resourcesUrl中已经加上,替换掉后面的防止两根斜杠
|
||||
location = location.startsWith("/") ? location.replaceFirst("/", "") : location;
|
||||
if (location.contains("?")) {
|
||||
try {
|
||||
String encode = URLEncoder.encode(resourceName, "utf-8");
|
||||
location = location.substring(0, location.lastIndexOf("?")) + "?group=" + encode;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return resourcesDomain + location;
|
||||
}
|
||||
|
||||
/**
|
||||
* url编码参数
|
||||
*/
|
||||
private String encodeUrlParam(String resourcesUrl) {
|
||||
int indexOf = resourcesUrl.indexOf("?");
|
||||
if (indexOf < 0) {
|
||||
return resourcesUrl;
|
||||
}
|
||||
StringBuilder baseUrl = new StringBuilder(resourcesUrl.substring(0, indexOf + 1));
|
||||
String[] paramArr = resourcesUrl.substring(indexOf + 1).split("&");
|
||||
for (int i = 0; i < paramArr.length; i++) {
|
||||
String param = paramArr[i];
|
||||
String[] split = param.split("=");
|
||||
if (i > 0) {
|
||||
baseUrl.append("&");
|
||||
}
|
||||
if (split.length == 2) {
|
||||
String value = split[1];
|
||||
try {
|
||||
value = URLEncoder.encode(split[1], "utf-8");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
baseUrl.append(split[0]).append("=").append(value);
|
||||
} else {
|
||||
baseUrl.append(param);
|
||||
}
|
||||
}
|
||||
return baseUrl.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.zyplayer.doc.swagger.controller;
|
||||
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.swagger.controller.vo.SwaggerResourcesInfoVo;
|
||||
import com.zyplayer.doc.swagger.framework.service.MgStorageService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 开放接口文档控制器
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年1月27日
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/swagger-mg-ui/open-doc")
|
||||
public class MgOpenDocController {
|
||||
private static Logger logger = LoggerFactory.getLogger(MgOpenDocController.class);
|
||||
|
||||
@Resource
|
||||
private MgStorageService storageService;
|
||||
|
||||
/**
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年1月27日
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/{source}")
|
||||
public ResponseJson<List<SwaggerResourcesInfoVo>> resourcesList(@PathVariable String source) {
|
||||
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.zyplayer.doc.swagger.controller.vo;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Objects;
|
||||
|
||||
public class LocationListVo {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String location;
|
||||
private String resources;
|
||||
private String rewriteDomainUrl;
|
||||
|
||||
public LocationListVo(){
|
||||
|
||||
}
|
||||
|
||||
public LocationListVo(String location, String resources) {
|
||||
this.location = location;
|
||||
this.resources = resources;
|
||||
this.name = location;
|
||||
// 修正名字
|
||||
int indexGroup = location.indexOf("group=");
|
||||
int indexV2 = location.indexOf("/v2");
|
||||
if (indexGroup >= 0 && location.length() > indexGroup) {
|
||||
try {
|
||||
this.name = URLDecoder.decode(location.substring(indexGroup + 6), "utf-8");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (indexV2 >= 0) {
|
||||
this.name = location.substring(indexV2 + 3);
|
||||
}
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public String getResources() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
public void setResources(String resources) {
|
||||
this.resources = resources;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(this.location, ((LocationListVo) obj).getLocation());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return location.hashCode();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getRewriteDomainUrl() {
|
||||
return rewriteDomainUrl;
|
||||
}
|
||||
|
||||
public void setRewriteDomainUrl(String rewriteDomainUrl) {
|
||||
this.rewriteDomainUrl = rewriteDomainUrl;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.zyplayer.doc.swagger.framework.configuration;
|
||||
|
||||
import com.zyplayer.doc.swagger.framework.filter.ZyplayerProxyFilter;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import com.zyplayer.doc.swagger.framework.filter.ZyplayerProxyFilter;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
|
||||
@Target(value = {java.lang.annotation.ElementType.TYPE})
|
||||
@Documented
|
||||
@@ -26,4 +26,11 @@ public @interface EnableSwaggerMgUi {
|
||||
* @return swagger-resources地址
|
||||
*/
|
||||
String[] defaultResources() default {};
|
||||
|
||||
/**
|
||||
* 启动后第一次访问没有数据情况下需要加载进来的/v2/doc地址
|
||||
*
|
||||
* @return /v2/doc地址
|
||||
*/
|
||||
String[] defaultLocation() default {};
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ package com.zyplayer.doc.swagger.framework.constant;
|
||||
public class StorageKeys {
|
||||
// 所有文档地址
|
||||
public static final String SWAGGER_RESOURCES_LIST = "swagger-resources-list";
|
||||
// 已删除的文档
|
||||
public static final String SWAGGER_DOCS_DELETE_LIST = "swagger-docs-delete-list";
|
||||
// 已删除的文档(废弃,不在支持删除)
|
||||
// public static final String SWAGGER_DOCS_DELETE_LIST = "swagger-docs-delete-list";
|
||||
// 所有详细文档地址
|
||||
public static final String SWAGGER_LOCATION_LIST = "swagger-location-list";
|
||||
// 文档的离线数据key
|
||||
|
||||
@@ -17,6 +17,12 @@
|
||||
<i class="icon icon-bars" id="changeContentWidth"></i>
|
||||
</div>
|
||||
<div class="scrollbar-hover left-container">
|
||||
<div class="btn-group choice-location-list" id="choiceLocationList">
|
||||
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
|
||||
<span class="choice-text"></span> <span class="caret"></span>
|
||||
</button>
|
||||
<ul class="dropdown-menu" role="menu"></ul>
|
||||
</div>
|
||||
<!-- 样式类可选:tree-menu tree-folders tree-chevrons tree-angles -->
|
||||
<ul class="tree tree-lines tree-menu projects" data-ride="tree">
|
||||
<li>
|
||||
@@ -34,6 +40,7 @@
|
||||
<li id="onlineDebugLi1" class="local-storage"><a href="javascript:void(0)" path=""><i class="icon-bug"></i> 在线调试管理</a></li>
|
||||
<li><a href="#" class="page-nav" data-id="docShowConfig" data-href="webjars/zpages/docShowConfig.html" data-icon="icon-cog" data-reload="0"><i class="icon-cog"></i> 文档展示配置</a></li>
|
||||
<li><a href="#" class="page-nav" data-id="docUrlConfig" data-href="webjars/zpages/docUrlConfig.html" data-icon="icon-list-ul" data-reload="1"><i class="icon-list-ul"></i> 文档地址管理</a></li>
|
||||
<li><a href="#" class="page-nav" data-id="docUrlDetailConfig" data-href="webjars/zpages/docUrlDetailConfig.html" data-icon="icon-list-ul" data-reload="1"><i class="icon-list-ul"></i> 详细地址管理</a></li>
|
||||
<li><a href="#" class="page-nav" data-id="globalParamConfig" data-href="webjars/zpages/globalParamConfig.html" data-icon="icon-globe" data-reload="1"><i class="icon-globe"></i> 全局参数管理</a></li>
|
||||
<li><a href="#" class="page-nav" data-id="debugDataConfig" data-href="webjars/zpages/debugDataConfig.html" data-icon="icon-bug" data-reload="1"><i class="icon-bug"></i> 调试数据管理</a></li>
|
||||
</ul>
|
||||
@@ -48,7 +55,10 @@
|
||||
<div class="right-container" id="rightContent">
|
||||
<div class="right-container" id="rightContentMask"></div>
|
||||
<div class="tab-page tab-home-page">
|
||||
<div id="homePageDashboard" class="dashboard" data-height="320">
|
||||
<div id="homePageDashboard">
|
||||
<div class="dashboard" data-height="320"></div>
|
||||
</div>
|
||||
<div id="homePageDashboardTemplate" class="hidden">
|
||||
<section class="row">
|
||||
<div class="col-md-6 col-sm-6">
|
||||
<div class="panel" data-id="1">
|
||||
|
||||
@@ -18,6 +18,11 @@ ul{list-style: none;list-style-type: none;}
|
||||
|
||||
.local-storage{display: none;}
|
||||
|
||||
.choice-location-list{margin-bottom: 10px; width: 100%;}
|
||||
.choice-location-list .btn.dropdown-toggle{width: 100%; text-align: left;}
|
||||
.choice-location-list .dropdown-menu{width: 100%;}
|
||||
.choice-location-list .caret{float: right;margin-top: 8px;}
|
||||
|
||||
/**lable的覆盖样式*/
|
||||
.label{font-size: 100%;}
|
||||
.label-warning {background-color: #f9f5ee; color: #f1a325;}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
var cacheKeys = {
|
||||
userSettings: 'userSettings',
|
||||
swaggerResourcesList: 'swagger-resources-list',
|
||||
swaggerLocationList: 'swagger-location-list',
|
||||
globalParamList: 'zyplayer-doc-global-param-list',
|
||||
pRequestObjStart: 'p-request-obj-',
|
||||
pSimulationResponse: 'p-simulation-response',
|
||||
|
||||
@@ -49,13 +49,13 @@ $(document).ready(function(){
|
||||
ajaxTemp("swagger-mg-ui/storage/checkConfig", "post", "json", {
|
||||
}, function(json){
|
||||
}, function(msg){
|
||||
}, function(xhr){
|
||||
}, function (xhr) {
|
||||
showGlobalLoadingMessage('服务检查完成,请稍候...', true);
|
||||
var serverStorage = false;
|
||||
if(!isEmptyObject(xhr.responseJSON)) {
|
||||
if (!isEmptyObject(xhr.responseJSON)) {
|
||||
serverStorage = (xhr.responseJSON.errCode == 200);
|
||||
}
|
||||
if(!serverStorage) {
|
||||
if (!serverStorage) {
|
||||
Toast.error("服务器端能力配置有误,新版本必须开启才能使用", 999999);
|
||||
documentLoadFinish();
|
||||
} else {
|
||||
@@ -63,6 +63,12 @@ $(document).ready(function(){
|
||||
}
|
||||
}
|
||||
);
|
||||
// 定义配置的标签页
|
||||
var tabsArr = [
|
||||
{id: 'docShowConfig', url: 'webjars/zpages/docShowConfig.html', type: 'iframe', icon: 'icon-cog', forbidClose: true}
|
||||
];
|
||||
$('#rightZpages').tabs({tabs: tabsArr});
|
||||
rightContentTabs = $('#rightZpages').data('zui.tabs');
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -72,16 +78,28 @@ $(document).ready(function(){
|
||||
function getDocumentListByService() {
|
||||
$("#choiseDocListUl").empty();
|
||||
showGlobalLoadingMessage('获取文档列表中,请稍候...', true);
|
||||
ajaxTemp("swagger-mg-ui/document/resourcesList", "post", "json", {}, function(json){
|
||||
if(validateResult(json) && json.data.length > 1) {
|
||||
showGlobalLoadingMessage('等待选择需展示的文档,请选择...', true);
|
||||
// ajaxTemp("swagger-mg-ui/document/resourcesList", "post", "json", {}, function(json){
|
||||
// if(validateResult(json) && json.data.length > 1) {
|
||||
// showGlobalLoadingMessage('等待选择需展示的文档,请选择...', true);
|
||||
// for (var i = 0; i < json.data.length; i++) {
|
||||
// var item = json.data[i];
|
||||
// $("#choiseDocListUl").append('<li value="'+item.url+'">'+item.url+'</li>');
|
||||
// }
|
||||
// $('#choiseDocModal').modal({moveable:true, backdrop:'static', keyboard: false});
|
||||
// } else {
|
||||
// addDocumentByService();
|
||||
// }
|
||||
// });
|
||||
ajaxTemp("swagger-mg-ui/document/getLocationList", "post", "json", {}, function (json) {
|
||||
if (validateResult(json) && json.data.length >= 1) {
|
||||
for (var i = 0; i < json.data.length; i++) {
|
||||
var item = json.data[i];
|
||||
$("#choiseDocListUl").append('<li value="'+item.url+'">'+item.url+'</li>');
|
||||
$("#choiceLocationList .dropdown-menu").append('<li><a href="javascript:void(0);" data-location="' + item.location + '">' + item.name + '</a></li>');
|
||||
}
|
||||
$('#choiseDocModal').modal({moveable:true, backdrop:'static', keyboard: false});
|
||||
$("#choiceLocationList .choice-text").text(json.data[0].name);
|
||||
addDocumentByLocationService(json.data[0].location);
|
||||
} else {
|
||||
addDocumentByService();
|
||||
addDocumentByLocationService();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -90,13 +108,13 @@ function getDocumentListByService() {
|
||||
* 增加文档-通过服务器接口
|
||||
* @returns
|
||||
*/
|
||||
function addDocumentByService(choiseDocList) {
|
||||
function addDocumentByService(choiceDocList) {
|
||||
// 获取原始的swagger的json对象
|
||||
showGlobalLoadingMessage('通过服务器端获取文档中,请稍候...', true);
|
||||
ajaxTemp("swagger-mg-ui/document/docs", "post", "json", {choiseDocList: choiseDocList}, function(json){
|
||||
ajaxTemp("swagger-mg-ui/document/docs", "post", "json", {choiceDocList: choiceDocList}, function(json){
|
||||
//console.log(json);
|
||||
showGlobalLoadingMessage('文档获取成功,解析中,请稍候...', true);
|
||||
if(isEmptyObject(json) || json.errCode != 200) {
|
||||
if(isEmptyObject(json) || json.errCode !== 200) {
|
||||
Toast.error("获取文档错误,请检查!");
|
||||
return;
|
||||
}
|
||||
@@ -121,6 +139,48 @@ function addDocumentByService(choiseDocList) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加文档-通过服务器接口
|
||||
* @returns
|
||||
*/
|
||||
function addDocumentByLocationService(choiceDocList) {
|
||||
// 获取原始的swagger的json对象
|
||||
globalLoadingMessager.show();
|
||||
showGlobalLoadingMessage('通过服务器端获取文档中,请稍候...', true);
|
||||
ajaxTemp("swagger-mg-ui/document/docs", "post", "json", {choiceLocationList: choiceDocList}, function(json){
|
||||
//console.log(json);
|
||||
showGlobalLoadingMessage('文档获取成功,解析中,请稍候...', true);
|
||||
if(isEmptyObject(json) || json.errCode !== 200) {
|
||||
Toast.error("获取文档错误,请检查!");
|
||||
return;
|
||||
}
|
||||
documentJsonArr = [];
|
||||
var template = $('#homePageDashboardTemplate').html();
|
||||
$('#homePageDashboard').empty();
|
||||
$('#homePageDashboard').append('<div class="dashboard" data-height="320"></div>');
|
||||
$('#homePageDashboard .dashboard').append(template);
|
||||
|
||||
for (var i = 0; i < json.data.length; i++) {
|
||||
showGlobalLoadingMessage('解析第' + (i + 1) + '份文档,请稍候...', true);
|
||||
var tempDoc = deserialize(json.data[i]);
|
||||
console.log(tempDoc);
|
||||
documentJsonArr.push(tempDoc);// 加到所有文档
|
||||
addHomePageDashboard(tempDoc, tempDoc.fullUrl);
|
||||
createDefinitionsMapByJson(tempDoc);
|
||||
if (userSettings.catalogShowType == 1) {
|
||||
createTreeViewByTree(tempDoc);// url分成一层一层的展示
|
||||
} else if (userSettings.catalogShowType == 2) {
|
||||
createTreeViewByTag(tempDoc);// tag方式,整个url显示为一层
|
||||
} else {
|
||||
createTreeViewByTree(tempDoc);// url分成一层一层的展示
|
||||
}
|
||||
}
|
||||
documentLoadFinish();
|
||||
}, function(msg){
|
||||
Toast.error("获取文档失败,请检查!");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 自由拖动改变左右框架的宽度
|
||||
*/
|
||||
@@ -150,6 +210,17 @@ $("#changeContentWidth").click(function(){
|
||||
changeContentWidth(isMinWidth ? 360 : 120);
|
||||
});
|
||||
|
||||
/**
|
||||
* 切换文档
|
||||
*/
|
||||
$("#choiceLocationList").on("click", ".dropdown-menu li", function(){
|
||||
var text = $(this).find("a").text();
|
||||
var location = $(this).find("a").data("location");
|
||||
$("#choiceLocationList .choice-text").text(text);
|
||||
// 切换文档
|
||||
addDocumentByLocationService(location);
|
||||
});
|
||||
|
||||
/**
|
||||
* 搜索框回车事件
|
||||
*/
|
||||
@@ -344,7 +415,7 @@ $("#apiPathTree").on("click", ".show-doc", function(){
|
||||
$("#simulationResultText").val("");
|
||||
getStorage('p-simulation-response-' + docUrl, function(data){
|
||||
var resultText = getNotEmptyStr(data);
|
||||
resultText = (typeof resultText === 'string') ? resultText : JSON.stringify(resultText, null, 4);
|
||||
resultText = (typeof resultText == 'string') ? resultText : JSON.stringify(resultText, null, 4);
|
||||
$("#simulationResultText").val(resultText);
|
||||
});
|
||||
// 处理在线文档
|
||||
@@ -885,27 +956,6 @@ function addHomePageDashboard(json, fullUrl) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化用户的设置
|
||||
* @param
|
||||
* @returns
|
||||
*/
|
||||
function documentLoadFinish() {
|
||||
showGlobalLoadingMessage('文档解析完成!', false);
|
||||
// 定义配置的标签页
|
||||
var tabsArr = [
|
||||
{id: 'docShowConfig', url: 'webjars/zpages/docShowConfig.html', type: 'iframe', icon: 'icon-cog', forbidClose: true},
|
||||
];
|
||||
$('#rightZpages').tabs({tabs: tabsArr});
|
||||
rightContentTabs = $('#rightZpages').data('zui.tabs');
|
||||
// 隐藏提示框
|
||||
setTimeout(function() {
|
||||
globalLoadingMessager.hide();
|
||||
}, 1000);
|
||||
$('#apiPathTree .projects').tree();
|
||||
$('#homePageDashboard').dashboard({draggable: false});
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改树形菜单展示类型
|
||||
* @param
|
||||
@@ -935,6 +985,22 @@ function updateTreeShowType() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化用户的设置
|
||||
* @param
|
||||
* @returns
|
||||
*/
|
||||
function documentLoadFinish() {
|
||||
showGlobalLoadingMessage('文档解析完成!', false);
|
||||
// 隐藏提示框
|
||||
setTimeout(function() {
|
||||
globalLoadingMessager.hide();
|
||||
}, 1000);
|
||||
regeneratePathTree();
|
||||
//$('#apiPathTree .projects').tree();
|
||||
$('#homePageDashboard .dashboard').dashboard({draggable: false});
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储用户设置
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
<!doctype html>
|
||||
<html xmlns:v-on="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>详细地址管理</title>
|
||||
<link rel="stylesheet" type="text/css" href="../zui/css/zui.min.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<table class="table table-bordered setting-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<td style="width: 50px;">序号</td>
|
||||
<td>地址</td>
|
||||
<td>重写域名地址</td>
|
||||
<td>操作</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="(item,index) in swaggerLocationList" :key="item.id" :data-id="item.id" :data-index="index" >
|
||||
<td>{{index+1}}</td>
|
||||
<td>{{item.location}}</td>
|
||||
<td>{{item.rewriteDomainUrl}}</td>
|
||||
<td>
|
||||
<button class="btn btn-danger" type="button" v-on:click="deleteDocUrl($event)">删除</button>
|
||||
<button class="btn btn-info" type="button" v-on:click="editDocUrl($event)">编辑</button>
|
||||
<!--<button class="btn btn-danger" type="button" v-on:click="syncDocData($event)">持久化</button>-->
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="4" align="center">
|
||||
<button class="btn" type="button" v-on:click="btnRefreshList"> 刷新 </button>
|
||||
<button class="btn btn-info" type="button" v-on:click="exportDocument">导出文档</button>
|
||||
<button class="btn btn-primary" type="button" v-on:click="addNewDocument">增加文档</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<!-- 增加文档弹出框 -->
|
||||
<div class="modal fade" id="addNewDocumentModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal">
|
||||
<span aria-hidden="true">×</span><span class="sr-only">关闭</span>
|
||||
</button>
|
||||
<h4 class="modal-title">输入文档地址</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="input-line">
|
||||
地址:
|
||||
<input v-model="addNewDocumentInput" type="text" class="form-control" placeholder="例:http://192.168.0.172/swagger-resources 或 http://192.168.0.172/v2/api-docs">
|
||||
</div>
|
||||
<div class="input-line">
|
||||
重写域名地址:
|
||||
<input v-model="rewriteDomainUrl" type="text" name="rewriteDomainUrl" class="form-control" placeholder="文档展示配置页 勾选“重写域名”重写的地址">
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary" v-on:click="addNewDocumentBtn">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script src="../mg-ui/js/jquery-3.1.0.min.js"></script>
|
||||
<script src="../zui/js/zui.min.js"></script>
|
||||
<script src="../mg-ui/js/common.js"></script>
|
||||
<script src="../mg-ui/js/mg-ui-cache-keys.js"></script>
|
||||
<script src="../vue/vue.js"></script>
|
||||
<script src="../mg-ui/js/toast.js"></script>
|
||||
|
||||
<script>
|
||||
var urlBase = "../../";
|
||||
var app = new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
swaggerLocationList: [],
|
||||
addNewDocumentInput: '',
|
||||
rewriteDomainUrl: '',
|
||||
oldLocation: ''
|
||||
},
|
||||
methods: {
|
||||
btnRefreshList: function(){
|
||||
this.refreshList();
|
||||
Toast.success("刷新成功!");
|
||||
},
|
||||
refreshList: function () {
|
||||
getStorage(cacheKeys.swaggerLocationList, function (data) {
|
||||
//console.log(data);
|
||||
var swaggerLocationList = data;
|
||||
for (var i = 0; i < swaggerLocationList.length; i++) {
|
||||
swaggerLocationList[i].location = decodeURI(swaggerLocationList[i].location);
|
||||
}
|
||||
app.swaggerLocationList = swaggerLocationList;
|
||||
});
|
||||
},
|
||||
editDocUrl: function (event) {
|
||||
var tr = $(event.currentTarget).parents("tr");
|
||||
var index = tr.data("index");
|
||||
var item = app.swaggerLocationList[index];
|
||||
app.oldLocation = item.location;
|
||||
app.addNewDocumentInput = item.location;
|
||||
app.rewriteDomainUrl = item.rewriteDomainUrl;
|
||||
$('#addNewDocumentModal').modal({moveable: true});
|
||||
},
|
||||
addNewDocument: function(){
|
||||
app.oldLocation = '';
|
||||
app.addNewDocumentInput = '';
|
||||
$('#addNewDocumentModal').modal({moveable:true});
|
||||
},
|
||||
addNewDocumentBtn: function(){
|
||||
var addNewDocumentInput = app.addNewDocumentInput;
|
||||
if(isEmpty(addNewDocumentInput)) {
|
||||
Toast.error("地址不可以为空");return;
|
||||
}
|
||||
var param = {
|
||||
resourcesUrl: addNewDocumentInput,
|
||||
rewriteDomainUrl: app.rewriteDomainUrl,
|
||||
oldUrl: app.oldLocation
|
||||
};
|
||||
ajaxTemp(urlBase + "swagger-mg-ui/document/addSwaggerResources", "post", "json", param, function(json){
|
||||
if(validateResult(json)) {
|
||||
//window.parent.document.location.reload();
|
||||
//app.swaggerLocationList.push(addNewDocumentInput);
|
||||
app.refreshList();
|
||||
$('#addNewDocumentModal').modal('hide');
|
||||
Toast.success("保存成功,刷新后生效!");
|
||||
}
|
||||
});
|
||||
},
|
||||
exportDocument: function () {
|
||||
getExport().exportDocument();
|
||||
},
|
||||
deleteDocUrl: function (event) {
|
||||
if (!confirm("确定要删除吗?")) {
|
||||
return;
|
||||
}
|
||||
var tr = $(event.currentTarget).parents("tr");
|
||||
var index = tr.data("index");
|
||||
var newDocList = [];
|
||||
for (var i = 0; i < app.swaggerLocationList.length; i++) {
|
||||
if (i !== index) {
|
||||
newDocList.push(app.swaggerLocationList[i]);
|
||||
}
|
||||
}
|
||||
setStorage(cacheKeys.swaggerLocationList, newDocList, function () {
|
||||
app.swaggerLocationList = newDocList;
|
||||
});
|
||||
},
|
||||
syncDocData: function (event) {
|
||||
var tr = $(event.currentTarget).parents("tr");
|
||||
var index = tr.data("index");
|
||||
var newDocUrl = app.swaggerLocationList[index].location;
|
||||
ajaxTemp(urlBase + "swagger-mg-ui/document/syncDocData", "post", "json", {resourcesUrl: newDocUrl}, function (json) {
|
||||
if (validateResult(json)) {
|
||||
app.refreshList();
|
||||
Toast.success("持久化成功!");
|
||||
}
|
||||
});
|
||||
},
|
||||
encodeUrlParam: function(resourcesUrl) {
|
||||
var indexOf = resourcesUrl.indexOf("?");
|
||||
if (indexOf < 0) {
|
||||
return resourcesUrl;
|
||||
}
|
||||
var baseUrl = resourcesUrl.substring(0, indexOf + 1);
|
||||
var paramArr = resourcesUrl.substring(indexOf + 1).split("&");
|
||||
for (var i = 0; i < paramArr.length; i++) {
|
||||
var param = paramArr[i];
|
||||
var split = param.split("=");
|
||||
if (i > 0) {
|
||||
baseUrl.append("&");
|
||||
}
|
||||
if (split.length === 2) {
|
||||
baseUrl += split[0] + "=" + encodeURI(split[1]);
|
||||
} else {
|
||||
baseUrl += param;
|
||||
}
|
||||
}
|
||||
return baseUrl.toString();
|
||||
}
|
||||
},
|
||||
mounted: function(){
|
||||
this.refreshList();
|
||||
},
|
||||
watch: {
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
#app{padding-top: 10px;}
|
||||
.input-line{margin-bottom: 15px;}
|
||||
</style>
|
||||
</html>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user