fix: 代码生成器 模板内容预览 系统内容

This commit is contained in:
cuijiawang 2025-09-28 09:31:09 +08:00
parent 90f98d66cc
commit 852eb05cbf
4 changed files with 104 additions and 0 deletions

View File

@ -105,6 +105,18 @@ public class GeneratorController {
}
}
@GetMapping("/template/content")
public R<String> getTemplateContent(@RequestParam("templateSource") String templateSource,
@RequestParam("templateId") String templateId) {
try {
String content = generatorService.getTemplateContentBySource(templateSource, templateId);
return R.ok("获取成功", content);
} catch (Exception e) {
log.error("获取模板内容失败", e);
return R.fail(e.getMessage());
}
}
@GetMapping("/template/preview")
public R<?> previewTemplateGet(@RequestParam("templateSource") String templateSource,
@RequestParam("templateId") String templateId,

View File

@ -145,6 +145,54 @@ public class GeneratorServiceImpl implements IGeneratorService {
}
}
@Override
public String getTemplateContentBySource(String templateSource, String templateId) {
try {
if ("user".equals(templateSource)) {
// 用户模板从数据库获取
return userTemplateService.getTemplateContent(Long.valueOf(templateId));
} else if ("system".equals(templateSource)) {
// 系统模板从文件系统获取
return getSystemTemplateContent(templateId);
} else {
throw new RuntimeException("不支持的模板来源: " + templateSource);
}
} catch (Exception e) {
log.error("获取模板内容失败: templateSource={}, templateId={}", templateSource, templateId, e);
throw new RuntimeException("获取模板内容失败: " + e.getMessage());
}
}
/**
* 获取系统模板内容
*/
private String getSystemTemplateContent(String templateId) {
try {
// 从template.json配置中找到对应模板的文件路径
JSONArray allTemplates = JSONArray.parseArray(getTemplateConfig());
for (Object groupObj : allTemplates) {
JSONObject group = (JSONObject) groupObj;
String groupName = group.getString("group");
JSONArray templates = group.getJSONArray("templates");
for (Object templateObj : templates) {
JSONObject template = (JSONObject) templateObj;
if (templateId.equals(template.getString("id"))) {
String templateName = template.getString("name");
String templatePath = groupName + "/" + templateName + ".ftl";
// 读取模板文件内容
return FreemarkerUtil.getTemplateContent(templatePath);
}
}
}
return "// 系统模板不存在: " + templateId;
} catch (Exception e) {
log.error("获取系统模板内容失败", e);
return "// 获取系统模板内容失败: " + e.getMessage();
}
}
@Override
public String previewSingleTemplate(String templateSource, String templateId, String templateContent, Map<String, Object> options) {
try {

View File

@ -36,4 +36,13 @@ public interface IGeneratorService {
* @return 生成的代码内容
*/
String previewSingleTemplate(String templateSource, String templateId, String templateContent, Map<String, Object> options);
/**
* 根据模板来源和ID获取模板原始内容
*
* @param templateSource 模板来源(system/user)
* @param templateId 模板ID
* @return 模板原始内容
*/
String getTemplateContentBySource(String templateSource, String templateId);
}

View File

@ -81,4 +81,39 @@ public class FreemarkerUtil {
return false;
}
}
/**
* 获取模板文件的原始内容
*
* @param templateName 模板名称
* @return 模板原始内容
*/
public static String getTemplateContent(String templateName) {
try {
Template template = configuration.getTemplate(templateName);
// 通过反射或其他方式获取模板源内容
// 这里使用简单的方法读取模板文件
return readTemplateFile(templateName);
} catch (IOException e) {
log.error("获取模板内容失败: {}", templateName, e);
return "// 模板文件不存在或读取失败: " + templateName;
}
}
/**
* 读取模板文件内容
*/
private static String readTemplateFile(String templatePath) {
try {
java.io.InputStream inputStream = FreemarkerUtil.class.getResourceAsStream("/templates/code-generator/" + templatePath);
if (inputStream != null) {
return new String(inputStream.readAllBytes(), "UTF-8");
} else {
return "// 模板文件不存在: " + templatePath;
}
} catch (Exception e) {
log.error("读取模板文件失败: {}", templatePath, e);
return "// 读取模板文件失败: " + e.getMessage();
}
}
}