diff --git a/login/src/main/java/com/guwan/RBAC/SystemMenu.java b/login/src/main/java/com/guwan/RBAC/SystemMenu.java index 4bf9f07..f8dc192 100644 --- a/login/src/main/java/com/guwan/RBAC/SystemMenu.java +++ b/login/src/main/java/com/guwan/RBAC/SystemMenu.java @@ -6,6 +6,9 @@ import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + import lombok.Data; /** @@ -129,6 +132,13 @@ public class SystemMenu implements Serializable { @TableField(value = "deleted") private Boolean deleted; + + // 添加 children 属性,表示子菜单 + @TableField(exist = false) + private List children = new ArrayList<>(); + + + @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/login/src/main/java/com/guwan/controller/login/DemoController.java b/login/src/main/java/com/guwan/controller/login/DemoController.java index 257263e..48b7161 100644 --- a/login/src/main/java/com/guwan/controller/login/DemoController.java +++ b/login/src/main/java/com/guwan/controller/login/DemoController.java @@ -2,6 +2,7 @@ package com.guwan.controller.login; import com.guwan.RBAC.TempVo; import com.guwan.common.R; +import com.guwan.service.SystemUsersService; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -12,13 +13,14 @@ import org.springframework.web.bind.annotation.RestController; public class DemoController { @Resource - private service.SystemUsersService systemUsersService; + private SystemUsersService systemUsersService; @GetMapping("1") public R test(){ - systemUsersService.getRouterAsync(); + // systemUsersService.getRouterAsync(); - return R.ok().put("data", new TempVo().setName("1111")); + + return R.ok().put("data", systemUsersService.getRouterAsync()); } } diff --git a/login/src/main/java/com/guwan/service/SystemUsersService.java b/login/src/main/java/com/guwan/service/SystemUsersService.java index e11105a..820b4ae 100644 --- a/login/src/main/java/com/guwan/service/SystemUsersService.java +++ b/login/src/main/java/com/guwan/service/SystemUsersService.java @@ -1,4 +1,4 @@ -package service; +package com.guwan.service; import com.guwan.RBAC.SystemUsers; import com.baomidou.mybatisplus.extension.service.IService; @@ -11,6 +11,6 @@ import com.baomidou.mybatisplus.extension.service.IService; public interface SystemUsersService extends IService { - public void getRouterAsync(); + public String getRouterAsync(); } diff --git a/login/src/main/java/com/guwan/service/impl/SystemUsersServiceImpl.java b/login/src/main/java/com/guwan/service/impl/SystemUsersServiceImpl.java index f74643a..2309db9 100644 --- a/login/src/main/java/com/guwan/service/impl/SystemUsersServiceImpl.java +++ b/login/src/main/java/com/guwan/service/impl/SystemUsersServiceImpl.java @@ -2,16 +2,17 @@ package com.guwan.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.guwan.RBAC.*; import com.guwan.mapper.*; +import com.guwan.service.SystemUsersService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import service.SystemUsersService; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; + +import java.util.*; import java.util.stream.Collectors; /** @@ -22,7 +23,7 @@ import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class SystemUsersServiceImpl extends ServiceImpl - implements SystemUsersService{ + implements SystemUsersService { private final SystemUsersMapper systemUsersMapper; @@ -35,7 +36,7 @@ public class SystemUsersServiceImpl extends ServiceImpl roleList = ; @@ -98,12 +99,105 @@ public class SystemUsersServiceImpl extends ServiceImpl strings1 = new ArrayList<>(); + routerMeta.setAuths(strings1); + ArrayList strings2 = new ArrayList<>(); + routerMeta.setRoles(strings1); + tempVo.setMeta(routerMeta); + List list = new ArrayList<>(List.of(tempVo)); + //tempVo.setChildren(list); + tempVos.add(tempVo); + } + + System.out.println("tempVos = " + tempVos);*/ + + ArrayList integers = new ArrayList<>(); + + + for (SystemMenu systemMenu : systemMenus) { + System.out.println("systemMenu = " + systemMenu); + Integer count = 1; + Long parentId = systemMenu.getParentId(); + while (parentId != 0L){ + List temp = systemMenuMapper.selectObjs(new LambdaQueryWrapper() + .eq(SystemMenu::getId, parentId).select(SystemMenu::getParentId)); + if (temp.isEmpty()) { + // 如果找不到父菜单,跳出循环,避免死循环 + break; + } + parentId = temp.get(0); + + count++; + }; + integers.add(count); + } + + + + System.out.println("integers = " + integers); + + System.out.println("max = " + Collections.max(integers)); + + System.out.println("integers = " + integers.size()); + + + + + + + + return "111"; + + + + // System.out.println("systemMenus = " + buildMenuTree(systemMenus)); } + + + // 构建 JSON 数据结构 + private static Map buildJson(List rootMenus) { + Map rootJson = new HashMap<>(); + for (SystemMenu menu : rootMenus) { + Map menuJson = new HashMap<>(); + menuJson.put("path", menu.getPath()); + + // 构建 meta 数据 + Map meta = new HashMap<>(); + meta.put("title", menu.getName()); // 使用菜单名称作为 title + meta.put("icon", menu.getIcon()); + meta.put("rank", menu.getSort()); + + // 如果有子菜单,则继续构建 + if (menu.getChildren() != null && !menu.getChildren().isEmpty()) { + menuJson.put("children", buildJson(menu.getChildren())); + } + + menuJson.put("meta", meta); + rootJson.put(menu.getPath(), menuJson); + } + return rootJson; + } + + + + + + + + // 方法:将菜单列表构建为树形结构 public static List buildMenuTree(List menuList) { // 以 parentId 分组菜单 @@ -118,10 +212,10 @@ public class SystemUsersServiceImpl extends ServiceImpl buildTree(Map> parentIdMap, Long parentId) { List children = parentIdMap.getOrDefault(parentId, new ArrayList<>()); - for (SystemMenu menu : children) { + /* for (SystemMenu menu : children) { // 递归设置子菜单 menu.setChildren(buildTree(parentIdMap, menu.getId())); - } + }*/ return children; }