【优化】优化首页优惠券组件样式效果,优化优惠券领取列表样式效果。领取优惠券增加loading遮罩层效果。领取优惠券增加redis锁。

This commit is contained in:
大灰灰
2022-10-21 06:18:29 +08:00
parent adfbbc783e
commit c88951e1f3
4 changed files with 95 additions and 65 deletions

View File

@@ -12,6 +12,7 @@ using System;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
@@ -33,18 +34,19 @@ namespace CoreCms.Net.Web.WebApi.Controllers
private readonly IHttpContextUser _user;
private readonly ICoreCmsCouponServices _couponServices;
private readonly ICoreCmsPromotionServices _promotionServices;
private readonly IRedisOperationRepository _redisOperationRepository;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="user"></param>
/// <param name="couponServices"></param>
/// <param name="promotionServices"></param>
public CouponController(IHttpContextUser user
, ICoreCmsCouponServices couponServices, ICoreCmsPromotionServices promotionServices)
, ICoreCmsCouponServices couponServices, ICoreCmsPromotionServices promotionServices, IRedisOperationRepository redisOperationRepository)
{
_user = user;
_couponServices = couponServices;
_promotionServices = promotionServices;
_redisOperationRepository = redisOperationRepository;
}
//公共接口====================================================================================================
@@ -139,37 +141,62 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{
var jm = new WebApiCallBack();
if (entity.id == 0)
var lockKey = "LOCK_GetCoupon:user_" + entity.id;
var lockHolder = Guid.NewGuid().ToString("N"); //锁持有者
var redisUserLock = await _redisOperationRepository.LockTakeAsync(lockKey, lockHolder, TimeSpan.FromSeconds(5));
if (redisUserLock)
{
jm.msg = GlobalErrorCodeVars.Code15006;
return jm;
}
//判断优惠券是否可以领取?
var promotionModel = await _promotionServices.ReceiveCoupon(entity.id);
if (promotionModel.status == false)
{
return promotionModel;
}
var promotion = (CoreCmsPromotion)promotionModel.data;
if (promotion == null)
{
jm.msg = GlobalErrorCodeVars.Code15019;
return jm;
}
if (promotion.maxNums > 0)
{
//判断用户是否已领取?领取次数
var couponResult = await _couponServices.GetMyCoupon(_user.ID, entity.id, "all", 1, 9999);
if (couponResult.status && couponResult.code >= promotion.maxNums)
try
{
jm.msg = GlobalErrorCodeVars.Code15018;
if (entity.id == 0)
{
jm.msg = GlobalErrorCodeVars.Code15006;
return jm;
}
//判断优惠券是否可以领取?
var promotionModel = await _promotionServices.ReceiveCoupon(entity.id);
if (promotionModel.status == false)
{
return promotionModel;
}
var promotion = (CoreCmsPromotion)promotionModel.data;
if (promotion == null)
{
jm.msg = GlobalErrorCodeVars.Code15019;
return jm;
}
if (promotion.maxNums > 0)
{
//判断用户是否已领取?领取次数
var couponResult = await _couponServices.GetMyCoupon(_user.ID, entity.id, "all", 1, 9999);
if (couponResult.status && couponResult.code >= promotion.maxNums)
{
jm.msg = GlobalErrorCodeVars.Code15018;
return jm;
}
}
jm = await _couponServices.AddData(_user.ID, entity.id, promotion);
jm.otherData = promotionModel;
return jm;
}
catch (Exception e)
{
jm.msg = "数据处理异常";
jm.otherData = e;
}
finally
{
await _redisOperationRepository.LockReleaseAsync(lockKey, lockHolder);
}
}
jm = await _couponServices.AddData(_user.ID, entity.id, promotion);
jm.otherData = promotionModel;
else
{
jm.msg = "当前请求太频繁_请稍后再试";
}
return jm;
}
#endregion