完善控制器接口

This commit is contained in:
lzw
2021-10-23 15:47:15 +08:00
parent 0c1b3632b4
commit a48c9b4061
16 changed files with 192 additions and 137 deletions

View File

@@ -23,49 +23,22 @@ namespace Yi.Framework.WebCore.FilterExtend
/// <summary>
/// 防重复提交周期 单位秒
/// </summary>
public int TimeOut = 1;
//public int TimeOut = 1;
#region Identity
private readonly ILogger<CustomAction2CommitFilterAttribute> _logger;
private readonly CacheClientDB _cacheClientDB;
private static string KeyPrefix = "2CommitFilter";
//#region Identity
//private readonly ILogger<CustomAction2CommitFilterAttribute> _logger;
//private readonly CacheClientDB _cacheClientDB;
//private static string KeyPrefix = "2CommitFilter";
public CustomAction2CommitFilterAttribute(ILogger<CustomAction2CommitFilterAttribute> logger, CacheClientDB cacheClientDB)
{
this._logger = logger;
this._cacheClientDB = cacheClientDB;
}
#endregion
// public CustomAction2CommitFilterAttribute(ILogger<CustomAction2CommitFilterAttribute> logger, CacheClientDB cacheClientDB)
// {
// this._logger = logger;
// this._cacheClientDB = cacheClientDB;
// }
// #endregion
public override void OnActionExecuting(ActionExecutingContext context)
{
string url = context.HttpContext.Request.Path.Value;
string argument = JsonConvert.SerializeObject(context.ActionArguments);
string ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
string agent = context.HttpContext.Request.Headers["User-Agent"];
string sInfo = $"{url}-{argument}-{ip}-{agent}";
string summary = MD5Helper.MD5EncodingOnly(sInfo);
string totalKey = $"{KeyPrefix}-{summary}";
string result = this._cacheClientDB.Get<string>(totalKey);
if (string.IsNullOrEmpty(result))
{
this._cacheClientDB.Add(totalKey, "1", TimeSpan.FromSeconds(3));//3秒有效期
this._logger.LogInformation($"CustomAction2CommitFilterAttribute:{sInfo}");
}
else
{
//已存在
this._logger.LogWarning($"CustomAction2CommitFilterAttribute重复请求:{sInfo}");
context.Result = new JsonResult(Result.Error("请勿重复提交"));
}
//CurrentUser currentUser = context.HttpContext.GetCurrentUserBySession();
//if (currentUser == null)
//{
// //if (this.IsAjaxRequest(context.HttpContext.Request))
//if (this.IsAjaxRequest(context.HttpContext.Request))
// //{ }
// context.Result = new RedirectResult("~/Fourth/Login");
//}
@@ -73,11 +46,38 @@ namespace Yi.Framework.WebCore.FilterExtend
//{
// this._logger.LogDebug($"{currentUser.Name} 访问系统");
//}
}
private bool IsAjaxRequest(HttpRequest request)
{
string header = request.Headers["X-Requested-With"];
return "XMLHttpRequest".Equals(header);
}
//} public override void OnActionExecuting(ActionExecutingContext context)
//{
// string url = context.HttpContext.Request.Path.Value;
// string argument = JsonConvert.SerializeObject(context.ActionArguments);
// string ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
// string agent = context.HttpContext.Request.Headers["User-Agent"];
// string sInfo = $"{url}-{argument}-{ip}-{agent}";
// string summary = MD5Helper.MD5EncodingOnly(sInfo);
// string totalKey = $"{KeyPrefix}-{summary}";
// string result = this._cacheClientDB.Get<string>(totalKey);
// if (string.IsNullOrEmpty(result))
// {
// this._cacheClientDB.Add(totalKey, "1", TimeSpan.FromSeconds(3));//3秒有效期
// this._logger.LogInformation($"CustomAction2CommitFilterAttribute:{sInfo}");
// }
// else
// {
// //已存在
// this._logger.LogWarning($"CustomAction2CommitFilterAttribute重复请求:{sInfo}");
// context.Result = new JsonResult(Result.Error("请勿重复提交"));
// }
// //CurrentUser currentUser = context.HttpContext.GetCurrentUserBySession();
// //if (currentUser == null)
// //{
// // //
//private bool IsAjaxRequest(HttpRequest request)
//{
// string header = request.Headers["X-Requested-With"];
// return "XMLHttpRequest".Equals(header);
//}
}
}

View File

@@ -25,6 +25,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
#endregion
services.AddScoped<DbContext, DataContext>();
return services;
}