コード例 #1
0
ファイル: MenuController.cs プロジェクト: xudahe/Xu.Core
        public async Task <object> GetMenu(string ids = "", string menuName = "", string parentId = "")
        {
            var data = await _menuSvc.GetDataByids(ids);

            for (int i = 0; i < data.Count; i++)
            {
                data[i].ParentName = data[i].ParentId.HasValue ? (data.FirstOrDefault(s => s.Id == data[i].ParentId)?.MenuName) : "";
            }

            if (!string.IsNullOrEmpty(menuName))
            {
                data = data.Where(a => a.MenuName.Contains(menuName)).ToList();
            }

            if (!string.IsNullOrEmpty(parentId))
            {
                data = data.Where(a => a.ParentId == parentId.ToInt32Req()).ToList();
            }

            return(new MessageModel <List <Menu> >()
            {
                Message = "获取成功",
                Success = true,
                Response = data
            });
        }
コード例 #2
0
ファイル: RoleController.cs プロジェクト: xudahe/Xu.Core
        public async Task <object> PutRole([FromBody] Role model)
        {
            var data = new MessageModel <string>();

            var menuList = await _menuSvc.GetDataByids(model.MenuIds);

            model.MenuInfoList = _mapper.Map <IList <Menu>, IList <InfoMenu> >(menuList);

            if (model != null && model.Id > 0)
            {
                model.ModifyTime = DateTime.Now;
                data.Success     = await _roleSvc.Update(model);

                if (data.Success)
                {
                    data.Message  = "更新成功";
                    data.Response = model?.Id.ToString();
                }
            }

            return(data);
        }
コード例 #3
0
        public async Task <object> GetLoginByToken(string token)
        {
            var data           = new MessageModel <LoginViewModel>();
            var loginViewModel = new LoginViewModel();

            if (!string.IsNullOrEmpty(token))
            {
                var tokenModel = JwtHelper.SerializeJwt(token);
                if (tokenModel != null && tokenModel.Id > 0)
                {
                    var model = await _userSvc.QueryById(tokenModel.Id);

                    if (model != null)
                    {
                        loginViewModel = _mapper.Map <LoginViewModel>(model);

                        if (model.RoleIds != "")
                        {
                            var roleList = await _roleSvc.GetDataByids(model.RoleIds);

                            loginViewModel.RoleInfoList = _mapper.Map <IList <Role>, IList <RoleViewModel> >(roleList);

                            var menuIds = roleList.Select(s => s.MenuIds).ToList().JoinToString(",");

                            var menuData = await _menuSvc.Query();

                            var menuList = await _menuSvc.GetDataByids(menuIds, menuData);

                            if (menuList.Count > 0)
                            {
                                for (int i = 0; i < menuList.Count; i++)
                                {
                                    menuList[i].ParentName = menuList[i].ParentId.HasValue ? (menuData.FirstOrDefault(s => s.Id == menuList[i].ParentId).MenuName) : "";
                                }

                                var menuList1 = menuList.Where(s => !s.ParentId.HasValue).OrderBy(s => s.Index).ToList(); //获取一级菜单(顶部)
                                for (int i = 0; i < menuList1.Count; i++)
                                {
                                    var menuList2 = menuList.Where(s => s.ParentId == menuList1[i].Id).OrderBy(s => s.Index).ToList(); //获取二级菜单

                                    for (int j = 0; j < menuList2.Count; j++)
                                    {
                                        menuList2[j].Children = menuList.Where(s => s.ParentId == menuList2[j].Id).OrderBy(s => s.Index).ToList(); //获取三级菜单
                                    }
                                    menuList1[i].Children = menuList2;
                                }

                                loginViewModel.MenuInfoList = _mapper.Map <IList <Menu>, IList <MenuViewModel> >(menuList1);

                                data.Success  = true;
                                data.Message  = "登陆成功";
                                data.Response = loginViewModel;
                            }
                            else
                            {
                                data.Message = "该角色没有绑定菜单!";
                            }
                        }
                        else
                        {
                            data.Message = "该用户没有绑定角色!";
                        }
                    }
                    else
                    {
                        data.Message = "该用户不存在,请核实!";
                    }
                }
            }
            return(data);
        }