Skip to content

Commit

Permalink
【V3.2.0】1、左侧菜单Logo和标题固定;2、Excel导出添加水印;3、长时间不在线自动返回登录页;4、移除sa-token的 t…
Browse files Browse the repository at this point in the history
…oken-prefix 配置;5、升级 ant deign vue到最新版4.2;6、登录页面引入登录类型图标方式;7、文件预览组件的文件下载方式为接口方式下载;
  • Loading branch information
zhuoda committed Apr 22, 2024
1 parent 9c44fe3 commit fd27cd6
Show file tree
Hide file tree
Showing 27 changed files with 731 additions and 282 deletions.
7 changes: 7 additions & 0 deletions smart-admin-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<xerces.version>2.12.0</xerces.version>
<easy-excel.version>3.3.2</easy-excel.version>
<poi.version>5.2.4</poi.version>
<ooxml-schemas.version>1.4</ooxml-schemas.version>
<aws-java-sdk.version>1.11.842</aws-java-sdk.version>
<log4j-spring-boot.version>2.17.2</log4j-spring-boot.version>
<hutool.version>5.7.22</hutool.version>
Expand Down Expand Up @@ -288,6 +289,12 @@
<version>${poi.version}</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>${ooxml-schemas.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.lab1024.sa.admin.module.business.goods.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
import net.lab1024.sa.admin.module.business.goods.domain.form.GoodsAddForm;
import net.lab1024.sa.admin.module.business.goods.domain.form.GoodsQueryForm;
Expand All @@ -14,7 +13,7 @@
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.ValidateList;
import net.lab1024.sa.base.common.util.SmartResponseUtil;
import net.lab1024.sa.base.common.util.SmartExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -88,17 +87,8 @@ public ResponseDTO<String> importGoods(@RequestParam MultipartFile file) {
@GetMapping("/goods/exportGoods")
@SaCheckPermission("goods:exportGoods")
public void exportGoods(HttpServletResponse response) throws IOException {

List<GoodsExcelVO> goodsList = goodsService.getAllGoods();

// 设置下载消息头
SmartResponseUtil.setDownloadFileHeader(response, "商品列表.xls", null);

// 下载
EasyExcel.write(response.getOutputStream(), GoodsExcelVO.class)
.autoCloseStream(Boolean.FALSE)
.sheet("商品")
.doWrite(goodsList);
SmartExcelUtil.exportExcel(response,"商品列表.xlsx","商品",GoodsExcelVO.class, goodsList);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.lab1024.sa.admin.module.business.oa.enterprise;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,11 +10,11 @@
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseExcelVO;
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseListVO;
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseVO;
import net.lab1024.sa.admin.util.AdminRequestUtil;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.common.util.SmartResponseUtil;
import net.lab1024.sa.base.common.util.*;
import net.lab1024.sa.base.module.support.operatelog.annotation.OperateLog;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;
Expand All @@ -24,6 +23,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
Expand Down Expand Up @@ -60,14 +60,11 @@ public void exportExcel(@RequestBody @Valid EnterpriseQueryForm queryForm, HttpS
return;
}

// 设置下载消息头
SmartResponseUtil.setDownloadFileHeader(response, "企业基本信息.xls", null);
String watermark = AdminRequestUtil.getRequestUser().getActualName();
watermark += SmartLocalDateUtil.format(LocalDateTime.now(), SmartDateFormatterEnum.YMD_HMS);

SmartExcelUtil.exportExcelWithWatermark(response,"企业基本信息.xlsx","企业信息",EnterpriseExcelVO.class,data,watermark);

// 下载
EasyExcel.write(response.getOutputStream(), EnterpriseExcelVO.class)
.autoCloseStream(Boolean.FALSE)
.sheet("企业信息")
.doWrite(data);
}

@Operation(summary = "查询企业详情 @author 开云")
Expand Down
5 changes: 5 additions & 0 deletions smart-admin-api/sa-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@
<artifactId>poi-scratchpad</artifactId>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package net.lab1024.sa.base.common.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;

/**
*
* excel 工具类
*
* @Author 1024创新实验室-主任:卓大
* @Date 2024/4/22 22:49:07
* @Wechat zhuoda1024
* @Email [email protected]
* @Copyright 1024创新实验室 ( https://1024lab.net ),2012-2024
*/
public final class SmartExcelUtil {

/**
* 通用单sheet导出
*/
public static void exportExcel(HttpServletResponse response, String fileName, String sheetName, Class head,Collection<?> data) throws IOException {
// 设置下载消息头
SmartResponseUtil.setDownloadFileHeader(response, fileName, null);
// 下载
EasyExcel.write(response.getOutputStream(), head)
.autoCloseStream(Boolean.FALSE)
.sheet(sheetName)
.doWrite(data);
}

/**
* 通用单 sheet水印 导出
*/
public static void exportExcelWithWatermark(HttpServletResponse response, String fileName, String sheetName, Class head,Collection<?> data, String watermarkString) throws IOException {
// 设置下载消息头
SmartResponseUtil.setDownloadFileHeader(response, fileName, null);
// 水印
Watermark watermark = new Watermark(watermarkString);
// 一定要inMemory
EasyExcel.write(response.getOutputStream(), head)
.inMemory(true)
.sheet(sheetName)
.registerWriteHandler(new CustomWaterMarkHandler(watermark))
.doWrite(data);
}


@Slf4j
private static class CustomWaterMarkHandler implements SheetWriteHandler {

private final Watermark watermark;

public CustomWaterMarkHandler(Watermark watermark) {
this.watermark = watermark;
}

@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
BufferedImage bufferedImage = createWatermarkImage();
XSSFWorkbook workbook = (XSSFWorkbook) writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook();
try {
// 添加水印的具体操作
addWatermarkToSheet(workbook, bufferedImage);
} catch (Exception e) {
log.error("添加水印出错:", e);
}

}

/**
* 创建水印图片
*
* @return
*/
private BufferedImage createWatermarkImage() {
// 获取水印相关参数
Font font = watermark.getFont();
int width = watermark.getWidth();
int height = watermark.getHeight();
Color color = watermark.getColor();
String text = watermark.getContent();

// 创建带有透明背景的 BufferedImage
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();

// 设置画笔字体、平滑、颜色
g.setFont(font);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);

// 计算水印位置和角度
int y = watermark.getYAxis();
int x = watermark.getXAxis();
AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(-watermark.getAngle()), 0, y);
g.setTransform(transform);
// 绘制水印文字
g.drawString(text, x, y);

// 释放资源
g.dispose();

return image;
}

private void addWatermarkToSheet(XSSFWorkbook workbook, BufferedImage watermarkImage) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
ImageIO.write(watermarkImage, "png", os);
int pictureIdx = workbook.addPicture(os.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG);
XSSFPictureData pictureData = workbook.getAllPictures().get(pictureIdx);
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
// 获取每个Sheet表
XSSFSheet sheet = workbook.getSheetAt(i);
PackagePartName ppn = pictureData.getPackagePart().getPartName();
String relType = XSSFRelation.IMAGES.getRelation();
PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
}
} catch (Exception e) {
// 处理ImageIO.write可能抛出的异常
log.error("添加水印图片时发生错误", e);
}
}
}

@Data
private static class Watermark {

public Watermark(String content) {
this.content = content;
init();
}

public Watermark(String content, Color color, Font font, double angle) {
this.content = content;
this.color = color;
this.font = font;
this.angle = angle;
init();
}

/**
* 根据水印内容长度自适应水印图片大小,简单的三角函数
*/
private void init() {
FontMetrics fontMetrics = new JLabel().getFontMetrics(this.font);
int stringWidth = fontMetrics.stringWidth(this.content);
int charWidth = fontMetrics.charWidth('A');
this.width = (int) Math.abs(stringWidth * Math.cos(Math.toRadians(this.angle))) + 5 * charWidth;
this.height = (int) Math.abs(stringWidth * Math.sin(Math.toRadians(this.angle))) + 5 * charWidth;
this.yAxis = this.height;
this.xAxis = charWidth;
}

/**
* 水印内容
*/
private String content;

/**
* 画笔颜色
*/
private Color color = new Color(239,239,239);

/**
* 字体样式
*/
private Font font = new Font("Microsoft YaHei", Font.BOLD, 26);

/**
* 水印宽度
*/
private int width;

/**
* 水印高度
*/
private int height;

/**
* 倾斜角度,非弧度制
*/
private double angle = 25;

/**
* 字体的y轴位置
*/
private int yAxis = 50;

/**
* 字体的X轴位置
*/
private int xAxis;

/**
* 水平倾斜度
*/
private double shearX = 0.1;

/**
* 垂直倾斜度
*/
private double shearY = -0.26;
}
}
4 changes: 1 addition & 3 deletions smart-admin-api/sa-base/src/main/resources/dev/sa-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ spring:
datasource:
url: jdbc:p6spy:mysql://127.0.0.1:3306/smart_admin_v3?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: Zhuoda.123
password: Zhuoda1024lab
initial-size: 2
min-idle: 2
max-active: 10
Expand Down Expand Up @@ -120,8 +120,6 @@ reload:
sa-token:
# token 名称(同时也是 cookie 名称)
token-name: x-access-token
# token 前缀 例如:Bear
token-prefix:
# token 有效期(单位:秒) 默认30天(2592000秒),-1 代表永久有效
timeout: 2592000
# token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
Expand Down
2 changes: 0 additions & 2 deletions smart-admin-api/sa-base/src/main/resources/pre/sa-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ reload:
sa-token:
# token 名称(同时也是 cookie 名称)
token-name: x-access-token
# token 前缀 例如:Bear
token-prefix:
# token 有效期(单位:秒) 默认30天(2592000秒),-1 代表永久有效
timeout: 2592000
# token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
Expand Down
2 changes: 0 additions & 2 deletions smart-admin-api/sa-base/src/main/resources/prod/sa-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ reload:
sa-token:
# token 名称(同时也是 cookie 名称)
token-name: x-access-token
# token 前缀 例如:Bear
token-prefix:
# token 有效期(单位:秒) 默认30天(2592000秒),-1 代表永久有效
timeout: 2592000
# token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
Expand Down
2 changes: 0 additions & 2 deletions smart-admin-api/sa-base/src/main/resources/test/sa-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ reload:
sa-token:
# token 名称(同时也是 cookie 名称)
token-name: x-access-token
# token 前缀 例如:Bear
token-prefix:
# token 有效期(单位:秒) 默认30天(2592000秒),-1 代表永久有效
timeout: 2592000
# token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
Expand Down
Loading

0 comments on commit fd27cd6

Please sign in to comment.