添加项目文件。
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using Common;
|
||||
using Domain.Admin.Admin;
|
||||
using Domain.Admin.Admin.Param;
|
||||
using MongoRepository;
|
||||
using MongoRepository.Config;
|
||||
|
||||
namespace AdminService.Admin
|
||||
{
|
||||
public class AdminOperationLogService : IAdminOperationLogService
|
||||
{
|
||||
private readonly IMongoBaseRepository<AdminOperationLog> _logRepo;
|
||||
|
||||
public AdminOperationLogService(IMongoBaseRepository<AdminOperationLog> logRepo)
|
||||
{
|
||||
_logRepo = logRepo;
|
||||
}
|
||||
|
||||
public async Task<bool> AddLog(AdminOperationLog log)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _logRepo.InsertOneAsync(log);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(List<AdminOperationLog> list, long count)> QueryPage(OperationLogQueryParam param)
|
||||
{
|
||||
var result = await _logRepo.QueryPageAsync(
|
||||
filterExpression: x =>
|
||||
(string.IsNullOrEmpty(param.AdminId) || x.AdminId == param.AdminId) &&
|
||||
(string.IsNullOrEmpty(param.AdminName) || (x.AdminName != null && x.AdminName.Contains(param.AdminName))) &&
|
||||
(!param.Module.HasValue || x.Module == param.Module) &&
|
||||
(!param.StartTime.HasValue || x.OperateTime >= param.StartTime) &&
|
||||
(!param.EndTime.HasValue || x.OperateTime <= param.EndTime) &&
|
||||
(!param.Status.HasValue || x.Status == param.Status) &&
|
||||
x.HttpMethod != "GET",
|
||||
orderExpression: x => x.OperateTime,
|
||||
desc: true,
|
||||
pageSize: param.PageSize,
|
||||
pageIndex: param.PageIndex
|
||||
);
|
||||
|
||||
return (result.list.ToList(), result.count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Security.Claims;
|
||||
using Domain.Admin.Admin;
|
||||
using Domain.Admin.Admin.ENum;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AdminService.Admin
|
||||
{
|
||||
public static class OperationLogHelper
|
||||
{
|
||||
public static AdminOperationLog CreateLog(
|
||||
EnumOperationModule module,
|
||||
string actionType,
|
||||
string description,
|
||||
string httpMethod,
|
||||
string requestPath,
|
||||
string? requestParams = null,
|
||||
string? response = null,
|
||||
int status = 1)
|
||||
{
|
||||
return new AdminOperationLog
|
||||
{
|
||||
Module = (int)module,
|
||||
ActionType = actionType,
|
||||
Description = description,
|
||||
HttpMethod = httpMethod,
|
||||
RequestPath = requestPath,
|
||||
RequestParams = requestParams,
|
||||
Response = response,
|
||||
Status = status,
|
||||
OperateTime = DateTime.Now
|
||||
};
|
||||
}
|
||||
|
||||
public static (string? adminId, string? adminName) GetAdminInfo(ClaimsPrincipal? user)
|
||||
{
|
||||
if (user == null) return (null, null);
|
||||
|
||||
var adminId = user.FindFirst("uid")?.Value;
|
||||
var adminName = user.FindFirst("dname")?.Value;
|
||||
|
||||
if (string.IsNullOrEmpty(adminName))
|
||||
{
|
||||
adminName = user.FindFirst(ClaimTypes.Name)?.Value;
|
||||
}
|
||||
|
||||
return (adminId, adminName);
|
||||
}
|
||||
|
||||
public static void FillAdminInfo(AdminOperationLog log, ClaimsPrincipal? user)
|
||||
{
|
||||
var (adminId, adminName) = GetAdminInfo(user);
|
||||
log.AdminId = adminId;
|
||||
log.AdminName = adminName;
|
||||
}
|
||||
|
||||
public static string? GetClientIp(HttpContext? context)
|
||||
{
|
||||
if (context == null) return null;
|
||||
|
||||
var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(ip))
|
||||
{
|
||||
ip = context.Connection.RemoteIpAddress?.ToString();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user