添加项目文件。
This commit is contained in:
52
HT.Cloud.Code/Attribute/LockAttribute.cs
Normal file
52
HT.Cloud.Code/Attribute/LockAttribute.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 防重复提交,api使用
|
||||
/// </summary>
|
||||
public class LockAttribute : ActionFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 拦截
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="next"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
if (GlobalContext.SystemConfig.Debug == false)
|
||||
{
|
||||
if (OperatorProvider.Provider.GetCurrent() == null)
|
||||
{
|
||||
context.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "抱歉,没有操作权限" });
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
string token = context.HttpContext.Request.Headers[GlobalContext.SystemConfig.TokenName].ParseToString();
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
context.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "token不能空" });
|
||||
return;
|
||||
}
|
||||
//固定加锁5秒
|
||||
bool result = CacheHelper.SetNx(token, token, 5);
|
||||
if (!result)
|
||||
{
|
||||
context.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = "请求太频繁,请稍后" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
await next();
|
||||
|
||||
sw.Stop();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user