feat: 代码生成器 模板内容预览

This commit is contained in:
cuijiawang 2025-09-27 19:38:07 +08:00
parent bf6e0dd952
commit 90f98d66cc
7 changed files with 64 additions and 1 deletions

View File

@ -201,5 +201,19 @@ public class UserTemplateController {
return R.fail(e.getMessage());
}
}
/**
* 获取模板内容
*/
@GetMapping("/content/{id}")
public R<String> getTemplateContent(@PathVariable("id") Long id) {
try {
String content = userTemplateService.getTemplateContent(id);
return R.ok("获取成功", content);
} catch (Exception e) {
log.error("获取模板内容失败", e);
return R.fail(e.getMessage());
}
}
}

View File

@ -55,5 +55,10 @@ public class UserTemplateDTO {
* 是否启用显示(0-隐藏 1-显示)
*/
private Integer isEnabled;
/**
* 模板内容是否公开(0-不公开 1-公开)
*/
private Integer isContentPublic;
}

View File

@ -74,4 +74,9 @@ public class UserTemplate extends BaseEntity {
* 是否启用显示(0-隐藏 1-显示)
*/
private Integer isEnabled;
/**
* 模板内容是否公开(0-不公开 1-公开)
*/
private Integer isContentPublic;
}

View File

@ -117,5 +117,13 @@ public interface IUserTemplateService {
* @return 是否成功
*/
boolean toggleTemplateVisibility(Long id, Integer isEnabled);
/**
* 获取模板内容考虑权限控制
*
* @param id 模板ID
* @return 模板内容或权限提示
*/
String getTemplateContent(Long id);
}

View File

@ -94,6 +94,7 @@ public class UserTemplateServiceImpl implements IUserTemplateService {
template.setStatus(dto.getStatus() != null ? dto.getStatus() : 1);
template.setVersion(StringUtils.isNotBlank(dto.getVersion()) ? dto.getVersion() : "1.0.0");
template.setIsEnabled(dto.getIsEnabled() != null ? dto.getIsEnabled() : 1); // 默认显示
template.setIsContentPublic(dto.getIsContentPublic() != null ? dto.getIsContentPublic() : 0); // 默认不公开内容
return userTemplateMapper.insert(template) > 0;
}
@ -285,6 +286,27 @@ public class UserTemplateServiceImpl implements IUserTemplateService {
return updateResult;
}
@Override
public String getTemplateContent(Long id) {
UserTemplate template = userTemplateMapper.selectById(id);
if (template == null) {
throw new RuntimeException("模板不存在或已被删除");
}
Long currentUserId = LoginHelper.getUserId();
// 权限检查只能查看自己的模板或内容公开的模板
if (!currentUserId.equals(template.getUserId())) {
// 检查内容是否公开如果字段不存在则默认为不公开
Integer isContentPublic = template.getIsContentPublic();
if (isContentPublic == null || isContentPublic != 1) {
return "// 模板内容未公开,无法预览";
}
}
return template.getTemplateContent();
}
/**
* 转换实体为VO
*/

View File

@ -78,6 +78,11 @@ public class UserTemplateVO {
*/
private Integer isEnabled;
/**
* 模板内容是否公开(0-不公开 1-公开)
*/
private Integer isContentPublic;
/**
* 创建人
*/

View File

@ -1,7 +1,11 @@
-- 为用户模板表添加显示隐藏控制字段
-- 为用户模板表添加显示隐藏控制字段和内容公开控制字段
-- 用于控制私有模板的显示状态,以及公开模板创建者的显示偏好
ALTER TABLE `user_template` ADD COLUMN `is_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用显示(0-隐藏 1-显示)' AFTER `status`;
-- 添加模板内容是否公开字段
ALTER TABLE `user_template` ADD COLUMN `is_content_public` tinyint(1) NOT NULL DEFAULT '0' COMMENT '模板内容是否公开(0-不公开 1-公开)' AFTER `is_enabled`;
-- 添加索引以提高查询性能
ALTER TABLE `user_template` ADD INDEX `idx_is_enabled` (`is_enabled`);
ALTER TABLE `user_template` ADD INDEX `idx_is_content_public` (`is_content_public`);