From f9fb3e891682473f1979310924bbd6bd13a7a0a6 Mon Sep 17 00:00:00 2001 From: manghe Date: Fri, 21 Mar 2025 19:14:08 +0000 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98-=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E4=B8=AA=E5=95=86=E6=88=B7=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Config.php | 23 +++- app/admin/view/Config/systemconfig.html | 48 +++++++ .../view/Config/wechatofficialaccount.html | 13 +- app/admin/view/Config/weixinpay.html | 127 ++++++++++++++++-- app/api/controller/AliNotify.php | 9 +- app/api/controller/Notify.php | 10 +- app/api/controller/Pay.php | 35 +++-- app/common/helper/WxPayHelper.php | 88 ++++++++++++ app/common/server/WechatOfficialAccount.php | 13 +- app/common/server/WechatRefund.php | 31 +++-- app/common/server/Wx.php | 16 ++- 11 files changed, 362 insertions(+), 51 deletions(-) create mode 100644 app/common/helper/WxPayHelper.php diff --git a/app/admin/controller/Config.php b/app/admin/controller/Config.php index 7ac7013..a14bce0 100755 --- a/app/admin/controller/Config.php +++ b/app/admin/controller/Config.php @@ -20,8 +20,25 @@ class Config extends Base //微信支付 public function weixinpay(Request $request) { - $config = getConfig('weixinpay'); - View::assign("key", "weixinpay"); + $config = getConfig('weixinpay_setting'); + if(empty($config)) { + // 兼容旧数据,如果新配置为空,则尝试获取旧配置并转换格式 + $old_config = getConfig('weixinpay'); + if(!empty($old_config)) { + $config = [ + 'merchants' => [ + [ + 'name' => '默认商户', + 'mch_id' => $old_config['mch_id'] ?? '', + 'keys' => $old_config['keys'] ?? '', + 'weight' => 1 + ] + ] + ]; + } + } + + View::assign("key", "weixinpay_setting"); View::assign("data", $config); return View::fetch('Config/weixinpay'); } @@ -60,10 +77,12 @@ class Config extends Base $config = getConfig('systemconfig'); $user_config = getConfig('user_config'); $wechat_setting = getConfig('wechat_setting'); + $wechatofficialaccount_setting = getConfig('wechatofficialaccount_setting'); View::assign("key", "systemconfig"); View::assign("data", $config); View::assign("user_config", $user_config); View::assign("wechat_setting", $wechat_setting); + View::assign("wechatofficialaccount_setting", $wechatofficialaccount_setting); return View::fetch('Config/systemconfig'); } diff --git a/app/admin/view/Config/systemconfig.html b/app/admin/view/Config/systemconfig.html index 1ac6677..30c63a2 100755 --- a/app/admin/view/Config/systemconfig.html +++ b/app/admin/view/Config/systemconfig.html @@ -125,6 +125,34 @@ + +
+
+
微信公众号设置
+
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+
+
+
@@ -219,6 +247,26 @@ return false; }); + // 微信公众号设置表单提交 + form.on('submit(wechatofficialaccount-setting-form)', function (data) { + var field = data.field; + + // 提交到后台 + $.ajax({ + url: '{:url("/admin/update")}', + type: 'post', + data: field, + success: function (res) { + if (res.status) { + layer.msg(res.msg, { icon: 1 }); + } else { + layer.msg(res.msg, { icon: 2 }); + } + } + }); + return false; + }); + // UID类型切换事件 form.on('radio(uid_type)', function (data) { var value = this.value; diff --git a/app/admin/view/Config/wechatofficialaccount.html b/app/admin/view/Config/wechatofficialaccount.html index f75912b..fa08314 100755 --- a/app/admin/view/Config/wechatofficialaccount.html +++ b/app/admin/view/Config/wechatofficialaccount.html @@ -13,15 +13,8 @@
- -
- -
-
-
- -
- +
+ 微信公众号Appid和AppSecret已移至系统设置页面,请在那里设置
@@ -37,6 +30,8 @@
+ +
diff --git a/app/admin/view/Config/weixinpay.html b/app/admin/view/Config/weixinpay.html index de7a824..c3b109e 100755 --- a/app/admin/view/Config/weixinpay.html +++ b/app/admin/view/Config/weixinpay.html @@ -17,23 +17,74 @@ 微信Appid和AppSecret已移至系统设置页面,请在那里设置
-
- -
- + + +
+ {if isset($data.merchants) && is_array($data.merchants)} + {foreach $data.merchants as $index => $merchant} +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
-
-
- -
- + {/foreach} + {else} + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ {/if}
- - - +
+
@@ -51,6 +102,58 @@ layui.use(['layer','form','upload','element'], function(){ var $ = layui.$; + // 添加商户按钮点击事件 + $('#add-merchant').on('click', function() { + var index = $('.merchant-item').length; + var newItemHtml = ` +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+ `; + $('#merchants-container').append(newItemHtml); + }); + + // 删除商户按钮点击事件(使用事件委托) + $(document).on('click', '.remove-merchant', function() { + if ($('.merchant-item').length <= 1) { + layer.msg('至少保留一个商户信息', {icon: 2}); + } else { + $(this).closest('.merchant-item').remove(); + // 重新排序索引 + $('.merchant-item').each(function(idx) { + $(this).find('input').each(function() { + var name = $(this).attr('name'); + if (name) { + $(this).attr('name', name.replace(/merchants\[\d+\]/, 'merchants[' + idx + ']')); + } + }); + }); + } + }); }); function check(){ diff --git a/app/api/controller/AliNotify.php b/app/api/controller/AliNotify.php index 96e670a..09fb732 100755 --- a/app/api/controller/AliNotify.php +++ b/app/api/controller/AliNotify.php @@ -40,10 +40,15 @@ class AliNotify extends Base public function initialize() { + // 获取微信支付配置 + $wxpayConfig = \app\common\helper\WxPayHelper::getWxPayConfig(); + $merchant = $wxpayConfig['merchant']; - $secret_key = getConfig('weixinpay')['keys']; + // 使用随机商户的密钥 + static::$secretKey = $merchant['keys']; + + // 获取公众号密钥 static::$secretKeyAccount = getConfig('wechatofficialaccount')['keys']; - static::$secretKey = $secret_key;//秘钥 } public function ceshi() diff --git a/app/api/controller/Notify.php b/app/api/controller/Notify.php index 5d2c6a1..6fb5ac3 100755 --- a/app/api/controller/Notify.php +++ b/app/api/controller/Notify.php @@ -40,9 +40,15 @@ class Notify extends Base public function initialize() { - $secret_key = getConfig('weixinpay')['keys']; + // 获取微信支付配置 + $wxpayConfig = \app\common\helper\WxPayHelper::getWxPayConfig(); + $merchant = $wxpayConfig['merchant']; + + // 使用随机商户的密钥 + static::$secretKey = $merchant['keys']; + + // 获取公众号密钥 static::$secretKeyAccount = getConfig('wechatofficialaccount')['keys']; - static::$secretKey = $secret_key;//秘钥 } public function ceshi() diff --git a/app/api/controller/Pay.php b/app/api/controller/Pay.php index c3a8a97..f833770 100755 --- a/app/api/controller/Pay.php +++ b/app/api/controller/Pay.php @@ -6,6 +6,7 @@ namespace app\api\controller; use app\common\model\Order as OrderModel; use app\common\model\ProductOrder; use app\common\model\UserRecharge; +use app\common\helper\WxPayHelper; use think\App; use think\facade\Db; @@ -22,23 +23,39 @@ class Pay extends Base { // 获取系统微信设置 $wechat_setting = getConfig('wechat_setting'); + $wechatofficialaccount_setting = getConfig('wechatofficialaccount_setting'); if ($this->ish5()) { + // 公众号配置不变 $config = getConfig('wechatofficialaccount'); - // 公众号使用自己的appid - $this->appid = $config['appid']; - $this->merchant = $config['mch_id']; - $this->secretKey = $config['keys']; - } else { - $config = getConfig('weixinpay'); - // 如果系统设置中存在微信配置,则优先使用 - if (!empty($wechat_setting) && !empty($wechat_setting['appid']) && !empty($wechat_setting['appSecret'])) { - $this->appid = $wechat_setting['appid']; + // 如果系统设置中存在微信公众号配置,则优先使用 + if (!empty($wechatofficialaccount_setting) && !empty($wechatofficialaccount_setting['appid']) && !empty($wechatofficialaccount_setting['appSecret'])) { + $this->appid = $wechatofficialaccount_setting['appid']; } else { + // 公众号使用自己的appid $this->appid = $config['appid']; } $this->merchant = $config['mch_id']; $this->secretKey = $config['keys']; + } else { + // 小程序使用随机商户 + $wxpayConfig = WxPayHelper::getWxPayConfig(); + if (!empty($wxpayConfig['merchant'])) { + $this->appid = $wxpayConfig['appid']; + $this->merchant = $wxpayConfig['merchant']['mch_id']; + $this->secretKey = $wxpayConfig['merchant']['keys']; + } else { + // 如果没有获取到商户信息,则使用旧方式 + $config = getConfig('weixinpay'); + // 如果系统设置中存在微信配置,则优先使用 + if (!empty($wechat_setting) && !empty($wechat_setting['appid']) && !empty($wechat_setting['appSecret'])) { + $this->appid = $wechat_setting['appid']; + } else { + $this->appid = $config['appid']; + } + $this->merchant = $config['mch_id']; + $this->secretKey = $config['keys']; + } } $this->noticeurl = request()->domain() . '/api/notify/order_notify';#订单回调URL diff --git a/app/common/helper/WxPayHelper.php b/app/common/helper/WxPayHelper.php new file mode 100644 index 0000000..c62e5c1 --- /dev/null +++ b/app/common/helper/WxPayHelper.php @@ -0,0 +1,88 @@ + '默认商户', + 'mch_id' => $old_config['mch_id'], + 'keys' => $old_config['keys'], + 'weight' => 1 + ]; + } + } + + // 获取微信AppID + $appid = ''; + if (!empty($wechat_setting) && !empty($wechat_setting['appid'])) { + $appid = $wechat_setting['appid']; + } else if (!empty($old_config) && !empty($old_config['appid'])) { + $appid = $old_config['appid']; + } + + return [ + 'merchant' => $merchant, + 'appid' => $appid + ]; + } +} \ No newline at end of file diff --git a/app/common/server/WechatOfficialAccount.php b/app/common/server/WechatOfficialAccount.php index d9dd62c..9c07479 100755 --- a/app/common/server/WechatOfficialAccount.php +++ b/app/common/server/WechatOfficialAccount.php @@ -15,8 +15,17 @@ class WechatOfficialAccount extends MyController public function initialize() { $weixinpay = getConfig("wechatofficialaccount"); - self::$wx_appid = $weixinpay['appid']; - self::$wx_secret = $weixinpay['appSecret']; + $wechatofficialaccount_setting = getConfig("wechatofficialaccount_setting"); + + // 如果系统设置中存在微信公众号配置,则优先使用 + if (!empty($wechatofficialaccount_setting) && !empty($wechatofficialaccount_setting['appid']) && !empty($wechatofficialaccount_setting['appSecret'])) { + self::$wx_appid = $wechatofficialaccount_setting['appid']; + self::$wx_secret = $wechatofficialaccount_setting['appSecret']; + } else { + self::$wx_appid = $weixinpay['appid']; + self::$wx_secret = $weixinpay['appSecret']; + } + self::$mch = $weixinpay['mch_id']; self::$key = $weixinpay['keys']; } diff --git a/app/common/server/WechatRefund.php b/app/common/server/WechatRefund.php index a8716a5..db49f2f 100755 --- a/app/common/server/WechatRefund.php +++ b/app/common/server/WechatRefund.php @@ -3,6 +3,7 @@ namespace app\common\server; use app\MyController; +use app\common\helper\WxPayHelper; class WechatRefund extends MyController @@ -14,10 +15,11 @@ class WechatRefund extends MyController */ public function OrderRefund($info) { - $weixinpay = getConfig("weixinpay"); - $appid = $weixinpay['appid']; - $merchant = $weixinpay['mch_id']; - + // 使用WxPayHelper获取随机商户配置 + $wxpayConfig = WxPayHelper::getWxPayConfig(); + $appid = $wxpayConfig['appid']; + $merchant = $wxpayConfig['merchant']['mch_id']; + $params['appid'] = $appid; $params['mch_id'] = $merchant; $params['nonce_str'] = $this->genRandomString(); @@ -45,8 +47,10 @@ class WechatRefund extends MyController */ public function MakeSign($params) { - $weixinpay = getConfig("weixinpay"); - $secretKey = $weixinpay['keys']; + // 使用WxPayHelper获取随机商户配置 + $wxpayConfig = WxPayHelper::getWxPayConfig(); + $secretKey = $wxpayConfig['merchant']['keys']; + //签名步骤一:按字典序排序数组参数 ksort($params); $string = $this->ToUrlParams($params); @@ -153,8 +157,19 @@ class WechatRefund extends MyController private function postXmlCurl($xml, $url, $second = 30) { $path = app()->getRootPath(); - $ssl_cert = $path . 'app/common/ssl/apiclient_cert.pem'; - $ssl_key = $path . 'app/common/ssl/apiclient_key.pem'; + + // 获取商户配置 + $wxpayConfig = WxPayHelper::getWxPayConfig(); + $merchant = $wxpayConfig['merchant']; + + // 优先使用商户配置中的证书路径,如果未设置则使用默认路径 + $ssl_cert = isset($merchant['ssl_cert']) && !empty($merchant['ssl_cert']) + ? $merchant['ssl_cert'] + : $path . 'app/common/ssl/apiclient_cert.pem'; + + $ssl_key = isset($merchant['ssl_key']) && !empty($merchant['ssl_key']) + ? $merchant['ssl_key'] + : $path . 'app/common/ssl/apiclient_key.pem'; $ch = curl_init(); //设置超时 diff --git a/app/common/server/Wx.php b/app/common/server/Wx.php index 220738f..b76253a 100755 --- a/app/common/server/Wx.php +++ b/app/common/server/Wx.php @@ -14,20 +14,26 @@ class Wx extends MyController public function initialize() { - $weixinpay = getConfig("weixinpay"); $wechat_setting = getConfig("wechat_setting"); + // 获取微信支付配置 + $wxpayConfig = \app\common\helper\WxPayHelper::getWxPayConfig(); + // 如果系统设置中存在微信配置,则优先使用 if (!empty($wechat_setting) && !empty($wechat_setting['appid']) && !empty($wechat_setting['appSecret'])) { self::$wx_appid = $wechat_setting['appid']; self::$wx_secret = $wechat_setting['appSecret']; } else { - self::$wx_appid = $weixinpay['appid']; - self::$wx_secret = $weixinpay['appSecret']; + self::$wx_appid = $wxpayConfig['appid']; + // 从旧配置或商户配置中获取appSecret + $weixinpay = getConfig("weixinpay"); + self::$wx_secret = !empty($weixinpay['appSecret']) ? $weixinpay['appSecret'] : ''; } - self::$mch = $weixinpay['mch_id']; - self::$key = $weixinpay['keys']; + // 使用商户信息 + $merchant = $wxpayConfig['merchant']; + self::$mch = $merchant['mch_id']; + self::$key = $merchant['keys']; } /**