From 083c8e0f6eca816d0b73784cd421e1b071a5dfb6 Mon Sep 17 00:00:00 2001 From: zhaown Date: Thu, 21 Oct 2021 15:34:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=80=BC=EF=BC=8C=E5=A6=82=E6=9E=9C=E5=80=BC=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=EF=BC=8C=E8=BF=94=E5=9B=9E=E9=BB=98=E8=AE=A4=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rocketapi/function/UtilsFunction.java | 107 +++++++++++------- 1 file changed, 66 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/github/alenfive/rocketapi/function/UtilsFunction.java b/src/main/java/com/github/alenfive/rocketapi/function/UtilsFunction.java index c6a498e..2dbf58d 100644 --- a/src/main/java/com/github/alenfive/rocketapi/function/UtilsFunction.java +++ b/src/main/java/com/github/alenfive/rocketapi/function/UtilsFunction.java @@ -26,12 +26,13 @@ import java.net.URLEncoder; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; /** * 工具类 */ @Component -public class UtilsFunction implements IFunction{ +public class UtilsFunction implements IFunction { @Autowired private ApiInfoContent apiInfoContent; @@ -59,31 +60,45 @@ public class UtilsFunction implements IFunction{ /** * 获取请求域中的指定参数 + * * @param varName */ - public Object val(String varName){ - return scriptParseService.buildRequestScopeParamItem(apiInfoContent.getApiParams(),null,varName); + public Object val(String varName) { + return scriptParseService.buildRequestScopeParamItem(apiInfoContent.getApiParams(), null, varName); + } + + /** + * 获取请求域中的指定参数,如果参数不存在,则给默认值 + * + * @param varName + * @param defaultValue + * @return + */ + public Object getOrDefault(String varName, Object defaultValue) { + return Optional.ofNullable(val(varName)).orElse(defaultValue); } /** * 无返回结构体方法 + * * @param data * @return */ - public IgnoreWrapper ignoreWrapper(Object data){ + public IgnoreWrapper ignoreWrapper(Object data) { return new IgnoreWrapper(data); } /** * 文件下载 + * * @param fileName * @param inputStream */ public ResponseEntity download(String fileName, InputStream inputStream) throws IOException { String suffix = fileName.substring(fileName.lastIndexOf(".")); - String prefix = fileName.substring(0,fileName.indexOf(".")); + String prefix = fileName.substring(0, fileName.indexOf(".")); HttpHeaders headers = new HttpHeaders(); - headers.setContentDisposition(ContentDisposition.parse("attachment; filename="+ URLEncoder.encode(prefix, "utf-8")+suffix)); + headers.setContentDisposition(ContentDisposition.parse("attachment; filename=" + URLEncoder.encode(prefix, "utf-8") + suffix)); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); InputStreamResource inputStreamResource = new InputStreamResource(inputStream); return new ResponseEntity(inputStreamResource, headers, HttpStatus.OK); @@ -91,6 +106,7 @@ public class UtilsFunction implements IFunction{ /** * 图片在线预览 + * * @param inputStream * @return */ @@ -103,98 +119,105 @@ public class UtilsFunction implements IFunction{ /** * 导出csv + * * @param fileName * @param title * @param list * @return * @throws IOException */ - public ResponseEntity exportCsv(String fileName, Map title, List> list) throws IOException { - ByteArrayInputStream inputStream = CsvUtils.writeCsv(title,list); - return this.download(fileName+".csv",inputStream); + public ResponseEntity exportCsv(String fileName, Map title, List> list) throws IOException { + ByteArrayInputStream inputStream = CsvUtils.writeCsv(title, list); + return this.download(fileName + ".csv", inputStream); } - public ResponseEntity exportCsv(String fileName, List> list) throws IOException { - Map title = new LinkedHashMap<>(); - if (list.size() > 0){ - list.get(0).keySet().stream().forEach(item->title.put(item,item)); + public ResponseEntity exportCsv(String fileName, List> list) throws IOException { + Map title = new LinkedHashMap<>(); + if (list.size() > 0) { + list.get(0).keySet().stream().forEach(item -> title.put(item, item)); } - return exportCsv(fileName,title,list); + return exportCsv(fileName, title, list); } /** * 导出xls + * * @param fileName * @param title * @param list * @return * @throws IOException */ - public ResponseEntity exportXls(String fileName, Map title, List> list) throws IOException { - ByteArrayInputStream inputStream = ExcelUtils.writeXls(title,list); - return this.download(fileName+".xls",inputStream); + public ResponseEntity exportXls(String fileName, Map title, List> list) throws IOException { + ByteArrayInputStream inputStream = ExcelUtils.writeXls(title, list); + return this.download(fileName + ".xls", inputStream); } - public ResponseEntity exportXls(String fileName, List> list) throws IOException { - Map title = new LinkedHashMap<>(); - if (list.size() > 0){ - list.get(0).keySet().stream().forEach(item->title.put(item,item)); + public ResponseEntity exportXls(String fileName, List> list) throws IOException { + Map title = new LinkedHashMap<>(); + if (list.size() > 0) { + list.get(0).keySet().stream().forEach(item -> title.put(item, item)); } - return exportXls(fileName,title,list); + return exportXls(fileName, title, list); } /** * 解析XLS文件 + * * @param in * @return */ - public List> parseXls(InputStream in) throws IOException { + public List> parseXls(InputStream in) throws IOException { return ExcelUtils.parseXls(in); } /** * 解析CSV文件 + * * @param in * @return */ - public List> parseCsv(List titles,InputStream in) throws IOException { - return CsvUtils.parseCsv(titles,in); + public List> parseCsv(List titles, InputStream in) throws IOException { + return CsvUtils.parseCsv(titles, in); } - public List> parseCsv(InputStream in) throws IOException { + public List> parseCsv(InputStream in) throws IOException { return CsvUtils.parseCsv(in); } /** * 解析XLSX文件 + * * @param in * @return */ - public List> parseXlsx(InputStream in) throws IOException { + public List> parseXlsx(InputStream in) throws IOException { return ExcelUtils.parseXlsx(in); } /** * 导出xlsx + * * @param fileName * @param title * @param list */ - public ResponseEntity exportXlsx(String fileName, Map title, List> list) throws IOException { - ByteArrayInputStream inputStream = ExcelUtils.writeXlsx(title,list); - return this.download(fileName+".xlsx",inputStream); + public ResponseEntity exportXlsx(String fileName, Map title, List> list) throws IOException { + ByteArrayInputStream inputStream = ExcelUtils.writeXlsx(title, list); + return this.download(fileName + ".xlsx", inputStream); } - public ResponseEntity exportXlsx(String fileName, List> list) throws IOException { - Map title = new LinkedHashMap<>(); - if (list.size() > 0){ - list.get(0).keySet().stream().forEach(item->title.put(item,item)); + public ResponseEntity exportXlsx(String fileName, List> list) throws IOException { + Map title = new LinkedHashMap<>(); + if (list.size() > 0) { + list.get(0).keySet().stream().forEach(item -> title.put(item, item)); } - return exportXlsx(fileName,title,list); + return exportXlsx(fileName, title, list); } /** * json转Object + * * @param obj * @return * @throws JsonProcessingException @@ -205,36 +228,38 @@ public class UtilsFunction implements IFunction{ /** * 字符串转Object + * * @param str * @return * @throws IOException */ public Object pasreToObject(String str) throws IOException { - return objectMapper.readValue(str,Object.class); + return objectMapper.readValue(str, Object.class); } /** * 接口调用 + * * @param target * @return */ public Object loadAPI(String target) throws Throwable { - if(StringUtils.isEmpty(target)){ + if (StringUtils.isEmpty(target)) { throw new IllegalArgumentException("parameter `target` is empty"); } String[] targetArr = target.split(":"); - if (targetArr.length != 2){ + if (targetArr.length != 2) { throw new IllegalArgumentException("parameter `target` needs to \"GET:/test/pager\""); } String method = targetArr[0]; String fullPath = targetArr[1]; ApiInfo apiInfo = apiInfoCache.get(ApiInfo.builder().method(method).fullPath(fullPath).build()); - if (apiInfo == null || !ApiType.Ql.name().equals(apiInfo.getType())){ - throw new IllegalArgumentException("API not found "+target); + if (apiInfo == null || !ApiType.Ql.name().equals(apiInfo.getType())) { + throw new IllegalArgumentException("API not found " + target); } StringBuilder script = new StringBuilder(scriptEncrypt.decrypt(apiInfo.getScript())); - return scriptParse.engineEval(script.toString(),apiInfoContent.getEngineBindings()); + return scriptParse.engineEval(script.toString(), apiInfoContent.getEngineBindings()); } } -- Gitee