Exemplo n.º 1
0
        /// <summary>
        /// 根据权限ID获取菜单
        /// </summary>
        /// <param name="id">权限ID</param>
        /// <returns></returns>
        public List <MenuTreeModel> GetMenuTreeByRoleId(int id)
        {
            List <MenuTreeModel> menuTreeList  = new List <MenuTreeModel>();
            List <Menu>          allFirstMenus = GetFirstMenu();

            int[] allFirstMenuIdArray = new int[allFirstMenus.Count];
            int   i = 0;

            //将菜单集合里的菜单ID转换成数组
            foreach (var firstMenu in allFirstMenus)
            {
                allFirstMenuIdArray[i] = firstMenu.Id;
                i++;
            }
            int[] allMenuIdArray = _roleMenuService.GetMenuArray(id);
            //通过求交集获取需要的一级菜单id数组
            int[] firstMenuIdArray = allMenuIdArray.Intersect(allFirstMenuIdArray).ToArray();
            //通过求差集获取需要的二级菜单id数组
            int[] secondMenuIdArray = allMenuIdArray.Except(firstMenuIdArray).ToArray();
            //获取允许展示的二级菜单集合
            List <Menu> secondMenuList = new List <Menu>();

            foreach (var i1 in secondMenuIdArray)
            {
                Menu tempMenu = _repository.Find(c => c.Id == i1 && c.State == 1);
                if (tempMenu != null)
                {
                    secondMenuList.Add(tempMenu);
                }
            }
            foreach (var firstMenuId in firstMenuIdArray)
            {
                Menu tempMenu = _repository.Find(c => c.Id == firstMenuId && c.State == 1);
                if (tempMenu != null)
                {
                    List <Menu> tempSecondMenuList = new List <Menu>();
                    foreach (var menu in secondMenuList)
                    {
                        if (menu.PId == firstMenuId)
                        {
                            tempSecondMenuList.Add(menu);
                        }
                    }
                    MenuTreeModel tempMenuTreeModel = new MenuTreeModel()
                    {
                        Id       = tempMenu.Id,
                        Icon     = tempMenu.Icon,
                        Name     = tempMenu.Name,
                        Url      = tempMenu.Url,
                        PId      = tempMenu.PId,
                        State    = tempMenu.State,
                        MenuList = tempSecondMenuList
                    };
                    menuTreeList.Add(tempMenuTreeModel);
                }
            }
            return(menuTreeList);
        }
Exemplo n.º 2
0
 public IAjaxResult GetMenuArray(int id)
 {
     return(Success(_roleMenuService.GetMenuArray(id)));
 }