From 36c058188a1be8b32ceb09f1b038b830925f2303 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 20 Sep 2021 19:04:40 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E9=98=B2=E6=AD=A2=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=BD=AC=E6=8D=A2=E5=87=BA=E7=8E=B0=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/framework/aspectj/LogAspect.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java index 10fa58fc..a2aaf222 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java @@ -167,8 +167,14 @@ public class LogAspect { if (StringUtils.isNotNull(o) && !isFilterObject(o)) { - Object jsonObj = JSON.toJSON(o); - params += jsonObj.toString() + " "; + try + { + Object jsonObj = JSON.toJSON(o); + params += jsonObj.toString() + " "; + } + catch (Exception e) + { + } } } } From 54bfa627f0fd37550f9b828f630e98e78aea536d Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 20 Sep 2021 19:04:57 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E9=98=B2=E6=AD=A2Excel=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8F=AF=E8=83=BD=E5=87=BA=E7=8E=B0=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 54157f35..0ab074ed 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -340,8 +340,11 @@ public class ExcelUtil { val = ""; } - byte[] data = image.getData(); - val = FileUtils.writeImportBytes(data); + else + { + byte[] data = image.getData(); + val = FileUtils.writeImportBytes(data); + } } ReflectUtils.invokeSetter(entity, propertyName, val); } From ac942428751d2e4fbd73096dfa2df7a025d77909 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 20 Sep 2021 19:08:50 +0800 Subject: [PATCH 3/5] reset dataSourceAspect --- .../framework/aspectj/DataSourceAspect.java | 36 +++++++++++++++++-- .../framework/aspectj/RateLimiterAspect.java | 2 -- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java index 54bd4dd1..4648fcd3 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java @@ -1,13 +1,18 @@ package com.ruoyi.framework.aspectj; +import java.util.Objects; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import com.ruoyi.common.annotation.DataSource; +import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder; /** @@ -22,11 +27,22 @@ public class DataSourceAspect { protected Logger logger = LoggerFactory.getLogger(getClass()); - @Around("@annotation(dataSource) || @within(dataSource)") - public Object around(ProceedingJoinPoint point, DataSource dataSource) throws Throwable + @Pointcut("@annotation(com.ruoyi.common.annotation.DataSource)" + + "|| @within(com.ruoyi.common.annotation.DataSource)") + public void dsPointCut() { - DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name()); + } + + @Around("dsPointCut()") + public Object around(ProceedingJoinPoint point) throws Throwable + { + DataSource dataSource = getDataSource(point); + + if (StringUtils.isNotNull(dataSource)) + { + DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name()); + } try { @@ -39,4 +55,18 @@ public class DataSourceAspect } } + /** + * 获取需要切换的数据源 + */ + public DataSource getDataSource(ProceedingJoinPoint point) + { + MethodSignature signature = (MethodSignature) point.getSignature(); + DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class); + if (Objects.nonNull(dataSource)) + { + return dataSource; + } + + return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class); + } } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java index 7727051e..5020d000 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java @@ -4,10 +4,8 @@ import java.lang.reflect.Method; import java.util.Collections; import java.util.List; import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From 26f0737c6099f13ab4eedd36a37463c83a666cc6 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 20 Sep 2021 19:09:25 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E9=98=B2=E9=87=8D=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AE=E9=97=B4?= =?UTF-8?q?=E9=9A=94=E6=97=B6=E9=97=B4/=E6=8F=90=E7=A4=BA=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/annotation/RepeatSubmit.java | 8 +++++++ .../interceptor/RepeatSubmitInterceptor.java | 6 ++--- .../impl/SameUrlDataInterceptor.java | 23 +++++-------------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/RepeatSubmit.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/RepeatSubmit.java index 628eef1e..40830052 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/RepeatSubmit.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/RepeatSubmit.java @@ -19,5 +19,13 @@ import java.lang.annotation.Target; @Documented public @interface RepeatSubmit { + /** + * 间隔时间(ms),小于此时间视为重复提交 + */ + public int interval() default 5000; + /** + * 提示消息 + */ + public String message() default "不允许重复提交,请稍后再试"; } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/RepeatSubmitInterceptor.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/RepeatSubmitInterceptor.java index d310382f..7b7f5a40 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/RepeatSubmitInterceptor.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/RepeatSubmitInterceptor.java @@ -29,9 +29,9 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); if (annotation != null) { - if (this.isRepeatSubmit(request)) + if (this.isRepeatSubmit(request, annotation)) { - AjaxResult ajaxResult = AjaxResult.error("不允许重复提交,请稍后再试"); + AjaxResult ajaxResult = AjaxResult.error(annotation.message()); ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult)); return false; } @@ -51,5 +51,5 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter * @return * @throws Exception */ - public abstract boolean isRepeatSubmit(HttpServletRequest request); + public abstract boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation); } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/impl/SameUrlDataInterceptor.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/impl/SameUrlDataInterceptor.java index 3e82309e..33c9c876 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/impl/SameUrlDataInterceptor.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/impl/SameUrlDataInterceptor.java @@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; +import com.ruoyi.common.annotation.RepeatSubmit; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.filter.RepeatedlyRequestWrapper; @@ -35,21 +36,9 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor @Autowired private RedisCache redisCache; - /** - * 间隔时间,单位:秒 默认10秒 - * - * 两次相同参数的请求,如果间隔时间大于该参数,系统不会认定为重复提交的数据 - */ - private int intervalTime = 10; - - public void setIntervalTime(int intervalTime) - { - this.intervalTime = intervalTime; - } - @SuppressWarnings("unchecked") @Override - public boolean isRepeatSubmit(HttpServletRequest request) + public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation) { String nowParams = ""; if (request instanceof RepeatedlyRequestWrapper) @@ -87,7 +76,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor if (sessionMap.containsKey(url)) { Map preDataMap = (Map) sessionMap.get(url); - if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap)) + if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap, annotation.interval())) { return true; } @@ -95,7 +84,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor } Map cacheMap = new HashMap(); cacheMap.put(url, nowDataMap); - redisCache.setCacheObject(cacheRepeatKey, cacheMap, intervalTime, TimeUnit.SECONDS); + redisCache.setCacheObject(cacheRepeatKey, cacheMap, annotation.interval(), TimeUnit.MILLISECONDS); return false; } @@ -112,11 +101,11 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor /** * 判断两次间隔时间 */ - private boolean compareTime(Map nowMap, Map preMap) + private boolean compareTime(Map nowMap, Map preMap, int interval) { long time1 = (Long) nowMap.get(REPEAT_TIME); long time2 = (Long) preMap.get(REPEAT_TIME); - if ((time1 - time2) < (this.intervalTime * 1000)) + if ((time1 - time2) < interval) { return true; } From 3b42abef44bbc439ec2fd63d1b223efc121e945d Mon Sep 17 00:00:00 2001 From: RuoYi Date: Wed, 22 Sep 2021 09:03:01 +0800 Subject: [PATCH 5/5] =?UTF-8?q?Excel=E6=B3=A8=E8=A7=A3=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/annotation/Excel.java | 13 +++++++- .../common/utils/poi/ExcelHandlerAdapter.java | 19 ++++++++++++ .../com/ruoyi/common/utils/poi/ExcelUtil.java | 31 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelHandlerAdapter.java diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java index 50482c57..9fb64da1 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java @@ -5,6 +5,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.math.BigDecimal; +import com.ruoyi.common.utils.poi.ExcelHandlerAdapter; /** * 自定义导出Excel数据注解 @@ -108,7 +109,17 @@ public @interface Excel /** * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右) */ - Align align() default Align.AUTO; + public Align align() default Align.AUTO; + + /** + * 自定义数据处理器 + */ + public Class handler() default ExcelHandlerAdapter.class; + + /** + * 自定义数据处理器参数 + */ + public String[] args() default {}; public enum Align { diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelHandlerAdapter.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelHandlerAdapter.java new file mode 100644 index 00000000..c9ca2d5e --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelHandlerAdapter.java @@ -0,0 +1,19 @@ +package com.ruoyi.common.utils.poi; + +/** + * Excel数据格式处理适配器 + * + * @author ruoyi + */ +public interface ExcelHandlerAdapter +{ + /** + * 格式化 + * + * @param value 单元格数据值 + * @param args excel注解args参数组 + * + * @return 处理后的值 + */ + Object format(Object value, String[] args); +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 0ab074ed..38847905 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; @@ -333,6 +334,10 @@ public class ExcelUtil { val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator()); } + else if (!attr.handler().equals(ExcelHandlerAdapter.class)) + { + val = dataFormatHandlerAdapter(val, attr); + } else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) { PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey()); @@ -729,6 +734,10 @@ public class ExcelUtil { cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString()); } + else if (!attr.handler().equals(ExcelHandlerAdapter.class)) + { + cell.setCellValue(dataFormatHandlerAdapter(value, attr)); + } else { // 设置列类型 @@ -901,6 +910,28 @@ public class ExcelUtil return DictUtils.getDictValue(dictType, dictLabel, separator); } + /** + * 数据处理器 + * + * @param value 数据值 + * @param excel 数据注解 + * @return + */ + public String dataFormatHandlerAdapter(Object value, Excel excel) + { + try + { + Object instance = excel.handler().newInstance(); + Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class }); + value = formatMethod.invoke(instance, value, excel.args()); + } + catch (Exception e) + { + log.error("不能格式化数据 " + excel.handler(), e.getMessage()); + } + return Convert.toStr(value); + } + /** * 合计统计信息 */