feature: 新增生成前端文件模板接口

This commit is contained in:
violateer
2025-08-10 17:06:32 +08:00
parent 210a9d9b14
commit 5fe8bd7706
4 changed files with 108 additions and 57 deletions

View File

@@ -1,5 +1,6 @@
package org.ruoyi.generator.controller; package org.ruoyi.generator.controller;
import cn.hutool.core.net.URLDecoder;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.domain.R; import org.ruoyi.common.core.domain.R;
@@ -8,10 +9,11 @@ import org.ruoyi.generator.service.IGenTableService;
import org.ruoyi.generator.service.SchemaFieldService; import org.ruoyi.generator.service.SchemaFieldService;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
/** /**
* 代码生成 操作处理 * 代码生成 操作处理
* *
@@ -46,4 +48,18 @@ public class GenController extends BaseController {
genTableService.generateCodeToClasspathByTableNames(tableNameStr); genTableService.generateCodeToClasspathByTableNames(tableNameStr);
return R.ok("代码生成成功"); return R.ok("代码生成成功");
} }
/**
* 生成前端代码
*
* @param workPath 执行命令路径
* @param previewCode 执行生成前端文件命令
*/
@GetMapping("/batchGenFrontendCode")
public R<String> batchGenFrontendCode(@NotNull(message = "路径不能为空") String workPath, @NotNull(message = "指令不能为空") String previewCode) {
String decodedWorkPath = URLDecoder.decode(workPath, StandardCharsets.UTF_8);
String decodedPreviewCode = URLDecoder.decode(previewCode, StandardCharsets.UTF_8);
genTableService.generateFrontendTemplateFiles(decodedWorkPath, decodedPreviewCode);
return R.ok("代码生成成功");
}
} }

View File

@@ -19,18 +19,9 @@ import org.ruoyi.generator.util.VelocityInitializer;
import org.ruoyi.generator.util.VelocityUtils; import org.ruoyi.generator.util.VelocityUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.File; import java.io.*;
import java.io.FileWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/** /**
* 业务 服务层实现 * 业务 服务层实现
@@ -59,6 +50,41 @@ public class GenTableServiceImpl implements IGenTableService {
} }
} }
@Override
public void generateFrontendTemplateFiles(String workPath, String previewCode) {
String os = System.getProperty("os.name").toLowerCase();
ProcessBuilder builder;
if (os.contains("win")) {
// Windows下用 cmd /c 执行 previewCode
builder = new ProcessBuilder("cmd.exe", "/c", previewCode);
} else {
// macOS/Linux 用 bash -c 执行 previewCode
builder = new ProcessBuilder("bash", "-c", previewCode);
}
// 设置工作目录
builder.directory(new File(workPath));
builder.redirectErrorStream(true);
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
builder.start().getInputStream(),
StandardCharsets.UTF_8
)
)) {
String line;
log.info("执行结果:");
while ((line = reader.readLine()) != null) {
log.info(line);
}
} catch (Exception e) {
log.error("生成前端代码出错", e);
throw new RuntimeException("生成前端代码失败", e);
}
}
/** /**
* 根据表名称生成代码到classpath * 根据表名称生成代码到classpath
*/ */
@@ -199,8 +225,8 @@ public class GenTableServiceImpl implements IGenTableService {
public boolean isSuperColumn(String javaField) { public boolean isSuperColumn(String javaField) {
// 定义超类字段BaseEntity中的字段 // 定义超类字段BaseEntity中的字段
return "createBy".equals(javaField) || "createTime".equals(javaField) return "createBy".equals(javaField) || "createTime".equals(javaField)
|| "updateBy".equals(javaField) || "updateTime".equals(javaField) || "updateBy".equals(javaField) || "updateTime".equals(javaField)
|| "remark".equals(javaField) || "tenantId".equals(javaField); || "remark".equals(javaField) || "tenantId".equals(javaField);
} }
}); });
@@ -284,18 +310,19 @@ public class GenTableServiceImpl implements IGenTableService {
} }
/** /**
* 根据Java类型添加相应的导入 * 根据Java类型添加相应的导入
*/ */
private void addImportForJavaType(String javaType, Set<String> importList) { private void addImportForJavaType(String javaType, Set<String> importList) {
switch (javaType) { switch (javaType) {
case "BigDecimal" -> importList.add("java.math.BigDecimal"); case "BigDecimal" -> importList.add("java.math.BigDecimal");
case "Date" -> importList.add("java.util.Date"); case "Date" -> importList.add("java.util.Date");
case "LocalDateTime" -> importList.add("java.time.LocalDateTime"); case "LocalDateTime" -> importList.add("java.time.LocalDateTime");
case "LocalDate" -> importList.add("java.time.LocalDate"); case "LocalDate" -> importList.add("java.time.LocalDate");
case "LocalTime" -> importList.add("java.time.LocalTime"); case "LocalTime" -> importList.add("java.time.LocalTime");
default -> {} default -> {
} }
} }
}
/** /**
* 从包名中提取模块名 * 从包名中提取模块名

View File

@@ -13,4 +13,12 @@ public interface IGenTableService {
* @param tableName 表名称数组 * @param tableName 表名称数组
*/ */
void generateCodeToClasspathByTableNames(String tableName); void generateCodeToClasspathByTableNames(String tableName);
/**
* 生成前端文件
*
* @param workPath 执行命令路径
* @param previewCode 执行生成前端文件命令
*/
void generateFrontendTemplateFiles(String workPath, String previewCode);
} }

View File

@@ -74,7 +74,7 @@ SET FOREIGN_KEY_CHECKS = 1;
-- 菜单 -- 菜单
INSERT INTO `sys_menu` (`menu_id`, `menu_name`, `parent_id`, `order_num`, `path`, `component`, `query_param`, `is_frame`, `is_cache`, `menu_type`, `visible`, `status`, `perms`, `icon`, `create_dept`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (1946483381643743233, '知识库角色管理', 1775500307898949634, '12', 'knowledgeRole', 'system/knowledgeRole/index', NULL, 1, 0, 'C', '0', '0', NULL, 'ri:user-3-fill', 103, 1, '2025-07-19 16:41:17', NULL, NULL, '知识库角色管理'); INSERT INTO `sys_menu` (`menu_id`, `menu_name`, `parent_id`, `order_num`, `path`, `component`, `query_param`, `is_frame`, `is_cache`, `menu_type`, `visible`, `status`, `perms`, `icon`, `create_dept`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (1946483381643743233, '知识库角色管理', 1775500307898949634, '12', 'knowledgeRole', 'operator/knowledgeRole/index', NULL, 1, 0, 'C', '0', '0', NULL, 'ri:user-3-fill', 103, 1, '2025-07-19 16:41:17', NULL, NULL, '知识库角色管理');
-- 用户表添加字段 -- 用户表添加字段
ALTER TABLE sys_user ALTER TABLE sys_user