【优化】用户支付api增加redis锁,防止出现多次点击可能存在二次支付的问题。

This commit is contained in:
大灰灰
2022-10-15 00:04:59 +08:00
parent 1ebbe31894
commit 481dbe3ce5
2 changed files with 41 additions and 19 deletions

View File

@@ -43,6 +43,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Nito.AsyncEx;
using NLog;
using SKIT.FlurlHttpClient.Wechat.Api;
@@ -1218,25 +1219,46 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{
var jm = new WebApiCallBack();
if (string.IsNullOrEmpty(entity.ids))
var lockKey = "LOCK_Pay:user_" + _user.ID;
var lockHolder = Guid.NewGuid().ToString("N"); //锁持有者
var redisUserLock = await _redisOperationRepository.LockTakeAsync(lockKey, lockHolder, TimeSpan.FromSeconds(10));
if (redisUserLock)
{
jm.code = 13100;
jm.msg = GlobalErrorCodeVars.Code13100;
try
{
if (string.IsNullOrEmpty(entity.ids))
{
jm.code = 13100;
jm.msg = GlobalErrorCodeVars.Code13100;
}
else if (string.IsNullOrEmpty(entity.payment_code))
{
jm.code = 10055;
jm.msg = GlobalErrorCodeVars.Code10055;
}
else if (entity.payment_type == 0)
{
jm.code = 10051;
jm.msg = GlobalErrorCodeVars.Code10051;
}
//生成支付单,并发起支付
jm = await _billPaymentsServices.Pay(entity.ids, entity.payment_code, _user.ID, entity.payment_type, entity.@params);
}
catch (Exception e)
{
jm.msg = "数据处理异常";
jm.otherData = e;
NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.ApiRequest, "用户支付支付",JsonConvert.SerializeObject(jm));
}
finally
{
await _redisOperationRepository.LockReleaseAsync(lockKey, lockHolder);
}
}
else if (string.IsNullOrEmpty(entity.payment_code))
else
{
jm.code = 10055;
jm.msg = GlobalErrorCodeVars.Code10055;
jm.msg = "当前请求太频繁_请稍后再试";
}
else if (entity.payment_type == 0)
{
jm.code = 10051;
jm.msg = GlobalErrorCodeVars.Code10051;
}
//生成支付单,并发起支付
jm = await _billPaymentsServices.Pay(entity.ids, entity.payment_code, _user.ID, entity.payment_type,
entity.@params);
//NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.RedisMessageQueue, "支付",JsonConvert.SerializeObject(jm));
return jm;
}