|
@@ -1,9 +1,10 @@
|
|
|
package com.boman.system.service.impl;
|
|
|
|
|
|
-import java.util.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.boman.common.core.constant.UserConstants;
|
|
|
+import com.boman.common.core.utils.SecurityUtils;
|
|
|
+import com.boman.common.core.utils.StringUtils;
|
|
|
+import com.boman.common.core.utils.array.ArrayUtils;
|
|
|
+import com.boman.common.core.utils.obj.ObjectUtils;
|
|
|
import com.boman.common.core.web.domain.AjaxResult;
|
|
|
import com.boman.common.redis.RedisKey;
|
|
|
import com.boman.common.redis.service.RedisService;
|
|
@@ -12,15 +13,9 @@ import com.boman.domain.RoleEnum;
|
|
|
import com.boman.domain.constant.GlobalBtn;
|
|
|
import com.boman.domain.dto.RoleMenuDto;
|
|
|
import com.boman.system.api.domain.SysMenu;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import com.boman.common.core.constant.UserConstants;
|
|
|
-import com.boman.common.core.utils.SecurityUtils;
|
|
|
-import com.boman.common.core.utils.StringUtils;
|
|
|
import com.boman.system.api.domain.SysRole;
|
|
|
import com.boman.system.api.domain.SysUser;
|
|
|
+import com.boman.system.domain.SysRoleMenu;
|
|
|
import com.boman.system.domain.vo.MetaVo;
|
|
|
import com.boman.system.domain.vo.RouterVo;
|
|
|
import com.boman.system.domain.vo.TreeSelect;
|
|
@@ -28,11 +23,17 @@ import com.boman.system.mapper.SysMenuMapper;
|
|
|
import com.boman.system.mapper.SysRoleMapper;
|
|
|
import com.boman.system.mapper.SysRoleMenuMapper;
|
|
|
import com.boman.system.service.ISysMenuService;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import org.apache.commons.lang3.BooleanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.boman.common.core.utils.obj.ObjectUtils.*;
|
|
|
-import static com.boman.domain.constant.FormDataConstant.COLON;
|
|
|
|
|
|
/**
|
|
|
* 菜单 业务层处理
|
|
@@ -53,6 +54,8 @@ public class SysMenuServiceImpl implements ISysMenuService {
|
|
|
private SysRoleMenuMapper roleMenuMapper;
|
|
|
@Resource
|
|
|
private RedisService redisService;
|
|
|
+ @Resource
|
|
|
+ private ISysRoleMenuService roleMenuService;
|
|
|
|
|
|
/**
|
|
|
* 根据用户查询系统菜单列表
|
|
@@ -132,13 +135,27 @@ public class SysMenuServiceImpl implements ISysMenuService {
|
|
|
*/
|
|
|
@Override
|
|
|
public List<SysMenu> allLeafNodeById(Long menuId) {
|
|
|
+ List<SysMenu> tempList = Lists.newArrayListWithCapacity(16);
|
|
|
SysMenu menu = selectMenuById(menuId);
|
|
|
List<SysMenu> menus = menuMapper.selectMenuList(new SysMenu());
|
|
|
- List<SysMenu> tempList = Lists.newArrayListWithCapacity(16);
|
|
|
- recursionList(menus, menu, tempList);
|
|
|
- // 把父亲去掉
|
|
|
- if (tempList.size() > 1) {
|
|
|
- tempList.remove(0);
|
|
|
+ List<SysMenu> sysMenus = recChildList(menus, menu, tempList);
|
|
|
+ return ObjectUtils.filter(sysMenus, menu1 -> SysMenu.BUTTON.equals(menu1.getMenuType()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<SysMenu> recChildList(List<SysMenu> menus, SysMenu menu, List<SysMenu> tempList) {
|
|
|
+ int child = 0;
|
|
|
+ // 得到子节点列表
|
|
|
+ List<SysMenu> childList = getChildList(menus, menu);
|
|
|
+ for (SysMenu tChild : childList) {
|
|
|
+ if (hasChild(menus, tChild)) {
|
|
|
+ recChildList(menus, tChild, tempList);
|
|
|
+ } else {
|
|
|
+ child++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (child == childList.size() && childList.size() != 0) {
|
|
|
+ tempList.addAll(childList);
|
|
|
}
|
|
|
|
|
|
return tempList;
|
|
@@ -539,11 +556,11 @@ public class SysMenuServiceImpl implements ISysMenuService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> listMenus(Long menuId) {
|
|
|
+ public Map<String, Object> listMenus(Long roleId, Long menuId) {
|
|
|
SysMenu menu = selectMenuById(menuId);
|
|
|
- List<SysMenu> menus = menuMapper.selectMenuList(new SysMenu());
|
|
|
+ List<SysMenu> allMenu = menuMapper.selectMenuList(new SysMenu());
|
|
|
List<SysMenu> tempList = Lists.newArrayListWithCapacity(16);
|
|
|
- recursionList(menus, menu, tempList);
|
|
|
+ recursionList(allMenu, menu, tempList);
|
|
|
// 把父亲去掉
|
|
|
if (tempList.size() > 1) {
|
|
|
tempList.remove(0);
|
|
@@ -557,29 +574,155 @@ public class SysMenuServiceImpl implements ISysMenuService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ // 找到当前menuId对应的所有的叶子结点
|
|
|
+ List<SysMenu> leafNodes = allLeafNodeById(menuId);
|
|
|
+ List<Long> menuIdList = map(leafNodes, SysMenu::getId);
|
|
|
+ List<SysRoleMenu> roleMenuList = roleMenuService.listByRoleIdMenuIdList(roleId, menuIdList);
|
|
|
+ if (isEmpty(roleMenuList)) {
|
|
|
+ result.put("sysMenus", tempList);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Long> roleMenuIdList = map(roleMenuList, SysRoleMenu::getMenuId);
|
|
|
+ List<String> allBtnList = RoleEnum.roles();
|
|
|
+
|
|
|
for (SysMenu sysMenu : tempList) {
|
|
|
- List<JSONObject> containsHead = RoleEnum.initData();
|
|
|
- List<SysMenu> childList = getChildList(menus, sysMenu);
|
|
|
- for (SysMenu childMenu : childList) {
|
|
|
- String btn = RoleMenuDto.getBtnFromPerms(childMenu.getPerms());
|
|
|
- String tableName = RoleMenuDto.getTableNameFromPerms(childMenu.getPerms());
|
|
|
- GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
|
|
|
- String menuRole = genTable.getMenuRole();
|
|
|
- if (menuRole.contains(btn)) {
|
|
|
- switchBtn(containsHead, btn, true);
|
|
|
- } else {
|
|
|
- switchBtn(containsHead, btn, false);
|
|
|
+ List<Map<String, Object>> containsHead = RoleEnum.initData();
|
|
|
+ List<SysMenu> childList = getChildList(allMenu, sysMenu);
|
|
|
+ if (isEmpty(childList)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> btnList = getBtnFromMenu(roleMenuIdList, childList);
|
|
|
+
|
|
|
+ String tableName = RoleMenuDto.getTableNameFromPerms(childList.get(0).getPerms());
|
|
|
+ GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
|
|
|
+ if (isEmpty(genTable)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> menuRoleList = ArrayUtils.split(genTable.getMenuRole(), "");
|
|
|
+
|
|
|
+ for (String allBtn : allBtnList) {
|
|
|
+ boolean one = btnList.contains(allBtn);
|
|
|
+ boolean another = menuRoleList.contains(allBtn);
|
|
|
+ if (BooleanUtils.isTrue(one) && BooleanUtils.isTrue(another)) {
|
|
|
+ switchBtn(containsHead, allBtn, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (BooleanUtils.isFalse(one) && BooleanUtils.isTrue(another)) {
|
|
|
+ switchBtn(containsHead, allBtn, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (BooleanUtils.isFalse(one) && BooleanUtils.isFalse(another)) {
|
|
|
+ switchBtn(containsHead, allBtn, "0");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sysMenu.setContainsHead(containsHead);
|
|
|
}
|
|
|
+
|
|
|
result.put("sysMenus", tempList);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- private void switchBtn(List<JSONObject> containsHead, String btn, Object value) {
|
|
|
- JSONObject jsonObject = new JSONObject(2);
|
|
|
+ /**
|
|
|
+ * 功能描述: roleMenuIdList包含childList中的id的全部取出来
|
|
|
+ *
|
|
|
+ * @param roleMenuIdList roleMenuIdList
|
|
|
+ * @param childList childList
|
|
|
+ * @return java.util.List<java.lang.String>
|
|
|
+ */
|
|
|
+ private List<String> getBtnFromMenu(List<Long> roleMenuIdList, List<SysMenu> childList) {
|
|
|
+ List<String> btnList = Lists.newArrayList();
|
|
|
+ for (SysMenu childMenu : childList) {
|
|
|
+ if (roleMenuIdList.contains(childMenu.getId())) {
|
|
|
+ String btn = RoleMenuDto.getBtnFromPerms(childMenu.getPerms());
|
|
|
+ btnList.add(btn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return btnList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 根据roleId查找的叶子结点去匹配所对应的菜单树
|
|
|
+ *
|
|
|
+ * @param roleId roleId
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<SysMenu> listTreeByRoleId(Long roleId) {
|
|
|
+ List<SysMenu> roleMenus = menuMapper.listTreeByRoleId(roleId);
|
|
|
+ List<SysMenu> allMenus = selectMenuListAll(new SysMenu());
|
|
|
+
|
|
|
+ List<Long> roleMenuIdList = map(roleMenus, SysMenu::getId);
|
|
|
+ List<SysMenu> parentMenus = Lists.newArrayListWithCapacity(16);
|
|
|
+
|
|
|
+ // 把roleMenus对应的所有的爹parentMenus找到
|
|
|
+ recGetParent(allMenus, roleMenus, parentMenus);
|
|
|
+
|
|
|
+ // 递归,把孩子放进父亲的怀抱
|
|
|
+ for (SysMenu parentMenu : parentMenus) {
|
|
|
+ recursionFn(parentMenus, parentMenu);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 把不在roleMenus中的去除掉
|
|
|
+ return filter(parentMenus, menu -> isNotEmpty(menu.getChildren()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 找到sonMenus对应的所有的父类,结果存放于result中
|
|
|
+ *
|
|
|
+ * @param allMenus allMenus
|
|
|
+ * @param sonMenus sonMenus
|
|
|
+ * @param result 没有按照规则排序只是把父类全部存放于result中
|
|
|
+ */
|
|
|
+ private void recGetParent(List<SysMenu> allMenus, List<SysMenu> roleMenus, List<SysMenu> result) {
|
|
|
+ for (SysMenu sonMenu : roleMenus) {
|
|
|
+ for (SysMenu parentMenu : allMenus) {
|
|
|
+ if (sonMenu.getParentId().equals(parentMenu.getId())) {
|
|
|
+ if (isNotEmpty(result)) {
|
|
|
+ List<Long> idList = map(result, SysMenu::getId);
|
|
|
+ if (!idList.contains(parentMenu.getId())) {
|
|
|
+ result.add(parentMenu);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result.add(parentMenu);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 顶级目录
|
|
|
+ if (parentMenu.getId() != 1) {
|
|
|
+ List<SysMenu> list = Collections.singletonList(parentMenu);
|
|
|
+ recGetParent(allMenus, list, result);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void recBuildTree(List<SysMenu> roleMenus, SysMenu parent) {
|
|
|
+ List<SysMenu> childList = getChildList(roleMenus, parent);
|
|
|
+
|
|
|
+ parent.setChildren(childList);
|
|
|
+ for (SysMenu tChild : childList) {
|
|
|
+ if (hasChild(roleMenus, tChild)) {
|
|
|
+ recursionFn(roleMenus, tChild);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 得到子节点列表
|
|
|
+// List<SysMenu> childList = getChildList(list, t);
|
|
|
+// t.setChildren(childList);
|
|
|
+// for (SysMenu tChild : childList) {
|
|
|
+// if (hasChild(list, tChild)) {
|
|
|
+// recursionFn(list, tChild);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ private void switchBtn(List<Map<String, Object>> containsHead, String btn, Object value) {
|
|
|
+ Map<String, Object> jsonObject = new HashMap<>(2);
|
|
|
jsonObject.put("name", btn);
|
|
|
jsonObject.put("type", value);
|
|
|
switch (btn) {
|