Пример #1
0
    public static ZTreeNodeDto <TKey, TData> CreateParent()
    {
        ZTreeNodeDto <TKey, TData> node = new ZTreeNodeDto <TKey, TData>
        {
            Checked = true,
            Id      = default(TKey),
            Name    = "顶级",
            Open    = true,
            PID     = default(TKey)
        };

        return(node);
    }
Пример #2
0
        public async Task <dynamic> GetMenuTreeListByRoleId(long roleId)
        {
            var menuIds = await this.GetMenuIdsByRoleId(roleId) ?? new List <long>();

            List <ZTreeNodeDto <long, dynamic> > roleTreeList = new List <ZTreeNodeDto <long, dynamic> >();
            //List<SysMenu> menus = await _menuRepository.GetAsync(q => q.WithOrderBy(o => o.OrderBy(x => x.ID)));

            var menus = await _menuRepository.SelectAsync(m => m, q => true, q => q.ID, true);


            foreach (var menu in menus)
            {
                var parentMenu = menus.FirstOrDefault(x => x.Code == menu.PCode);
                ZTreeNodeDto <long, dynamic> node = new ZTreeNodeDto <long, dynamic>
                {
                    ID      = menu.ID,
                    PID     = parentMenu != null ? parentMenu.ID : 0,
                    Name    = menu.Name,
                    Open    = parentMenu != null,
                    Checked = menuIds.Contains(menu.ID)
                };
                roleTreeList.Add(node);
            }

            List <Node <long> > nodes = _mapper.Map <List <Node <long> > >(roleTreeList);

            foreach (var node in nodes)
            {
                foreach (var child in nodes)
                {
                    if (child.PID == node.ID)
                    {
                        node.Children.Add(child);
                    }
                }
            }

            var groups = roleTreeList.GroupBy(x => x.PID).Where(x => x.Key > 1);

            foreach (var group in groups)
            {
                roleTreeList.RemoveAll(x => x.ID == group.Key);
            }

            return(new
            {
                treeData = nodes.Where(x => x.PID == 0),
                checkedIds = roleTreeList.Where(x => x.Checked && x.PID != 0).Select(x => x.ID)
            });
        }
Пример #3
0
        public async Task <AppSrvResult <dynamic> > GetMenuTreeListByRoleId(long roleId)
        {
            var menuIds = (await this.GetAllRelations()).Where(x => x.RoleId.Value == roleId).Select(r => r.MenuId.Value) ?? new List <long>();
            List <ZTreeNodeDto <long, dynamic> > roleTreeList = new List <ZTreeNodeDto <long, dynamic> >();

            //var menus = await _menuRepository.SelectAsync(m=>m, q => true, q => q.Id, true);
            var menus = await _menuRepository.Where(w => true).OrderByDescending(x => x.Id).ToListAsync();

            foreach (var menu in menus)
            {
                var parentMenu = menus.FirstOrDefault(x => x.Code == menu.PCode);
                ZTreeNodeDto <long, dynamic> node = new ZTreeNodeDto <long, dynamic>
                {
                    Id      = menu.Id,
                    PID     = parentMenu != null ? parentMenu.Id : 0,
                    Name    = menu.Name,
                    Open    = parentMenu != null,
                    Checked = menuIds.Contains(menu.Id)
                };
                roleTreeList.Add(node);
            }

            List <Node <long> > nodes = _mapper.Map <List <Node <long> > >(roleTreeList);

            foreach (var node in nodes)
            {
                foreach (var child in nodes)
                {
                    if (child.PID == node.Id)
                    {
                        node.Children.Add(child);
                    }
                }
            }

            var groups = roleTreeList.GroupBy(x => x.PID).Where(x => x.Key > 1);

            foreach (var group in groups)
            {
                roleTreeList.RemoveAll(x => x.Id == group.Key);
            }

            return(new
            {
                treeData = nodes.Where(x => x.PID == 0),
                checkedIds = roleTreeList.Where(x => x.Checked && x.PID != 0).Select(x => x.Id)
            });
        }
Пример #4
0
        public async Task <MenuTreeDto> GetMenuTreeListByRoleIdAsync(long roleId)
        {
            var menuIds      = (await _cacheService.GetAllRelationsFromCacheAsync()).Where(x => x.RoleId.Value == roleId).Select(r => r.MenuId.Value) ?? new List <long>();
            var roleTreeList = new List <ZTreeNodeDto <long, dynamic> >();

            var menus = (await _cacheService.GetAllMenusFromCacheAsync()).Where(w => true).OrderBy(x => x.Ordinal);

            foreach (var menu in menus)
            {
                var parentMenu = menus.FirstOrDefault(x => x.Code == menu.PCode);
                var node       = new ZTreeNodeDto <long, dynamic>
                {
                    Id      = menu.Id,
                    PID     = parentMenu != null ? parentMenu.Id : 0,
                    Name    = menu.Name,
                    Open    = parentMenu != null,
                    Checked = menuIds.Contains(menu.Id)
                };
                roleTreeList.Add(node);
            }

            var nodes = Mapper.Map <List <Node <long> > >(roleTreeList);

            foreach (var node in nodes)
            {
                foreach (var child in nodes)
                {
                    if (child.PID == node.Id)
                    {
                        node.Children.Add(child);
                    }
                }
            }

            var groups = roleTreeList.GroupBy(x => x.PID).Where(x => x.Key > 1);

            foreach (var group in groups)
            {
                roleTreeList.RemoveAll(x => x.Id == group.Key);
            }

            return(new MenuTreeDto
            {
                TreeData = nodes.Where(x => x.PID == 0),
                CheckedIds = roleTreeList.Where(x => x.Checked && x.PID != 0).Select(x => x.Id)
            });
        }