Example #1
0
        public async Task <IActionResult> Login(LoginInput input)
        {
            if (string.IsNullOrWhiteSpace(input.UserName))
            {
                throw new UserOperationException("请输入用户名!");
            }
            if (string.IsNullOrWhiteSpace(input.Password))
            {
                throw new UserOperationException("请输入密码!");
            }
            var user = (await _userServices.Query(u => u.UserName == input.UserName && u.Password == MD5Helper.MD5Encrypt32(input.Password))).SingleOrDefault();

            if (user == null)
            {
                throw new UserOperationException("用户不存在或密码错误!");
            }
            if (user.Status == 0)
            {
                throw new UserOperationException("您的账号已被禁用!");
            }
            var tokenModel = new TokenModelJwt {
                UserId = user.Id
            };
            var userRoles = await _userRoleServices.Query(ur => ur.UserId == user.Id);

            if (userRoles.Any())
            {
                var roleIds = userRoles.Select(ur => ur.RoleId).ToList();
                var roles   = await _roleServices.Query(r => roleIds.Contains(r.Id));

                tokenModel.Roles = roles.Select(r => r.Name).ToList();
            }

            var userPermissions = await _permissionServices.GetUserPermissions(user.Id);

            _requirement.Permissions = userPermissions.Select(p => new Permission
            {
                Role = p.RoleName,
                Url  = p.LinkUrl
            }).ToList();

            var token = _jwtHelper.BuildJwtToken(tokenModel);

            return(Ok(token));
        }