123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- package com.ruoyi.system.service.impl;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Set;
- import java.util.stream.Collectors;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.ruoyi.common.constant.Constants;
- import com.ruoyi.common.constant.UserConstants;
- import com.ruoyi.common.core.domain.TreeSelect;
- import com.ruoyi.common.core.domain.entity.SysMenu;
- import com.ruoyi.common.core.domain.entity.SysRole;
- import com.ruoyi.common.core.domain.entity.SysUser;
- import com.ruoyi.common.utils.SecurityUtils;
- import com.ruoyi.common.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;
- /**
- * 菜单 业务层处理
- *
- * @author ruoyi
- */
- @Service
- public class SysMenuServiceImpl implements ISysMenuService
- {
- public static final String PREMISSION_STRING = "perms[\"{0}\"]";
- @Autowired
- private SysMenuMapper menuMapper;
- @Autowired
- private SysRoleMapper roleMapper;
- @Autowired
- private SysRoleMenuMapper roleMenuMapper;
- /**
- * 根据用户查询系统菜单列表
- *
- * @param userId 用户ID
- * @return 菜单列表
- */
- @Override
- public List<SysMenu> selectMenuList(Long userId)
- {
- return selectMenuList(new SysMenu(), userId);
- }
- /**
- * 查询系统菜单列表
- *
- * @param menu 菜单信息
- * @return 菜单列表
- */
- @Override
- public List<SysMenu> selectMenuList(SysMenu menu, Long userId)
- {
- List<SysMenu> menuList = null;
- // 管理员显示所有菜单信息
- if (SysUser.isAdmin(userId))
- {
- menuList = menuMapper.selectMenuList(menu);
- }
- else
- {
- menu.getParams().put("userId", userId);
- menuList = menuMapper.selectMenuListByUserId(menu);
- }
- return menuList;
- }
- /**
- * 根据用户ID查询权限
- *
- * @param userId 用户ID
- * @return 权限列表
- */
- @Override
- public Set<String> selectMenuPermsByUserId(Long userId)
- {
- List<String> perms = menuMapper.selectMenuPermsByUserId(userId);
- Set<String> permsSet = new HashSet<>();
- for (String perm : perms)
- {
- if (StringUtils.isNotEmpty(perm))
- {
- permsSet.addAll(Arrays.asList(perm.trim().split(",")));
- }
- }
- return permsSet;
- }
- /**
- * 根据用户ID查询菜单
- *
- * @param userId 用户名称
- * @return 菜单列表
- */
- @Override
- public List<SysMenu> selectMenuTreeByUserId(Long userId)
- {
- List<SysMenu> menus = null;
- if (SecurityUtils.isAdmin(userId))
- {
- menus = menuMapper.selectMenuTreeAll();
- }
- else
- {
- menus = menuMapper.selectMenuTreeByUserId(userId);
- }
- return getChildPerms(menus, 0);
- }
- /**
- * 根据角色ID查询菜单树信息
- *
- * @param roleId 角色ID
- * @return 选中菜单列表
- */
- @Override
- public List<Long> selectMenuListByRoleId(Long roleId)
- {
- SysRole role = roleMapper.selectRoleById(roleId);
- return menuMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
- }
- /**
- * 构建前端路由所需要的菜单
- *
- * @param menus 菜单列表
- * @return 路由列表
- */
- @Override
- public List<RouterVo> buildMenus(List<SysMenu> menus)
- {
- List<RouterVo> routers = new LinkedList<RouterVo>();
- for (SysMenu menu : menus)
- {
- RouterVo router = new RouterVo();
- router.setHidden("1".equals(menu.getVisible()));
- router.setName(getRouteName(menu));
- router.setPath(getRouterPath(menu));
- router.setComponent(getComponent(menu));
- router.setQuery(menu.getQuery());
- router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath()));
- List<SysMenu> cMenus = menu.getChildren();
- if (!cMenus.isEmpty() && cMenus.size() > 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType()))
- {
- router.setAlwaysShow(true);
- router.setRedirect("noRedirect");
- router.setChildren(buildMenus(cMenus));
- }
- else if (isMenuFrame(menu))
- {
- router.setMeta(null);
- List<RouterVo> childrenList = new ArrayList<RouterVo>();
- RouterVo children = new RouterVo();
- children.setPath(menu.getPath());
- 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());
- childrenList.add(children);
- router.setChildren(childrenList);
- }
- else if (menu.getParentId().intValue() == 0 && isInnerLink(menu))
- {
- router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon()));
- router.setPath("/");
- List<RouterVo> childrenList = new ArrayList<RouterVo>();
- RouterVo children = new RouterVo();
- String routerPath = innerLinkReplaceEach(menu.getPath());
- children.setPath(routerPath);
- children.setComponent(UserConstants.INNER_LINK);
- children.setName(StringUtils.capitalize(routerPath));
- children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), menu.getPath()));
- childrenList.add(children);
- router.setChildren(childrenList);
- }
- routers.add(router);
- }
- return routers;
- }
- /**
- * 构建前端所需要树结构
- *
- * @param menus 菜单列表
- * @return 树结构列表
- */
- @Override
- public List<SysMenu> buildMenuTree(List<SysMenu> menus)
- {
- List<SysMenu> returnList = new ArrayList<SysMenu>();
- List<Long> tempList = new ArrayList<Long>();
- for (SysMenu dept : menus)
- {
- tempList.add(dept.getMenuId());
- }
- for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext();)
- {
- SysMenu menu = (SysMenu) iterator.next();
- // 如果是顶级节点, 遍历该父节点的所有子节点
- if (!tempList.contains(menu.getParentId()))
- {
- recursionFn(menus, menu);
- returnList.add(menu);
- }
- }
- if (returnList.isEmpty())
- {
- returnList = menus;
- }
- return returnList;
- }
- /**
- * 构建前端所需要下拉树结构
- *
- * @param menus 菜单列表
- * @return 下拉树结构列表
- */
- @Override
- public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus)
- {
- List<SysMenu> menuTrees = buildMenuTree(menus);
- return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
- }
- /**
- * 根据菜单ID查询信息
- *
- * @param menuId 菜单ID
- * @return 菜单信息
- */
- @Override
- public SysMenu selectMenuById(Long menuId)
- {
- return menuMapper.selectMenuById(menuId);
- }
- /**
- * 是否存在菜单子节点
- *
- * @param menuId 菜单ID
- * @return 结果
- */
- @Override
- public boolean hasChildByMenuId(Long menuId)
- {
- int result = menuMapper.hasChildByMenuId(menuId);
- return result > 0;
- }
- /**
- * 查询菜单使用数量
- *
- * @param menuId 菜单ID
- * @return 结果
- */
- @Override
- public boolean checkMenuExistRole(Long menuId)
- {
- int result = roleMenuMapper.checkMenuExistRole(menuId);
- return result > 0;
- }
- /**
- * 新增保存菜单信息
- *
- * @param menu 菜单信息
- * @return 结果
- */
- @Override
- public int insertMenu(SysMenu menu)
- {
- return menuMapper.insertMenu(menu);
- }
- /**
- * 修改保存菜单信息
- *
- * @param menu 菜单信息
- * @return 结果
- */
- @Override
- public int updateMenu(SysMenu menu)
- {
- return menuMapper.updateMenu(menu);
- }
- /**
- * 删除菜单管理信息
- *
- * @param menuId 菜单ID
- * @return 结果
- */
- @Override
- public int deleteMenuById(Long menuId)
- {
- return menuMapper.deleteMenuById(menuId);
- }
- /**
- * 校验菜单名称是否唯一
- *
- * @param menu 菜单信息
- * @return 结果
- */
- @Override
- public String checkMenuNameUnique(SysMenu menu)
- {
- Long menuId = StringUtils.isNull(menu.getMenuId()) ? -1L : menu.getMenuId();
- SysMenu info = menuMapper.checkMenuNameUnique(menu.getMenuName(), menu.getParentId());
- if (StringUtils.isNotNull(info) && info.getMenuId().longValue() != menuId.longValue())
- {
- return UserConstants.NOT_UNIQUE;
- }
- return UserConstants.UNIQUE;
- }
- /**
- * 获取路由名称
- *
- * @param menu 菜单信息
- * @return 路由名称
- */
- public String getRouteName(SysMenu menu)
- {
- String routerName = StringUtils.capitalize(menu.getPath());
- // 非外链并且是一级目录(类型为目录)
- if (isMenuFrame(menu))
- {
- routerName = StringUtils.EMPTY;
- }
- return routerName;
- }
- /**
- * 获取路由地址
- *
- * @param menu 菜单信息
- * @return 路由地址
- */
- public String getRouterPath(SysMenu menu)
- {
- String routerPath = menu.getPath();
- // 内链打开外网方式
- if (menu.getParentId().intValue() != 0 && isInnerLink(menu))
- {
- routerPath = innerLinkReplaceEach(routerPath);
- }
- // 非外链并且是一级目录(类型为目录)
- if (0 == menu.getParentId().intValue() && UserConstants.TYPE_DIR.equals(menu.getMenuType())
- && UserConstants.NO_FRAME.equals(menu.getIsFrame()))
- {
- routerPath = "/" + menu.getPath();
- }
- // 非外链并且是一级目录(类型为菜单)
- else if (isMenuFrame(menu))
- {
- routerPath = "/";
- }
- return routerPath;
- }
- /**
- * 获取组件信息
- *
- * @param menu 菜单信息
- * @return 组件信息
- */
- public String getComponent(SysMenu menu)
- {
- String component = UserConstants.LAYOUT;
- if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu))
- {
- component = menu.getComponent();
- }
- else if (StringUtils.isEmpty(menu.getComponent()) && menu.getParentId().intValue() != 0 && isInnerLink(menu))
- {
- component = UserConstants.INNER_LINK;
- }
- else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu))
- {
- component = UserConstants.PARENT_VIEW;
- }
- return component;
- }
- /**
- * 是否为菜单内部跳转
- *
- * @param menu 菜单信息
- * @return 结果
- */
- public boolean isMenuFrame(SysMenu menu)
- {
- return menu.getParentId().intValue() == 0 && UserConstants.TYPE_MENU.equals(menu.getMenuType())
- && menu.getIsFrame().equals(UserConstants.NO_FRAME);
- }
- /**
- * 是否为内链组件
- *
- * @param menu 菜单信息
- * @return 结果
- */
- public boolean isInnerLink(SysMenu menu)
- {
- return menu.getIsFrame().equals(UserConstants.NO_FRAME) && StringUtils.ishttp(menu.getPath());
- }
- /**
- * 是否为parent_view组件
- *
- * @param menu 菜单信息
- * @return 结果
- */
- public boolean isParentView(SysMenu menu)
- {
- return menu.getParentId().intValue() != 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType());
- }
- /**
- * 根据父节点的ID获取所有子节点
- *
- * @param list 分类表
- * @param parentId 传入的父节点ID
- * @return String
- */
- public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId)
- {
- List<SysMenu> returnList = new ArrayList<SysMenu>();
- for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext();)
- {
- SysMenu t = (SysMenu) iterator.next();
- // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
- if (t.getParentId() == parentId)
- {
- recursionFn(list, t);
- returnList.add(t);
- }
- }
- return returnList;
- }
- /**
- * 递归列表
- *
- * @param list
- * @param t
- */
- private void recursionFn(List<SysMenu> list, SysMenu t)
- {
- // 得到子节点列表
- List<SysMenu> childList = getChildList(list, t);
- t.setChildren(childList);
- for (SysMenu tChild : childList)
- {
- if (hasChild(list, tChild))
- {
- recursionFn(list, tChild);
- }
- }
- }
- /**
- * 得到子节点列表
- */
- private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t)
- {
- List<SysMenu> tlist = new ArrayList<SysMenu>();
- Iterator<SysMenu> it = list.iterator();
- while (it.hasNext())
- {
- SysMenu n = (SysMenu) it.next();
- if (n.getParentId().longValue() == t.getMenuId().longValue())
- {
- tlist.add(n);
- }
- }
- return tlist;
- }
- /**
- * 判断是否有子节点
- */
- private boolean hasChild(List<SysMenu> list, SysMenu t)
- {
- return getChildList(list, t).size() > 0;
- }
- /**
- * 内链域名特殊字符替换
- *
- * @return
- */
- public String innerLinkReplaceEach(String path)
- {
- return StringUtils.replaceEach(path, new String[] { Constants.HTTP, Constants.HTTPS },
- new String[] { "", "" });
- }
- }
|