From 3271c7f86da0263a719500ca94102ad1231a56aa Mon Sep 17 00:00:00 2001 From: lizhengwei <171332584@qq.com> Date: Sun, 8 Mar 2026 22:39:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E9=9C=80=E6=B1=82=E5=BC=80?= =?UTF-8?q?=E5=8F=91=EF=BC=9A=E7=BB=84=E4=BB=B6=E5=BA=93=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../md/form/api/constant/BaseConstant.java | 2 +- .../H5ComponentCategoryController.java | 5 +- .../service/H5ComponentCategoryService.java | 3 +- .../impl/H5ComponentCategoryServiceImpl.java | 91 ++++++++++++++----- .../api/vo/H5ComponentCategoryTreeVO.java | 66 ++++++++++++++ 5 files changed, 140 insertions(+), 27 deletions(-) create mode 100644 ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/vo/H5ComponentCategoryTreeVO.java diff --git a/ruoyi-modules/ruoyi-md-form/ruoyi-md-form-api/src/main/java/com/dromara/md/form/api/constant/BaseConstant.java b/ruoyi-modules/ruoyi-md-form/ruoyi-md-form-api/src/main/java/com/dromara/md/form/api/constant/BaseConstant.java index 1843669fc..fa941ead9 100644 --- a/ruoyi-modules/ruoyi-md-form/ruoyi-md-form-api/src/main/java/com/dromara/md/form/api/constant/BaseConstant.java +++ b/ruoyi-modules/ruoyi-md-form/ruoyi-md-form-api/src/main/java/com/dromara/md/form/api/constant/BaseConstant.java @@ -4,7 +4,7 @@ public interface BaseConstant { /** * 微服务上下文路径 */ - String CONTEXT_PATH = "/booksflow/form/v1.0/"; + String CONTEXT_PATH = "/form/"; /** * 微服务名称 diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/H5ComponentCategoryController.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/H5ComponentCategoryController.java index a828c8e91..f9ad181d3 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/H5ComponentCategoryController.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/H5ComponentCategoryController.java @@ -2,6 +2,7 @@ package com.luban.api.controller; import com.luban.api.entity.H5ComponentCategory; import com.luban.api.service.H5ComponentCategoryService; +import com.luban.api.vo.H5ComponentCategoryTreeVO; import com.luban.api.vo.Result; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -53,8 +54,8 @@ public class H5ComponentCategoryController { @Operation(summary = "获取分类树形结构") @GetMapping("/tree") - public Result> getCategoryTree() { - List result = h5ComponentCategoryService.getCategoryTree(); + public Result> getCategoryTree() { + List result = h5ComponentCategoryService.getCategoryTree(); return new Result<>(result); } diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/H5ComponentCategoryService.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/H5ComponentCategoryService.java index 88283ba62..b05cedd6d 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/H5ComponentCategoryService.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/H5ComponentCategoryService.java @@ -1,6 +1,7 @@ package com.luban.api.service; import com.luban.api.entity.H5ComponentCategory; +import com.luban.api.vo.H5ComponentCategoryTreeVO; import java.util.List; @@ -62,5 +63,5 @@ public interface H5ComponentCategoryService { * * @return 树形分类列表 */ - List getCategoryTree(); + List getCategoryTree(); } diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentCategoryServiceImpl.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentCategoryServiceImpl.java index 23ebe8fae..a8955e15b 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentCategoryServiceImpl.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentCategoryServiceImpl.java @@ -1,17 +1,21 @@ package com.luban.api.service.impl; -import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.luban.api.dao.H5ComponentCategoryDao; import com.luban.api.entity.H5ComponentCategory; import com.luban.api.service.H5ComponentCategoryService; +import com.luban.api.vo.H5ComponentCategoryTreeVO; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import java.time.Instant; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * H5 组件分类服务实现类 @@ -31,7 +35,7 @@ public class H5ComponentCategoryServiceImpl extends ServiceImpl wrapper = new LambdaQueryWrapper<>(); wrapper.eq(H5ComponentCategory::getParentId, 0L) .orderByAsc(H5ComponentCategory::getSort); - + return baseMapper.selectList(wrapper); } @@ -40,7 +44,7 @@ public class H5ComponentCategoryServiceImpl extends ServiceImpl wrapper = new LambdaQueryWrapper<>(); wrapper.eq(H5ComponentCategory::getParentId, parentId) .orderByAsc(H5ComponentCategory::getSort); - + return baseMapper.selectList(wrapper); } @@ -50,23 +54,23 @@ public class H5ComponentCategoryServiceImpl extends ServiceImpl wrapper = new LambdaQueryWrapper<>(); wrapper.eq(H5ComponentCategory::getParentId, id); Long count = baseMapper.selectCount(wrapper); - + if (count > 0) { throw new RuntimeException("该分类下存在子分类,无法删除"); } - + baseMapper.deleteById(id); } @Override - public List getCategoryTree() { - // 查询所有一级分类 - List topLevelCategories = getTopLevelCategories(); - - // 为每个一级分类查询子分类 - for (H5ComponentCategory topLevel : topLevelCategories) { - List childCategories = getChildCategories(topLevel.getId()); - // 这里简单处理,实际可以考虑使用专门的树形结构 VO - if (!childCategories.isEmpty()) { - // 可以在返回时添加额外信息 - log.debug("分类 {} 有 {} 个子分类", topLevel.getName(), childCategories.size()); + public List getCategoryTree() { + // 一次性查询所有分类(排除已删除的) + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.orderByAsc(H5ComponentCategory::getParentId, H5ComponentCategory::getSort); + + List allCategories = baseMapper.selectList(wrapper); + + // 转换为 VO + List allVOs = allCategories.stream() + .map(this::convertToTreeVO) + .toList(); + + // 构建树形结构 + return buildTree(allVOs); + } + + /** + * 构建树形结构 + * + * @param allNodes 所有节点 + * @return 树形结构(只包含一级分类) + */ + private List buildTree(List allNodes) { + List tree = new ArrayList<>(); + + // 使用 Map 提高查找效率 + Map nodeMap = allNodes.stream() + .collect(Collectors.toMap( + H5ComponentCategoryTreeVO::getId, + vo -> vo + )); + + // 遍历所有节点,构建父子关系 + for (H5ComponentCategoryTreeVO node : allNodes) { + if (node.getParentId() == null || node.getParentId() == 0) { + // 一级分类直接添加到树中 + tree.add(node); + } else { + // 子分类添加到父分类的 children 中 + H5ComponentCategoryTreeVO parent = nodeMap.get(node.getParentId()); + if (parent != null) { + parent.getChildren().add(node); + } } } - - return topLevelCategories; + + return tree; + } + + /** + * 转换为树形 VO + */ + private H5ComponentCategoryTreeVO convertToTreeVO(H5ComponentCategory entity) { + H5ComponentCategoryTreeVO vo = new H5ComponentCategoryTreeVO(); + BeanUtils.copyProperties(entity, vo); + return vo; } } diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/vo/H5ComponentCategoryTreeVO.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/vo/H5ComponentCategoryTreeVO.java new file mode 100644 index 000000000..c1b475568 --- /dev/null +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/vo/H5ComponentCategoryTreeVO.java @@ -0,0 +1,66 @@ +package com.luban.api.vo; + +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +/** + * H5 组件分类树形结构 VO + * + * @author system + * @since 2026-03-06 + */ +@Data +public class H5ComponentCategoryTreeVO { + + /** + * 主键 ID + */ + private Long id; + + /** + * 父分类 ID(0 为一级分类) + */ + private Long parentId; + + /** + * 分类名称 + */ + private String name; + + /** + * 分类类型 (text/image/extension/background/music) + */ + private String type; + + /** + * 排序值 + */ + private Integer sort; + + /** + * 分类图标 URL + */ + private String icon; + + /** + * 分类描述 + */ + private String description; + + /** + * 是否系统预设 (1:是 0:否) + */ + private Integer isSystem; + + /** + * 创建时间 (Unix 时间戳秒级) + */ + private Long createTime; + + /** + * 子分类列表 + */ + private List children = new ArrayList<>(); +}