Merge branch 'develop' into 'master'

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

See merge request jianweie/coreshoppro!7
This commit is contained in:
大灰灰
2022-10-14 16:05:37 +00:00
2 changed files with 41 additions and 19 deletions

View File

@@ -67,11 +67,9 @@ namespace CoreCms.Net.Services
{
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)
{
@@ -90,7 +88,6 @@ namespace CoreCms.Net.Services
if (type != (int)GlobalEnumVars.UserBalanceSourceTypes.Admin)
{
//后台充值或调不改绝对值
}
//如果是减余额的操作,还是加余额操作
if (type is (int)GlobalEnumVars.UserBalanceSourceTypes.Pay or (int)GlobalEnumVars.UserBalanceSourceTypes.Tocash)
@@ -117,6 +114,9 @@ namespace CoreCms.Net.Services
balanceModel.sourceId = sourceId;
balanceModel.memo = memo;
balanceModel.createTime = DateTime.Now;
_unitOfWork.BeginTran();
//增加记录
var balanceModelId = await _dal.InsertAsync(balanceModel);
balanceModel.id = balanceModelId;

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;
}