根据需求开发:组件库系统开发完成

This commit is contained in:
lizhengwei 2026-03-08 22:39:21 +08:00
parent 2d79940ce8
commit 3271c7f86d
5 changed files with 140 additions and 27 deletions

View File

@ -4,7 +4,7 @@ public interface BaseConstant {
/**
* 微服务上下文路径
*/
String CONTEXT_PATH = "/booksflow/form/v1.0/";
String CONTEXT_PATH = "/form/";
/**
* 微服务名称

View File

@ -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<List<H5ComponentCategory>> getCategoryTree() {
List<H5ComponentCategory> result = h5ComponentCategoryService.getCategoryTree();
public Result<List<H5ComponentCategoryTreeVO>> getCategoryTree() {
List<H5ComponentCategoryTreeVO> result = h5ComponentCategoryService.getCategoryTree();
return new Result<>(result);
}

View File

@ -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<H5ComponentCategory> getCategoryTree();
List<H5ComponentCategoryTreeVO> getCategoryTree();
}

View File

@ -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<H5ComponentCateg
LambdaQueryWrapper<H5ComponentCategory> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(H5ComponentCategory::getParentId, 0L)
.orderByAsc(H5ComponentCategory::getSort);
return baseMapper.selectList(wrapper);
}
@ -40,7 +44,7 @@ public class H5ComponentCategoryServiceImpl extends ServiceImpl<H5ComponentCateg
LambdaQueryWrapper<H5ComponentCategory> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(H5ComponentCategory::getParentId, parentId)
.orderByAsc(H5ComponentCategory::getSort);
return baseMapper.selectList(wrapper);
}
@ -50,23 +54,23 @@ public class H5ComponentCategoryServiceImpl extends ServiceImpl<H5ComponentCateg
wrapper.eq(H5ComponentCategory::getType, type)
.orderByAsc(H5ComponentCategory::getParentId)
.orderByAsc(H5ComponentCategory::getSort);
return baseMapper.selectList(wrapper);
}
@Override
public Long create(H5ComponentCategory category) {
long now = Instant.now().getEpochSecond();
if (category.getParentId() == null) {
category.setParentId(0L);
}
category.setCreateTime(now);
category.setUpdateTime(now);
baseMapper.insert(category);
return category.getId();
}
@ -82,29 +86,70 @@ public class H5ComponentCategoryServiceImpl extends ServiceImpl<H5ComponentCateg
LambdaQueryWrapper<H5ComponentCategory> 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<H5ComponentCategory> getCategoryTree() {
// 查询所有一级分类
List<H5ComponentCategory> topLevelCategories = getTopLevelCategories();
// 为每个一级分类查询子分类
for (H5ComponentCategory topLevel : topLevelCategories) {
List<H5ComponentCategory> childCategories = getChildCategories(topLevel.getId());
// 这里简单处理实际可以考虑使用专门的树形结构 VO
if (!childCategories.isEmpty()) {
// 可以在返回时添加额外信息
log.debug("分类 {} 有 {} 个子分类", topLevel.getName(), childCategories.size());
public List<H5ComponentCategoryTreeVO> getCategoryTree() {
// 一次性查询所有分类排除已删除的
LambdaQueryWrapper<H5ComponentCategory> wrapper = new LambdaQueryWrapper<>();
wrapper.orderByAsc(H5ComponentCategory::getParentId, H5ComponentCategory::getSort);
List<H5ComponentCategory> allCategories = baseMapper.selectList(wrapper);
// 转换为 VO
List<H5ComponentCategoryTreeVO> allVOs = allCategories.stream()
.map(this::convertToTreeVO)
.toList();
// 构建树形结构
return buildTree(allVOs);
}
/**
* 构建树形结构
*
* @param allNodes 所有节点
* @return 树形结构只包含一级分类
*/
private List<H5ComponentCategoryTreeVO> buildTree(List<H5ComponentCategoryTreeVO> allNodes) {
List<H5ComponentCategoryTreeVO> tree = new ArrayList<>();
// 使用 Map 提高查找效率
Map<Long, H5ComponentCategoryTreeVO> 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;
}
}

View File

@ -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<H5ComponentCategoryTreeVO> children = new ArrayList<>();
}