From 1b184ffc63e1bbcc7dac14a90b539cc0a55cb127 Mon Sep 17 00:00:00 2001 From: dataprince Date: Fri, 15 Dec 2023 11:44:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84menu=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=8E=BB=E6=8E=89xml=E4=B8=AD?= =?UTF-8?q?=E7=9A=84sql=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/system/domain/SysMenu.java | 4 +- .../com/ruoyi/system/domain/bo/SysMenuBo.java | 2 +- .../com/ruoyi/system/domain/vo/RouterVo.java | 94 +----------- .../com/ruoyi/system/domain/vo/SysMenuVo.java | 2 +- .../ruoyi/system/mapper/SysMenuMapper.java | 49 ------- .../service/impl/SysMenuServiceImpl.java | 135 +++++++++++++++--- .../resources/mapper/system/SysMenuMapper.xml | 91 ------------ 7 files changed, 126 insertions(+), 251 deletions(-) diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysMenu.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysMenu.java index b063fcb..5349f26 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysMenu.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysMenu.java @@ -27,7 +27,7 @@ public class SysMenu extends BaseEntity /** 菜单ID */ - @Id(keyType = KeyType.Auto) + @Id private Long menuId; /** 菜单名称 */ @@ -50,7 +50,7 @@ public class SysMenu extends BaseEntity private String component; /** 路由参数 */ - private String query; + private String queryParam; /** 是否为外链(0是 1否) */ private String isFrame; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/bo/SysMenuBo.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/bo/SysMenuBo.java index 2670191..d647afe 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/bo/SysMenuBo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/bo/SysMenuBo.java @@ -59,7 +59,7 @@ public class SysMenuBo extends BaseEntity { /** * 路由参数 */ - private String query; + private String queryParam; /** * 是否为外链(0是 1否) diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/RouterVo.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/RouterVo.java index afff8c9..267cd5f 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/RouterVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/RouterVo.java @@ -1,13 +1,16 @@ package com.ruoyi.system.domain.vo; import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; + import java.util.List; /** * 路由配置信息 - * + * * @author ruoyi */ +@Data @JsonInclude(JsonInclude.Include.NON_EMPTY) public class RouterVo { @@ -56,93 +59,4 @@ public class RouterVo */ private List children; - public String getName() - { - return name; - } - - public void setName(String name) - { - this.name = name; - } - - public String getPath() - { - return path; - } - - public void setPath(String path) - { - this.path = path; - } - - public boolean getHidden() - { - return hidden; - } - - public void setHidden(boolean hidden) - { - this.hidden = hidden; - } - - public String getRedirect() - { - return redirect; - } - - public void setRedirect(String redirect) - { - this.redirect = redirect; - } - - public String getComponent() - { - return component; - } - - public void setComponent(String component) - { - this.component = component; - } - - public String getQuery() - { - return query; - } - - public void setQuery(String query) - { - this.query = query; - } - - public Boolean getAlwaysShow() - { - return alwaysShow; - } - - public void setAlwaysShow(Boolean alwaysShow) - { - this.alwaysShow = alwaysShow; - } - - public MetaVo getMeta() - { - return meta; - } - - public void setMeta(MetaVo meta) - { - this.meta = meta; - } - - public List getChildren() - { - return children; - } - - public void setChildren(List children) - { - this.children = children; - } } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/SysMenuVo.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/SysMenuVo.java index d5c5365..0131c50 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/SysMenuVo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/SysMenuVo.java @@ -56,7 +56,7 @@ public class SysMenuVo implements Serializable { /** * 路由参数 */ - private String query; + private String queryParam; /** * 是否为外链(0是 1否) diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysMenuMapper.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysMenuMapper.java index 6433099..d2ec0ab 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysMenuMapper.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysMenuMapper.java @@ -1,12 +1,9 @@ package com.ruoyi.system.mapper; -import java.util.List; import com.mybatisflex.core.BaseMapper; import com.ruoyi.system.domain.SysMenu; -import com.ruoyi.system.domain.bo.SysMenuBo; import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Param; /** * 菜单表 数据层 @@ -16,51 +13,5 @@ import org.apache.ibatis.annotations.Param; @Mapper public interface SysMenuMapper extends BaseMapper { - /** - * 根据用户所有权限 - * - * @return 权限列表 - */ - List selectMenuPerms(); - /** - * 根据用户查询系统菜单列表 - * - * @param menu 菜单信息 - * @return 菜单列表 - */ - List selectMenuListByUserId(SysMenuBo menu); - - /** - * 根据角色ID查询权限 - * - * @param roleId 角色ID - * @return 权限列表 - */ - List selectMenuPermsByRoleId(Long roleId); - - /** - * 根据用户ID查询权限 - * - * @param userId 用户ID - * @return 权限列表 - */ - List selectMenuPermsByUserId(Long userId); - - /** - * 根据用户ID查询菜单 - * - * @param userId 用户ID - * @return 菜单列表 - */ - List selectMenuTreeByUserId(Long userId); - - /** - * 根据角色ID查询菜单树信息 - * - * @param roleId 角色ID - * @param menuCheckStrictly 菜单树选择项是否关联显示 - * @return 选中菜单列表 - */ - List selectMenuListByRoleId(@Param("roleId") Long roleId, @Param("menuCheckStrictly") boolean menuCheckStrictly); } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java index 915dff7..94648c5 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java @@ -9,6 +9,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; +import com.mybatisflex.core.query.QueryMethods; import com.mybatisflex.core.query.QueryWrapper; import com.ruoyi.common.core.utils.MapstructUtils; import com.ruoyi.common.core.utils.StreamUtils; @@ -29,11 +30,15 @@ import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.system.domain.vo.MetaVo; import com.ruoyi.system.domain.vo.RouterVo; import com.ruoyi.system.mapper.SysMenuMapper; -import com.ruoyi.system.mapper.SysRoleMapper; -import com.ruoyi.system.mapper.SysRoleMenuMapper; import com.ruoyi.system.service.ISysMenuService; +import static com.mybatisflex.core.query.QueryMethods.distinct; +import static com.mybatisflex.core.query.QueryMethods.select; import static com.ruoyi.system.domain.table.SysMenuTableDef.SYS_MENU; +import static com.ruoyi.system.domain.table.SysRoleMenuTableDef.SYS_ROLE_MENU; +import static com.ruoyi.system.domain.table.SysRoleTableDef.SYS_ROLE; +import static com.ruoyi.system.domain.table.SysUserRoleTableDef.SYS_USER_ROLE; +import static com.ruoyi.system.domain.table.SysUserTableDef.SYS_USER; /** * 菜单 业务层处理 @@ -91,16 +96,46 @@ public class SysMenuServiceImpl extends BaseServiceImpl i */ @Override public List selectMenuList(SysMenuBo menuBo, Long userId) { - List menuList; + QueryWrapper queryWrapper = QueryWrapper.create(); // 管理员显示所有菜单信息 if (LoginHelper.isSuperAdmin(userId)) { - QueryWrapper queryWrapper = buildQueryWrapper(menuBo); - menuList = this.listAs(queryWrapper, SysMenuVo.class); + queryWrapper = buildQueryWrapper(menuBo); } else { - menuBo.getParams().put("userId", userId); - List list = menuMapper.selectMenuListByUserId(menuBo); - menuList = MapstructUtils.convert(list, SysMenuVo.class); + /*select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time + from sys_menu m + left join sys_role_menu rm on m.menu_id = rm.menu_id + left join sys_user_role ur on rm.role_id = ur.role_id + left join sys_role ro on ur.role_id = ro.role_id + where ur.user_id = #{params.userId} + + AND m.menu_name like concat('%', #{menuName}, '%') + + + AND m.visible = #{visible} + + + AND m.status = #{status} + + order by m.parent_id, m.order_num*/ + queryWrapper = queryWrapper + .select(QueryMethods.distinct(SYS_MENU.MENU_ID,SYS_MENU.PARENT_ID,SYS_MENU.MENU_NAME,SYS_MENU.PATH,SYS_MENU.COMPONENT,SYS_MENU.QUERY_PARAM,SYS_MENU.VISIBLE,SYS_MENU.STATUS,SYS_MENU.PERMS,SYS_MENU.IS_FRAME,SYS_MENU.IS_CACHE,SYS_MENU.MENU_TYPE,SYS_MENU.ICON,SYS_MENU.ORDER_NUM,SYS_MENU.CREATE_TIME)) + .from(SYS_MENU) + .leftJoin(SYS_ROLE_MENU).on(SYS_MENU.MENU_ID.eq(SYS_ROLE_MENU.MENU_ID)) + .leftJoin(SYS_USER_ROLE).on(SYS_ROLE_MENU.ROLE_ID.eq(SYS_USER_ROLE.ROLE_ID)) + .leftJoin(SYS_ROLE).on(SYS_ROLE.ROLE_ID.eq(SYS_USER_ROLE.ROLE_ID)) + .where(SYS_USER_ROLE.USER_ID.eq(userId)); + if (StringUtils.isNotEmpty(menuBo.getMenuName())) { + queryWrapper.and(SYS_MENU.MENU_NAME.like(menuBo.getMenuName())); + } + if (StringUtils.isNotEmpty(menuBo.getVisible())) { + queryWrapper.and(SYS_MENU.VISIBLE.eq(menuBo.getVisible())); + } + if (StringUtils.isNotEmpty(menuBo.getStatus())) { + queryWrapper.and(SYS_MENU.STATUS.eq(menuBo.getStatus())); + } + queryWrapper.orderBy(SYS_MENU.PARENT_ID.asc(),SYS_MENU.ORDER_NUM.asc()); } + List menuList = this.listAs(queryWrapper, SysMenuVo.class); return menuList; } @@ -112,7 +147,24 @@ public class SysMenuServiceImpl extends BaseServiceImpl i */ @Override public Set selectMenuPermsByUserId(Long userId) { - List perms = menuMapper.selectMenuPermsByUserId(userId); + /*select distinct m.perms + from sys_menu m + left join sys_role_menu rm on m.menu_id = rm.menu_id + left join sys_user_role ur on rm.role_id = ur.role_id + left join sys_role r on r.role_id = ur.role_id + where m.status = '0' and r.status = '0' and ur.user_id = #{userId}*/ + QueryWrapper queryWrapper = QueryWrapper.create() + .select(distinct(SYS_MENU.PERMS)) + .from(SYS_MENU) + .leftJoin(SYS_ROLE_MENU).on(SYS_ROLE_MENU.MENU_ID.eq(SYS_MENU.MENU_ID)) + .leftJoin(SYS_USER_ROLE).on(SYS_USER_ROLE.ROLE_ID.eq(SYS_ROLE_MENU.ROLE_ID)) + .leftJoin(SYS_ROLE).on(SYS_ROLE.ROLE_ID.eq(SYS_USER_ROLE.ROLE_ID)) + .where(SYS_MENU.STATUS.eq("0")) + .and(SYS_ROLE.STATUS.eq("0")) + .and(SYS_USER_ROLE.USER_ID.eq(userId)); + + List perms = this.listAs(queryWrapper, String.class); + Set permsSet = new HashSet<>(); for (String perm : perms) { if (StringUtils.isNotEmpty(perm)) { @@ -130,7 +182,19 @@ public class SysMenuServiceImpl extends BaseServiceImpl i */ @Override public Set selectMenuPermsByRoleId(Long roleId) { - List perms = menuMapper.selectMenuPermsByRoleId(roleId); + /*select distinct m.perms + from sys_menu m + left join sys_role_menu rm on m.menu_id = rm.menu_id + where m.status = '0' and rm.role_id = #{roleId}*/ + QueryWrapper queryWrapper = QueryWrapper.create() + .select(distinct(SYS_MENU.PERMS)) + .from(SYS_MENU) + .leftJoin(SYS_ROLE_MENU).on(SYS_ROLE_MENU.MENU_ID.eq(SYS_MENU.MENU_ID)) + .where(SYS_MENU.STATUS.eq("0")) + .and(SYS_ROLE_MENU.ROLE_ID.eq(roleId)); + + List perms = this.listAs(queryWrapper, String.class); + //List perms = menuMapper.selectMenuPermsByRoleId(roleId); Set permsSet = new HashSet<>(); for (String perm : perms) { if (StringUtils.isNotEmpty(perm)) { @@ -156,13 +220,32 @@ public class SysMenuServiceImpl extends BaseServiceImpl i */ @Override public List selectMenuTreeByUserId(Long userId) { - List menus; + QueryWrapper queryWrapper = QueryWrapper.create(); if (LoginHelper.isSuperAdmin(userId)) { - QueryWrapper queryWrapper = buildMenuTreeQueryWrapper(); - menus = this.list(queryWrapper); + queryWrapper = buildMenuTreeQueryWrapper(); } else { - menus = menuMapper.selectMenuTreeByUserId(userId); + /*select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time + from sys_menu m + left join sys_role_menu rm on m.menu_id = rm.menu_id + left join sys_user_role ur on rm.role_id = ur.role_id + left join sys_role ro on ur.role_id = ro.role_id + left join sys_user u on ur.user_id = u.user_id + where u.user_id = #{userId} and m.menu_type in ('M', 'C') and m.status = 0 AND ro.status = 0 + order by m.parent_id, m.order_num*/ + queryWrapper = queryWrapper + .select(QueryMethods.distinct(SYS_MENU.MENU_ID,SYS_MENU.PARENT_ID,SYS_MENU.MENU_NAME,SYS_MENU.PATH,SYS_MENU.COMPONENT,SYS_MENU.QUERY_PARAM,SYS_MENU.VISIBLE,SYS_MENU.STATUS,SYS_MENU.PERMS,SYS_MENU.IS_FRAME,SYS_MENU.IS_CACHE,SYS_MENU.MENU_TYPE,SYS_MENU.ICON,SYS_MENU.ORDER_NUM,SYS_MENU.CREATE_TIME)) + .from(SYS_MENU) + .leftJoin(SYS_ROLE_MENU).on(SYS_MENU.MENU_ID.eq(SYS_ROLE_MENU.MENU_ID)) + .leftJoin(SYS_USER_ROLE).on(SYS_ROLE_MENU.ROLE_ID.eq(SYS_USER_ROLE.ROLE_ID)) + .leftJoin(SYS_ROLE).on(SYS_ROLE.ROLE_ID.eq(SYS_USER_ROLE.ROLE_ID)) + .leftJoin(SYS_USER).on(SYS_USER.USER_ID.eq(SYS_USER_ROLE.USER_ID)) + .where(SYS_USER.USER_ID.eq(userId)) + .and(SYS_MENU.MENU_TYPE.in("M", "C")) + .and(SYS_MENU.STATUS.eq("0")) + .and(SYS_ROLE.STATUS.eq("0")) + .orderBy(SYS_MENU.PARENT_ID.asc(),SYS_MENU.ORDER_NUM.asc()); } + List menus= this.list(queryWrapper); return getChildPerms(menus, 0); } @@ -175,7 +258,25 @@ public class SysMenuServiceImpl extends BaseServiceImpl i @Override public List selectMenuListByRoleId(Long roleId) { SysRoleVo role = sysRoleService.selectRoleById(roleId); - return menuMapper.selectMenuListByRoleId(roleId, role.getMenuCheckStrictly()); + + /*select m.menu_id + from sys_menu m + left join sys_role_menu rm on m.menu_id = rm.menu_id + where rm.role_id = #{roleId} + + and m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id = rm.menu_id and rm.role_id = #{roleId}) + + order by m.parent_id, m.order_num*/ + QueryWrapper queryWrapper = QueryWrapper.create() + .select(SYS_MENU.MENU_ID) + .from(SYS_MENU) + .leftJoin(SYS_ROLE_MENU).on(SYS_ROLE_MENU.MENU_ID.eq(SYS_MENU.MENU_ID)) + .where(SYS_ROLE_MENU.ROLE_ID.eq(roleId)); + if(role.getMenuCheckStrictly()){ + queryWrapper.and(SYS_MENU.MENU_ID.notIn(select(SYS_MENU.PARENT_ID).from(SYS_MENU).innerJoin(SYS_ROLE_MENU).on(SYS_MENU.MENU_ID.eq(SYS_ROLE_MENU.MENU_ID)).and(SYS_ROLE_MENU.ROLE_ID.eq(roleId)))); + } + queryWrapper.orderBy(SYS_MENU.PARENT_ID.asc(),SYS_MENU.ORDER_NUM.asc()); + return this.listAs(queryWrapper, Long.class); } /** @@ -193,7 +294,7 @@ public class SysMenuServiceImpl extends BaseServiceImpl i router.setName(getRouteName(menu)); router.setPath(getRouterPath(menu)); router.setComponent(getComponent(menu)); - router.setQuery(menu.getQuery()); + router.setQuery(menu.getQueryParam()); router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath())); List cMenus = menu.getChildren(); if (StringUtils.isNotEmpty(cMenus) && UserConstants.TYPE_DIR.equals(menu.getMenuType())) { @@ -208,7 +309,7 @@ public class SysMenuServiceImpl extends BaseServiceImpl i children.setComponent(menu.getComponent()); children.setName(StringUtils.capitalize(menu.getPath())); children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath())); - children.setQuery(menu.getQuery()); + children.setQuery(menu.getQueryParam()); childrenList.add(children); router.setChildren(childrenList); } else if (menu.getParentId().intValue() == 0 && isInnerLink(menu)) { diff --git a/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysMenuMapper.xml b/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysMenuMapper.xml index 73a531d..b54e596 100644 --- a/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysMenuMapper.xml +++ b/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysMenuMapper.xml @@ -4,96 +4,5 @@ "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - - - - - - - - - - - - - - - - - - - - - - - - - select menu_id, menu_name, parent_id, order_num, path, component, `query`, is_frame, is_cache, menu_type, visible, status, ifnull(perms,'') as perms, icon, create_time - from sys_menu - - - - - - - - - - - - -