Files
BuYuHouDuan/AdminSrc/AdminApi/LoginLogs/UserLoginLogController.cs
T

138 lines
4.6 KiB
C#
Raw Normal View History

2026-10-12 16:22:04 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common;
using CommonInfrastructure;
using Microsoft.AspNetCore.Mvc;
using MongoRepository;
using UserDomain;
using UserDomain.Entity;
using UserDomain.Param;
namespace AdminApi.LoginLogs
{
[ApiController, Route("adminapi/[controller]")]
public class UserLoginLogController : BaseController
{
private readonly IMongoBaseRepository<UserLoginLog> _logRepo;
public UserLoginLogController(IMongoBaseRepository<UserLoginLog> logRepo)
{
_logRepo = logRepo;
}
[HttpPost("List")]
public async Task<PageResponse<UserLoginLogDto>> GetList([FromBody] UserLoginLogQueryParam param)
{
var result = await _logRepo.QueryPageAsync(
filterExpression: x =>
(string.IsNullOrEmpty(param.UserId) || x.UserId == param.UserId) &&
(string.IsNullOrEmpty(param.UserNumber) || x.UserNumber == param.UserNumber) &&
(string.IsNullOrEmpty(param.UserName) || (x.UserName != null && x.UserName.Contains(param.UserName))) &&
(!param.LoginType.HasValue || x.LoginType == param.LoginType) &&
(!param.Status.HasValue || x.Status == param.Status) &&
(!param.Platform.HasValue || x.Platform == param.Platform) &&
(!param.StartTime.HasValue || x.LoginTime >= param.StartTime) &&
(!param.EndTime.HasValue || x.LoginTime <= param.EndTime),
orderExpression: x => x.LoginTime,
desc: true,
pageSize: param.PageSize,
pageIndex: param.PageIndex
);
var dtos = result.list.Select(MapToDto);
return this.ToPageResult(dtos, (int)result.count);
}
[HttpGet("Detail")]
public async Task<BaseResponse<UserLoginLogDto>> GetDetail(string id)
{
var log = await _logRepo.FindOneAsync(x => x.Id == id);
if (log == null)
return this.ToFailResult<UserLoginLogDto>("日志不存在");
return this.ToResult(MapToDto(log));
}
[HttpPost("Delete")]
public async Task<BaseResponse<string>> Delete(string id)
{
var result = await _logRepo.DeleteByIdAsync(id);
if (result.ok)
return this.ToResult("删除成功");
else
return this.ToFailResult<string>("删除失败");
}
[HttpPost("BatchDelete")]
public async Task<BaseResponse<string>> BatchDelete([FromBody] List<string> ids)
{
int success = 0, fail = 0;
foreach (var id in ids)
{
var result = await _logRepo.DeleteByIdAsync(id);
if (result.ok)
success++;
else
fail++;
}
if (fail == 0)
return this.ToResult($"成功删除 {success} 条");
return this.ToResult($"删除完成:成功 {success} 条,失败 {fail} 条");
}
private static UserLoginLogDto MapToDto(UserLoginLog entity)
{
return new UserLoginLogDto
{
Id = entity.Id,
UserId = entity.UserId ?? "",
UserNumber = entity.UserNumber ?? "",
UserName = entity.UserName ?? "",
LoginType = entity.LoginType,
Status = entity.Status,
Ip = entity.Ip ?? "",
IpLocation = entity.IpLocation ?? "",
DeviceType = entity.DeviceType ?? "",
DeviceId = entity.DeviceId ?? "",
Platform = entity.Platform,
ClientVersion = entity.ClientVersion ?? "",
FailReason = entity.FailReason ?? "",
LoginTime = entity.LoginTime
};
}
}
public class UserLoginLogDto
{
public string Id { get; set; }
public string UserId { get; set; }
public string UserNumber { get; set; }
public string UserName { get; set; }
public int LoginType { get; set; }
public int Status { get; set; }
public string Ip { get; set; }
public string IpLocation { get; set; }
public string DeviceType { get; set; }
public string DeviceId { get; set; }
public int Platform { get; set; }
public string ClientVersion { get; set; }
public string FailReason { get; set; }
public DateTime LoginTime { get; set; }
}
}