【优化】用户支付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

@@ -67,11 +67,9 @@ namespace CoreCms.Net.Services
{ {
try try
{ {
_unitOfWork.BeginTran();
//取用户实际余额 //取用户实际余额
//(会员陌小北提供) //(会员陌小北提供)
var userInfo = await userServices.QueryByClauseWithTranLockAsync(p => p.id == userId, p => p.id, OrderByType.Desc, true); var userInfo = await userServices.QueryByClauseAsync(p => p.id == userId, p => p.id, OrderByType.Desc, true);
if (userInfo == null) if (userInfo == null)
{ {
@@ -90,7 +88,6 @@ namespace CoreCms.Net.Services
if (type != (int)GlobalEnumVars.UserBalanceSourceTypes.Admin) if (type != (int)GlobalEnumVars.UserBalanceSourceTypes.Admin)
{ {
//后台充值或调不改绝对值 //后台充值或调不改绝对值
} }
//如果是减余额的操作,还是加余额操作 //如果是减余额的操作,还是加余额操作
if (type is (int)GlobalEnumVars.UserBalanceSourceTypes.Pay or (int)GlobalEnumVars.UserBalanceSourceTypes.Tocash) if (type is (int)GlobalEnumVars.UserBalanceSourceTypes.Pay or (int)GlobalEnumVars.UserBalanceSourceTypes.Tocash)
@@ -117,6 +114,9 @@ namespace CoreCms.Net.Services
balanceModel.sourceId = sourceId; balanceModel.sourceId = sourceId;
balanceModel.memo = memo; balanceModel.memo = memo;
balanceModel.createTime = DateTime.Now; balanceModel.createTime = DateTime.Now;
_unitOfWork.BeginTran();
//增加记录 //增加记录
var balanceModelId = await _dal.InsertAsync(balanceModel); var balanceModelId = await _dal.InsertAsync(balanceModel);
balanceModel.id = balanceModelId; balanceModel.id = balanceModelId;

View File

@@ -43,6 +43,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Nito.AsyncEx; using Nito.AsyncEx;
using NLog; using NLog;
using SKIT.FlurlHttpClient.Wechat.Api; using SKIT.FlurlHttpClient.Wechat.Api;
@@ -1218,6 +1219,13 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{ {
var jm = new WebApiCallBack(); var jm = new WebApiCallBack();
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)
{
try
{
if (string.IsNullOrEmpty(entity.ids)) if (string.IsNullOrEmpty(entity.ids))
{ {
jm.code = 13100; jm.code = 13100;
@@ -1234,9 +1242,23 @@ namespace CoreCms.Net.Web.WebApi.Controllers
jm.msg = GlobalErrorCodeVars.Code10051; jm.msg = GlobalErrorCodeVars.Code10051;
} }
//生成支付单,并发起支付 //生成支付单,并发起支付
jm = await _billPaymentsServices.Pay(entity.ids, entity.payment_code, _user.ID, entity.payment_type, jm = await _billPaymentsServices.Pay(entity.ids, entity.payment_code, _user.ID, entity.payment_type, entity.@params);
entity.@params); }
//NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.RedisMessageQueue, "支付",JsonConvert.SerializeObject(jm)); 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
{
jm.msg = "当前请求太频繁_请稍后再试";
}
return jm; return jm;
} }