From 561af63ec8c71f014a4cec0141f5e88e42de4783 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Mon, 14 Sep 2020 18:06:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8F=9C=E5=8D=95=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E6=8E=A5=E5=8F=A3=E5=92=8C=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- youlai-admin/pom.xml | 5 ++ .../youlai/admin/common/AdminConstant.java | 2 + .../admin/controller/SysMenuController.java | 4 +- .../youlai/admin/domain/entity/SysDict.java | 4 +- .../youlai/admin/domain/entity/SysMenu.java | 10 ++- .../com/youlai/admin/domain/vo/RouterVO.java | 38 ++++++++ .../youlai/admin/mapper/SysDictMapper.java | 6 +- .../youlai/admin/mapper/SysMenuMapper.java | 16 ++++ .../admin/mapper/SysRoleMenuMapper.java | 7 ++ .../youlai/admin/service/ISysMenuService.java | 3 + .../service/impl/SysDictServiceImpl.java | 2 +- .../service/impl/SysMenuServiceImpl.java | 86 +++++++++++++------ .../youlai/admin/AdminApplicationTests.java | 27 ++++++ 13 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 youlai-admin/src/main/java/com/youlai/admin/domain/vo/RouterVO.java create mode 100644 youlai-admin/src/test/java/com/youlai/admin/AdminApplicationTests.java diff --git a/youlai-admin/pom.xml b/youlai-admin/pom.xml index dc5ef0c56..f94a74e73 100644 --- a/youlai-admin/pom.xml +++ b/youlai-admin/pom.xml @@ -97,6 +97,11 @@ minio + + org.springframework.boot + spring-boot-starter-test + test + diff --git a/youlai-admin/src/main/java/com/youlai/admin/common/AdminConstant.java b/youlai-admin/src/main/java/com/youlai/admin/common/AdminConstant.java index a70725ff7..ee177ee8c 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/common/AdminConstant.java +++ b/youlai-admin/src/main/java/com/youlai/admin/common/AdminConstant.java @@ -7,5 +7,7 @@ public interface AdminConstant { int ROOT_DEPT_ID = 0; // 根部门ID + int ROOT_MENU_ID = 0; // 根菜单ID + int ROOT_ROLE_ID = 1; // 超级管理员角色ID } diff --git a/youlai-admin/src/main/java/com/youlai/admin/controller/SysMenuController.java b/youlai-admin/src/main/java/com/youlai/admin/controller/SysMenuController.java index 81c4a22cb..6b5af620b 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/controller/SysMenuController.java +++ b/youlai-admin/src/main/java/com/youlai/admin/controller/SysMenuController.java @@ -41,7 +41,7 @@ public class SysMenuController { @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "query", dataType = "Integer"), - @ApiImplicitParam(name = "mode", value = "查询模式: 1-表格数据 2-树形数据", paramType = "query", dataType = "Integer") + @ApiImplicitParam(name = "mode", value = "查询模式: 1-表格数据 2-树形数据 3-菜单路由", paramType = "query", dataType = "Integer") }) @GetMapping public Result list(String name, Integer roleId, Integer mode) { @@ -71,6 +71,8 @@ public class SysMenuController { map.put("checkedKeys", checkedKeys); return Result.success(map); } + }else if(mode.equals(3)){ + list = iSysMenuService.listForRouter(); } else { list = iSysMenuService.list(baseQuery); } diff --git a/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysDict.java b/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysDict.java index e589bd399..63d2190e2 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysDict.java +++ b/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysDict.java @@ -10,9 +10,9 @@ public class SysDict extends BaseEntity { @TableId private Integer id; - private String value; + private String name; - private String text; + private String value; private String typeCode; diff --git a/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysMenu.java b/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysMenu.java index 684eae4b4..10c55dc78 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysMenu.java +++ b/youlai-admin/src/main/java/com/youlai/admin/domain/entity/SysMenu.java @@ -1,14 +1,17 @@ package com.youlai.admin.domain.entity; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.youlai.common.core.entity.BaseEntity; import lombok.Data; +import java.util.List; + @Data public class SysMenu extends BaseEntity { - @TableId(type = IdType.AUTO) + @TableId private Integer id; private String name; @@ -25,6 +28,11 @@ public class SysMenu extends BaseEntity { private String path; + private String component; + private String perms; + @TableField(exist = false) + private List roles; + } diff --git a/youlai-admin/src/main/java/com/youlai/admin/domain/vo/RouterVO.java b/youlai-admin/src/main/java/com/youlai/admin/domain/vo/RouterVO.java new file mode 100644 index 000000000..9568ce818 --- /dev/null +++ b/youlai-admin/src/main/java/com/youlai/admin/domain/vo/RouterVO.java @@ -0,0 +1,38 @@ +package com.youlai.admin.domain.vo; + +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +@Data +public class RouterVO { + + @ApiModelProperty(example = "/admin") + private String path; + + @ApiModelProperty(example = "Layout") + private String component; + + @ApiModelProperty(example = "/admin/user") + private String redirect; + + @ApiModelProperty(example = "true") + private boolean alwaysShow; + + @ApiModelProperty(example = "Admin") + private String name; + + private Meta meta; + + @Data + @AllArgsConstructor + public class Meta { + private String title; + private String icon; + private List roles; + } + private List children; + +} diff --git a/youlai-admin/src/main/java/com/youlai/admin/mapper/SysDictMapper.java b/youlai-admin/src/main/java/com/youlai/admin/mapper/SysDictMapper.java index 4465e4aef..e4926a64b 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/mapper/SysDictMapper.java +++ b/youlai-admin/src/main/java/com/youlai/admin/mapper/SysDictMapper.java @@ -13,9 +13,13 @@ public interface SysDictMapper extends BaseMapper { @Select("") List list(Page page, SysDict dict); } diff --git a/youlai-admin/src/main/java/com/youlai/admin/mapper/SysMenuMapper.java b/youlai-admin/src/main/java/com/youlai/admin/mapper/SysMenuMapper.java index 3684a5c65..9fa835c42 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/mapper/SysMenuMapper.java +++ b/youlai-admin/src/main/java/com/youlai/admin/mapper/SysMenuMapper.java @@ -2,9 +2,25 @@ package com.youlai.admin.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.youlai.admin.domain.entity.SysMenu; +import org.apache.ibatis.annotations.Many; +import org.apache.ibatis.annotations.Result; +import org.apache.ibatis.annotations.Results; +import org.apache.ibatis.annotations.Select; import org.mapstruct.Mapper; +import java.util.List; + @Mapper public interface SysMenuMapper extends BaseMapper { + @Select("") + @Results({ + @Result(id=true, column="id", property="id"), + @Result(property = "roles",column="id",many = @Many(select="com.youlai.admin.mapper.SysRoleMenuMapper.listByMenuId")) + }) + List listForRouter(); } diff --git a/youlai-admin/src/main/java/com/youlai/admin/mapper/SysRoleMenuMapper.java b/youlai-admin/src/main/java/com/youlai/admin/mapper/SysRoleMenuMapper.java index f93152e1a..1f6f19fdd 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/mapper/SysRoleMenuMapper.java +++ b/youlai-admin/src/main/java/com/youlai/admin/mapper/SysRoleMenuMapper.java @@ -2,9 +2,16 @@ package com.youlai.admin.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.youlai.admin.domain.entity.SysRoleMenu; +import org.apache.ibatis.annotations.Select; import org.mapstruct.Mapper; +import java.util.List; + @Mapper public interface SysRoleMenuMapper extends BaseMapper { + @Select("") + List listByMenuId(Integer menuId); } diff --git a/youlai-admin/src/main/java/com/youlai/admin/service/ISysMenuService.java b/youlai-admin/src/main/java/com/youlai/admin/service/ISysMenuService.java index c65c20706..8eae4f11d 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/service/ISysMenuService.java +++ b/youlai-admin/src/main/java/com/youlai/admin/service/ISysMenuService.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.vo.MenuVO; +import com.youlai.admin.domain.vo.RouterVO; import com.youlai.admin.domain.vo.TreeSelectVO; import java.util.List; @@ -14,4 +15,6 @@ public interface ISysMenuService extends IService { List listForTableData(LambdaQueryWrapper baseQuery); List listForTreeSelect(LambdaQueryWrapper baseQuery); + + List listForRouter(); } diff --git a/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysDictServiceImpl.java b/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysDictServiceImpl.java index 34a4eb023..a200262bb 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysDictServiceImpl.java +++ b/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysDictServiceImpl.java @@ -18,6 +18,6 @@ public class SysDictServiceImpl extends ServiceImpl impl public IPage list(Page page, SysDict dict) { List list = this.baseMapper.list(page,dict); page.setRecords(list); - return null; + return page; } } diff --git a/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java b/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java index 1d30e69ee..e1f96a562 100644 --- a/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java +++ b/youlai-admin/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java @@ -1,14 +1,17 @@ package com.youlai.admin.service.impl; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.youlai.admin.common.AdminConstant; import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.vo.MenuVO; +import com.youlai.admin.domain.vo.RouterVO; import com.youlai.admin.domain.vo.TreeSelectVO; import com.youlai.admin.mapper.SysMenuMapper; import com.youlai.admin.service.ISysMenuService; +import org.apache.logging.log4j.util.Strings; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -21,39 +24,73 @@ public class SysMenuServiceImpl extends ServiceImpl impl @Override public List listForTableData(LambdaQueryWrapper baseQuery) { - List deptList = this.baseMapper.selectList(baseQuery); - List list = recursionForTableData(AdminConstant.ROOT_DEPT_ID, deptList); + List menuList = this.baseMapper.selectList(baseQuery); + List list = recursionForTableData(AdminConstant.ROOT_MENU_ID, menuList); return list; } - - @Override public List listForTreeSelect(LambdaQueryWrapper baseQuery) { - List deptList = this.baseMapper.selectList(baseQuery); - List list = recursionForTreeSelect(AdminConstant.ROOT_DEPT_ID, deptList); + List menuList = this.baseMapper.selectList(baseQuery); + List list = recursionForTreeSelect(AdminConstant.ROOT_MENU_ID, menuList); return list; } + @Override + public List listForRouter() { + List menuList = this.baseMapper.listForRouter(); + List list = recursionForRouter(AdminConstant.ROOT_MENU_ID, menuList); + return list; + } + + private List recursionForRouter(int parentId, List menuList) { + List list = new ArrayList<>(); + Optional.ofNullable(menuList).orElse(new ArrayList<>()) + .stream() + .filter(menu -> menu.getParentId().equals(parentId)) + .forEach(menu -> { + RouterVO routerVO = new RouterVO(); + routerVO.setName(menu.getName()); + routerVO.setPath(menu.getPath()); + String component = StrUtil.isNotBlank(menu.getComponent()) ? menu.getComponent() : "Layout"; + routerVO.setComponent(component); + RouterVO.Meta meta = routerVO.new Meta( + menu.getName(), + menu.getIcon(), + menu.getRoles() + ); + routerVO.setMeta(meta); + + if (AdminConstant.ROOT_MENU_ID == parentId) { + routerVO.setAlwaysShow(Boolean.TRUE); + routerVO.setRedirect("noRedirect"); + } + List children = recursionForRouter(menu.getId(), menuList); + routerVO.setChildren(children); + list.add(routerVO); + }); + return list; + } /** * 递归生成部门表格数据 + * * @param parentId - * @param deptList + * @param menuList * @return */ - public static List recursionForTableData(int parentId, List deptList) { + public static List recursionForTableData(int parentId, List menuList) { List list = new ArrayList<>(); - Optional.ofNullable(deptList).orElse(new ArrayList<>()) + Optional.ofNullable(menuList).orElse(new ArrayList<>()) .stream() - .filter(dept -> dept.getParentId().equals(parentId)) - .forEach(dept -> { - MenuVO deptVO = new MenuVO(); - BeanUtil.copyProperties(dept, deptVO); - List children = recursionForTableData(dept.getId(), deptList); - deptVO.setChildren(children); - list.add(deptVO); + .filter(menu -> menu.getParentId().equals(parentId)) + .forEach(menu -> { + MenuVO menuVO = new MenuVO(); + BeanUtil.copyProperties(menu, menuVO); + List children = recursionForTableData(menu.getId(), menuList); + menuVO.setChildren(children); + list.add(menuVO); }); return list; } @@ -61,20 +98,21 @@ public class SysMenuServiceImpl extends ServiceImpl impl /** * 递归生成部门树形下拉数据 + * * @param parentId - * @param deptList + * @param menuList * @return */ - public static List recursionForTreeSelect(int parentId, List deptList) { + public static List recursionForTreeSelect(int parentId, List menuList) { List list = new ArrayList<>(); - Optional.ofNullable(deptList).orElse(new ArrayList<>()) + Optional.ofNullable(menuList).orElse(new ArrayList<>()) .stream() - .filter(dept -> dept.getParentId().equals(parentId)) - .forEach(dept -> { + .filter(menu -> menu.getParentId().equals(parentId)) + .forEach(menu -> { TreeSelectVO treeSelectVO = new TreeSelectVO(); - treeSelectVO.setId(dept.getId()); - treeSelectVO.setLabel(dept.getName()); - List children = recursionForTreeSelect(dept.getId(), deptList); + treeSelectVO.setId(menu.getId()); + treeSelectVO.setLabel(menu.getName()); + List children = recursionForTreeSelect(menu.getId(), menuList); treeSelectVO.setChildren(children); list.add(treeSelectVO); }); diff --git a/youlai-admin/src/test/java/com/youlai/admin/AdminApplicationTests.java b/youlai-admin/src/test/java/com/youlai/admin/AdminApplicationTests.java new file mode 100644 index 000000000..11feb2e17 --- /dev/null +++ b/youlai-admin/src/test/java/com/youlai/admin/AdminApplicationTests.java @@ -0,0 +1,27 @@ +package com.youlai.admin; + +import cn.hutool.core.lang.Assert; +import com.youlai.admin.service.ISysMenuService; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +public class AdminApplicationTests { + + @Autowired + private ISysMenuService iSysMenuService; + + @Test + public void testListForRouter() { + List list = iSysMenuService.listForRouter(); + Assert.isTrue(list.size()>0); + } +}