|
|
@ -45,14 +45,14 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
@Override |
|
|
|
public List<MenuDto> queryAll(MenuQueryCriteria criteria, Boolean isQuery) throws Exception { |
|
|
|
Sort sort = Sort.by(Sort.Direction.ASC, "menuSort"); |
|
|
|
if(Boolean.TRUE.equals(isQuery)){ |
|
|
|
if (Boolean.TRUE.equals(isQuery)) { |
|
|
|
criteria.setPidIsNull(true); |
|
|
|
List<Field> fields = QueryHelp.getAllFields(criteria.getClass(), new ArrayList<>()); |
|
|
|
for (Field field : fields) { |
|
|
|
//设置对象的访问权限,保证对private的属性的访问 |
|
|
|
field.setAccessible(true); |
|
|
|
Object val = field.get(criteria); |
|
|
|
if("pidIsNull".equals(field.getName())){ |
|
|
|
if ("pidIsNull".equals(field.getName())) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (ObjectUtil.isNotNull(val)) { |
|
|
@ -61,19 +61,20 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return menuMapper.toDto(menuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),sort)); |
|
|
|
return menuMapper.toDto(menuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), sort)); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Cacheable(key = "'id:' + #p0") |
|
|
|
public MenuDto findById(long id) { |
|
|
|
Menu menu = menuRepository.findById(id).orElseGet(Menu::new); |
|
|
|
ValidationUtil.isNull(menu.getId(),"Menu","id",id); |
|
|
|
ValidationUtil.isNull(menu.getId(), "Menu", "id", id); |
|
|
|
return menuMapper.toDto(menu); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 用户角色改变时需清理缓存 |
|
|
|
* |
|
|
|
* @param currentUserId / |
|
|
|
* @return / |
|
|
|
*/ |
|
|
@ -89,23 +90,23 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void create(Menu resources) { |
|
|
|
if(menuRepository.findByTitle(resources.getTitle()) != null){ |
|
|
|
throw new BaseException("title",resources.getTitle()); |
|
|
|
if (menuRepository.findByTitle(resources.getTitle()) != null) { |
|
|
|
throw new BaseException("title", resources.getTitle()); |
|
|
|
} |
|
|
|
if(StringUtils.isNotBlank(resources.getComponentName())){ |
|
|
|
if(menuRepository.findByComponentName(resources.getComponentName()) != null){ |
|
|
|
throw new BaseException("componentName",resources.getComponentName()); |
|
|
|
if (StringUtils.isNotBlank(resources.getComponentName())) { |
|
|
|
if (menuRepository.findByComponentName(resources.getComponentName()) != null) { |
|
|
|
throw new BaseException("componentName", resources.getComponentName()); |
|
|
|
} |
|
|
|
} |
|
|
|
if(resources.getPid().equals(0L)){ |
|
|
|
if (resources.getPid().equals(0L)) { |
|
|
|
resources.setPid(null); |
|
|
|
} |
|
|
|
// if(resources.getIFrame()){ |
|
|
|
// String http = "http://", https = "https://"; |
|
|
|
// if (!(resources.getPath().toLowerCase().startsWith(http)||resources.getPath().toLowerCase().startsWith(https))) { |
|
|
|
// throw new BaseException("外链必须以http://或者https://开头"); |
|
|
|
// } |
|
|
|
// } |
|
|
|
if (resources.getIFrame()) { |
|
|
|
String http = "http://", https = "https://"; |
|
|
|
if (!(resources.getPath().toLowerCase().startsWith(http) || resources.getPath().toLowerCase().startsWith(https))) { |
|
|
|
throw new BaseException("外链必须以http://或者https://开头"); |
|
|
|
} |
|
|
|
} |
|
|
|
menuRepository.save(resources); |
|
|
|
// 计算子节点数目 |
|
|
|
resources.setSubCount(0); |
|
|
@ -116,25 +117,27 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void update(Menu resources) { |
|
|
|
if(resources.getId().equals(resources.getPid())) { |
|
|
|
if (resources.getId().equals(resources.getPid())) { |
|
|
|
throw new BaseException("上级不能为自己"); |
|
|
|
} |
|
|
|
Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new); |
|
|
|
ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId()); |
|
|
|
|
|
|
|
if(resources.getIFrame()){ |
|
|
|
ValidationUtil.isNull(menu.getId(), "Permission", "id", resources.getId()); |
|
|
|
if (resources.getIFrame() == null) { |
|
|
|
resources.setIFrame(false); |
|
|
|
} |
|
|
|
if (resources.getIFrame()) { |
|
|
|
String http = "http://", https = "https://"; |
|
|
|
if (!(resources.getPath().toLowerCase().startsWith(http)||resources.getPath().toLowerCase().startsWith(https))) { |
|
|
|
if (!(resources.getPath().toLowerCase().startsWith(http) || resources.getPath().toLowerCase().startsWith(https))) { |
|
|
|
throw new BaseException("外链必须以http://或者https://开头"); |
|
|
|
} |
|
|
|
} |
|
|
|
Menu menu1 = menuRepository.findByTitle(resources.getTitle()); |
|
|
|
|
|
|
|
if(menu1 != null && !menu1.getId().equals(menu.getId())){ |
|
|
|
throw new BaseException("title",resources.getTitle()); |
|
|
|
if (menu1 != null && !menu1.getId().equals(menu.getId())) { |
|
|
|
throw new BaseException("title", resources.getTitle()); |
|
|
|
} |
|
|
|
|
|
|
|
if(resources.getPid().equals(0L)){ |
|
|
|
if (resources.getPid().equals(0L)) { |
|
|
|
resources.setPid(null); |
|
|
|
} |
|
|
|
|
|
|
@ -142,10 +145,10 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
Long oldPid = menu.getPid(); |
|
|
|
Long newPid = resources.getPid(); |
|
|
|
|
|
|
|
if(StringUtils.isNotBlank(resources.getComponentName())){ |
|
|
|
if (StringUtils.isNotBlank(resources.getComponentName())) { |
|
|
|
menu1 = menuRepository.findByComponentName(resources.getComponentName()); |
|
|
|
if(menu1 != null && !menu1.getId().equals(menu.getId())){ |
|
|
|
throw new BaseException("componentName",resources.getComponentName()); |
|
|
|
if (menu1 != null && !menu1.getId().equals(menu.getId())) { |
|
|
|
throw new BaseException("componentName", resources.getComponentName()); |
|
|
|
} |
|
|
|
} |
|
|
|
menu.setTitle(resources.getTitle()); |
|
|
@ -173,7 +176,7 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
for (Menu menu : menuList) { |
|
|
|
menuSet.add(menu); |
|
|
|
List<Menu> menus = menuRepository.findByPid(menu.getId()); |
|
|
|
if(menus!=null && menus.size()!=0){ |
|
|
|
if (menus != null && menus.size() != 0) { |
|
|
|
getChildMenus(menus, menuSet); |
|
|
|
} |
|
|
|
} |
|
|
@ -195,7 +198,7 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
@Override |
|
|
|
public List<MenuDto> getMenus(Long pid) { |
|
|
|
List<Menu> menus; |
|
|
|
if(pid != null && !pid.equals(0L)){ |
|
|
|
if (pid != null && !pid.equals(0L)) { |
|
|
|
menus = menuRepository.findByPid(pid); |
|
|
|
} else { |
|
|
|
menus = menuRepository.findByPidIsNull(); |
|
|
@ -205,7 +208,7 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<MenuDto> getSuperior(MenuDto menuDto, List<Menu> menus) { |
|
|
|
if(menuDto.getPid() == null){ |
|
|
|
if (menuDto.getPid() == null) { |
|
|
|
menus.addAll(menuRepository.findByPidIsNull()); |
|
|
|
return menuMapper.toDto(menus); |
|
|
|
} |
|
|
@ -231,7 +234,7 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(trees.size() == 0){ |
|
|
|
if (trees.size() == 0) { |
|
|
|
trees = menuDtos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList()); |
|
|
|
} |
|
|
|
return trees; |
|
|
@ -241,35 +244,35 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
public List<MenuVo> buildMenus(List<MenuDto> menuDtos) { |
|
|
|
List<MenuVo> list = new LinkedList<>(); |
|
|
|
menuDtos.forEach(menuDTO -> { |
|
|
|
if (menuDTO!=null){ |
|
|
|
if (menuDTO != null) { |
|
|
|
List<MenuDto> menuDtoList = menuDTO.getChildren(); |
|
|
|
MenuVo menuVo = new MenuVo(); |
|
|
|
menuVo.setName(ObjectUtil.isNotEmpty(menuDTO.getComponentName()) ? menuDTO.getComponentName() : menuDTO.getTitle()); |
|
|
|
menuVo.setName(ObjectUtil.isNotEmpty(menuDTO.getComponentName()) ? menuDTO.getComponentName() : menuDTO.getTitle()); |
|
|
|
// 一级目录需要加斜杠,不然会报警告 |
|
|
|
menuVo.setPath(menuDTO.getPid() == null ? "/" + menuDTO.getPath() :menuDTO.getPath()); |
|
|
|
menuVo.setPath(menuDTO.getPid() == null ? "/" + menuDTO.getPath() : menuDTO.getPath()); |
|
|
|
menuVo.setHidden(menuDTO.getHidden()); |
|
|
|
// 如果不是外链 |
|
|
|
if(!menuDTO.getIFrame()){ |
|
|
|
if(menuDTO.getPid() == null){ |
|
|
|
menuVo.setComponent(StringUtils.isEmpty(menuDTO.getComponent())?"Layout":menuDTO.getComponent()); |
|
|
|
if (!menuDTO.getIFrame()) { |
|
|
|
if (menuDTO.getPid() == null) { |
|
|
|
menuVo.setComponent(StringUtils.isEmpty(menuDTO.getComponent()) ? "Layout" : menuDTO.getComponent()); |
|
|
|
// 如果不是一级菜单,并且菜单类型为目录,则代表是多级菜单 |
|
|
|
}else if(menuDTO.getType() == 0){ |
|
|
|
menuVo.setComponent(StringUtils.isEmpty(menuDTO.getComponent())?"ParentView":menuDTO.getComponent()); |
|
|
|
}else if(StringUtils.isNoneBlank(menuDTO.getComponent())){ |
|
|
|
} else if (menuDTO.getType() == 0) { |
|
|
|
menuVo.setComponent(StringUtils.isEmpty(menuDTO.getComponent()) ? "ParentView" : menuDTO.getComponent()); |
|
|
|
} else if (StringUtils.isNoneBlank(menuDTO.getComponent())) { |
|
|
|
menuVo.setComponent(menuDTO.getComponent()); |
|
|
|
} |
|
|
|
} |
|
|
|
menuVo.setMeta(new MenuMetaVo(menuDTO.getTitle(),menuDTO.getIcon(),!menuDTO.getCache())); |
|
|
|
if(CollectionUtil.isNotEmpty(menuDtoList)){ |
|
|
|
menuVo.setMeta(new MenuMetaVo(menuDTO.getTitle(), menuDTO.getIcon(), !menuDTO.getCache())); |
|
|
|
if (CollectionUtil.isNotEmpty(menuDtoList)) { |
|
|
|
menuVo.setAlwaysShow(true); |
|
|
|
menuVo.setRedirect("noredirect"); |
|
|
|
menuVo.setChildren(buildMenus(menuDtoList)); |
|
|
|
// 处理是一级菜单并且没有子菜单的情况 |
|
|
|
} else if(menuDTO.getPid() == null){ |
|
|
|
} else if (menuDTO.getPid() == null) { |
|
|
|
MenuVo menuVo1 = new MenuVo(); |
|
|
|
menuVo1.setMeta(menuVo.getMeta()); |
|
|
|
// 非外链 |
|
|
|
if(!menuDTO.getIFrame()){ |
|
|
|
if (!menuDTO.getIFrame()) { |
|
|
|
menuVo1.setPath("index"); |
|
|
|
menuVo1.setName(menuVo.getName()); |
|
|
|
menuVo1.setComponent(menuVo.getComponent()); |
|
|
@ -293,7 +296,7 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
@Override |
|
|
|
public Menu findOne(Long id) { |
|
|
|
Menu menu = menuRepository.findById(id).orElseGet(Menu::new); |
|
|
|
ValidationUtil.isNull(menu.getId(),"Menu","id",id); |
|
|
|
ValidationUtil.isNull(menu.getId(), "Menu", "id", id); |
|
|
|
return menu; |
|
|
|
} |
|
|
|
|
|
|
@ -301,7 +304,7 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
public void download(List<MenuDto> menuDtos, HttpServletResponse response) throws IOException { |
|
|
|
List<Map<String, Object>> list = new ArrayList<>(); |
|
|
|
for (MenuDto menuDTO : menuDtos) { |
|
|
|
Map<String,Object> map = new LinkedHashMap<>(); |
|
|
|
Map<String, Object> map = new LinkedHashMap<>(); |
|
|
|
map.put("菜单标题", menuDTO.getTitle()); |
|
|
|
map.put("菜单类型", menuDTO.getType() == null ? "目录" : menuDTO.getType() == 1 ? "菜单" : "按钮"); |
|
|
|
map.put("权限标识", menuDTO.getPermission()); |
|
|
@ -314,8 +317,8 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
FileUtil.downloadExcel(list, response); |
|
|
|
} |
|
|
|
|
|
|
|
private void updateSubCnt(Long menuId){ |
|
|
|
if(menuId != null){ |
|
|
|
private void updateSubCnt(Long menuId) { |
|
|
|
if (menuId != null) { |
|
|
|
int count = menuRepository.countByPid(menuId); |
|
|
|
menuRepository.updateSubCntById(count, menuId); |
|
|
|
} |
|
|
@ -323,14 +326,15 @@ public class MenuServiceImpl implements MenuService { |
|
|
|
|
|
|
|
/** |
|
|
|
* 清理缓存 |
|
|
|
* |
|
|
|
* @param id 菜单ID |
|
|
|
*/ |
|
|
|
public void delCaches(Long id){ |
|
|
|
public void delCaches(Long id) { |
|
|
|
List<User> users = userRepository.findByMenuId(id); |
|
|
|
redisUtils.del(CacheKey.MENU_ID + id); |
|
|
|
redisUtils.delByKeys(CacheKey.MENU_USER, users.stream().map(User::getId).collect(Collectors.toSet())); |
|
|
|
// 清除 Role 缓存 |
|
|
|
List<Role> roles = roleService.findInMenuId(new ArrayList<Long>(){{ |
|
|
|
List<Role> roles = roleService.findInMenuId(new ArrayList<Long>() {{ |
|
|
|
add(id); |
|
|
|
}}); |
|
|
|
redisUtils.delByKeys(CacheKey.ROLE_ID, roles.stream().map(Role::getId).collect(Collectors.toSet())); |
|
|
|