diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs index ba9e3f02..6a7a7edd 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs @@ -20,6 +20,7 @@ public class WechatService : IWechatService private readonly WechatPaySettings _wechatPaySettings; private readonly IRedisService _redisService; private readonly HoneyBoxDbContext _dbContext; + private readonly IWechatPayConfigService? _wechatPayConfigService; // 微信API端点 private const string WechatCodeToSessionUrl = "https://api.weixin.qq.com/sns/jscode2session"; @@ -35,13 +36,15 @@ public class WechatService : IWechatService ILogger logger, IOptions wechatPaySettings, IRedisService redisService, - HoneyBoxDbContext dbContext) + HoneyBoxDbContext dbContext, + IWechatPayConfigService? wechatPayConfigService = null) { _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _wechatPaySettings = wechatPaySettings?.Value ?? throw new ArgumentNullException(nameof(wechatPaySettings)); _redisService = redisService ?? throw new ArgumentNullException(nameof(redisService)); _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); + _wechatPayConfigService = wechatPayConfigService; } /// @@ -1110,8 +1113,19 @@ public class WechatService : IWechatService /// private async Task GetMerchantConfigByOrderNoAsync(string orderNo) { - // 直接使用现有的 GetMerchantConfigAsync 方法获取默认商户配置 - // 该方法已经从数据库读取配置并支持缓存 + // 优先使用 WechatPayConfigService(支持多商户配置) + if (_wechatPayConfigService != null) + { + var config = _wechatPayConfigService.GetMerchantByOrderNo(orderNo); + if (!string.IsNullOrEmpty(config.MchId)) + { + _logger.LogInformation("[发货] 使用 WechatPayConfigService 获取商户配置: MchId={MchId}, AppId={AppId}", + config.MchId, config.AppId); + return config; + } + } + + // 回退到旧的配置读取方式 return await GetMerchantConfigAsync(); } diff --git a/server/HoneyBox/src/HoneyBox.Infrastructure/Modules/ServiceModule.cs b/server/HoneyBox/src/HoneyBox.Infrastructure/Modules/ServiceModule.cs index 48d90849..6e7b5af6 100644 --- a/server/HoneyBox/src/HoneyBox.Infrastructure/Modules/ServiceModule.cs +++ b/server/HoneyBox/src/HoneyBox.Infrastructure/Modules/ServiceModule.cs @@ -29,7 +29,8 @@ public class ServiceModule : Module var wechatPaySettings = c.Resolve>(); var redisService = c.Resolve(); var dbContext = c.Resolve(); - return new WechatService(httpClientFactory.CreateClient(), logger, wechatPaySettings, redisService, dbContext); + var configService = c.ResolveOptional(); + return new WechatService(httpClientFactory.CreateClient(), logger, wechatPaySettings, redisService, dbContext, configService); }).As().InstancePerLifetimeScope(); // 注册 IP 地理位置服务