using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using ZhonTai.Common.Helpers;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Domain.LoginLog;
using ZhonTai.Admin.Services.LoginLog.Dto;
using ZhonTai.Admin.Domain;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;
using Microsoft.AspNetCore.Mvc;
using ZhonTai.Admin.Core.Consts;
namespace ZhonTai.Admin.Services.LoginLog;
///
/// 登录日志服务
///
[DynamicApi(Area = AdminConsts.AreaName)]
public class LoginLogService : BaseService, ILoginLogService, IDynamicApi
{
private readonly IHttpContextAccessor _context;
private readonly ILoginLogRepository _loginLogRepository;
public LoginLogService(
IHttpContextAccessor context,
ILoginLogRepository loginLogRepository
)
{
_context = context;
_loginLogRepository = loginLogRepository;
}
///
/// 查询登录日志列表
///
///
///
[HttpPost]
public async Task> GetPageAsync(PageInput input)
{
var userName = input.Filter?.CreatedUserName;
var list = await _loginLogRepository.Select
.WhereDynamicFilter(input.DynamicFilter)
.WhereIf(userName.NotNull(), a => a.CreatedUserName.Contains(userName))
.Count(out var total)
.OrderByDescending(true, c => c.Id)
.Page(input.CurrentPage, input.PageSize)
.ToListAsync();
var data = new PageOutput()
{
List = list,
Total = total
};
return data;
}
///
/// 新增
///
///
///
public async Task AddAsync(LoginLogAddInput input)
{
input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);
string ua = _context.HttpContext.Request.Headers["User-Agent"];
if (ua.NotNull())
{
var client = UAParser.Parser.GetDefault().Parse(ua);
var device = client.Device.Family;
device = device.ToLower() == "other" ? "" : device;
input.Browser = client.UA.Family;
input.Os = client.OS.Family;
input.Device = device;
input.BrowserInfo = ua;
}
var entity = Mapper.Map(input);
await _loginLogRepository.InsertAsync(entity);
return entity.Id;
}
}