更新 'ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java'

dev
zhouhy 1 year ago
parent ec65589f68
commit 545f8536f5

@ -1,14 +1,10 @@
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.*;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.UserConstants;
@ -61,16 +57,12 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public List<SysMenu> selectMenuList(SysMenu menu, Long userId)
{
public List<SysMenu> selectMenuList(SysMenu menu, Long userId) {
List<SysMenu> menuList = null;
// 管理员显示所有菜单信息
if (SysUser.isAdmin(userId))
{
if (SysUser.isAdmin(userId)) {
menuList = menuMapper.selectMenuList(menu);
}
else
{
} else {
menu.getParams().put("userId", userId);
menuList = menuMapper.selectMenuListByUserId(menu);
}
@ -84,14 +76,11 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public Set<String> selectMenuPermsByUserId(Long userId)
{
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))
{
for (String perm : perms) {
if (StringUtils.isNotEmpty(perm)) {
permsSet.addAll(Arrays.asList(perm.trim().split(",")));
}
}
@ -105,14 +94,11 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public Set<String> selectMenuPermsByRoleId(Long roleId)
{
public Set<String> selectMenuPermsByRoleId(Long roleId) {
List<String> perms = menuMapper.selectMenuPermsByRoleId(roleId);
Set<String> permsSet = new HashSet<>();
for (String perm : perms)
{
if (StringUtils.isNotEmpty(perm))
{
for (String perm : perms) {
if (StringUtils.isNotEmpty(perm)) {
permsSet.addAll(Arrays.asList(perm.trim().split(",")));
}
}
@ -126,16 +112,16 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public List<SysMenu> selectMenuTreeByUserId(Long userId)
{
public List<SysMenu> selectMenuTreeByUserId(Long userId) {
Locale locale = LocaleContextHolder.getLocale();
String country = locale.getCountry();
String language = locale.getLanguage();
String languageCode = language + "_" + country;
List<SysMenu> menus = null;
if (SecurityUtils.isAdmin(userId))
{
if (SecurityUtils.isAdmin(userId)) {
menus = menuMapper.selectMenuTreeAll();
}
else
{
menus = menuMapper.selectMenuTreeByUserId(userId);
} else {
menus = menuMapper.selectMenuTreeByUserId(userId, languageCode);
}
return getChildPerms(menus, 0);
}
@ -147,8 +133,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public List<Long> selectMenuListByRoleId(Long roleId)
{
public List<Long> selectMenuListByRoleId(Long roleId) {
SysRole role = roleMapper.selectRoleById(roleId);
return menuMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
}
@ -160,11 +145,9 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public List<RouterVo> buildMenus(List<SysMenu> menus)
{
public List<RouterVo> buildMenus(List<SysMenu> menus) {
List<RouterVo> routers = new LinkedList<RouterVo>();
for (SysMenu menu : menus)
{
for (SysMenu menu : menus) {
RouterVo router = new RouterVo();
router.setHidden("1".equals(menu.getVisible()));
router.setName(getRouteName(menu));
@ -173,14 +156,11 @@ public class SysMenuServiceImpl implements ISysMenuService {
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()))
{
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))
{
} else if (isMenuFrame(menu)) {
router.setMeta(null);
List<RouterVo> childrenList = new ArrayList<RouterVo>();
RouterVo children = new RouterVo();
@ -191,9 +171,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
children.setQuery(menu.getQuery());
childrenList.add(children);
router.setChildren(childrenList);
}
else if (menu.getParentId().intValue() == 0 && isInnerLink(menu))
{
} else if (menu.getParentId().intValue() == 0 && isInnerLink(menu)) {
router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon()));
router.setPath("/");
List<RouterVo> childrenList = new ArrayList<RouterVo>();
@ -218,22 +196,18 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public List<SysMenu> buildMenuTree(List<SysMenu> menus)
{
public List<SysMenu> buildMenuTree(List<SysMenu> menus) {
List<SysMenu> returnList = new ArrayList<SysMenu>();
List<Long> tempList = menus.stream().map(SysMenu::getMenuId).collect(Collectors.toList());
for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext();)
{
for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext(); ) {
SysMenu menu = (SysMenu) iterator.next();
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(menu.getParentId()))
{
if (!tempList.contains(menu.getParentId())) {
recursionFn(menus, menu);
returnList.add(menu);
}
}
if (returnList.isEmpty())
{
if (returnList.isEmpty()) {
returnList = menus;
}
return returnList;
@ -246,8 +220,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus)
{
public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus) {
List<SysMenu> menuTrees = buildMenuTree(menus);
return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
}
@ -259,8 +232,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public SysMenu selectMenuById(Long menuId)
{
public SysMenu selectMenuById(Long menuId) {
return menuMapper.selectMenuById(menuId);
}
@ -271,8 +243,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public boolean hasChildByMenuId(Long menuId)
{
public boolean hasChildByMenuId(Long menuId) {
int result = menuMapper.hasChildByMenuId(menuId);
return result > 0;
}
@ -284,8 +255,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public boolean checkMenuExistRole(Long menuId)
{
public boolean checkMenuExistRole(Long menuId) {
int result = roleMenuMapper.checkMenuExistRole(menuId);
return result > 0;
}
@ -297,8 +267,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public int insertMenu(SysMenu menu)
{
public int insertMenu(SysMenu menu) {
return menuMapper.insertMenu(menu);
}
@ -309,8 +278,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public int updateMenu(SysMenu menu)
{
public int updateMenu(SysMenu menu) {
return menuMapper.updateMenu(menu);
}
@ -321,8 +289,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public int deleteMenuById(Long menuId)
{
public int deleteMenuById(Long menuId) {
return menuMapper.deleteMenuById(menuId);
}
@ -333,12 +300,10 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @return
*/
@Override
public boolean checkMenuNameUnique(SysMenu menu)
{
public boolean 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())
{
if (StringUtils.isNotNull(info) && info.getMenuId().longValue() != menuId.longValue()) {
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
@ -350,12 +315,10 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param menu
* @return
*/
public String getRouteName(SysMenu menu)
{
public String getRouteName(SysMenu menu) {
String routerName = StringUtils.capitalize(menu.getPath());
// 非外链并且是一级目录(类型为目录)
if (isMenuFrame(menu))
{
if (isMenuFrame(menu)) {
routerName = StringUtils.EMPTY;
}
return routerName;
@ -367,23 +330,19 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param menu
* @return
*/
public String getRouterPath(SysMenu menu)
{
public String getRouterPath(SysMenu menu) {
String routerPath = menu.getPath();
// 内链打开外网方式
if (menu.getParentId().intValue() != 0 && isInnerLink(menu))
{
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()))
{
&& UserConstants.NO_FRAME.equals(menu.getIsFrame())) {
routerPath = "/" + menu.getPath();
}
// 非外链并且是一级目录(类型为菜单)
else if (isMenuFrame(menu))
{
else if (isMenuFrame(menu)) {
routerPath = "/";
}
return routerPath;
@ -395,19 +354,13 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param menu
* @return
*/
public String getComponent(SysMenu menu)
{
public String getComponent(SysMenu menu) {
String component = UserConstants.LAYOUT;
if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu))
{
if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu)) {
component = menu.getComponent();
}
else if (StringUtils.isEmpty(menu.getComponent()) && menu.getParentId().intValue() != 0 && isInnerLink(menu))
{
} else if (StringUtils.isEmpty(menu.getComponent()) && menu.getParentId().intValue() != 0 && isInnerLink(menu)) {
component = UserConstants.INNER_LINK;
}
else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu))
{
} else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu)) {
component = UserConstants.PARENT_VIEW;
}
return component;
@ -419,8 +372,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param menu
* @return
*/
public boolean isMenuFrame(SysMenu menu)
{
public boolean isMenuFrame(SysMenu menu) {
return menu.getParentId().intValue() == 0 && UserConstants.TYPE_MENU.equals(menu.getMenuType())
&& menu.getIsFrame().equals(UserConstants.NO_FRAME);
}
@ -431,8 +383,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param menu
* @return
*/
public boolean isInnerLink(SysMenu menu)
{
public boolean isInnerLink(SysMenu menu) {
return menu.getIsFrame().equals(UserConstants.NO_FRAME) && StringUtils.ishttp(menu.getPath());
}
@ -442,8 +393,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param menu
* @return
*/
public boolean isParentView(SysMenu menu)
{
public boolean isParentView(SysMenu menu) {
return menu.getParentId().intValue() != 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType());
}
@ -454,15 +404,12 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param parentId ID
* @return String
*/
public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId)
{
public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
List<SysMenu> returnList = new ArrayList<SysMenu>();
for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext();)
{
for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext(); ) {
SysMenu t = (SysMenu) iterator.next();
// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
if (t.getParentId() == parentId)
{
if (t.getParentId() == parentId) {
recursionFn(list, t);
returnList.add(t);
}
@ -476,15 +423,12 @@ public class SysMenuServiceImpl implements ISysMenuService {
* @param list
* @param t
*/
private void recursionFn(List<SysMenu> list, SysMenu 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))
{
for (SysMenu tChild : childList) {
if (hasChild(list, tChild)) {
recursionFn(list, tChild);
}
}
@ -493,15 +437,12 @@ public class SysMenuServiceImpl implements ISysMenuService {
/**
*
*/
private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t)
{
private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
List<SysMenu> tlist = new ArrayList<SysMenu>();
Iterator<SysMenu> it = list.iterator();
while (it.hasNext())
{
while (it.hasNext()) {
SysMenu n = (SysMenu) it.next();
if (n.getParentId().longValue() == t.getMenuId().longValue())
{
if (n.getParentId().longValue() == t.getMenuId().longValue()) {
tlist.add(n);
}
}
@ -511,8 +452,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
/**
*
*/
private boolean hasChild(List<SysMenu> list, SysMenu t)
{
private boolean hasChild(List<SysMenu> list, SysMenu t) {
return getChildList(list, t).size() > 0;
}
@ -521,8 +461,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
*
* @return
*/
public String innerLinkReplaceEach(String path)
{
public String innerLinkReplaceEach(String path) {
return StringUtils.replaceEach(path, new String[]{Constants.HTTP, Constants.HTTPS, Constants.WWW, "."},
new String[]{"", "", "", "/"});
}

Loading…
Cancel
Save