51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using AI.Common.Dtos;
|
|
using AI.Common.Entities;
|
|
using AI.Common.Helpers;
|
|
using AI.Common.Services.Interface;
|
|
using Masuit.Tools;
|
|
using Microsoft.Extensions.Configuration;
|
|
using SqlSugar;
|
|
using System.Security.Claims;
|
|
|
|
namespace AI.Common.Services
|
|
{
|
|
public class AuthService : IAuthService
|
|
{
|
|
private readonly ISqlSugarClient _sqlSugarClient;
|
|
private readonly JwtHelper _jwtHelper;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public AuthService(ISqlSugarClient sqlSugarClient, JwtHelper jwtHelper, IConfiguration configuration)
|
|
{
|
|
this._sqlSugarClient = sqlSugarClient.AsTenant().GetConnection("usercenter");
|
|
this._jwtHelper = jwtHelper;
|
|
this._configuration = configuration;
|
|
}
|
|
|
|
|
|
public async Task<object> LoginAsync(LoginDto loginDto)
|
|
{
|
|
var user = await _sqlSugarClient.Queryable<User>()
|
|
.FirstAsync(x => x.Account == loginDto.Account && x.Password == loginDto.Password && x.DeleteState == 0);
|
|
if (user == null)
|
|
{
|
|
throw Oops.Oh("登录失败,账号或密码错误!");
|
|
}
|
|
|
|
var accessToken = _jwtHelper.CreateToken(user.Id.ToString(), new List<Claim>
|
|
{
|
|
new Claim("account",user.Account),
|
|
new Claim(ClaimTypes.Name,user.RealName),
|
|
});
|
|
|
|
return new
|
|
{
|
|
token = accessToken,
|
|
userName = user.RealName,
|
|
account = user.Account,
|
|
expire = _configuration["Jwt:Expires"].ToInt32(),
|
|
};
|
|
}
|
|
}
|
|
}
|