添加文件服务aop,优化代码生成工具

This commit is contained in:
2026-05-19 09:37:51 +08:00
parent a3e4089e2c
commit 0de0d3a134
3 changed files with 139 additions and 31 deletions

View File

@@ -333,18 +333,18 @@ public class GenServiceImpl implements GenService, BaseParams {
String subModuleName = datasource.getSubModuleName(); String subModuleName = datasource.getSubModuleName();
String tableName = table.getTableName(); String tableName = table.getTableName();
// 获取uri // 获取uri
int indexOf = tableName.indexOf("_"); int indexOf = tableName.indexOf(CP_UNDERLINE);
String uri; String uri;
if (indexOf > CP_NEGATIVE1) { if (indexOf > CP_NEGATIVE1) {
uri = tableName.substring(CP_NUM0, indexOf) + CP_SLASH + tableName.substring(indexOf + CP_NUM1).replaceAll("_", CP_LINE); uri = tableName.substring(CP_NUM0, indexOf) + CP_SLASH + tableName.substring(indexOf + CP_NUM1).replace(CP_UNDERLINE, CP_LINE);
} else { } else {
uri = tableName; uri = tableName;
} }
// 获取按钮权限前缀 // 获取按钮权限前缀
String btnPermPrefix = uri.replaceAll(CP_SLASH, CP_COLON); String btnPermPrefix = uri.replace(CP_SLASH, CP_COLON);
// 获取dbid // 获取dbId
List<Long> dbIdList = getDbIdList(CP_NUM20); List<Long> dbIdList = getDbIdList();
// 获取父级菜单ID和树路径 // 获取父级菜单ID和树路径
String parentMenuId = table.getParentMenuId(); String parentMenuId = table.getParentMenuId();
String treePath; String treePath;
@@ -356,7 +356,7 @@ public class GenServiceImpl implements GenService, BaseParams {
parentId = "parentId"; parentId = "parentId";
treePath = "treePath"; treePath = "treePath";
} }
String component = subModuleName + CP_SLASH + tableName.replace("_", "-") + CP_SLASH + "index"; String component = subModuleName + CP_SLASH + tableName.replace(CP_UNDERLINE, CP_LINE) + CP_SLASH + "index";
// 根据配置文件生成模板 // 根据配置文件生成模板
Map<String, GenProperties.TemplateConfig> templateConfigs = genProperties.getTemplateConfigs(); Map<String, GenProperties.TemplateConfig> templateConfigs = genProperties.getTemplateConfigs();
@@ -429,7 +429,7 @@ public class GenServiceImpl implements GenService, BaseParams {
map.put("subPackageName", value.getSubPackageName()); map.put("subPackageName", value.getSubPackageName());
map.put("uri", uri); map.put("uri", uri);
map.put("btnPermPrefix", btnPermPrefix); map.put("btnPermPrefix", btnPermPrefix);
map.put("apiPath", tableName.replace("_", "-") + "-api"); map.put("apiPath", tableName.replace(CP_UNDERLINE, CP_LINE) + "-api");
map.put("dbIds", dbIdList); map.put("dbIds", dbIdList);
map.put("parentId", parentId); map.put("parentId", parentId);
@@ -455,10 +455,10 @@ public class GenServiceImpl implements GenService, BaseParams {
/** /**
* 获取数据库 ID 列表 * 获取数据库 ID 列表
* *
* @param count 获取个数
* @return 数据库 ID 列表 * @return 数据库 ID 列表
*/ */
private List<Long> getDbIdList(int count) { private List<Long> getDbIdList() {
int count = CP_NUM20;
InetAddress localHost; InetAddress localHost;
try { try {
localHost = InetAddress.getLocalHost(); localHost = InetAddress.getLocalHost();
@@ -487,7 +487,7 @@ public class GenServiceImpl implements GenService, BaseParams {
return switch (type) { return switch (type) {
case "Entity", "Sql" -> entityName + extension; case "Entity", "Sql" -> entityName + extension;
case "TS_API" -> tableName.replace("_", "-") + "-api" + extension; case "TS_API" -> tableName.replace(CP_UNDERLINE, CP_LINE) + "-api" + extension;
case "VUE_VIEW" -> "index" + extension; case "VUE_VIEW" -> "index" + extension;
default -> entityName + type + extension; default -> entityName + type + extension;
}; };
@@ -511,7 +511,7 @@ public class GenServiceImpl implements GenService, BaseParams {
case ".java" -> { case ".java" -> {
joiner.add(genProperties.getBackendAppName()); joiner.add(genProperties.getBackendAppName());
joiner.add(moduleName); joiner.add(moduleName);
joiner.add(moduleName + "-" + projectModule); joiner.add(moduleName + CP_LINE + projectModule);
joiner.add("src"); joiner.add("src");
joiner.add("main"); joiner.add("main");
joiner.add("java"); joiner.add("java");
@@ -529,7 +529,7 @@ public class GenServiceImpl implements GenService, BaseParams {
joiner.add("src"); joiner.add("src");
joiner.add(subPackageName.replace(CP_DOT, CP_SLASH)); joiner.add(subPackageName.replace(CP_DOT, CP_SLASH));
joiner.add(subModuleName); joiner.add(subModuleName);
joiner.add(tableName.replace("_", "-")); joiner.add(tableName.replace(CP_UNDERLINE, CP_LINE));
} }
case ".sql" -> joiner.add("sql"); case ".sql" -> joiner.add("sql");
default -> joiner.add(CP_EMPTY); default -> joiner.add(CP_EMPTY);

View File

@@ -0,0 +1,97 @@
package xtools.app.sys.file.aop;
import com.alibaba.fastjson2.JSONObject;
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.springframework.stereotype.Component;
import xtools.base.config.BaseParams;
import xtools.boot.api.model.dto.log.LogTrack;
import xtools.boot.log.LogBus;
import xtools.boot.log.enums.LogBusBaseType;
import xtools.boot.log.holder.LogTrackHolder;
import xtools.core.enums.LogLevel;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
/**
* <p>Title : StorageServiceAop</p>
* <p>Description : StorageServiceAop</p>
* <p>DevelopTools : Idea_x64_v2026.1</p>
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
* <p>Company : org.xujun</p>
*
* @author : XuJun
* @version : 1.0.0
* @date : 2026/2/11 14:07
*/
@Aspect
@Component
public class StorageServiceAop implements BaseParams {
/**
* 方法切面
*/
@Pointcut("execution(* xtools.boot.storage.base.service.StorageService.*(..))")
public void methods() {
}
/**
* 调用记录
*
* @param joinPoint 切点
* @return 调用结果
* @throws Throwable 异常信息
*/
@Around("methods()")
public Object logMethodCall(ProceedingJoinPoint joinPoint) throws Throwable {
LogTrack logTrack = LogTrackHolder.getDefNull();
if (Objects.isNull(logTrack)) {
return joinPoint.proceed();
}
long startTime = System.currentTimeMillis();
String method = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
Exception err = null;
Object result = null;
try {
result = joinPoint.proceed();
return result;
} catch (Exception e) {
err = e;
throw e;
} finally {
long endTime = System.currentTimeMillis();
JSONObject logData = JSONObject.of("method", method, "args", filterStreamArgs(args), "result", result, "execTime", endTime - startTime);
LogBus.init(err == null ? LogLevel.INFO : LogLevel.ERROR, LogBusBaseType.STORAGE, logTrack)
.data(logData)
.error(err)
.save();
}
}
/**
* 过滤掉 InputStream 和 OutputStream 参数
*
* @param args 参数
* @return 过滤后的参数
*/
private Object[] filterStreamArgs(Object[] args) {
if (args == null || args.length == 0) {
return args;
}
Object[] filtered = new Object[args.length];
for (int i = CP_NUM0; i < args.length; i++) {
if (args[i] instanceof InputStream || args[i] instanceof OutputStream) {
filtered[i] = "<" + args[i].getClass().getSimpleName() + ">";
} else {
filtered[i] = args[i];
}
}
return filtered;
}
}

View File

@@ -119,25 +119,10 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams {
) { ) {
// 上传时间 // 上传时间
Instant uploadTime = Instant.now(); Instant uploadTime = Instant.now();
// 获取文件扩展名
// 文件名校验
if (StringUtils.isBlank(fileName)) {
throw new BizError("文件名称不能为空");
}
// 文件格式校验
String suffix = FileUtils.getSuffix(fileName); String suffix = FileUtils.getSuffix(fileName);
String[] extArr = bizType.extArr(); // 检查文件
if (ArrUtils.isNotEmpty(extArr)) { checkFile(fileName, contentLength, suffix, bizType);
if (!CheckUtils.suffix(suffix, extArr)) {
throw new BizError("文件格式错误");
}
}
// 文件大小限制
long sizeLimit = bizType.sizeLimit();
if (sizeLimit > CP_NEGATIVE1 && contentLength > sizeLimit) {
throw new BizError("文件大小超限");
}
// 计算文件码 // 计算文件码
String md5 = null; String md5 = null;
String sm3 = null; String sm3 = null;
@@ -163,7 +148,6 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams {
throw new BizError("计算文件码失败"); throw new BizError("计算文件码失败");
} }
} }
// 文件存储器的文件名 // 文件存储器的文件名
String filePath = UuidUtils.get() + CP_LINE + fileName; String filePath = UuidUtils.get() + CP_LINE + fileName;
// 获取存储桶 // 获取存储桶
@@ -203,6 +187,33 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams {
throw new BizError("文件上传失败"); throw new BizError("文件上传失败");
} }
/**
* 校验文件
*
* @param fileName 文件名
* @param contentLength 文件长度
* @param suffix 文件扩展名
* @param bizType 业务类型
*/
private void checkFile(String fileName, long contentLength, String suffix, FileBizBaseEnum bizType) {
// 文件名校验
if (StringUtils.isBlank(fileName)) {
throw new BizError("文件名称不能为空");
}
// 文件扩展名校验
String[] extArr = bizType.extArr();
if (ArrUtils.isNotEmpty(extArr)) {
if (!CheckUtils.suffix(suffix, extArr)) {
throw new BizError("文件格式错误");
}
}
// 文件长度校验
long sizeLimit = bizType.sizeLimit();
if (sizeLimit > CP_NEGATIVE1 && contentLength > sizeLimit) {
throw new BizError("文件大小超限");
}
}
/** /**
* 文件下载 * 文件下载
* *