using Furion;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.JsonSerialization;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Diagnostics;
using System.Security.Claims;
using UAParser;
namespace iWare.Wms.Core
{
///
/// 请求日志拦截
///
public class RequestActionFilter : IAsyncActionFilter
{
private readonly IEventPublisher _eventPublisher;
public RequestActionFilter(IEventPublisher eventPublisher)
{
_eventPublisher = eventPublisher;
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var httpContext = context.HttpContext;
var httpRequest = httpContext.Request;
//获取head信息
//
//var headrparam= httpContext.head
var sw = new Stopwatch();
sw.Start();
var actionContext = await next();
sw.Stop();
// 判断是否请求成功(没有异常就是请求成功)
var isRequestSucceed = actionContext.Exception == null;
var headers = httpRequest.Headers;
var clientInfo = headers.ContainsKey("User-Agent") ? Parser.GetDefault().Parse(headers["User-Agent"]) : null;
var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (headers["AppVersion"].Count ==1 && headers["AppVersion"]!= App.Configuration["AppVersion:Version"] && headers["IsDevelopment"] == "true")
{
throw Oops.Oh("请更新PDA版本信息");
}
var ip = httpContext.GetRequestIPv4();
//判断是否需有禁用操作日志属性
foreach (var metadata in actionDescriptor.EndpointMetadata)
{
if (metadata.GetType() == typeof(DisableOpLogAttribute))
{
//禁用操作日志,直接返回
return;
}
}
await _eventPublisher.PublishAsync(new ChannelEventSource("Create:OpLog",
new SysLogOp
{
Name = httpContext.User?.FindFirstValue(ClaimConst.CLAINM_NAME),
Success = isRequestSucceed ? YesOrNot.Y : YesOrNot.N,
Ip = ip,
Location = httpRequest.GetRequestUrlAddress(),
Browser = clientInfo?.UA.Family + clientInfo?.UA.Major,
Os = clientInfo?.OS.Family + clientInfo?.OS.Major,
Url = httpRequest.Path,
ClassName = context.Controller.ToString(),
MethodName = actionDescriptor?.ActionName,
ReqMethod = httpRequest.Method,
Param = context.ActionArguments.Count < 1 ? string.Empty : JSON.Serialize(context.ActionArguments),
Result = actionContext.Result?.GetType() == typeof(JsonResult) ? JSON.Serialize(actionContext.Result) : string.Empty,
ElapsedTime = sw.ElapsedMilliseconds,
OpTime = DateTimeOffset.Now,
Account = httpContext.User?.FindFirstValue(ClaimConst.CLAINM_ACCOUNT)
}));
}
///
/// 异常返回值
///
///
///
///
public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
{
return new JsonResult(new XnRestfulResult