From ff76df9ae0f2383db903bab3efc6ef1223d0d071 Mon Sep 17 00:00:00 2001 From: liyang Date: Wed, 11 Dec 2024 11:27:13 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E5=BF=85=E5=A1=AB=E3=80=81=E5=A4=87=E6=B3=A8?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E5=8D=95=E7=8B=AC=E7=9A=84=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=A8=A1=E6=9D=BF=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/annotation/ExcelNotation.java | 24 +++++ .../excel/annotation/ExcelRequired.java | 26 ++++++ .../excel/handler/DataWriteHandler.java | 77 +++++++++++++++ .../dromara/common/excel/utils/ExcelUtil.java | 93 +++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelNotation.java create mode 100644 ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelRequired.java create mode 100644 ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelNotation.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelNotation.java new file mode 100644 index 000000000..f358afcd6 --- /dev/null +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelNotation.java @@ -0,0 +1,24 @@ +package org.dromara.common.excel.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 批注 + * @author guzhouyanyu + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ExcelNotation { + + /** + * col index + */ + int index() default -1; + /** + * 批注内容 + */ + String value() default ""; +} diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelRequired.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelRequired.java new file mode 100644 index 000000000..15784e140 --- /dev/null +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/ExcelRequired.java @@ -0,0 +1,26 @@ +package org.dromara.common.excel.annotation; + +import org.apache.poi.ss.usermodel.IndexedColors; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 是否必填 + * @author guzhouyanyu + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ExcelRequired { + + /** + * col index + */ + int index() default -1; + /** + * 字体颜色 + */ + IndexedColors fontColor() default IndexedColors.RED; +} diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java new file mode 100644 index 000000000..0d11360ba --- /dev/null +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java @@ -0,0 +1,77 @@ +package org.dromara.common.excel.handler; + +import cn.hutool.core.collection.CollUtil; +import com.alibaba.excel.metadata.data.DataFormatData; +import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.util.StyleUtil; +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.SheetWriteHandler; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; +import com.alibaba.excel.write.metadata.style.WriteCellStyle; +import com.alibaba.excel.write.metadata.style.WriteFont; +import lombok.Data; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; + +import java.util.Map; + +/** + * 批注、必填 + * + * @author guzhouyanyu + */ +@Data +public class DataWriteHandler implements SheetWriteHandler, CellWriteHandler { + + /** + * 批注 + */ + private final Map notationMap; + + /** + * 头列字体颜色 + */ + private final Map headColumnMap; + + + @Override + public void afterCellDispose(CellWriteHandlerContext context) { + WriteCellData cellData = context.getFirstCellData(); + WriteCellStyle writeCellStyle = cellData.getOrCreateStyle(); + + DataFormatData dataFormatData = new DataFormatData(); + // 单元格设置为文本格式 + dataFormatData.setIndex((short) 49); + writeCellStyle.setDataFormatData(dataFormatData); + + if (context.getHead()) { + Cell cell = context.getCell(); + WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder(); + Sheet sheet = writeSheetHolder.getSheet(); + Workbook workbook = writeSheetHolder.getSheet().getWorkbook(); + Drawing drawing = sheet.createDrawingPatriarch(); + // 设置标题字体样式 + WriteFont headWriteFont = new WriteFont(); + // 加粗 + headWriteFont.setBold(true); + if (CollUtil.isNotEmpty(headColumnMap) && headColumnMap.containsKey(cell.getColumnIndex())) { + // 设置字体颜色 + headWriteFont.setColor(headColumnMap.get(cell.getColumnIndex())); + } + writeCellStyle.setWriteFont(headWriteFont); + CellStyle cellStyle = StyleUtil.buildCellStyle(workbook, null, writeCellStyle); + cell.setCellStyle(cellStyle); + + if (CollUtil.isNotEmpty(notationMap) && notationMap.containsKey(cell.getColumnIndex())) { + // 批注内容 + String notationContext = notationMap.get(cell.getColumnIndex()); + // 创建绘图对象 + Comment comment = drawing.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), 0, (short) 5, 5)); + comment.setString(new XSSFRichTextString(notationContext)); + cell.setCellComment(comment); + } + } + } +} diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java index a6c14ad51..fa1fb902d 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java @@ -16,14 +16,20 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.file.FileUtils; +import org.dromara.common.core.utils.reflect.ReflectUtils; +import org.dromara.common.excel.annotation.ExcelNotation; +import org.dromara.common.excel.annotation.ExcelRequired; import org.dromara.common.excel.convert.ExcelBigNumberConvert; import org.dromara.common.excel.core.*; +import org.dromara.common.excel.handler.DataWriteHandler; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.lang.reflect.Field; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -433,4 +439,91 @@ public class ExcelUtil { return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx"; } + /** + * 获取必填列Map + * + * @param clazz 类class + * @return java.util.Map + * @author SunLingDa + * @date 2022/11/3 13:23 + */ + private static Map getRequiredMap(Class clazz) { + Map requiredMap = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields(); + // 检查 fields 数组是否为空 + if (fields.length == 0) { + return requiredMap; + } + Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName())); + + for (int i = 0; i < filteredFields.length; i++) { + Field field = filteredFields[i]; + if (!field.isAnnotationPresent(ExcelRequired.class)) { + continue; + } + ExcelRequired excelRequired = field.getAnnotation(ExcelRequired.class); + int columnIndex = excelRequired.index() == -1 ? i : excelRequired.index(); + requiredMap.put(columnIndex, excelRequired.fontColor().getIndex()); + } + return requiredMap; + } + + /** + * 获取批注Map + * + * @param clazz 类class + * @return java.util.Map + * @author SunLingDa + * @date 2022/11/3 13:24 + */ + private static Map getNotationMap(Class clazz) { + Map notationMap = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields(); + // 检查 fields 数组是否为空 + if (fields.length == 0) { + return notationMap; + } + Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName())); + for (int i = 0; i < filteredFields.length; i++) { + Field field = filteredFields[i]; + if (!field.isAnnotationPresent(ExcelNotation.class)) { + continue; + } + ExcelNotation excelNotation = field.getAnnotation(ExcelNotation.class); + int columnIndex = excelNotation.index() == -1 ? i : excelNotation.index(); + notationMap.put(columnIndex, excelNotation.value()); + } + return notationMap; + } + public static void exportExcelRequire(List list, String sheetName, Class clazz,HttpServletResponse response) { + exportExcelTemplate(list,sheetName,clazz,response); + } + /** + * 导出excel模板 + * + * @param list 导出数据集合 + * @param sheetName 工作表的名称 + * @param clazz 实体类 + * @param response 响应体 + */ + public static void exportExcelTemplate(List list, String sheetName, Class clazz, HttpServletResponse response) { + try { + Map requiredMap = getRequiredMap(clazz); + Map notationMap = getNotationMap(clazz); + resetResponse(sheetName, response); + ServletOutputStream os = response.getOutputStream(); + DataWriteHandler writeHandler = new DataWriteHandler(notationMap, requiredMap); + ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) + .autoCloseStream(false) + // 自动适配 + .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) + .registerWriteHandler(writeHandler) + // 大数值自动转换 防止失真 + .registerConverter(new ExcelBigNumberConvert()) + .sheet(sheetName); + builder.doWrite(list); + } catch (IOException e) { + throw new RuntimeException("导出Excel异常"); + } + } } From 9ad64521d39e722b74379cc33c78de4cd0b426b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Wed, 18 Dec 2024 17:46:53 +0800 Subject: [PATCH 02/13] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20!pr=5F610?= =?UTF-8?q?=20=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/handler/DataWriteHandler.java | 62 ++++++++- .../dromara/common/excel/utils/ExcelUtil.java | 118 +++--------------- .../dromara/demo/domain/vo/TestDemoVo.java | 7 ++ 3 files changed, 81 insertions(+), 106 deletions(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java index 0d11360ba..a2aa4951b 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/handler/DataWriteHandler.java @@ -10,11 +10,15 @@ import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; -import lombok.Data; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.dromara.common.core.utils.reflect.ReflectUtils; +import org.dromara.common.excel.annotation.ExcelNotation; +import org.dromara.common.excel.annotation.ExcelRequired; +import java.lang.reflect.Field; +import java.util.HashMap; import java.util.Map; /** @@ -22,7 +26,6 @@ import java.util.Map; * * @author guzhouyanyu */ -@Data public class DataWriteHandler implements SheetWriteHandler, CellWriteHandler { /** @@ -36,8 +39,16 @@ public class DataWriteHandler implements SheetWriteHandler, CellWriteHandler { private final Map headColumnMap; + public DataWriteHandler(Class clazz) { + notationMap = getNotationMap(clazz); + headColumnMap = getRequiredMap(clazz); + } + @Override public void afterCellDispose(CellWriteHandlerContext context) { + if (CollUtil.isEmpty(notationMap) && CollUtil.isEmpty(headColumnMap)) { + return; + } WriteCellData cellData = context.getFirstCellData(); WriteCellStyle writeCellStyle = cellData.getOrCreateStyle(); @@ -74,4 +85,51 @@ public class DataWriteHandler implements SheetWriteHandler, CellWriteHandler { } } } + + /** + * 获取必填列 + */ + private static Map getRequiredMap(Class clazz) { + Map requiredMap = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields(); + // 检查 fields 数组是否为空 + if (fields.length == 0) { + return requiredMap; + } + Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName())); + + for (int i = 0; i < filteredFields.length; i++) { + Field field = filteredFields[i]; + if (!field.isAnnotationPresent(ExcelRequired.class)) { + continue; + } + ExcelRequired excelRequired = field.getAnnotation(ExcelRequired.class); + int columnIndex = excelRequired.index() == -1 ? i : excelRequired.index(); + requiredMap.put(columnIndex, excelRequired.fontColor().getIndex()); + } + return requiredMap; + } + + /** + * 获取批注 + */ + private static Map getNotationMap(Class clazz) { + Map notationMap = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields(); + // 检查 fields 数组是否为空 + if (fields.length == 0) { + return notationMap; + } + Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName())); + for (int i = 0; i < filteredFields.length; i++) { + Field field = filteredFields[i]; + if (!field.isAnnotationPresent(ExcelNotation.class)) { + continue; + } + ExcelNotation excelNotation = field.getAnnotation(ExcelNotation.class); + int columnIndex = excelNotation.index() == -1 ? i : excelNotation.index(); + notationMap.put(columnIndex, excelNotation.value()); + } + return notationMap; + } } diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java index fa1fb902d..856699ae0 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java @@ -16,9 +16,6 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.file.FileUtils; -import org.dromara.common.core.utils.reflect.ReflectUtils; -import org.dromara.common.excel.annotation.ExcelNotation; -import org.dromara.common.excel.annotation.ExcelRequired; import org.dromara.common.excel.convert.ExcelBigNumberConvert; import org.dromara.common.excel.core.*; import org.dromara.common.excel.handler.DataWriteHandler; @@ -27,9 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; -import java.lang.reflect.Field; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -197,6 +192,7 @@ public class ExcelUtil { .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 大数值自动转换 防止失真 .registerConverter(new ExcelBigNumberConvert()) + .registerWriteHandler(new DataWriteHandler(list.get(0).getClass())) .sheet(sheetName); if (merge) { // 合并处理器 @@ -217,7 +213,7 @@ public class ExcelUtil { * @param data 模板需要的数据 * @param response 响应体 */ - public static void exportTemplate(List data, String filename, String templatePath, HttpServletResponse response) { + public static void exportTemplate(List data, String filename, String templatePath, HttpServletResponse response) { try { resetResponse(filename, response); ServletOutputStream os = response.getOutputStream(); @@ -236,20 +232,21 @@ public class ExcelUtil { * @param data 模板需要的数据 * @param os 输出流 */ - public static void exportTemplate(List data, String templatePath, OutputStream os) { + public static void exportTemplate(List data, String templatePath, OutputStream os) { + if (CollUtil.isEmpty(data)) { + throw new IllegalArgumentException("数据为空"); + } ClassPathResource templateResource = new ClassPathResource(templatePath); ExcelWriter excelWriter = EasyExcel.write(os) .withTemplate(templateResource.getStream()) .autoCloseStream(false) // 大数值自动转换 防止失真 .registerConverter(new ExcelBigNumberConvert()) + .registerWriteHandler(new DataWriteHandler(data.get(0).getClass())) .build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); - if (CollUtil.isEmpty(data)) { - throw new IllegalArgumentException("数据为空"); - } // 单表多数据导出 模板格式为 {.属性} - for (Object d : data) { + for (T d : data) { excelWriter.fill(d, writeSheet); } excelWriter.finish(); @@ -305,6 +302,9 @@ public class ExcelUtil { * @param os 输出流 */ public static void exportTemplateMultiList(Map data, String templatePath, OutputStream os) { + if (CollUtil.isEmpty(data)) { + throw new IllegalArgumentException("数据为空"); + } ClassPathResource templateResource = new ClassPathResource(templatePath); ExcelWriter excelWriter = EasyExcel.write(os) .withTemplate(templateResource.getStream()) @@ -313,9 +313,6 @@ public class ExcelUtil { .registerConverter(new ExcelBigNumberConvert()) .build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); - if (CollUtil.isEmpty(data)) { - throw new IllegalArgumentException("数据为空"); - } for (Map.Entry map : data.entrySet()) { // 设置列表后续还有数据 FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); @@ -339,6 +336,9 @@ public class ExcelUtil { * @param os 输出流 */ public static void exportTemplateMultiSheet(List> data, String templatePath, OutputStream os) { + if (CollUtil.isEmpty(data)) { + throw new IllegalArgumentException("数据为空"); + } ClassPathResource templateResource = new ClassPathResource(templatePath); ExcelWriter excelWriter = EasyExcel.write(os) .withTemplate(templateResource.getStream()) @@ -346,9 +346,6 @@ public class ExcelUtil { // 大数值自动转换 防止失真 .registerConverter(new ExcelBigNumberConvert()) .build(); - if (CollUtil.isEmpty(data)) { - throw new IllegalArgumentException("数据为空"); - } for (int i = 0; i < data.size(); i++) { WriteSheet writeSheet = EasyExcel.writerSheet(i).build(); for (Map.Entry map : data.get(i).entrySet()) { @@ -439,91 +436,4 @@ public class ExcelUtil { return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx"; } - /** - * 获取必填列Map - * - * @param clazz 类class - * @return java.util.Map - * @author SunLingDa - * @date 2022/11/3 13:23 - */ - private static Map getRequiredMap(Class clazz) { - Map requiredMap = new HashMap<>(); - Field[] fields = clazz.getDeclaredFields(); - // 检查 fields 数组是否为空 - if (fields.length == 0) { - return requiredMap; - } - Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName())); - - for (int i = 0; i < filteredFields.length; i++) { - Field field = filteredFields[i]; - if (!field.isAnnotationPresent(ExcelRequired.class)) { - continue; - } - ExcelRequired excelRequired = field.getAnnotation(ExcelRequired.class); - int columnIndex = excelRequired.index() == -1 ? i : excelRequired.index(); - requiredMap.put(columnIndex, excelRequired.fontColor().getIndex()); - } - return requiredMap; - } - - /** - * 获取批注Map - * - * @param clazz 类class - * @return java.util.Map - * @author SunLingDa - * @date 2022/11/3 13:24 - */ - private static Map getNotationMap(Class clazz) { - Map notationMap = new HashMap<>(); - Field[] fields = clazz.getDeclaredFields(); - // 检查 fields 数组是否为空 - if (fields.length == 0) { - return notationMap; - } - Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName())); - for (int i = 0; i < filteredFields.length; i++) { - Field field = filteredFields[i]; - if (!field.isAnnotationPresent(ExcelNotation.class)) { - continue; - } - ExcelNotation excelNotation = field.getAnnotation(ExcelNotation.class); - int columnIndex = excelNotation.index() == -1 ? i : excelNotation.index(); - notationMap.put(columnIndex, excelNotation.value()); - } - return notationMap; - } - public static void exportExcelRequire(List list, String sheetName, Class clazz,HttpServletResponse response) { - exportExcelTemplate(list,sheetName,clazz,response); - } - /** - * 导出excel模板 - * - * @param list 导出数据集合 - * @param sheetName 工作表的名称 - * @param clazz 实体类 - * @param response 响应体 - */ - public static void exportExcelTemplate(List list, String sheetName, Class clazz, HttpServletResponse response) { - try { - Map requiredMap = getRequiredMap(clazz); - Map notationMap = getNotationMap(clazz); - resetResponse(sheetName, response); - ServletOutputStream os = response.getOutputStream(); - DataWriteHandler writeHandler = new DataWriteHandler(notationMap, requiredMap); - ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) - .autoCloseStream(false) - // 自动适配 - .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) - .registerWriteHandler(writeHandler) - // 大数值自动转换 防止失真 - .registerConverter(new ExcelBigNumberConvert()) - .sheet(sheetName); - builder.doWrite(list); - } catch (IOException e) { - throw new RuntimeException("导出Excel异常"); - } - } } diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/vo/TestDemoVo.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/vo/TestDemoVo.java index 016c2f7dc..e7ea8075c 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/vo/TestDemoVo.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/domain/vo/TestDemoVo.java @@ -2,6 +2,8 @@ package org.dromara.demo.domain.vo; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; +import org.dromara.common.excel.annotation.ExcelNotation; +import org.dromara.common.excel.annotation.ExcelRequired; import org.dromara.common.translation.annotation.Translation; import org.dromara.common.translation.constant.TransConstant; import org.dromara.demo.domain.TestDemo; @@ -36,30 +38,35 @@ public class TestDemoVo implements Serializable { /** * 部门id */ + @ExcelRequired @ExcelProperty(value = "部门id") private Long deptId; /** * 用户id */ + @ExcelRequired @ExcelProperty(value = "用户id") private Long userId; /** * 排序号 */ + @ExcelRequired @ExcelProperty(value = "排序号") private Integer orderNum; /** * key键 */ + @ExcelNotation(value = "测试key") @ExcelProperty(value = "key键") private String testKey; /** * 值 */ + @ExcelNotation(value = "测试value") @ExcelProperty(value = "值") private String value; From 1a403361c951b91f6e3a33da1d8c0ee77e07bb86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 19 Dec 2024 09:19:12 +0800 Subject: [PATCH 03/13] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E5=85=B3?= =?UTF-8?q?=E9=97=ADsse=E5=90=8E=20=E4=BD=BF=E7=94=A8=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/common/sse/utils/SseMessageUtils.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java index c6abdc8fd..ff79a15c2 100644 --- a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java +++ b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java @@ -16,6 +16,7 @@ import org.dromara.common.sse.dto.SseMessageDto; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class SseMessageUtils { + private final static Boolean SSE_ENABLE = SpringUtils.getProperty("sse.enabled", Boolean.class, true); private final static SseEmitterManager MANAGER = SpringUtils.getBean(SseEmitterManager.class); /** @@ -25,6 +26,9 @@ public class SseMessageUtils { * @param message 要发送的消息内容 */ public static void sendMessage(Long userId, String message) { + if (SSE_ENABLE) { + return; + } MANAGER.sendMessage(userId, message); } @@ -34,6 +38,9 @@ public class SseMessageUtils { * @param message 要发送的消息内容 */ public static void sendMessage(String message) { + if (SSE_ENABLE) { + return; + } MANAGER.sendMessage(message); } @@ -43,6 +50,9 @@ public class SseMessageUtils { * @param sseMessageDto 要发布的SSE消息对象 */ public static void publishMessage(SseMessageDto sseMessageDto) { + if (SSE_ENABLE) { + return; + } MANAGER.publishMessage(sseMessageDto); } @@ -52,6 +62,9 @@ public class SseMessageUtils { * @param message 要发布的消息内容 */ public static void publishAll(String message) { + if (SSE_ENABLE) { + return; + } MANAGER.publishAll(message); } From ad85fa201649695919b037b028e53d19932c4a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 19 Dec 2024 10:54:08 +0800 Subject: [PATCH 04/13] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20sse?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/common/sse/utils/SseMessageUtils.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java index ff79a15c2..45f354ce7 100644 --- a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java +++ b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java @@ -26,7 +26,7 @@ public class SseMessageUtils { * @param message 要发送的消息内容 */ public static void sendMessage(Long userId, String message) { - if (SSE_ENABLE) { + if (!isEnable()) { return; } MANAGER.sendMessage(userId, message); @@ -38,7 +38,7 @@ public class SseMessageUtils { * @param message 要发送的消息内容 */ public static void sendMessage(String message) { - if (SSE_ENABLE) { + if (!isEnable()) { return; } MANAGER.sendMessage(message); @@ -50,7 +50,7 @@ public class SseMessageUtils { * @param sseMessageDto 要发布的SSE消息对象 */ public static void publishMessage(SseMessageDto sseMessageDto) { - if (SSE_ENABLE) { + if (!isEnable()) { return; } MANAGER.publishMessage(sseMessageDto); @@ -62,10 +62,17 @@ public class SseMessageUtils { * @param message 要发布的消息内容 */ public static void publishAll(String message) { - if (SSE_ENABLE) { + if (!isEnable()) { return; } MANAGER.publishAll(message); } + /** + * 是否开启 + */ + public static Boolean isEnable() { + return SSE_ENABLE; + } + } From f20c27197278ce133f0d605618702f8e629e6678 Mon Sep 17 00:00:00 2001 From: zst_2001 <1493232063@qq.com> Date: Thu, 19 Dec 2024 16:26:26 +0000 Subject: [PATCH 05/13] =?UTF-8?q?!622=20fix=20=E4=BF=AE=E6=AD=A3=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E9=94=99=E5=88=AB=E5=AD=97=E5=92=8C=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=20*=20fix=20=E4=BF=AE=E6=AD=A3=E6=B3=A8=E9=87=8A=E9=94=99?= =?UTF-8?q?=E5=88=AB=E5=AD=97=E5=92=8C=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/common/json/handler/BigNumberSerializer.java | 2 +- .../org/dromara/common/sensitive/core/SensitiveStrategy.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ruoyi-common/ruoyi-common-json/src/main/java/org/dromara/common/json/handler/BigNumberSerializer.java b/ruoyi-common/ruoyi-common-json/src/main/java/org/dromara/common/json/handler/BigNumberSerializer.java index f2a7c2d8c..8752353c5 100644 --- a/ruoyi-common/ruoyi-common-json/src/main/java/org/dromara/common/json/handler/BigNumberSerializer.java +++ b/ruoyi-common/ruoyi-common-json/src/main/java/org/dromara/common/json/handler/BigNumberSerializer.java @@ -32,7 +32,7 @@ public class BigNumberSerializer extends NumberSerializer { @Override public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException { - // 超出范围 序列化位字符串 + // 超出范围 序列化为字符串 if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) { super.serialize(value, gen, provider); } else { diff --git a/ruoyi-common/ruoyi-common-sensitive/src/main/java/org/dromara/common/sensitive/core/SensitiveStrategy.java b/ruoyi-common/ruoyi-common-sensitive/src/main/java/org/dromara/common/sensitive/core/SensitiveStrategy.java index 995dcbd96..7af5cee9e 100644 --- a/ruoyi-common/ruoyi-common-sensitive/src/main/java/org/dromara/common/sensitive/core/SensitiveStrategy.java +++ b/ruoyi-common/ruoyi-common-sensitive/src/main/java/org/dromara/common/sensitive/core/SensitiveStrategy.java @@ -80,12 +80,12 @@ public enum SensitiveStrategy { FIRST_MASK(DesensitizedUtil::firstMask), /** - * 清空为null + * 清空为"" */ CLEAR(s -> DesensitizedUtil.clear()), /** - * 清空为"" + * 清空为null */ CLEAR_TO_NULL(s -> DesensitizedUtil.clearToNull()); From 3a0fbd45ae107e0805dbca4cfa1042892a6c9fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Sat, 21 Dec 2024 13:30:19 +0800 Subject: [PATCH 06/13] =?UTF-8?q?update=20=E5=A2=9E=E5=8A=A0=20=E8=B5=9E?= =?UTF-8?q?=E5=8A=A9=E5=95=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5ed7878f1..37ca4c3c3 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ MaxKey 业界领先单点登录产品 - https://gitee.com/dromara/MaxKey
CCFlow 驰聘低代码-流程-表单 - https://gitee.com/opencc/RuoYi-JFlow
数舵科技 软件定制开发APP小程序等 - http://www.shuduokeji.com/
引迈信息 软件开发平台 - https://www.jnpfsoft.com/index.html?from=plus-doc
+启山商城系统 多租户商城源码可免费商用可二次开发 - https://www.73app.cn/
[如何成为赞助商 加群联系作者详谈](https://plus-doc.dromara.org/#/common/add_group) # 本框架与RuoYi的功能差异 From f4f052deb49afe0fed71af3e92cdbefeaae81c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Sat, 21 Dec 2024 13:32:19 +0800 Subject: [PATCH 07/13] =?UTF-8?q?update=20=E5=A2=9E=E5=8A=A0=20=E8=B5=9E?= =?UTF-8?q?=E5=8A=A9=E5=95=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 37ca4c3c3..42a76367e 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ MaxKey 业界领先单点登录产品 - https://gitee.com/dromara/MaxKey
CCFlow 驰聘低代码-流程-表单 - https://gitee.com/opencc/RuoYi-JFlow
数舵科技 软件定制开发APP小程序等 - http://www.shuduokeji.com/
引迈信息 软件开发平台 - https://www.jnpfsoft.com/index.html?from=plus-doc
-启山商城系统 多租户商城源码可免费商用可二次开发 - https://www.73app.cn/
+启山商城系统 多租户商城源码可免费商用可二次开发 - https://www.73app.cn/
[如何成为赞助商 加群联系作者详谈](https://plus-doc.dromara.org/#/common/add_group) # 本框架与RuoYi的功能差异 From 36de389fa48c9ce5a2b916c740d7e143c42a40f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Sat, 21 Dec 2024 13:38:24 +0800 Subject: [PATCH 08/13] =?UTF-8?q?update=20=E5=A2=9E=E5=8A=A0=20=E8=B5=9E?= =?UTF-8?q?=E5=8A=A9=E5=95=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42a76367e..7a8864116 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ MaxKey 业界领先单点登录产品 - https://gitee.com/dromara/MaxKey
CCFlow 驰聘低代码-流程-表单 - https://gitee.com/opencc/RuoYi-JFlow
数舵科技 软件定制开发APP小程序等 - http://www.shuduokeji.com/
引迈信息 软件开发平台 - https://www.jnpfsoft.com/index.html?from=plus-doc
-启山商城系统 多租户商城源码可免费商用可二次开发 - https://www.73app.cn/
+**启山商城系统 多租户商城源码可免费商用可二次开发 - https://www.73app.cn/**
[如何成为赞助商 加群联系作者详谈](https://plus-doc.dromara.org/#/common/add_group) # 本框架与RuoYi的功能差异 From 251a617ecc57276bde7da7440cf954bb2adb53fb Mon Sep 17 00:00:00 2001 From: QianRj <14974713+qianrj@user.noreply.gitee.com> Date: Tue, 24 Dec 2024 03:04:27 +0000 Subject: [PATCH 09/13] =?UTF-8?q?!626=20fix=20=E4=BF=AE=E5=A4=8Dexcel?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E5=AF=BC=E5=87=BA=E5=BC=82=E5=B8=B8=20*=20fi?= =?UTF-8?q?x:=20=E4=BF=AE=E5=A4=8Dexcel=E6=A8=A1=E6=9D=BF=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/dromara/common/excel/utils/ExcelUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java index 856699ae0..b22e6f987 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java @@ -192,7 +192,7 @@ public class ExcelUtil { .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 大数值自动转换 防止失真 .registerConverter(new ExcelBigNumberConvert()) - .registerWriteHandler(new DataWriteHandler(list.get(0).getClass())) + .registerWriteHandler(new DataWriteHandler(clazz)) .sheet(sheetName); if (merge) { // 合并处理器 From 5480e419b6fb47e239ab28240252bb7f4a101e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Wed, 25 Dec 2024 09:23:08 +0800 Subject: [PATCH 10/13] update sqlkeyword --- .../main/java/org/dromara/common/core/utils/sql/SqlUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/sql/SqlUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/sql/SqlUtil.java index fcf9fb4b6..1020c81eb 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/sql/SqlUtil.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/sql/SqlUtil.java @@ -15,7 +15,7 @@ public class SqlUtil { /** * 定义常用的 sql关键字 */ - public static String SQL_REGEX = "and |extractvalue|updatexml|sleep|exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |or |union |like |+|/*|user()"; + public static String SQL_REGEX = "\u000B|and |extractvalue|updatexml|sleep|exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |or |union |like |+|/*|user()"; /** * 仅支持字母、数字、下划线、空格、逗号、小数点(支持多个字段排序) From 492e7dab26b9efff7a68962f1dfffd5fa583c464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Wed, 25 Dec 2024 09:30:14 +0800 Subject: [PATCH 11/13] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E6=9C=AA?= =?UTF-8?q?=E5=BC=80=E5=90=AFsse=20=E6=89=BE=E4=B8=8D=E5=88=B0bean?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/common/sse/utils/SseMessageUtils.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java index 45f354ce7..586103417 100644 --- a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java +++ b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/utils/SseMessageUtils.java @@ -17,7 +17,13 @@ import org.dromara.common.sse.dto.SseMessageDto; public class SseMessageUtils { private final static Boolean SSE_ENABLE = SpringUtils.getProperty("sse.enabled", Boolean.class, true); - private final static SseEmitterManager MANAGER = SpringUtils.getBean(SseEmitterManager.class); + private static SseEmitterManager MANAGER; + + static { + if (isEnable() && MANAGER == null) { + MANAGER = SpringUtils.getBean(SseEmitterManager.class); + } + } /** * 向指定的WebSocket会话发送消息 From c2746c23923b7f28219f670a8cd13ac70cd17668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 26 Dec 2024 13:26:18 +0800 Subject: [PATCH 12/13] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9D=83=E9=99=90=E5=A4=84=E7=90=86=E5=99=A8=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4=E5=80=BC=E5=A4=84=E7=90=86?= =?UTF-8?q?=20=E9=92=88=E5=AF=B9=E4=BA=8E=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E4=B8=8E=E6=B3=A8=E8=A7=A3=E4=B8=8D=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E6=88=96=E8=80=85=E8=A1=A8=E8=BE=BE=E5=BC=8F=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E4=B8=BAnull=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/PlusDataPermissionHandler.java | 69 +++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java index c529c5332..783d3526e 100644 --- a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java +++ b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/handler/PlusDataPermissionHandler.java @@ -3,6 +3,7 @@ package org.dromara.common.mybatis.handler; import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import net.sf.jsqlparser.JSQLParserException; import net.sf.jsqlparser.expression.Expression; @@ -28,9 +29,7 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.type.ClassMetadata; import org.springframework.core.type.classreading.CachingMetadataReaderFactory; -import org.springframework.expression.BeanResolver; -import org.springframework.expression.ExpressionParser; -import org.springframework.expression.ParserContext; +import org.springframework.expression.*; import org.springframework.expression.common.TemplateParserContext; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -130,7 +129,9 @@ public class PlusDataPermissionHandler { joinStr = " " + dataPermission.joinStr() + " "; } LoginUser user = DataPermissionHelper.getVariable("user"); - StandardEvaluationContext context = new StandardEvaluationContext(); + Object defaultValue = "-1"; + NullSafeStandardEvaluationContext context = new NullSafeStandardEvaluationContext(defaultValue); + context.addPropertyAccessor(new NullSafePropertyAccessor(context.getPropertyAccessors().get(0), defaultValue)); context.setBeanResolver(beanResolver); DataPermissionHelper.getContext().forEach(context::setVariable); Set conditions = new HashSet<>(); @@ -257,4 +258,64 @@ public class PlusDataPermissionHandler { return getDataPermission(mapperId) == null; } + /** + * 对所有null变量找不到的变量返回默认值 + */ + @AllArgsConstructor + private static class NullSafeStandardEvaluationContext extends StandardEvaluationContext { + + private final Object defaultValue; + + @Override + public Object lookupVariable(String name) { + Object obj = super.lookupVariable(name); + // 如果读取到的值是 null,则返回默认值 + if (obj == null) { + return defaultValue; + } + return obj; + } + + } + + /** + * 对所有null变量找不到的变量返回默认值 委托模式 将不需要处理的方法委托给原处理器 + */ + @AllArgsConstructor + private static class NullSafePropertyAccessor implements PropertyAccessor { + + private final PropertyAccessor delegate; + private final Object defaultValue; + + @Override + public Class[] getSpecificTargetClasses() { + return delegate.getSpecificTargetClasses(); + } + + @Override + public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException { + return delegate.canRead(context, target, name); + } + + @Override + public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { + TypedValue value = delegate.read(context, target, name); + // 如果读取到的值是 null,则返回默认值 + if (value.getValue() == null) { + return new TypedValue(defaultValue); + } + return value; + } + + @Override + public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { + return delegate.canWrite(context, target, name); + } + + @Override + public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { + delegate.write(context, target, name, newValue); + } + } + } From a7b83672ba4241ad3a16e81b155266f888a396ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 27 Dec 2024 11:17:09 +0800 Subject: [PATCH 13/13] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20sse=20?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E8=8E=B7=E5=8F=96token=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=20=E5=88=A0=E9=99=A4userid=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E7=9A=84=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/common/sse/core/SseEmitterManager.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java index c26adca50..ba1ce5696 100644 --- a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java +++ b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java @@ -1,5 +1,6 @@ package org.dromara.common.sse.core; +import cn.hutool.core.map.MapUtil; import lombok.extern.slf4j.Slf4j; import org.dromara.common.redis.utils.RedisUtils; import org.dromara.common.sse.dto.SseMessageDto; @@ -65,7 +66,7 @@ public class SseEmitterManager { */ public void disconnect(Long userId, String token) { Map emitters = USER_TOKEN_EMITTERS.get(userId); - if (emitters != null) { + if (MapUtil.isNotEmpty(emitters)) { try { SseEmitter sseEmitter = emitters.get(token); sseEmitter.send(SseEmitter.event().comment("disconnected")); @@ -73,6 +74,8 @@ public class SseEmitterManager { } catch (Exception ignore) { } emitters.remove(token); + } else { + USER_TOKEN_EMITTERS.remove(userId); } } @@ -93,7 +96,7 @@ public class SseEmitterManager { */ public void sendMessage(Long userId, String message) { Map emitters = USER_TOKEN_EMITTERS.get(userId); - if (emitters != null) { + if (MapUtil.isNotEmpty(emitters)) { for (Map.Entry entry : emitters.entrySet()) { try { entry.getValue().send(SseEmitter.event() @@ -103,6 +106,8 @@ public class SseEmitterManager { emitters.remove(entry.getKey()); } } + } else { + USER_TOKEN_EMITTERS.remove(userId); } }