From 481dbe3ce542df80cffb414f9649e7789b40cd8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=81=B0=E7=81=B0?= Date: Sat, 15 Oct 2022 00:04:59 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=94=AF=E4=BB=98api=E5=A2=9E=E5=8A=A0redis=E9=94=81?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=87=BA=E7=8E=B0=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E5=8F=AF=E8=83=BD=E5=AD=98=E5=9C=A8=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E6=94=AF=E4=BB=98=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../User/CoreCmsUserBalanceServices.cs | 8 +-- .../Controllers/UserController.cs | 52 +++++++++++++------ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/CoreCms.Net.Services/User/CoreCmsUserBalanceServices.cs b/CoreCms.Net.Services/User/CoreCmsUserBalanceServices.cs index 78eca58e..8f89dd0b 100644 --- a/CoreCms.Net.Services/User/CoreCmsUserBalanceServices.cs +++ b/CoreCms.Net.Services/User/CoreCmsUserBalanceServices.cs @@ -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; diff --git a/CoreCms.Net.Web.WebApi/Controllers/UserController.cs b/CoreCms.Net.Web.WebApi/Controllers/UserController.cs index 4149764b..55c02c91 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/UserController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/UserController.cs @@ -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; }