/// <summary> /// 获取树形菜单,渲染左侧菜单使用 /// </summary> /// <returns></returns> public async Task <RouterBar> RouterBar(Guid userId) { var Rolelist = await _userRoleRepository.GetAllListAsync(x => x.UserIdOrDepId == userId); //根据用户ID获取角色列表 var RoleIds = Rolelist.Select(x => x.RoleId).ToList(); //获取到角色列表中的角色ID var MenuList = await _roleRightAssigRepository.GetAllListAsync(x => RoleIds.Contains(x.RoleId)); //根据角色Id获取菜单列表 var MenuIds = MenuList.Select(x => x.MenuId).ToList(); //获取角色权限中的菜单ID var baselist = await _menuRepositoty.GetAllListAsync(x => MenuIds.Contains(x.Id)); //根据菜单ID获取菜单列表 x=>MenuIds.Contains(x.Id) var btnlist = await _buttonRepositoty.GetAllListAsync(x => x.IsShow == true && x.IsDrop == false); RouterBar routerbar = new RouterBar() { Id = Guid.Empty, name = "根节点", order = 0, path = "", meta = new NavigationBarMeta(), ParentId = Guid.Empty, }; //定义一个树形列表 var routerbarTreeall = baselist.Where(x => x.IsDrop == false).Select(s => new RouterBar { Id = s.Id, name = s.Name, ParentId = s.ParentId, iconCls = s.Icon, order = s.Sort, path = s.UrlAddress, meta = new NavigationBarMeta { requireAuth = true, title = s.Name, } }).ToList(); try { //递归形成前端路由器格式的树形菜单 CreateMenuTree(routerbarTreeall, routerbar, btnlist, MenuList); return(routerbar); } catch (Exception ex) { throw ex; } }
/// <summary> /// 递归循环树状结构 /// </summary> /// <param name="basemenu"></param> /// <param name="menuTreeModel"></param> //[HttpPost] public void CreateMenuTree(List <RouterBar> all, RouterBar curItem, List <SysButton> sysButtons, List <SysRoleRight> sysRoleRights) { //得到根节点下的子集 var child = all.FindAll(m => m.ParentId == curItem.Id); //根据根节判断是否存在子集, if (child.Any()) { curItem.children = new List <RouterBar>(); curItem.children.AddRange(child); } else { curItem.children = new List <RouterBar>(); } foreach (var item in child) { var roles = sysRoleRights.Where(x => x.MenuId == item.Id).FirstOrDefault(); if (roles != null) { //判断改角色的菜单下是否存在按钮ID, if (!roles.ButtonIds.IsNullOrEmpty()) { var btnIds = roles.ButtonIds.Split(',').Select(x => x.ToGuid()).ToList();//获取Id的字符串 //查询出拥有权限的按钮 var btn = sysButtons.Where(x => btnIds.Contains(x.Id)).ToList().Select(x => new BtnIsDisplayViewModel { Name = x.Name, BtnStyle = x.ButtonStyle, KeyCode = x.KeyCode }).ToList(); item.btnIsDisplayViews = btn;//将按钮添加到菜单下面 } } CreateMenuTree(all, item, sysButtons, sysRoleRights); } }
public async Task <BaseResponse <RouterBar> > GetNavigationBar(int uid) { var data = new BaseResponse <RouterBar>(); try { #region MyRegion //RouterBar rootRoot = new RouterBar() //{ // id = 0, // pid = 0, // order = 0, // name = "根节点", // path = "", // icon = "", // //meta = new NavigationBarMeta(), //}; //RouterBar lst1 = new RouterBar //{ // id = 1, // APIAddress = "", // icon = "fa-qq", // leaf = true, // path = "/", // name = "首页", // pid = 1 //}; //RouterBar lst1_1 = new RouterBar //{ // id = 1, // APIAddress = "", // leaf = true, // path = "", // name = "About", // pid = 1 //}; //lst1.children.Add(lst1_1); //RouterBar lst2 = new RouterBar //{ // id = 1, // APIAddress = "", // icon = "fa-users", // leaf = false, // path = "/Home", // name = "权限管理", // pid = 1 //}; //RouterBar lst2_1 = new RouterBar //{ // id = 1, // APIAddress = "", // icon = "fa-qq", // leaf = false, // path = "/User/User", // name = "用户管理", // pid = 1 //}; //RouterBar lst2_2 = new RouterBar //{ // id = 1, // APIAddress = "", // icon = "fa-qq", // leaf = false, // path = "/User/Roles", // name = "角色管理", // pid = 1 //}; //RouterBar lst2_3 = new RouterBar //{ // id = 1, // APIAddress = "", // icon = "fa-qq", // leaf = false, // path = "/Menu/Menu", // name = "菜单管理", // pid = 1 //}; //lst2.children.Add(lst2_1); //lst2.children.Add(lst2_2); //lst2.children.Add(lst2_3); //rootRoot.children.Add(lst1); //rootRoot.children.Add(lst2); //data.Data = rootRoot; #endregion // 三种方式获取 uid var uidInHttpcontext1 = (from item in _httpContext.HttpContext.User.Claims where item.Type == "jti" select item.Value).FirstOrDefault(); var uidInHttpcontext = (JwtHelper.SerializeJwt(_httpContext.HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "")))?.Uid; var uName = _user.Name; if (uid > 0 && uid == uidInHttpcontext) { var roleId = ((await _userRoleServices.Query(d => d.UserId == uid)).FirstOrDefault()?.RoleId); if (roleId > 0) { var menus = await _menuServices.Query(d => d.IsDrop == false); var menusTrees = (from item in menus where item.IsDrop == false orderby item.Id select new RouterBar { id = item.Id, APIAddress = item.ApiUrl, icon = item.Icon, leaf = item.RoutePath == "-" ? false : true, path = item.RoutePath, name = item.Name, pid = item.ParentId, order = item.OrderSort, }).ToList(); RouterBar rootRoot = new RouterBar() { id = 0, pid = 0, order = 0, name = "根节点", path = "", icon = "" }; menusTrees = menusTrees.OrderBy(d => d.order).ToList(); RecursionHelper.LoopNaviBarAppendChildren(menusTrees, rootRoot); data.Data = rootRoot; data.Msg = "获取成功"; } } } catch (Exception ex) { } return(data); }
/// <summary> /// 获取树形菜单,渲染左侧菜单使用 /// </summary> /// <returns></returns> public async Task <RouterBar> RouterBar(Guid userId) { //记录Job时间 //Stopwatch stopwatch = new Stopwatch(); //stopwatch.Start(); var Rolelist = await _userRoleRepository.GetAllListAsync(x => x.UserIdOrDepId == userId); //根据用户ID获取角色列表 var RoleIds = Rolelist.Select(x => x.RoleId).ToList(); //获取到角色列表中的角色ID var MenuList = await _roleRightAssigRepository.GetAllListAsync(x => RoleIds.Contains(x.RoleId)); //根据角色Id获取菜单列表 var MenuIds = MenuList.Select(x => x.MenuId).ToList(); //获取角色权限中的菜单ID List <SysMenu> baselist = new List <SysMenu>(); if (await _redisCacheManager.Get(Appsettings.app(new string[] { "CacheOptions", "Menukey" }))) //判断菜单缓存是否存在,如果存在则取缓存不存在则取数据库 { baselist = await _redisCacheManager.GetList <SysMenu>(Appsettings.app(new string[] { "CacheOptions", "Menukey" })); //.Where(x=>MenuIds.Contains(x.Id)).ToList(); baselist = baselist.Where(x => MenuIds.Contains(x.Id)).ToList(); } else { await RestMenuCache(); baselist = await _redisCacheManager.GetList <SysMenu>(Appsettings.app(new string[] { "CacheOptions", "Menukey" })); baselist = baselist.Where(x => MenuIds.Contains(x.Id)).ToList(); } if (!baselist.Any()) { baselist = await _menuRepositoty.GetAllListAsync(x => MenuIds.Contains(x.Id));//根据菜单ID获取菜单列表 x=>MenuIds.Contains(x.Id) } var btnlist = await _buttonRepositoty.GetAllListAsync(x => x.IsShow == true && x.IsDrop == false); RouterBar routerbar = new RouterBar() { Id = Guid.Empty, name = "根节点", order = 0, path = "", meta = new NavigationBarMeta(), ParentId = Guid.Empty, }; //定义一个树形列表 var routerbarTreeall = baselist.Where(x => x.IsDrop == false).Select(s => new RouterBar { Id = s.Id, name = s.Name, ParentId = s.ParentId, iconCls = s.Icon, order = s.Sort, path = s.UrlAddress, meta = new NavigationBarMeta { requireAuth = true, title = s.Name, } }).ToList(); try { //递归形成前端路由器格式的树形菜单 CreateMenuTree(routerbarTreeall, routerbar, btnlist, MenuList); return(routerbar); } catch (Exception ex) { throw ex; } }