From 3bf173d744d68c8e447c8018c575f098434ab975 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Thu, 7 Jan 2021 01:57:19 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E7=AE=80=E5=8C=96=20ruoyi=20=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=8F=9C=E5=8D=95=E5=88=97=E8=A1=A8=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=92=8C=E5=89=8D=E7=AB=AF=E8=80=A6=E5=90=88=E6=80=A7=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E9=87=8D=E7=9A=84=E6=83=85=E5=86=B5=202.=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=20MenuDO=20=E8=A1=A8=E9=9D=9E=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E6=80=A7=E5=AD=97=E6=AE=B5=EF=BC=8C=E8=BF=9B=E4=B8=80=E6=AD=A5?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysLoginController.java | 40 ------- .../controller/system/SysMenuController.java | 57 ++++------ ruoyi-ui/src/api/login.js | 2 +- ruoyi-ui/src/api/menu.js | 2 +- ruoyi-ui/src/store/modules/permission.js | 29 +++-- .../controller/auth/SysAuthController.http | 8 +- .../controller/auth/SysAuthController.java | 20 ++-- .../auth/vo/SysAuthGetRouterRespVO.java | 93 --------------- .../controller/auth/vo/SysAuthMenuRespVO.java | 51 +++++++++ ....java => SysAuthPermissionInfoRespVO.java} | 8 +- .../system/convert/auth/SysAuthConvert.java | 107 ++---------------- .../dataobject/permission/SysMenuDO.java | 17 +-- .../system/service/auth/SysAuthService.java | 8 +- .../service/auth/impl/SysAuthServiceImpl.java | 14 +-- .../permission/impl/SysMenuServiceImpl.java | 4 +- 15 files changed, 131 insertions(+), 329 deletions(-) delete mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java delete mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthGetRouterRespVO.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthMenuRespVO.java rename src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/{SysAuthGetInfoRespVO.java => SysAuthPermissionInfoRespVO.java} (76%) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java deleted file mode 100644 index 0391eba54..000000000 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.ruoyi.web.controller.system; - -import java.util.List; -import java.util.Set; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; -import com.ruoyi.common.constant.Constants; -import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.common.core.domain.entity.SysMenu; -import com.ruoyi.common.core.domain.entity.SysUser; -import com.ruoyi.common.core.domain.model.LoginBody; -import com.ruoyi.common.core.domain.model.LoginUser; -import com.ruoyi.common.utils.ServletUtils; -import com.ruoyi.framework.web.service.SysLoginService; -import com.ruoyi.framework.web.service.SysPermissionService; -import com.ruoyi.framework.web.service.TokenService; -import com.ruoyi.system.service.ISysMenuService; - - -public class SysLoginController { - - /** - * 获取路由信息 - * - * @return 路由信息 - */ - @GetMapping("getRouters") - public AjaxResult getRouters() - { - LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); - // 用户信息 - SysUser user = loginUser.getUser(); - List menus = menuService.selectMenuTreeByUserId(user.getUserId()); - return AjaxResult.success(menuService.buildMenus(menus)); - } - -} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysMenuController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysMenuController.java index 4c7b7c001..89c6b691a 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysMenuController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysMenuController.java @@ -1,6 +1,7 @@ package com.ruoyi.web.controller.system; import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; @@ -28,13 +29,12 @@ import com.ruoyi.system.service.ISysMenuService; /** * 菜单信息 - * + * * @author ruoyi */ @RestController @RequestMapping("/system/menu") -public class SysMenuController extends BaseController -{ +public class SysMenuController extends BaseController { @Autowired private ISysMenuService menuService; @@ -46,8 +46,7 @@ public class SysMenuController extends BaseController */ @PreAuthorize("@ss.hasPermi('system:menu:list')") @GetMapping("/list") - public AjaxResult list(SysMenu menu) - { + public AjaxResult list(SysMenu menu) { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); Long userId = loginUser.getUser().getUserId(); List menus = menuService.selectMenuList(menu, userId); @@ -59,8 +58,7 @@ public class SysMenuController extends BaseController */ @PreAuthorize("@ss.hasPermi('system:menu:query')") @GetMapping(value = "/{menuId}") - public AjaxResult getInfo(@PathVariable Long menuId) - { + public AjaxResult getInfo(@PathVariable Long menuId) { return AjaxResult.success(menuService.selectMenuById(menuId)); } @@ -68,8 +66,7 @@ public class SysMenuController extends BaseController * 获取菜单下拉树列表 */ @GetMapping("/treeselect") - public AjaxResult treeselect(SysMenu menu) - { + public AjaxResult treeselect(SysMenu menu) { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); Long userId = loginUser.getUser().getUserId(); List menus = menuService.selectMenuList(menu, userId); @@ -80,8 +77,7 @@ public class SysMenuController extends BaseController * 加载对应角色菜单列表树 */ @GetMapping(value = "/roleMenuTreeselect/{roleId}") - public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) - { + public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); List menus = menuService.selectMenuList(loginUser.getUser().getUserId()); AjaxResult ajax = AjaxResult.success(); @@ -96,15 +92,11 @@ public class SysMenuController extends BaseController @PreAuthorize("@ss.hasPermi('system:menu:add')") @Log(title = "菜单管理", businessType = BusinessType.INSERT) @PostMapping - public AjaxResult add(@Validated @RequestBody SysMenu menu) - { - if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) - { + public AjaxResult add(@Validated @RequestBody SysMenu menu) { + if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) { return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在"); - } - else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) - && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) - { + } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) + && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) { return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头"); } menu.setCreateBy(SecurityUtils.getUsername()); @@ -117,19 +109,13 @@ public class SysMenuController extends BaseController @PreAuthorize("@ss.hasPermi('system:menu:edit')") @Log(title = "菜单管理", businessType = BusinessType.UPDATE) @PutMapping - public AjaxResult edit(@Validated @RequestBody SysMenu menu) - { - if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) - { + public AjaxResult edit(@Validated @RequestBody SysMenu menu) { + if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) { return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在"); - } - else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) - && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) - { + } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) + && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) { return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头"); - } - else if (menu.getMenuId().equals(menu.getParentId())) - { + } else if (menu.getMenuId().equals(menu.getParentId())) { return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己"); } menu.setUpdateBy(SecurityUtils.getUsername()); @@ -142,16 +128,13 @@ public class SysMenuController extends BaseController @PreAuthorize("@ss.hasPermi('system:menu:remove')") @Log(title = "菜单管理", businessType = BusinessType.DELETE) @DeleteMapping("/{menuId}") - public AjaxResult remove(@PathVariable("menuId") Long menuId) - { - if (menuService.hasChildByMenuId(menuId)) - { + public AjaxResult remove(@PathVariable("menuId") Long menuId) { + if (menuService.hasChildByMenuId(menuId)) { return AjaxResult.error("存在子菜单,不允许删除"); } - if (menuService.checkMenuExistRole(menuId)) - { + if (menuService.checkMenuExistRole(menuId)) { return AjaxResult.error("菜单已分配,不允许删除"); } return toAjax(menuService.deleteMenuById(menuId)); } -} \ No newline at end of file +} diff --git a/ruoyi-ui/src/api/login.js b/ruoyi-ui/src/api/login.js index 147486151..7592d72c2 100644 --- a/ruoyi-ui/src/api/login.js +++ b/ruoyi-ui/src/api/login.js @@ -18,7 +18,7 @@ export function login(username, password, code, uuid) { // 获取用户详细信息 export function getInfo() { return request({ - url: '/get-info', + url: '/get-permission-info', method: 'get' }) } diff --git a/ruoyi-ui/src/api/menu.js b/ruoyi-ui/src/api/menu.js index d0a375553..5dae14e7a 100644 --- a/ruoyi-ui/src/api/menu.js +++ b/ruoyi-ui/src/api/menu.js @@ -3,7 +3,7 @@ import request from '@/utils/request' // 获取路由 export const getRouters = () => { return request({ - url: '/get-routers', + url: '/list-menus', method: 'get' }) } diff --git a/ruoyi-ui/src/store/modules/permission.js b/ruoyi-ui/src/store/modules/permission.js index 18ad8e0e8..85018b1e0 100644 --- a/ruoyi-ui/src/store/modules/permission.js +++ b/ruoyi-ui/src/store/modules/permission.js @@ -43,19 +43,28 @@ const permission = { // 遍历后台传来的路由字符串,转换为组件对象 function filterAsyncRouter(asyncRouterMap, isRewrite = false) { return asyncRouterMap.filter(route => { + // 将 ruoyi 后端原有耦合前端的逻辑,迁移到此处 + // 处理 meta 属性 + route.meta = { + title: route.menuName, + icon: route.icon + } + // 处理 component 属性 + if (route.children) { // 父节点 + // debugger + if (route.parentId === 0) { + route.component = Layout + } else { + route.component = ParentView + } + } else { // 根节点 + route.component = loadView(route.component) + } + + // filterChildren if (isRewrite && route.children) { route.children = filterChildren(route.children) } - if (route.component) { - // Layout ParentView 组件特殊处理 - if (route.component === 'Layout') { - route.component = Layout - } else if (route.component === 'ParentView') { - route.component = ParentView - } else { - route.component = loadView(route.component) - } - } if (route.children != null && route.children && route.children.length) { route.children = filterAsyncRouter(route.children, route, isRewrite) } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.http b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.http index 26407ff49..125a36846 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.http +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.http @@ -9,12 +9,12 @@ Content-Type: application/json "code": "1024" } -### 请求 /get-info 接口 => 成功 -GET {{baseUrl}}/get-info +### 请求 /get-permission-info 接口 => 成功 +GET {{baseUrl}}/get-permission-info Authorization: Bearer {{token}} -### 请求 /get-routers 接口 => 成功 -GET {{baseUrl}}/get-routers +### 请求 /list-menus 接口 => 成功 +GET {{baseUrl}}/list-menus Authorization: Bearer {{token}} ### 请求 /druid/xxx 接口 => 失败 TODO 临时测试 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java index e4a24d7e0..68e469c0c 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java @@ -1,8 +1,8 @@ package cn.iocoder.dashboard.modules.system.controller.auth; import cn.iocoder.dashboard.common.pojo.CommonResult; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO; import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthLoginReqVO; import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthLoginRespVO; import cn.iocoder.dashboard.modules.system.service.auth.SysAuthService; @@ -35,17 +35,17 @@ public class SysAuthController { return success(SysAuthLoginRespVO.builder().token(token).build()); } - @ApiOperation("获取登陆用户的信息") - @GetMapping("/get-info") - public CommonResult getInfo() { - SysAuthGetInfoRespVO respVO = authService.getInfo(getLoginUserId(), getLoginUserRoleIds()); + @ApiOperation("获取登陆用户的权限信息") + @GetMapping("/get-permission-info") + public CommonResult getPermissionInfo() { + SysAuthPermissionInfoRespVO respVO = authService.getPermissionInfo(getLoginUserId(), getLoginUserRoleIds()); return success(respVO); } - @ApiOperation("获得菜单 Vue 路由") - @GetMapping("get-routers") - public CommonResult> getRouters() { - List respVOList = authService.getRouters(getLoginUserId(), getLoginUserRoleIds()); + @ApiOperation("获得登陆用户的菜单列表") + @GetMapping("list-menus") + public CommonResult> listMenus() { + List respVOList = authService.listMenus(getLoginUserId(), getLoginUserRoleIds()); return success(respVOList); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthGetRouterRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthGetRouterRespVO.java deleted file mode 100644 index 91da9af88..000000000 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthGetRouterRespVO.java +++ /dev/null @@ -1,93 +0,0 @@ -package cn.iocoder.dashboard.modules.system.controller.auth.vo; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.List; - -/** - * 路由配置信息 - * - * @author ruoyi - */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder -public class SysAuthGetRouterRespVO { - - /** - * 菜单编号 - */ - private Long menuId; - /** - * 父菜单编号 - */ - private Long parentId; - - /** - * 路由名字 - */ - private String name; - - /** - * 路由地址 - */ - private String path; - - /** - * 是否隐藏路由,当设置 true 的时候该路由不会再侧边栏出现 - */ - private boolean hidden; - - /** - * 重定向地址,当设置 noRedirect 的时候该路由在面包屑导航中不可被点击 - */ - private String redirect; - - /** - * 组件地址 - */ - private String component; - - /** - * 当你一个路由下面的 children 声明的路由大于1个时,自动会变成嵌套的模式--如组件页面 - */ - private Boolean alwaysShow; - - /** - * 其他元素 - */ - private MetaVO meta; - - /** - * 子路由 - */ - private List children; - - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder - public static class MetaVO { - - /** - * 设置该路由在侧边栏和面包屑中展示的名字 - */ - private String title; - - /** - * 设置该路由的图标,对应路径src/assets/icons/svg - */ - private String icon; - - /** - * 设置为true,则不会被 缓存 - */ - private boolean noCache; - - } - -} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthMenuRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthMenuRespVO.java new file mode 100644 index 000000000..b60ed5513 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthMenuRespVO.java @@ -0,0 +1,51 @@ +package cn.iocoder.dashboard.modules.system.controller.auth.vo; + +import io.swagger.annotations.ApiModel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@ApiModel("登陆用户的菜单信息 Response VO") +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class SysAuthMenuRespVO { + + /** + * 菜单编号 + */ + private Long menuId; + /** + * 父菜单编号 + */ + private Long parentId; + + /** + * 菜单名称 + */ + private String menuName; + + /** + * 路由地址 + */ + private String path; + /** + * 组件地址 + */ + private String component; + + /** + * 菜单图标 + */ + private String icon; + + /** + * 子路由 + */ + private List children; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthGetInfoRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthPermissionInfoRespVO.java similarity index 76% rename from src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthGetInfoRespVO.java rename to src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthPermissionInfoRespVO.java index c6fbcd8bc..c8594c446 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthGetInfoRespVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/vo/SysAuthPermissionInfoRespVO.java @@ -9,20 +9,20 @@ import lombok.NoArgsConstructor; import java.util.Set; -@ApiModel("获得用户信息 Resp VO") +@ApiModel(value = "登陆用户的权限信息 Response VO", description = "额外包括用户信息和角色列表") @Data @NoArgsConstructor @AllArgsConstructor @Builder -public class SysAuthGetInfoRespVO { +public class SysAuthPermissionInfoRespVO { @ApiModelProperty(value = "用户信息", required = true) private UserVO user; - @ApiModelProperty(value = "角色权限数组", required = true) + @ApiModelProperty(value = "角色标识数组", required = true) private Set roles; - @ApiModelProperty(value = "菜单权限数组", required = true) + @ApiModelProperty(value = "操作权限数组", required = true) private Set permissions; @ApiModel("用户信息 VO") diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/convert/auth/SysAuthConvert.java b/src/main/java/cn/iocoder/dashboard/modules/system/convert/auth/SysAuthConvert.java index 8967d7fb1..2e006d876 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/convert/auth/SysAuthConvert.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/convert/auth/SysAuthConvert.java @@ -1,17 +1,12 @@ package cn.iocoder.dashboard.modules.system.convert.auth; -import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.framework.security.core.LoginUser; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; -import cn.iocoder.dashboard.modules.system.enums.permission.MenuIdEnum; -import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum; import cn.iocoder.dashboard.util.collection.CollectionUtils; -import jodd.util.StringUtil; -import org.apache.commons.lang3.StringUtils; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.factory.Mappers; @@ -21,107 +16,19 @@ import java.util.List; @Mapper public interface SysAuthConvert { - /** Layout组件标识 */ - public final static String LAYOUT = "Layout"; - - /** ParentView组件标识 */ - public final static String PARENT_VIEW = "ParentView"; - SysAuthConvert INSTANCE = Mappers.getMapper(SysAuthConvert.class); @Mapping(source = "updateTime", target = "updateTime", ignore = true) // 字段相同,但是含义不同,忽略 LoginUser convert(SysUserDO bean); - default SysAuthGetInfoRespVO convert(SysUserDO user, List roleList, List menuList) { - return SysAuthGetInfoRespVO.builder() - .user(SysAuthGetInfoRespVO.UserVO.builder().nickname(user.getNickname()).avatar(user.getAvatar()).build()) + default SysAuthPermissionInfoRespVO convert(SysUserDO user, List roleList, List menuList) { + return SysAuthPermissionInfoRespVO.builder() + .user(SysAuthPermissionInfoRespVO.UserVO.builder().nickname(user.getNickname()).avatar(user.getAvatar()).build()) .roles(CollectionUtils.convertSet(roleList, SysRoleDO::getRoleKey)) - .permissions(CollectionUtils.convertSet(menuList, SysMenuDO::getPerms)) + .permissions(CollectionUtils.convertSet(menuList, SysMenuDO::getPermission)) .build(); } - default SysAuthGetRouterRespVO convertTreeNode(SysMenuDO menu) { - SysAuthGetRouterRespVO.SysAuthGetRouterRespVOBuilder respVOBuilder = SysAuthGetRouterRespVO.builder(); - respVOBuilder.menuId(menu.getMenuId()).parentId(menu.getParentId()) - .hidden("1".equals(menu.getVisible())) // TODO 芋艿:需要处理 - .name(getRouteName(menu)).path(menu.getPath()).component(getComponent(menu)) - .meta(SysAuthGetRouterRespVO.MetaVO.builder().title(menu.getMenuName()).icon(menu.getIcon()) - .noCache("1".equals(menu.getIsCache())).build()) - ; - return respVOBuilder.build(); - } - - - - // TODO 芋艿:需要预处理。存储的时候 - default String getRouteName(SysMenuDO menu) { - String routerName = StringUtil.capitalize(menu.getPath()); // TODO 芋艿:看看怎么去除掉 - // 非外链并且是一级目录(类型为目录) - if (isMenuFrame(menu)) { - routerName = StringUtils.EMPTY; - } - return routerName; - } - - /** - * 是否为菜单内部跳转 - * - * @param menu 菜单信息 - * @return 是否 - */ - // TODO 芋艿:思考下是不是可以重构下 - default boolean isMenuFrame(SysMenuDO menu) { - return MenuIdEnum.ROOT.getId().equals(menu.getParentId()) - && MenuTypeEnum.MENU.getType().equals(menu.getMenuType()) - && CommonStatusEnum.ENABLE.getStatus().equals(menu.getIsFrame()); // TODO 芋艿:思考是不是用这个好 - } - -// /** // TODO 芋艿:后面重构下 -// * 获取路由地址 -// * -// * @param menu 菜单信息 -// * @return 路由地址 -// */ -// default String getRouterPath(SysMenuDO menu) { -// String routerPath = menu.getPath(); -// // 非外链并且是一级目录(类型为目录) -// if (MenuIdEnum.ROOT.getId().equals(menu.getParentId()) -// && MenuTypeEnum.MENU.getType().equals(menu.getMenuType()) -// && CommonStatusEnum.DISABLE.getStatus().equals(menu.getIsFrame())) { -// routerPath = "/" + menu.getPath(); -// } -// // 非外链并且是一级目录(类型为菜单) -// else if (isMenuFrame(menu)) { -// routerPath = "/"; -// } -// return routerPath; -// } - - /** - * 获取组件信息 - * - * @param menu 菜单信息 - * @return 组件信息 - */ - default String getComponent(SysMenuDO menu) { - String component = LAYOUT; - if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu)) { - component = menu.getComponent(); - } else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu)) { - component = PARENT_VIEW; - } - return component; - } - - /** - * 是否为parent_view组件 - * - * @param menu 菜单信息 - * @return 结果 - */ - default boolean isParentView(SysMenuDO menu) { - return !MenuIdEnum.ROOT.getId().equals(menu.getParentId()) - && MenuTypeEnum.DIR.getType().equals(menu.getMenuType()); - } + SysAuthMenuRespVO convertTreeNode(SysMenuDO menu); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysMenuDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysMenuDO.java index c0040fba1..65d6f1f58 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysMenuDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysMenuDO.java @@ -55,27 +55,12 @@ public class SysMenuDO extends BaseDO { @Size(max = 200, message = "组件路径不能超过255个字符") private String component; - /** - * 是否为外链(0是 1否) - */ - private String isFrame; - - /** - * 是否缓存(0缓存 1不缓存) - */ - private String isCache; - /** * 类型(M目录 C菜单 F按钮) */ @NotBlank(message = "菜单类型不能为空") private String menuType; - /** - * 显示状态(0显示 1隐藏) - */ - private String visible; - /** * 菜单状态(0显示 1隐藏) */ @@ -85,7 +70,7 @@ public class SysMenuDO extends BaseDO { * 权限字符串 */ @Size(max = 100, message = "权限标识长度不能超过100个字符") - private String perms; + private String permission; /** * 菜单图标 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthService.java index a9109c261..fb13a6397 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthService.java @@ -1,8 +1,8 @@ package cn.iocoder.dashboard.modules.system.service.auth; import cn.iocoder.dashboard.framework.security.core.service.SecurityFrameworkService; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO; import java.util.List; import java.util.Set; @@ -28,7 +28,7 @@ public interface SysAuthService extends SecurityFrameworkService { * @param roleIds 用户拥有的角色编号数组 * @return 用户的信息,包括角色权限和菜单权限 */ - SysAuthGetInfoRespVO getInfo(Long userId, Set roleIds); + SysAuthPermissionInfoRespVO getPermissionInfo(Long userId, Set roleIds); /** * 获得用户的菜单 Vue 路由 @@ -40,6 +40,6 @@ public interface SysAuthService extends SecurityFrameworkService { * @param roleIds 用户拥有的角色编号数组 * @return 菜单 Vue 路由 */ - List getRouters(Long userId, Set roleIds); + List listMenus(Long userId, Set roleIds); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java index 1189a66d8..1eaa47cb0 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -5,8 +5,8 @@ import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.framework.security.config.SecurityProperties; import cn.iocoder.dashboard.framework.security.core.LoginUser; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO; +import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO; import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; @@ -225,7 +225,7 @@ public class SysAuthServiceImpl implements SysAuthService { } @Override - public SysAuthGetInfoRespVO getInfo(Long userId, Set roleIds) { + public SysAuthPermissionInfoRespVO getPermissionInfo(Long userId, Set roleIds) { // 获得用户信息 SysUserDO user = userService.getUser(userId); if (user == null) { @@ -242,7 +242,7 @@ public class SysAuthServiceImpl implements SysAuthService { } @Override - public List getRouters(Long userId, Set roleIds) { + public List listMenus(Long userId, Set roleIds) { List menuList = permissionService.listRoleMenusFromCache(roleIds, SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType()), SetUtils.asSet(CommonStatusEnum.ENABLE.getStatus())); @@ -250,17 +250,17 @@ public class SysAuthServiceImpl implements SysAuthService { return buildRouterTree(menuList); } - private static List buildRouterTree(List menuList) { + private static List buildRouterTree(List menuList) { // 排序,保证菜单的有序性 menuList.sort(Comparator.comparing(SysMenuDO::getOrderNum)); // 构建菜单树 // 使用 LinkedHashMap 的原因,是为了排序 。实际也可以用 Stream API ,就是太丑了。 - Map treeNodeMap = new LinkedHashMap<>(); + Map treeNodeMap = new LinkedHashMap<>(); menuList.forEach(menu -> treeNodeMap.put(menu.getMenuId(), SysAuthConvert.INSTANCE.convertTreeNode(menu))); // 处理父子关系 treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(MenuIdEnum.ROOT.getId())).forEach((childNode) -> { // 获得父节点 - SysAuthGetRouterRespVO parentNode = treeNodeMap.get(childNode.getParentId()); + SysAuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId()); if (parentNode == null) { log.error("[buildRouterTree][resource({}) 找不到父资源({})]", childNode.getMenuId(), childNode.getParentId()); return; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java index 6fbaf7f04..598dd2959 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java @@ -34,7 +34,7 @@ public class SysMenuServiceImpl implements SysMenuService { private volatile Map menuCache; /** * 权限与菜单缓存 - * key:权限 {@link SysMenuDO#getPerms()} + * key:权限 {@link SysMenuDO#getPermission()} * value:SysMenuDO 数组,因为一个权限可能对应多个 SysMenuDO 对象 * * 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向 @@ -55,7 +55,7 @@ public class SysMenuServiceImpl implements SysMenuService { ImmutableMultimap.Builder permMenuCacheBuilder = ImmutableMultimap.builder(); menuList.forEach(menuDO -> { menuCacheBuilder.put(menuDO.getMenuId(), menuDO); - permMenuCacheBuilder.put(menuDO.getPerms(), menuDO); + permMenuCacheBuilder.put(menuDO.getPermission(), menuDO); }); menuCache = menuCacheBuilder.build(); permMenuCache = permMenuCacheBuilder.build();