Exemplo n.º 1
0
        public async Task <MessageModel <RoleTree> > GetRoleTree()
        {
            var data  = new MessageModel <RoleTree>();
            var roles = await _roleServices.Query(d => d.IsDeleted == false);

            var roleTrees = (from child in roles
                             where child.IsDeleted == false
                             orderby child.OrderSort
                             select new RoleTree
            {
                value = child.Id,
                label = child.Name,
                pid = child.Pid,
                order = child.OrderSort,
            }).ToList();
            RoleTree rootRoot = new RoleTree
            {
                value = 0,
                pid   = 0,
                label = "根节点"
            };

            RecursionHelper.LoopToAppendChildrenT(roleTrees, rootRoot);

            data.success = true;
            if (data.success)
            {
                data.response = rootRoot;
                data.msg      = "获取成功";
            }

            return(data);
        }
Exemplo n.º 2
0
        public async Task <MessageModel <List <RoleTree> > > GetCurrentUserRoleTree(bool hasCurrentRole = false)
        {
            var userRoles = await _userRoleServices.Query(it => it.UserId == _user.ID);

            var roleIds = userRoles.Select(it => it.RoleId).ToList();
            var data    = new MessageModel <List <RoleTree> >();
            //超级管理员例外,可以操作所有角色。
            var allRoles = await _roleServices.Query(d => d.IsDeleted == false);

            var roleTrees = (from child in allRoles
                             where child.IsDeleted == false
                             orderby child.OrderSort
                             select new RoleTree
            {
                value = child.Id,
                label = child.Name,
                pid = child.Pid,
                order = child.OrderSort,
            }).ToList();
            var roleTreeList = new List <RoleTree>();

            if (roleIds.Contains(1))
            {
                roleIds = allRoles.Where(it => it.Pid == 0 && it.Id != 1).Select(it => it.Id).ToList();
            }
            roleIds.ForEach(id =>
            {
                RoleTree rootRoot = new RoleTree
                {
                    value = id,
                    pid   = 0,
                    label = allRoles.Find(it => it.Id == id).Name
                };
                RecursionHelper.LoopToAppendChildrenT(roleTrees, rootRoot);
                roleTreeList.Add(rootRoot);
            });


            data.success = true;
            if (data.success)
            {
                data.response = roleTreeList;
                data.msg      = "获取成功";
            }

            return(data);
        }