api文档支持开放访问
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.zyplayer.doc.api.controller;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
@@ -12,6 +13,7 @@ import com.zyplayer.doc.data.config.security.DocUserDetails;
|
||||
import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.ApiDoc;
|
||||
import com.zyplayer.doc.data.service.manage.ApiDocService;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -86,6 +88,17 @@ public class ApiDocumentController {
|
||||
apiDoc.setCreateTime(new Date());
|
||||
apiDoc.setCreateUserId(currentUser.getUserId());
|
||||
apiDoc.setCreateUserName(currentUser.getUsername());
|
||||
if (apiDoc.getId() == null) {
|
||||
apiDoc.setShareUuid(IdUtil.simpleUUID());
|
||||
} else {
|
||||
ApiDoc apiDocSel = swaggerDocService.getById(apiDoc.getId());
|
||||
if (apiDocSel == null) {
|
||||
return DocResponseJson.warn("未找到指定的文档记录信息");
|
||||
}
|
||||
if (StringUtils.isBlank(apiDocSel.getShareUuid())) {
|
||||
apiDoc.setShareUuid(IdUtil.simpleUUID());
|
||||
}
|
||||
}
|
||||
// url类型
|
||||
if (Objects.equals(apiDoc.getDocType(), 1)) {
|
||||
// UI地址替换为文档json地址
|
||||
@@ -112,6 +125,7 @@ public class ApiDocumentController {
|
||||
apiDoc.setId(null);
|
||||
apiDoc.setDocUrl(swaggerDomain + resource.getUrl());
|
||||
apiDoc.setName(resource.getName());
|
||||
apiDoc.setShareUuid(IdUtil.simpleUUID());
|
||||
swaggerDocService.save(apiDoc);
|
||||
}
|
||||
} else {
|
||||
@@ -141,11 +155,7 @@ public class ApiDocumentController {
|
||||
ApiDoc swaggerDocUp = new ApiDoc();
|
||||
swaggerDocUp.setId(apiDoc.getId());
|
||||
swaggerDocUp.setDocStatus(apiDoc.getDocStatus());
|
||||
swaggerDocUp.setDocUrl(apiDoc.getDocUrl());
|
||||
swaggerDocUp.setJsonContent(apiDoc.getJsonContent());
|
||||
swaggerDocUp.setName(apiDoc.getName());
|
||||
swaggerDocUp.setOpenVisit(apiDoc.getOpenVisit());
|
||||
swaggerDocUp.setRewriteDomain(apiDoc.getRewriteDomain());
|
||||
swaggerDocUp.setShareInstruction(apiDoc.getShareInstruction());
|
||||
swaggerDocUp.setYn(apiDoc.getYn());
|
||||
swaggerDocService.updateById(swaggerDocUp);
|
||||
return DocResponseJson.ok();
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.zyplayer.doc.api.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.zyplayer.doc.api.framework.utils.SwaggerDocUtil;
|
||||
import com.zyplayer.doc.api.service.SwaggerHttpRequestService;
|
||||
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.data.config.security.DocUserDetails;
|
||||
import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.ApiDoc;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.ApiGlobalParam;
|
||||
import com.zyplayer.doc.data.service.manage.ApiDocService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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 java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 文档控制器
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2021年10月16日
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/doc-api/share")
|
||||
public class ApiShareDocumentController {
|
||||
private static Logger logger = LoggerFactory.getLogger(ApiShareDocumentController.class);
|
||||
|
||||
@Resource
|
||||
private ApiDocService swaggerDocService;
|
||||
@Resource
|
||||
private SwaggerHttpRequestService swaggerHttpRequestService;
|
||||
|
||||
/**
|
||||
* 获取文档内容
|
||||
*
|
||||
* @return 文档内容
|
||||
* @author 暮光:城中城
|
||||
* @since 2021年10月16日
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/detail")
|
||||
public ResponseJson<List<ApiDoc>> detail(String shareUuid) {
|
||||
ApiDoc apiDoc = swaggerDocService.getByShareUuid(shareUuid);
|
||||
if (apiDoc == null) {
|
||||
return DocResponseJson.warn("文档不存在");
|
||||
}
|
||||
apiDoc.setDocUrl(null);
|
||||
apiDoc.setJsonContent(null);
|
||||
return DocResponseJson.ok(apiDoc);
|
||||
}
|
||||
|
||||
@RequestMapping("/apis/detail")
|
||||
public ResponseJson<Object> detail(HttpServletRequest request, String shareUuid) {
|
||||
ApiDoc apiDoc = swaggerDocService.getByShareUuid(shareUuid);
|
||||
if (apiDoc == null) {
|
||||
return DocResponseJson.warn("文档不存在");
|
||||
}
|
||||
if (Objects.equals(apiDoc.getDocType(), 1)) {
|
||||
try {
|
||||
String docsDomain = SwaggerDocUtil.getV2ApiDocsDomain(apiDoc.getDocUrl());
|
||||
String contentStr = swaggerHttpRequestService.requestSwaggerUrl(request, apiDoc.getDocUrl(), docsDomain);
|
||||
return DocResponseJson.ok(contentStr);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return DocResponseJson.warn("请求文档失败");
|
||||
}
|
||||
}
|
||||
if (Objects.equals(apiDoc.getDocType(), 2) || Objects.equals(apiDoc.getDocType(), 4)) {
|
||||
return DocResponseJson.ok(apiDoc.getJsonContent());
|
||||
}
|
||||
return DocResponseJson.warn("暂不支持的文档类型");
|
||||
}
|
||||
}
|
||||
@@ -177,8 +177,10 @@ public class SwaggerHttpRequestService {
|
||||
*/
|
||||
private List<HttpCookie> getHttpCookie(HttpServletRequest request, Map<String, String> globalCookieParamMap, String headerCookie) {
|
||||
List<HttpCookie> httpCookies = new LinkedList<>();
|
||||
for (Cookie cookie : request.getCookies()) {
|
||||
httpCookies.add(new HttpCookie(cookie.getName(), cookie.getValue()));
|
||||
if (request.getCookies() != null) {
|
||||
for (Cookie cookie : request.getCookies()) {
|
||||
httpCookies.add(new HttpCookie(cookie.getName(), cookie.getValue()));
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(headerCookie)) {
|
||||
Arrays.stream(headerCookie.split(";")).map(String::trim).forEach(cookie -> {
|
||||
|
||||
Reference in New Issue
Block a user