菜单加载学习1
https://blog.csdn.net/zhuyu19911016520/article/details/52573072
/// <summary> /// 获取当前用户的角色菜单信息 /// </summary> /// <returns></returns> string GetRoleMenu() { var userId = CurrentUser.Id; var roleService = ServiceHelper.Create<IRoleService>(); var roleMenus = roleService.GetRoleMenuByUserId(userId);//获取用户角色权限菜单 var allMenus = initGlobalData.Menus; //获取缓存中的所有菜单 //关联得到所有角色权限菜单 --最多展现3级菜单 var menus = allMenus.Join(roleMenus, a => a.Id, b => b.MenuId, (a, b) => new { Id = a.Id, MenuName = a.MenuName, MenuIcon = a.MenuIcon, LinkUrl = a.LinkUrl, ParentId = a.ParentId, FullPath = a.FullPath, State = a.State, SortIndex = a.SortIndex, level = a.level }).OrderBy(item => item.SortIndex); StringBuilder sb = new StringBuilder(); var menus0 = menus.Where(item => item.level == 0).ToArray(); foreach (var menu in menus0) //一级菜单 { var menus1 = menus.Where(item => item.ParentId == menu.Id && item.level == 1).ToArray(); sb.AppendLine("<li>"); sb.AppendLine("<a href=\"javascript:void(0)\"><i class=\"" + menu.MenuIcon + "\"></i><span>" + menu.MenuName + "</span><b class=\"icon-keyboard_arrow_left\"></b></a>"); sb.AppendLine("<ul class=\"ms_fs2_menu\">"); foreach (var menu1 in menus1) //二级菜单 { var menus2 = menus.Where(item => item.ParentId == menu1.Id && item.level == 2).ToArray(); sb.AppendLine("<li>"); if (menus2.Count() > 0) sb.AppendLine("<a href=\"javascript:void(0)\">"); else sb.AppendLine("<a href=\"" + menu1.LinkUrl + "\" target=\"rightFrame\">"); sb.AppendLine("<i class=\"" + menu1.MenuIcon + "\"></i><span>" + menu1.MenuName + "</span>"); if (menus2.Count() > 0) sb.AppendLine("<b class=\"icon-keyboard_arrow_left\"></b>"); sb.AppendLine("</a>"); if (menus2.Count() > 0) //三级菜单 { sb.AppendLine("<ul class=\"ms_fs3_menu\">"); foreach (var menu2 in menus2) { sb.AppendLine("<li><a href=\"" + menu2.LinkUrl + "\" target=\"rightFrame\"><i class=\"" + menu2.MenuIcon + "\"></i><span>" + menu2.MenuName + "</span></a></li>"); } sb.AppendLine("</ul>"); } sb.AppendLine("</li>"); } sb.AppendLine("</ul>"); sb.AppendLine("</li>"); } return sb.ToString(); }