From 0fccad91e72b205f59f169eb7c62961635a7b292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=89=E6=9D=A5=E6=8A=80=E6=9C=AF?= <1490493387@qq.com> Date: Sun, 22 Aug 2021 21:59:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(MenuController.java):=20=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A5=E5=8F=8A=E6=95=B4=E5=90=88Spring=20?= =?UTF-8?q?Cache=20Redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/youlai/admin/pojo/vo/RouteVO.java | 5 +- .../admin/controller/MenuController.java | 92 +++++++--------- .../admin/controller/RouteController.java | 37 ------- .../youlai/admin/service/ISysMenuService.java | 25 ++++- .../service/impl/SysMenuServiceImpl.java | 103 ++++++++++++------ 5 files changed, 132 insertions(+), 130 deletions(-) delete mode 100644 youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RouteController.java diff --git a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/RouteVO.java b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/RouteVO.java index 785b6a495..dee26cda2 100644 --- a/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/RouteVO.java +++ b/youlai-admin/admin-api/src/main/java/com/youlai/admin/pojo/vo/RouteVO.java @@ -3,6 +3,7 @@ package com.youlai.admin.pojo.vo; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.List; @@ -29,12 +30,12 @@ public class RouteVO { private Meta meta; @Data + @NoArgsConstructor @AllArgsConstructor - public class Meta { + public static class Meta { private String title; private String icon; private List roles; } private List children; - } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java index 12178c322..fa8ceb1b7 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/MenuController.java @@ -1,115 +1,101 @@ package com.youlai.admin.controller; -import cn.hutool.core.util.StrUtil; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.youlai.admin.pojo.entity.SysMenu; +import com.youlai.admin.pojo.vo.MenuVO; +import com.youlai.admin.pojo.vo.RouteVO; +import com.youlai.admin.pojo.vo.TreeVO; import com.youlai.admin.service.ISysMenuService; -import com.youlai.admin.service.ISysRoleMenuService; -import com.youlai.common.enums.QueryModeEnum; import com.youlai.common.result.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; +import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.web.bind.annotation.*; import java.util.Arrays; import java.util.List; /** - * @author xianrui + * 菜单控制器 + * + * @author xianrui * @date 2020-11-06 */ @Api(tags = "菜单接口") @RestController @RequestMapping("/api/v1/menus") -@Slf4j -@AllArgsConstructor +@RequiredArgsConstructor public class MenuController { - private ISysMenuService iSysMenuService; + private final ISysMenuService menuService; - @ApiOperation(value = "菜单列表") - @ApiImplicitParams({ - @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "query", dataType = "Long"), - @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum") - }) - @GetMapping - public Result list(String queryMode, String name) { + @ApiOperation(value = "菜单表格(Table)层级列表") + @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String") + @GetMapping("/table") + public Result getMenuTableList(String name) { + List menuList = menuService.listTable(name); + return Result.success(menuList); + } - QueryModeEnum queryModeEnum = QueryModeEnum.getByCode(queryMode); - LambdaQueryWrapper baseQuery = new LambdaQueryWrapper() - .orderByAsc(SysMenu::getSort) - .orderByDesc(SysMenu::getGmtModified) - .orderByDesc(SysMenu::getGmtCreate); - List list; - switch (queryModeEnum) { - case LIST: - baseQuery = baseQuery.like(StrUtil.isNotBlank(name), SysMenu::getName, name); - list = iSysMenuService.listMenuVO(baseQuery); - break; - case TREE: - list = iSysMenuService.listTreeVO(baseQuery); - break; - default: - list = iSysMenuService.list(baseQuery); - break; - } - return Result.success(list); + @ApiOperation(value = "菜单下拉(Select)层级列表") + @GetMapping("/select") + public Result getMenuSelectList() { + List menuList = menuService.listSelect(); + return Result.success(menuList); + } + + @ApiOperation(value = "菜单路由(Route)层级列表") + @GetMapping("/route") + public Result getMenuRouteList() { + List menuList = menuService.listRoute(); + return Result.success(menuList); } @ApiOperation(value = "菜单详情") @ApiImplicitParam(name = "id", value = "菜单id", required = true, paramType = "path", dataType = "Long") @GetMapping("/{id}") public Result detail(@PathVariable Integer id) { - SysMenu menu = iSysMenuService.getById(id); + SysMenu menu = menuService.getById(id); return Result.success(menu); } @ApiOperation(value = "新增菜单") - @ApiImplicitParam(name = "menu", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysMenu") @PostMapping + @CacheEvict(cacheNames = "admin",key = "'routeList'") public Result add(@RequestBody SysMenu menu) { - boolean status = iSysMenuService.save(menu); + boolean status = menuService.save(menu); return Result.judge(status); } @ApiOperation(value = "修改菜单") - @ApiImplicitParams({ - @ApiImplicitParam(name = "id", value = "菜单id", required = true, paramType = "path", dataType = "Long"), - @ApiImplicitParam(name = "menu", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysMenu") - }) @PutMapping(value = "/{id}") + @CacheEvict(cacheNames = "admin",key = "'routeList'") public Result update( @PathVariable Integer id, @RequestBody SysMenu menu) { - boolean status = iSysMenuService.updateById(menu); + boolean status = menuService.updateById(menu); return Result.judge(status); } @ApiOperation(value = "删除菜单") @ApiImplicitParam(name = "ids", value = "id集合字符串,以,分割", required = true, paramType = "query", dataType = "String") @DeleteMapping("/{ids}") + @CacheEvict(cacheNames = "admin",key = "'routeList'") public Result delete(@PathVariable("ids") String ids) { - boolean status = iSysMenuService.removeByIds(Arrays.asList(ids.split(","))); + boolean status = menuService.removeByIds(Arrays.asList(ids.split(","))); return Result.judge(status); } - @ApiOperation(value = "修改菜单") - @ApiImplicitParams({ - @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "Long"), - @ApiImplicitParam(name = "menu", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysMenu") - }) + @ApiOperation(value = "选择性修改菜单") @PatchMapping(value = "/{id}") + @CacheEvict(cacheNames = "admin",key = "'routeList'") public Result patch(@PathVariable Integer id, @RequestBody SysMenu menu) { LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper().eq(SysMenu::getId, id); updateWrapper.set(menu.getVisible() != null, SysMenu::getVisible, menu.getVisible()); - boolean result = iSysMenuService.update(updateWrapper); + boolean result = menuService.update(updateWrapper); return Result.judge(result); } } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RouteController.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RouteController.java deleted file mode 100644 index e1baefc8f..000000000 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/controller/RouteController.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.youlai.admin.controller; - -import com.youlai.admin.pojo.vo.RouteVO; -import com.youlai.admin.service.ISysMenuService; -import com.youlai.common.result.Result; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; - -/** - * @author xianrui - * @date 2021/6/1 22:40 - */ -@Api(tags = "路由接口") -@RestController -@RequestMapping("/api/v1/routes") -@Slf4j -@AllArgsConstructor -public class RouteController { - - private ISysMenuService iSysMenuService; - - @ApiOperation(value = "路由列表") - @GetMapping - public Result list() { - List list = iSysMenuService.listRoute(); - return Result.success(list); - } -} - - diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysMenuService.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysMenuService.java index c20249941..28705ad2c 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysMenuService.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/ISysMenuService.java @@ -1,7 +1,6 @@ package com.youlai.admin.service; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.youlai.admin.pojo.entity.SysMenu; import com.youlai.admin.pojo.vo.MenuVO; @@ -9,15 +8,35 @@ import com.youlai.admin.pojo.vo.RouteVO; import com.youlai.admin.pojo.vo.TreeVO; import java.util.List; + /** * @author haoxr * @date 2020-11-06 */ public interface ISysMenuService extends IService { - List listMenuVO(LambdaQueryWrapper baseQuery); - List listTreeVO(LambdaQueryWrapper baseQuery); + /** + * 菜单表格(Table)层级列表 + * + * @param name 菜单名称 + * @return + */ + List listTable(String name); + + /** + * 菜单下拉(Select)层级列表 + * + * @return + */ + List listSelect(); + + + /** + * 菜单路由(Route)层级列表 + * + * @return + */ List listRoute(); } diff --git a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java index f5b2482e7..28b82f971 100644 --- a/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java +++ b/youlai-admin/admin-boot/src/main/java/com/youlai/admin/service/impl/SysMenuServiceImpl.java @@ -2,6 +2,7 @@ package com.youlai.admin.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollectionUtil; +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.constant.SystemConstants; @@ -12,6 +13,7 @@ import com.youlai.admin.mapper.SysMenuMapper; import com.youlai.admin.pojo.vo.TreeVO; import com.youlai.admin.service.ISysMenuService; import com.youlai.common.constant.GlobalConstants; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -20,61 +22,80 @@ import java.util.Optional; /** - * @author xianrui + * 菜单业务类 + * + * @author xianrui * @date 2020-11-06 */ @Service public class SysMenuServiceImpl extends ServiceImpl implements ISysMenuService { + /** + * 菜单表格(Table)层级列表 + * + * @param name 菜单名称 + * @return + */ @Override - public List listMenuVO(LambdaQueryWrapper baseQuery) { - List menuList = this.baseMapper.selectList(baseQuery); - List list = recursionForTree(SystemConstants.ROOT_MENU_ID, menuList); - return list; - } - - @Override - public List listTreeVO(LambdaQueryWrapper baseQuery) { - List menuList = this.list(baseQuery); - List list = recursionForTreeSelect(SystemConstants.ROOT_MENU_ID, menuList); - return list; + public List listTable(String name) { + List menuList = this.list( + new LambdaQueryWrapper() + .like(StrUtil.isNotBlank(name), SysMenu::getName, name) + .orderByAsc(SysMenu::getSort) + ); + List tableList = recursionTableList(SystemConstants.ROOT_MENU_ID, menuList); + return tableList; } /** - * 递归生成表格数据 + * 递归生成菜单表格层级列表 * - * @param parentId - * @param menuList + * @param parentId 父级ID + * @param menuList 菜单列表 * @return */ - public static List recursionForTree(Long parentId, List menuList) { - List list = new ArrayList<>(); + private static List recursionTableList(Long parentId, List menuList) { + List menuTableList = new ArrayList<>(); Optional.ofNullable(menuList).orElse(new ArrayList<>()) .stream() .filter(menu -> menu.getParentId().equals(parentId)) .forEach(menu -> { MenuVO menuVO = new MenuVO(); BeanUtil.copyProperties(menu, menuVO); - List children = recursionForTree(menu.getId(), menuList); + List children = recursionTableList(menu.getId(), menuList); + if (CollectionUtil.isNotEmpty(children)) { menuVO.setChildren(children); } - list.add(menuVO); + menuTableList.add(menuVO); }); - return list; + return menuTableList; } /** - * 递归生成部门树形下拉数据 + * 菜单下拉(Select)层级列表 * - * @param parentId - * @param menuList * @return */ - public static List recursionForTreeSelect(Long parentId, List menuList) { - List list = new ArrayList<>(); + @Override + public List listSelect() { + List menuList = this.list(new LambdaQueryWrapper().orderByAsc(SysMenu::getSort)); + List menuSelectList = recursionSelectList(SystemConstants.ROOT_MENU_ID, menuList); + return menuSelectList; + } + + + /** + * 递归生成菜单下拉层级列表 + * + * @param parentId 父级ID + * @param menuList 菜单列表 + * @return + */ + private static List recursionSelectList(Long parentId, List menuList) { + List menuSelectList = new ArrayList<>(); Optional.ofNullable(menuList).orElse(new ArrayList<>()) .stream() .filter(menu -> menu.getParentId().equals(parentId)) @@ -82,40 +103,52 @@ public class SysMenuServiceImpl extends ServiceImpl impl TreeVO treeVO = new TreeVO(); treeVO.setId(menu.getId()); treeVO.setLabel(menu.getName()); - List children = recursionForTreeSelect(menu.getId(), menuList); + List children = recursionSelectList(menu.getId(), menuList); if (CollectionUtil.isNotEmpty(children)) { treeVO.setChildren(children); } - list.add(treeVO); + menuSelectList.add(treeVO); }); - return list; + return menuSelectList; } + /** + * 菜单路由(Route)层级列表 + *

+ * 读多写少,缓存至Redis + * + * @return + * @Cacheable cacheNames:缓存名称,不同缓存的数据是彼此隔离; key: 缓存Key。 + */ @Override + @Cacheable(cacheNames = "admin", key = "'routeList'") public List listRoute() { List menuList = this.baseMapper.listRoute(); List list = recursionRoute(SystemConstants.ROOT_MENU_ID, menuList); return list; } - // 递归生成路由 + + /** + * 递归生成菜单路由层级列表 + * + * @param parentId 父级ID + * @param menuList 菜单列表 + * @return + */ private List recursionRoute(Long parentId, List menuList) { List list = new ArrayList<>(); Optional.ofNullable(menuList).ifPresent(menus -> menus.stream().filter(menu -> menu.getParentId().equals(parentId)) .forEach(menu -> { RouteVO routeVO = new RouteVO(); - routeVO.setName(menu.getId() + ""); // 根据name路由跳转 this.$router.push({path:xxx}) routeVO.setPath(menu.getPath()); // 根据path路由跳转 this.$router.push({name:xxx}) routeVO.setRedirect(menu.getRedirect()); routeVO.setComponent(menu.getComponent()); routeVO.setRedirect(menu.getRedirect()); - routeVO.setMeta(routeVO.new Meta( - menu.getName(), - menu.getIcon(), - menu.getRoles() - )); + RouteVO.Meta meta = new RouteVO.Meta(menu.getName(), menu.getIcon(), menu.getRoles()); + routeVO.setMeta(meta); // 菜单显示隐藏 routeVO.setHidden(!GlobalConstants.STATUS_YES.equals(menu.getVisible())); List children = recursionRoute(menu.getId(), menuList);