From 66135678d3ba2fd5975074b33e763ae6aa67191e Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 12 Feb 2026 01:00:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=95=86=E5=9F=8E?= =?UTF-8?q?=E6=AF=8F=E6=97=A5=E8=B4=AD=E4=B9=B0=E6=AC=A1=E6=95=B0=E9=99=90?= =?UTF-8?q?=E5=88=B6=E5=8A=9F=E8=83=BD=20(exchange=5Ftimes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HoneyBox.Core/Services/GoodsService.cs | 40 +------------ .../HoneyBox.Core/Services/OrderService.cs | 57 +++++++++++++++++++ server/php/app/admin/view/Config/base.html | 2 +- .../view/QyLevel/qy_level_jiang_list.html | 6 +- 4 files changed, 63 insertions(+), 42 deletions(-) diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/GoodsService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/GoodsService.cs index b0b4c8ab..e8026a45 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/GoodsService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/GoodsService.cs @@ -35,8 +35,7 @@ public class GoodsService : IGoodsService _logger = logger; } - // 缓存过期时间 - private static readonly TimeSpan GoodsListCacheExpiry = TimeSpan.FromSeconds(30); + // 缓存过期时间(商品详情仍使用缓存) private static readonly TimeSpan GoodsDetailCacheExpiry = TimeSpan.FromMinutes(5); /// @@ -52,30 +51,7 @@ public class GoodsService : IGoodsService pageSize = 999; } - // 尝试从缓存获取 - 缓存键格式与PHP一致: {type}_{userId}_{page} - var cacheKey = $"{type}_{userId}_{page}"; - var cachedData = await _cacheService.GetGoodsListCacheAsync(cacheKey); - if (!string.IsNullOrEmpty(cachedData)) - { - try - { - var cachedResult = JsonSerializer.Deserialize>(cachedData); - if (cachedResult != null) - { - // 更新参与次数(从缓存获取最新值) - foreach (var item in cachedResult.Data) - { - item.JoinCount = await GetJoinCountAsync(item.Id, item.Type); - } - _logger.LogDebug("商品列表从缓存获取: CacheKey={CacheKey}", cacheKey); - return cachedResult; - } - } - catch (JsonException ex) - { - _logger.LogWarning(ex, "商品列表缓存反序列化失败: CacheKey={CacheKey}", cacheKey); - } - } + // 商品列表不使用缓存,确保库存数据实时更新 // 构建基础查询 - 排除已删除的商品 var query = _dbContext.Goods @@ -205,18 +181,6 @@ public class GoodsService : IGoodsService LastPage = lastPage }; - // 写入缓存 - try - { - var jsonData = JsonSerializer.Serialize(response); - await _cacheService.SetGoodsListCacheAsync(cacheKey, jsonData, GoodsListCacheExpiry); - _logger.LogDebug("商品列表已缓存: CacheKey={CacheKey}", cacheKey); - } - catch (Exception ex) - { - _logger.LogWarning(ex, "商品列表缓存写入失败: CacheKey={CacheKey}", cacheKey); - } - return response; } diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/OrderService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/OrderService.cs index 4848cd5c..827a3442 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/OrderService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/OrderService.cs @@ -573,6 +573,40 @@ public class OrderService : IOrderService return 0; } + /// + /// 获取商城每日购买次数限制 + /// + private async Task GetExchangeTimesLimitAsync() + { + var config = await _dbContext.Configs + .Where(c => c.ConfigKey == "app_setting") + .Select(c => c.ConfigValue) + .FirstOrDefaultAsync(); + + if (string.IsNullOrEmpty(config)) + { + return 0; + } + + try + { + var configObj = System.Text.Json.JsonSerializer.Deserialize>(config); + if (configObj != null && configObj.TryGetValue("exchange_times", out var limitObj)) + { + if (int.TryParse(limitObj?.ToString(), out var limit)) + { + return limit; + } + } + } + catch + { + // 解析失败返回0 + } + + return 0; + } + /// /// 格式化图片URL /// @@ -2276,6 +2310,29 @@ public class OrderService : IOrderService } } + // 5.1 验证全局商城每日购买次数限制 + var exchangeTimes = await GetExchangeTimesLimitAsync(); + if (exchangeTimes > 0) + { + var todayStart2 = DateTime.Today; + var todayStartTimestamp2 = new DateTimeOffset(todayStart2).ToUnixTimeSeconds(); + var todayEndTimestamp2 = new DateTimeOffset(todayStart2.AddDays(1)).ToUnixTimeSeconds(); + + // 统计用户今日在商城(type=10)的购买次数 + var todayMallBuyCount = await _dbContext.Orders + .Where(o => o.UserId == userId + && o.OrderType == 10 + && o.Status == 1 + && o.Addtime >= todayStartTimestamp2 + && o.Addtime < todayEndTimestamp2) + .CountAsync(); + + if (todayMallBuyCount >= exchangeTimes) + { + throw new InvalidOperationException($"商城每日限购{exchangeTimes}次,今日已购买{todayMallBuyCount}次"); + } + } + // 6. 验证抽奖次数 if (request.PrizeNum != 1 && request.PrizeNum != 3 && request.PrizeNum != 5 && request.PrizeNum != 10 && request.PrizeNum != 50) { diff --git a/server/php/app/admin/view/Config/base.html b/server/php/app/admin/view/Config/base.html index e542c49a..9895fde7 100644 --- a/server/php/app/admin/view/Config/base.html +++ b/server/php/app/admin/view/Config/base.html @@ -39,7 +39,7 @@ 购买链接 -
+