From 6f92062305ffd06d768700c62473227c283537fe Mon Sep 17 00:00:00 2001 From: gpu Date: Sat, 24 Jan 2026 14:30:45 +0800 Subject: [PATCH] 321 --- .../src/HoneyBox.Api/appsettings.json | 10 +-- .../HoneyBox.Core/Services/WechatService.cs | 88 +++++++++++++++++-- 2 files changed, 86 insertions(+), 12 deletions(-) diff --git a/server/HoneyBox/src/HoneyBox.Api/appsettings.json b/server/HoneyBox/src/HoneyBox.Api/appsettings.json index e35520ff..8460e4ee 100644 --- a/server/HoneyBox/src/HoneyBox.Api/appsettings.json +++ b/server/HoneyBox/src/HoneyBox.Api/appsettings.json @@ -4,15 +4,15 @@ "Redis": "192.168.195.15:6379,abortConnect=false,connectTimeout=5000" }, "WechatSettings": { - "AppId": "wx595ec949c6efd72b", - "AppSecret": "1677345a20450146cf4610a41b49794d" + "AppId": "", + "AppSecret": "" }, "WechatPaySettings": { "DefaultMerchant": { "Name": "默认商户", - "MchId": "1680394019", - "AppId": "wx595ec949c6efd72b", - "Key": "bad162066cbc9c34bb457e6997b7255b", + "MchId": "", + "AppId": "", + "Key": "", "OrderPrefix": "MYH", "Weight": 1, "NotifyUrl": "" diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs index 0ed2219b..bd13ba0f 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/WechatService.cs @@ -4,6 +4,7 @@ using HoneyBox.Model.Data; using HoneyBox.Model.Entities; using HoneyBox.Model.Models.Auth; using HoneyBox.Model.Models.Payment; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -423,8 +424,8 @@ public class WechatService : IWechatService }; } - // 获取商户配置 - var merchantConfig = GetMerchantConfig(); + // 获取商户配置(优先从数据库读取) + var merchantConfig = await GetMerchantConfigAsync(); if (merchantConfig == null) { _logger.LogError("[创建支付订单] 未找到商户配置"); @@ -455,7 +456,7 @@ public class WechatService : IWechatService // 构建统一下单参数 var unifiedOrderParams = new SortedDictionary { - { "appid", _wechatSettings.AppId }, + { "appid", merchantConfig.AppId }, { "mch_id", merchantConfig.MchId }, { "nonce_str", nonceStr }, { "body", title }, @@ -528,7 +529,7 @@ public class WechatService : IWechatService var payParams = new SortedDictionary { - { "appId", _wechatSettings.AppId }, + { "appId", merchantConfig.AppId }, { "timeStamp", timeStamp }, { "nonceStr", payNonceStr }, { "package", $"prepay_id={prepayId}" }, @@ -545,7 +546,7 @@ public class WechatService : IWechatService OrderNo = orderNo, Res = new NativePayParams { - AppId = _wechatSettings.AppId, + AppId = merchantConfig.AppId, TimeStamp = timeStamp, NonceStr = payNonceStr, Package = $"prepay_id={prepayId}", @@ -590,11 +591,84 @@ public class WechatService : IWechatService } /// - /// 获取商户配置 + /// 获取商户配置(优先从数据库读取) + /// + private async Task GetMerchantConfigAsync() + { + try + { + // 1. 尝试从数据库读取 weixinpay 配置 + var weixinpayConfig = await _dbContext.Configs + .Where(c => c.ConfigKey == "weixinpay") + .Select(c => c.ConfigValue) + .FirstOrDefaultAsync(); + + if (!string.IsNullOrEmpty(weixinpayConfig)) + { + var config = JsonSerializer.Deserialize(weixinpayConfig); + + var mchId = config.TryGetProperty("mch_id", out var mchIdProp) ? mchIdProp.GetString() : null; + var appId = config.TryGetProperty("appid", out var appIdProp) ? appIdProp.GetString() : null; + var key = config.TryGetProperty("keys", out var keysProp) ? keysProp.GetString() : null; + + if (!string.IsNullOrEmpty(mchId) && !string.IsNullOrEmpty(key)) + { + // 如果 weixinpay 中没有 appid,尝试从 wechat_setting 获取 + if (string.IsNullOrEmpty(appId)) + { + var wechatSettingConfig = await _dbContext.Configs + .Where(c => c.ConfigKey == "wechat_setting") + .Select(c => c.ConfigValue) + .FirstOrDefaultAsync(); + + if (!string.IsNullOrEmpty(wechatSettingConfig)) + { + var wechatSetting = JsonSerializer.Deserialize(wechatSettingConfig); + appId = wechatSetting.TryGetProperty("appid", out var wechatAppIdProp) ? wechatAppIdProp.GetString() : null; + } + } + + // 如果还是没有 appid,使用 appsettings.json 中的配置 + if (string.IsNullOrEmpty(appId)) + { + appId = _wechatSettings.AppId; + } + + _logger.LogInformation("[微信支付] 从数据库读取配置: MchId={MchId}, AppId={AppId}", mchId, appId); + return new WechatPayMerchantConfig + { + Name = "数据库配置", + MchId = mchId, + AppId = appId ?? string.Empty, + Key = key, + OrderPrefix = "MYH", + Weight = 1 + }; + } + } + + _logger.LogWarning("[微信支付] 数据库中未找到有效的微信支付配置,使用 appsettings.json 配置"); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "[微信支付] 从数据库读取配置失败,使用 appsettings.json 配置"); + } + + // 2. 回退到 appsettings.json 配置 + if (!string.IsNullOrEmpty(_wechatPaySettings.DefaultMerchant?.MchId)) + { + return _wechatPaySettings.DefaultMerchant; + } + + return _wechatPaySettings.Merchants.FirstOrDefault(); + } + + /// + /// 获取商户配置(同步版本,用于兼容) /// private WechatPayMerchantConfig? GetMerchantConfig() { - // 优先使用默认商户配置 + // 优先使用 appsettings.json 中的默认商户配置 if (!string.IsNullOrEmpty(_wechatPaySettings.DefaultMerchant?.MchId)) { return _wechatPaySettings.DefaultMerchant;