/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package com.vci.ubcs.system.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.vci.ubcs.code.feign.ICodeClassifyClient; import com.vci.ubcs.starter.util.MybatisParameterUtil; import com.vci.ubcs.starter.web.util.VciBaseUtil; import com.vci.ubcs.system.cache.NacosConfigCache; import com.vci.ubcs.system.cache.SysCache; import com.vci.ubcs.system.dto.MenuDTO; import com.vci.ubcs.system.entity.*; import com.vci.ubcs.system.mapper.ClassifyAuthMapper; import com.vci.ubcs.system.service.*; import com.vci.ubcs.system.vo.MenuVO; import com.vci.ubcs.system.mapper.MenuMapper; import com.vci.ubcs.system.wrapper.MenuWrapper; import lombok.AllArgsConstructor; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.mp.support.Condition; import org.springblade.core.mp.support.Query; import org.springblade.core.secure.BladeUser; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.api.R; import org.springblade.core.tool.constant.BladeConstant; import org.springblade.core.tool.node.ForestNodeMerger; import org.springblade.core.tool.node.TreeNode; import org.springblade.core.tool.support.Kv; import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.StringUtil; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors; import static com.vci.ubcs.common.constant.CommonConstant.API_SCOPE_CATEGORY; import static com.vci.ubcs.common.constant.CommonConstant.DATA_SCOPE_CATEGORY; import static org.springblade.core.cache.constant.CacheConstant.MENU_CACHE; /** * 服务实现类 * * @author Chill */ @Service @AllArgsConstructor public class MenuServiceImpl extends ServiceImpl implements IMenuService { private final IRoleMenuService roleMenuService; private final IRoleScopeService roleScopeService; private final MenuMapper menuMapper; private final ITopMenuSettingService topMenuSettingService; private final ICodeClassifyClient codeClassifyClient; private final ClassifyAuthMapper classifyAuthMapper; private final static String PARENT_ID = "parentId"; private final static Integer MENU_CATEGORY = 1; @Override public List lazyList(Long parentId, Map param) { //int i = 1 / 0; if (Func.isEmpty(Func.toStr(param.get(PARENT_ID)))) { parentId = null; } return baseMapper.lazyList(parentId, param); } @Override public IPage lazyMenuPage(Long parentId, Map param, Query query) { if (Func.isEmpty(Func.toStr(param.get(PARENT_ID)))) { parentId = null; } return baseMapper.lazyMenuPage(parentId, param, Condition.getPage(query)); } @Override public List routes(String roleId, Long topMenuId) { if (StringUtil.isBlank(roleId)) { return null; } List allMenus = baseMapper.allMenu(); List roleMenus; // 超级管理员并且不是顶部菜单请求则返回全部菜单 if (VciBaseUtil.checkAdminTenant() && Func.isEmpty(topMenuId)) { roleMenus = allMenus; } // 非超级管理员并且不是顶部菜单请求则返回对应角色权限菜单 else if (!VciBaseUtil.checkAdminTenant() && Func.isEmpty(topMenuId)) { roleMenus = tenantPackageMenu(baseMapper.roleMenuByRoleId(Func.toLongList(roleId))); } // 顶部菜单请求返回对应角色权限菜单 else { // 角色配置对应菜单 List roleIdMenus = baseMapper.roleMenuByRoleId(Func.toLongList(roleId)); // 反向递归角色菜单所有父级 List routes = new LinkedList<>(roleIdMenus); roleIdMenus.forEach(roleMenu -> recursion(allMenus, routes, roleMenu)); // 顶部配置对应菜单 List topIdMenus = baseMapper.roleMenuByTopMenuId(topMenuId); // 筛选匹配角色对应的权限菜单 roleMenus = topIdMenus.stream().filter(x -> routes.stream().anyMatch(route -> route.getId().longValue() == x.getId().longValue()) ).collect(Collectors.toList()); } return buildRoutes(allMenus, roleMenus); } private List buildRoutes(List allMenus, List roleMenus) { List routes = new LinkedList<>(roleMenus); roleMenus.forEach(roleMenu -> recursion(allMenus, routes, roleMenu)); routes.sort(Comparator.comparing(Menu::getSort)); MenuWrapper menuWrapper = new MenuWrapper(); List collect = routes.stream().filter(x -> Func.equals(x.getCategory(), 1)).collect(Collectors.toList()); return menuWrapper.listNodeVO(collect); } private void recursion(List allMenus, List routes, Menu roleMenu) { Optional menu = allMenus.stream().filter(x -> Func.equals(x.getId(), roleMenu.getParentId())).findFirst(); if (menu.isPresent() && !routes.contains(menu.get())) { routes.add(menu.get()); recursion(allMenus, routes, menu.get()); } } @Override public List buttons(String roleId) { List buttons = (VciBaseUtil.checkAdminTenant()) ? baseMapper.allButtons() : baseMapper.buttons(Func.toLongList(roleId)); MenuWrapper menuWrapper = new MenuWrapper(); return menuWrapper.listNodeVO(buttons); } @Override public List tree() { return ForestNodeMerger.merge(baseMapper.tree()); } @Override public List grantTree(BladeUser user) { /*List menuTree = user.getTenantId().equals(nacosConfigCache.getAdminUserInfo().getTenantId()) && user.getUserId().equals(nacosConfigCache.getAdminUserInfo().getUserId()) ? baseMapper.grantTree() : baseMapper.grantTreeByRole(Func.toLongList(user.getRoleId()));*/ // 修改为所有用户都能查询所有,因为没权限的用户不具备这个菜单页面的权限,也就没必要限制用户查自己的 List menuTree = baseMapper.grantTree(); return ForestNodeMerger.merge(tenantPackageTree(menuTree, user.getTenantId())); } /** * 根据角色id获取菜单树形结构 * @param roleId * @return */ @Override public List grantTreeByRoleIds(List roleId) { List menuTree = baseMapper.grantTreeByRole(roleId); return ForestNodeMerger.merge(menuTree); } @Override public List grantTopTree(BladeUser user) { List menuTree = user.getTenantId().equals(NacosConfigCache.getAdminUserInfo().getTenantId()) ? baseMapper.grantTopTree() : baseMapper.grantTopTreeByRole(Func.toLongList(user.getRoleId())); return ForestNodeMerger.merge(tenantPackageTree(menuTree, user.getTenantId())); } /** * 租户菜单权限自定义筛选 */ private List tenantPackageTree(List menuTree, String tenantId) { TenantPackage tenantPackage = SysCache.getTenantPackage(tenantId); if (!AuthUtil.isAdministrator() && Func.isNotEmpty(tenantPackage) && tenantPackage.getId() > 0L) { List menuIds = Func.toLongList(tenantPackage.getMenuId()); // 筛选出两者菜单交集集合 List collect = menuTree.stream().filter(x -> menuIds.contains(x.getId())).collect(Collectors.toList()); // 创建递归基础集合 List packageTree = new LinkedList<>(collect); // 递归筛选出菜单集合所有父级 collect.forEach(treeNode -> recursionParent(menuTree, packageTree, treeNode)); // 递归筛选出菜单集合所有子级 collect.forEach(treeNode -> recursionChild(menuTree, packageTree, treeNode)); // 合并在一起返回最终集合 return packageTree; } return menuTree; } /** * 父节点递归 */ public void recursionParent(List menuTree, List packageTree, TreeNode treeNode) { Optional node = menuTree.stream().filter(x -> Func.equals(x.getId(), treeNode.getParentId())).findFirst(); if (node.isPresent() && !packageTree.contains(node.get())) { packageTree.add(node.get()); recursionParent(menuTree, packageTree, node.get()); } } /** * 子节点递归 */ public void recursionChild(List menuTree, List packageTree, TreeNode treeNode) { List nodes = menuTree.stream().filter(x -> Func.equals(x.getParentId(), treeNode.getId())).collect(Collectors.toList()); nodes.forEach(node -> { if (!packageTree.contains(node)) { packageTree.add(node); recursionChild(menuTree, packageTree, node); } }); } /** * 租户菜单权限自定义筛选 */ private List tenantPackageMenu(List menu) { // 租户包配置查询 TenantPackage tenantPackage = SysCache.getTenantPackage(AuthUtil.getTenantId()); if (Func.isNotEmpty(tenantPackage) && tenantPackage.getId() > 0L) { List menuIds = Func.toLongList(tenantPackage.getMenuId()); menu = menu.stream().filter(x -> menuIds.contains(x.getId())).collect(Collectors.toList()); } return menu; } @Override public List grantDataScopeTree(BladeUser user) { return ForestNodeMerger.merge(user.getTenantId().equals(NacosConfigCache.getAdminUserInfo().getTenantId()) ? baseMapper.grantDataScopeTree() : baseMapper.grantDataScopeTreeByRole(Func.toLongList(user.getRoleId()))); } @Override public List grantApiScopeTree(BladeUser user) { return ForestNodeMerger.merge(user.getTenantId().equals(NacosConfigCache.getAdminUserInfo().getTenantId()) ? baseMapper.grantApiScopeTree() : baseMapper.grantApiScopeTreeByRole(Func.toLongList(user.getRoleId()))); } @Override public List roleTreeKeys(String roleIds) { List roleMenus = roleMenuService.list(Wrappers.query().lambda().in(RoleMenu::getRoleId, Func.toLongList(roleIds))); return roleMenus.stream().map(roleMenu -> Func.toStr(roleMenu.getMenuId())).collect(Collectors.toList()); } @Override public List topTreeKeys(String topMenuIds) { List settings = topMenuSettingService.list(Wrappers.query().lambda().in(TopMenuSetting::getTopMenuId, Func.toLongList(topMenuIds))); return settings.stream().map(setting -> Func.toStr(setting.getMenuId())).collect(Collectors.toList()); } @Override public List dataScopeTreeKeys(String roleIds) { List roleScopes = roleScopeService.list(Wrappers.query().lambda().eq(RoleScope::getScopeCategory, DATA_SCOPE_CATEGORY).in(RoleScope::getRoleId, Func.toLongList(roleIds))); return roleScopes.stream().map(roleScope -> Func.toStr(roleScope.getScopeId())).collect(Collectors.toList()); } @Override public List apiScopeTreeKeys(String roleIds) { List roleScopes = roleScopeService.list(Wrappers.query().lambda().eq(RoleScope::getScopeCategory, API_SCOPE_CATEGORY).in(RoleScope::getRoleId, Func.toLongList(roleIds))); return roleScopes.stream().map(roleScope -> Func.toStr(roleScope.getScopeId())).collect(Collectors.toList()); } @Override @Cacheable(cacheNames = MENU_CACHE, key = "'auth:routes:' + #user.roleId") public List authRoutes(BladeUser user) { List routes = baseMapper.authRoutes(Func.toLongList(user.getRoleId())); List list = new ArrayList<>(); routes.forEach(route -> list.add(Kv.create().set(route.getPath(), Kv.create().set("authority", Func.toStrArray(route.getAlias()))))); return list; } @Override public boolean removeMenu(String ids) { Long cnt = baseMapper.selectCount(Wrappers.query().lambda().in(Menu::getParentId, Func.toLongList(ids))); if (cnt > 0L) { throw new ServiceException("请先删除子节点!"); } return removeByIds(Func.toLongList(ids)); } @Override public boolean submit(Menu menu) { LambdaQueryWrapper menuQueryWrapper = Wrappers.lambdaQuery(); // 新增 if (menu.getId() == null) { //检验是否重复菜单别名,只校验同一父分类菜单下的编号是否重复 menuQueryWrapper.eq(Menu::getParentId,menu.getParentId()!=null ? menu.getParentId():BladeConstant.TOP_PARENT_ID) .eq(Menu::getCode, menu.getCode()) .or(wrapper -> wrapper.eq(Menu::getName, menu.getName()).eq(Menu::getCategory, MENU_CATEGORY)) ; } else { // 修改 menuQueryWrapper.ne(Menu::getId, menu.getId()).and( wrapper -> wrapper.eq(Menu::getCode, menu.getCode()) .eq(Menu::getParentId,menu.getParentId()) .or(o -> o.eq(Menu::getName, menu.getName()).eq(Menu::getCategory, MENU_CATEGORY)) ); } Long cnt = baseMapper.selectCount(menuQueryWrapper); if (cnt > 0L) { throw new ServiceException("菜单名或编号已存在!"); } if (menu.getParentId() == null && menu.getId() == null) { menu.setParentId(BladeConstant.TOP_PARENT_ID); } if (menu.getParentId() != null && menu.getId() == null) { Menu parentMenu = baseMapper.selectById(menu.getParentId()); if (parentMenu != null && parentMenu.getCategory() != 1) { throw new ServiceException("父节点只可选择菜单类型!"); } } menu.setIsDeleted(BladeConstant.DB_NOT_DELETED); return saveOrUpdate(menu); } /** * 获取菜单下面的按钮,别乱调用,这个方法是针对主数据管理按钮查询的 * @param classifyId * @param btmType 业务类型 * @param authType * @return */ @Override //@Cacheable(cacheNames = MENU_CACHE, key = "'auth:menuButton:'+ #btmType +':'+ #userId ") public List getMenuButtonByType(String classifyId,String btmType,String authType) { // baseMapper.selectMenuChildByBtnType(btmType,roleIds); if(Func.isBlank(classifyId)){ throw new ServiceException("必传参数分类oid不能为空!"); } //查询分类节点的所有父级节点 R> listR = codeClassifyClient.selectAllParentOid(classifyId); if (!listR.isSuccess() && !listR.getData().isEmpty()) { throw new ServiceException("获取分类信息失败!"); } // 返回的分类oid是当前节点为第一个,后面依次是他的上层节点 List classifyOidList = listR.getData(); final List roleIds = Func.toStrList(",",AuthUtil.getUser().getRoleId()); // 先查询按钮id列表 LambdaQueryWrapper wrapper = Wrappers.query() .lambda().eq(ClassifyAuth::getClassifyId, classifyId) .eq(ClassifyAuth::getAuthType,authType) .in(ClassifyAuth::getRoleId, roleIds); List classifyAuths = classifyAuthMapper.selectList(wrapper); //如果当前分类没有找到授权配置,就依次从当前节点往上层节点找授权配置,找到了就停止,没找到就一直找到最后 if(classifyAuths.isEmpty()){ // 下标从1开始因为当前节点0已经查询过 for (int i = 1; i < classifyOidList.size(); i++) { classifyAuths = classifyAuthMapper.selectList( Wrappers.query() .lambda().eq(ClassifyAuth::getClassifyId, classifyOidList.get(i)) .eq(ClassifyAuth::getAuthType,authType) .in(ClassifyAuth::getRoleId, roleIds) ); if(!classifyAuths.isEmpty()){ break; } } } //出现了多条数据 if(classifyAuths.size()>1){ // 校验是否存在错误数据,同一个角色和同一个分类id存在多条授权记录 List finalClassifyAuths = classifyAuths; boolean hasDuplicate = classifyAuths.stream() .anyMatch(auth1 -> finalClassifyAuths.stream() .filter(auth2 -> auth1 != auth2) .anyMatch(auth2 -> auth1.getRoleId().equals(auth2.getRoleId()) && auth1.getClassifyId().equals(auth2.getClassifyId()))); if (hasDuplicate) { throw new ServiceException("角色和分类配置存在多条记录,请联系管理人员清理错误配置!"); } } // 是否为超管 Boolean isAdmin = VciBaseUtil.checkAdminTenant(); // 未配置按钮权限 if(!isAdmin && (classifyAuths.isEmpty() || Func.isBlank(classifyAuths.get(0).getButtonIds()))){ return new ArrayList<>(); } List ids = new ArrayList<>(); // 如果不是超管用户 if(!isAdmin){ String concatenatedButtonIds = classifyAuths.stream() .map(ClassifyAuth::getButtonIds) // 获取每个classifyAuths对象的buttonIds .collect(Collectors.joining(",")); // 用逗号分隔拼接成一个字符串 ids.addAll(Arrays.asList(concatenatedButtonIds.split(","))); } return this.getMenuListByCode(ids,btmType,roleIds); } /** * 根据code查询菜单信息 * @param codes * @param userId * @return */ @Override public List getMenuByCodes(List codes,Long userId) { if(codes.isEmpty()){ return new ArrayList<>(); } // 查询菜单信息 LambdaQueryWrapper wrapper = Wrappers.query() .lambda() .in(Menu::getCode, codes) .eq(Menu::getIsDeleted,BladeConstant.DB_NOT_DELETED) /*未被删除*/ .eq(Menu::getCategory,1) /*菜单类型不能为按钮*/ .orderByAsc(Menu::getCode); /*根据code排序与classify的btmtypeid对应*/ // 超管不用根据角色来查询 if(!VciBaseUtil.checkAdminTenant()){ if(Func.isBlank(userId.toString()) && Func.isBlank(AuthUtil.getUserId().toString())){ throw new ServiceException("获取用户id失败"); } List menuIds = roleMenuService.getMenuIdByUserId(userId); if(menuIds.isEmpty()){ return new ArrayList<>(); } if(menuIds.size()>=1000){ MybatisParameterUtil.cutInParameter(wrapper,Menu::getId,menuIds); }else { wrapper.in(Menu::getId,menuIds); } } return this.list(wrapper); } /** * 根据父级菜单的code查询按钮信息 * @param code * @return */ @Override public List getButtonByParentCode(String code) { return menuMapper.getButtonsByRoleIdAndCode(null,code); } /** * 对KeepAlive值转换成布尔类型进行封装 * * @param childMenu * @return */ @Override public void handleKeepAlive(List childMenu) { childMenu.forEach(list->{ list.getMeta().put("keepAlive","true".equals(list.getKeepAlive())); if(list.getChildren().size()>0){ handleKeepAlive(list.getChildren()); } }); } /** * 克隆其他菜单下按钮 * @param menuId 要克隆的菜单按钮主键 * @param buttonIds 被克隆的按钮主键 * @return */ @Override public R cloneMenuButton(Long menuId, List buttonIds) { if(Func.isEmpty(menuId)){ return R.fail("要克隆的菜单主键不能为空!"); } if(buttonIds.isEmpty() || buttonIds.size() <= 0){ return R.fail("被克隆的按钮主键不能为空!"); } // 先根据主键查询出所有按钮的信息 List buttons = this.listByIds(buttonIds); List newButtons = new ArrayList<>(); List addButtonCodes = new ArrayList(); buttons.parallelStream().forEach(item->{ // 判断是否为按钮,非按钮不处理 if(item.getCategory().equals(2)){ // 改变父节点信息 item.setParentId(menuId); // 将主键赋空 item.setId(null); addButtonCodes.add(item.getCode()); newButtons.add(item); } }); //检验是否重复菜单别名,只校验同一父分类菜单下的编号是否重复 LambdaQueryWrapper menuQueryWrapper = Wrappers.lambdaQuery() .eq(Menu::getParentId,menuId) .and(a -> a.in( Menu::getCode, addButtonCodes)); Long cnt = baseMapper.selectCount(menuQueryWrapper); if (cnt > 0L) { return R.fail("该菜单下已存在的编号与要克隆的按钮编号存在重复!"); } return this.saveBatch(newButtons) ? R.success("按钮克隆成功!"):R.fail("按钮克隆失败!"); } /** * 根据主键获取菜单信息 * @param ids * @param menuCode * @param roleIds * @return */ @Override public List getMenuListByCode(List ids,String menuCode,List roleIds){ List menuButtonList = null; if(VciBaseUtil.checkAdminTenant()){ // 正常情况下来说这个不存在为空的情况 // 查询该菜单下的所有按钮 menuButtonList = menuMapper.getButtonByIdsOrByParentCode(null, menuCode, null); }else { menuButtonList = menuMapper.getButtonByIdsOrByParentCode(roleIds,null,ids); } return menuButtonList; } /** * 根据角色id获取已授权的按钮信息 * @param roleId * @return */ @Override public List getButtonsByRoleId(String roleId, String menuCode) { return menuMapper.getButtonsByRoleIdAndCode(roleId,menuCode); } }