修改api文档

This commit is contained in:
D 2024-01-10 23:39:35 +08:00
parent c00a7c5375
commit 66fdc286de
5 changed files with 101 additions and 91 deletions

View File

@ -22,6 +22,8 @@ import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.common.utils.uuid.IdUtils; import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@ -30,9 +32,9 @@ import jakarta.servlet.http.HttpServletResponse;
* *
* @author ruoyi * @author ruoyi
*/ */
@Tag(name = "验证码")
@RestController @RestController
public class CaptchaController public class CaptchaController {
{
@Resource(name = "captchaProducer") @Resource(name = "captchaProducer")
private Producer captchaProducer; private Producer captchaProducer;
@ -44,18 +46,18 @@ public class CaptchaController
@Autowired @Autowired
private ISysConfigService configService; private ISysConfigService configService;
/** /**
* 生成验证码 * 生成验证码
*/ */
@Operation(summary = "获取验证码")
@Anonymous @Anonymous
@GetMapping("/captchaImage") @GetMapping("/captchaImage")
public AjaxResult getCode(HttpServletResponse response) throws IOException public AjaxResult getCode(HttpServletResponse response) throws IOException {
{
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
boolean captchaEnabled = configService.selectCaptchaEnabled(); boolean captchaEnabled = configService.selectCaptchaEnabled();
ajax.put("captchaEnabled", captchaEnabled); ajax.put("captchaEnabled", captchaEnabled);
if (!captchaEnabled) if (!captchaEnabled) {
{
return ajax; return ajax;
} }
@ -68,15 +70,12 @@ public class CaptchaController
// 生成验证码 // 生成验证码
String captchaType = RuoYiConfig.getCaptchaType(); String captchaType = RuoYiConfig.getCaptchaType();
if ("math".equals(captchaType)) if ("math".equals(captchaType)) {
{
String capText = captchaProducerMath.createText(); String capText = captchaProducerMath.createText();
capStr = capText.substring(0, capText.lastIndexOf("@")); capStr = capText.substring(0, capText.lastIndexOf("@"));
code = capText.substring(capText.lastIndexOf("@") + 1); code = capText.substring(capText.lastIndexOf("@") + 1);
image = captchaProducerMath.createImage(capStr); image = captchaProducerMath.createImage(capStr);
} } else if ("char".equals(captchaType)) {
else if ("char".equals(captchaType))
{
capStr = code = captchaProducer.createText(); capStr = code = captchaProducer.createText();
image = captchaProducer.createImage(capStr); image = captchaProducer.createImage(capStr);
} }
@ -84,12 +83,9 @@ public class CaptchaController
redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
// 转换流信息写出 // 转换流信息写出
FastByteArrayOutputStream os = new FastByteArrayOutputStream(); FastByteArrayOutputStream os = new FastByteArrayOutputStream();
try try {
{
ImageIO.write(image, "jpg", os); ImageIO.write(image, "jpg", os);
} } catch (IOException e) {
catch (IOException e)
{
return AjaxResult.error(e.getMessage()); return AjaxResult.error(e.getMessage());
} }

View File

@ -2,8 +2,7 @@ package com.ruoyi.web.controller.common;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -11,8 +10,10 @@ import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
@ -21,15 +22,22 @@ import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig; import com.ruoyi.framework.config.ServerConfig;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/** /**
* 通用请求处理 * 通用请求处理
* *
* @author ruoyi * @author ruoyi
*/ */
@Tag(name = "通用请求处理")
@RestController @RestController
@RequestMapping("/common") @RequestMapping("/common")
public class CommonController public class CommonController {
{
private static final Logger log = LoggerFactory.getLogger(CommonController.class); private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@Autowired @Autowired
@ -43,13 +51,19 @@ public class CommonController
* @param fileName 文件名称 * @param fileName 文件名称
* @param delete 是否删除 * @param delete 是否删除
*/ */
@Operation(summary = "通用下载请求")
@Parameters({
@Parameter(name = "fileName", description = "文件名称"),
@Parameter(name = "delete", description = "是否删除")
})
@GetMapping("/download") @GetMapping("/download")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) public void fileDownload(
{ @RequestParam("fileName") String fileName,
try @RequestParam("delete") Boolean delete,
{ HttpServletResponse response,
if (!FileUtils.checkAllowDownload(fileName)) HttpServletRequest request) {
{ try {
if (!FileUtils.checkAllowDownload(fileName)) {
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName)); throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
} }
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
@ -58,13 +72,10 @@ public class CommonController
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, realFileName); FileUtils.setAttachmentResponseHeader(response, realFileName);
FileUtils.writeBytes(filePath, response.getOutputStream()); FileUtils.writeBytes(filePath, response.getOutputStream());
if (delete) if (delete) {
{
FileUtils.deleteFile(filePath); FileUtils.deleteFile(filePath);
} }
} } catch (Exception e) {
catch (Exception e)
{
log.error("下载文件失败", e); log.error("下载文件失败", e);
} }
} }
@ -72,11 +83,10 @@ public class CommonController
/** /**
* 通用上传请求单个 * 通用上传请求单个
*/ */
@Operation(summary = "通用上传请求(单个)")
@PostMapping("/upload") @PostMapping("/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception public AjaxResult uploadFile(MultipartFile file) throws Exception {
{ try {
try
{
// 上传文件路径 // 上传文件路径
String filePath = RuoYiConfig.getUploadPath(); String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称 // 上传并返回新文件名称
@ -88,9 +98,7 @@ public class CommonController
ajax.put("newFileName", FileUtils.getName(fileName)); ajax.put("newFileName", FileUtils.getName(fileName));
ajax.put("originalFilename", file.getOriginalFilename()); ajax.put("originalFilename", file.getOriginalFilename());
return ajax; return ajax;
} } catch (Exception e) {
catch (Exception e)
{
return AjaxResult.error(e.getMessage()); return AjaxResult.error(e.getMessage());
} }
} }
@ -98,19 +106,18 @@ public class CommonController
/** /**
* 通用上传请求多个 * 通用上传请求多个
*/ */
@Operation(summary = "通用上传请求(多个)")
@PostMapping("/uploads") @PostMapping("/uploads")
public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception public AjaxResult uploadFiles(List<MultipartFile> files)
{ throws Exception {
try try {
{
// 上传文件路径 // 上传文件路径
String filePath = RuoYiConfig.getUploadPath(); String filePath = RuoYiConfig.getUploadPath();
List<String> urls = new ArrayList<String>(); List<String> urls = new ArrayList<String>();
List<String> fileNames = new ArrayList<String>(); List<String> fileNames = new ArrayList<String>();
List<String> newFileNames = new ArrayList<String>(); List<String> newFileNames = new ArrayList<String>();
List<String> originalFilenames = new ArrayList<String>(); List<String> originalFilenames = new ArrayList<String>();
for (MultipartFile file : files) for (MultipartFile file : files) {
{
// 上传并返回新文件名称 // 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file); String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName; String url = serverConfig.getUrl() + fileName;
@ -125,9 +132,7 @@ public class CommonController
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER)); ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
return ajax; return ajax;
} } catch (Exception e) {
catch (Exception e)
{
return AjaxResult.error(e.getMessage()); return AjaxResult.error(e.getMessage());
} }
} }
@ -135,14 +140,13 @@ public class CommonController
/** /**
* 本地资源通用下载 * 本地资源通用下载
*/ */
@Operation(summary = "本地资源通用下载")
@GetMapping("/download/resource") @GetMapping("/download/resource")
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) public void resourceDownload(@Parameter(name = "resource", description = "资源名称") String resource,
throws Exception HttpServletRequest request, HttpServletResponse response)
{ throws Exception {
try try {
{ if (!FileUtils.checkAllowDownload(resource)) {
if (!FileUtils.checkAllowDownload(resource))
{
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource)); throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
} }
// 本地资源路径 // 本地资源路径
@ -154,9 +158,7 @@ public class CommonController
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, downloadName); FileUtils.setAttachmentResponseHeader(response, downloadName);
FileUtils.writeBytes(downloadPath, response.getOutputStream()); FileUtils.writeBytes(downloadPath, response.getOutputStream());
} } catch (Exception e) {
catch (Exception e)
{
log.error("下载文件失败", e); log.error("下载文件失败", e);
} }
} }

View File

@ -1,12 +1,13 @@
package com.ruoyi.web.core.config; package com.ruoyi.web.core.config;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.ExternalDocumentation; import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License; import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class SwaggerConfig { public class SwaggerConfig {
@ -26,10 +27,18 @@ public class SwaggerConfig {
@Bean @Bean
public GroupedOpenApi sysApi() { public GroupedOpenApi sysApi() {
return GroupedOpenApi.builder() return GroupedOpenApi.builder()
.group("sys系统") .group("sys系统模块")
.pathsToMatch("/system/**") .pathsToMatch("/system/**")
.packagesToScan( .packagesToScan("com.ruoyi.web.controller")
"com.ruoyi.web.controller") .build();
}
@Bean
public GroupedOpenApi commonApi() {
return GroupedOpenApi.builder()
.group("基础模块")
.pathsToMatch("/common/**")
.packagesToScan("com.ruoyi.web.controller")
.build(); .build();
} }

View File

@ -23,8 +23,8 @@ import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.core.page.TableDataInfo;
#elseif($table.tree) #elseif($table.tree)
#end #end
import io.swagger.annotations.Api; import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.annotations.ApiOperation; import io.swagger.v3.oas.annotations.Operation;
/** /**
* ${functionName}Controller * ${functionName}Controller
@ -34,7 +34,7 @@ import io.swagger.annotations.ApiOperation;
*/ */
@RestController @RestController
@RequestMapping("/${moduleName}/${businessName}") @RequestMapping("/${moduleName}/${businessName}")
@Api(tags = "【${functionName}】管理") @Tag(name = "【${functionName}】管理")
public class ${ClassName}Controller extends BaseController public class ${ClassName}Controller extends BaseController
{ {
@Autowired @Autowired
@ -43,9 +43,9 @@ public class ${ClassName}Controller extends BaseController
/** /**
* 查询${functionName}列表 * 查询${functionName}列表
*/ */
@Operation(summary = "查询${functionName}列表")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:list')") @PreAuthorize("@ss.hasPermi('${permissionPrefix}:list')")
@GetMapping("/list") @GetMapping("/list")
@ApiOperation("查询${functionName}列表")
#if($table.crud || $table.sub) #if($table.crud || $table.sub)
public TableDataInfo list(${ClassName} ${className}) public TableDataInfo list(${ClassName} ${className})
{ {
@ -64,10 +64,10 @@ public class ${ClassName}Controller extends BaseController
/** /**
* 导出${functionName}列表 * 导出${functionName}列表
*/ */
@Operation(summary = "导出${functionName}列表")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:export')") @PreAuthorize("@ss.hasPermi('${permissionPrefix}:export')")
@Log(title = "${functionName}", businessType = BusinessType.EXPORT) @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
@PostMapping("/export") @PostMapping("/export")
@ApiOperation("导出${functionName}列表")
public void export(HttpServletResponse response, ${ClassName} ${className}) public void export(HttpServletResponse response, ${ClassName} ${className})
{ {
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className}); List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
@ -78,9 +78,9 @@ public class ${ClassName}Controller extends BaseController
/** /**
* 获取${functionName}详细信息 * 获取${functionName}详细信息
*/ */
@Operation(summary = "获取${functionName}详细信息")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')") @PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
@GetMapping(value = "/{${pkColumn.javaField}}") @GetMapping(value = "/{${pkColumn.javaField}}")
@ApiOperation("获取${functionName}详细信息")
public AjaxResult getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField}) public AjaxResult getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField})
{ {
return success(${className}Service.select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField})); return success(${className}Service.select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField}));
@ -89,10 +89,10 @@ public class ${ClassName}Controller extends BaseController
/** /**
* 新增${functionName} * 新增${functionName}
*/ */
@Operation(summary = "新增${functionName}")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:add')") @PreAuthorize("@ss.hasPermi('${permissionPrefix}:add')")
@Log(title = "${functionName}", businessType = BusinessType.INSERT) @Log(title = "${functionName}", businessType = BusinessType.INSERT)
@PostMapping @PostMapping
@ApiOperation("新增${functionName}")
public AjaxResult add(@RequestBody ${ClassName} ${className}) public AjaxResult add(@RequestBody ${ClassName} ${className})
{ {
return toAjax(${className}Service.insert${ClassName}(${className})); return toAjax(${className}Service.insert${ClassName}(${className}));
@ -101,10 +101,10 @@ public class ${ClassName}Controller extends BaseController
/** /**
* 修改${functionName} * 修改${functionName}
*/ */
@Operation(summary = "修改${functionName}")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:edit')") @PreAuthorize("@ss.hasPermi('${permissionPrefix}:edit')")
@Log(title = "${functionName}", businessType = BusinessType.UPDATE) @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
@PutMapping @PutMapping
@ApiOperation("修改${functionName}")
public AjaxResult edit(@RequestBody ${ClassName} ${className}) public AjaxResult edit(@RequestBody ${ClassName} ${className})
{ {
return toAjax(${className}Service.update${ClassName}(${className})); return toAjax(${className}Service.update${ClassName}(${className}));
@ -113,10 +113,10 @@ public class ${ClassName}Controller extends BaseController
/** /**
* 删除${functionName} * 删除${functionName}
*/ */
@Operation(summary = "删除${functionName}")
@PreAuthorize("@ss.hasPermi('${permissionPrefix}:remove')") @PreAuthorize("@ss.hasPermi('${permissionPrefix}:remove')")
@Log(title = "${functionName}", businessType = BusinessType.DELETE) @Log(title = "${functionName}", businessType = BusinessType.DELETE)
@DeleteMapping("/{${pkColumn.javaField}s}") @DeleteMapping("/{${pkColumn.javaField}s}")
@ApiOperation("删除${functionName}")
public AjaxResult remove(@PathVariable( name = "${pkColumn.javaField}s" ) ${pkColumn.javaType}[] ${pkColumn.javaField}s) public AjaxResult remove(@PathVariable( name = "${pkColumn.javaField}s" ) ${pkColumn.javaType}[] ${pkColumn.javaField}s)
{ {
return toAjax(${className}Service.delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaField}s)); return toAjax(${className}Service.delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaField}s));

View File

@ -13,6 +13,8 @@ import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.TreeEntity; import com.ruoyi.common.core.domain.TreeEntity;
#end #end
import io.swagger.v3.oas.annotations.media.Schema;
/** /**
* ${functionName}对象 ${tableName} * ${functionName}对象 ${tableName}
* *
@ -24,6 +26,7 @@ import com.ruoyi.common.core.domain.TreeEntity;
#elseif($table.tree) #elseif($table.tree)
#set($Entity="TreeEntity") #set($Entity="TreeEntity")
#end #end
@Schema(description = "${functionName}对象")
public class ${ClassName} extends ${Entity} public class ${ClassName} extends ${Entity}
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -32,7 +35,7 @@ public class ${ClassName} extends ${Entity}
#if(!$table.isSuperColumn($column.javaField)) #if(!$table.isSuperColumn($column.javaField))
/** $column.columnComment */ /** $column.columnComment */
@ApiModelProperty(value = "$column.columnComment") @Schema(defaultValue = "$column.columnComment")
#if($column.list) #if($column.list)
#set($parentheseIndex=$column.columnComment.indexOf("")) #set($parentheseIndex=$column.columnComment.indexOf(""))
#if($parentheseIndex != -1) #if($parentheseIndex != -1)