WeChatConfigService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop100%开源免费商用电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
  10. // | 访问官网:https://www.likemarket.net
  11. // | 访问社区:https://home.likemarket.net
  12. // | 访问手册:http://doc.likemarket.net
  13. // | 微信公众号:好象科技
  14. // | 好象科技开发团队 版权所有 拥有最终解释权
  15. // +----------------------------------------------------------------------
  16. // | Author: LikeShopTeam
  17. // +----------------------------------------------------------------------
  18. namespace app\common\service;
  19. use app\common\enum\PayEnum;
  20. use app\common\enum\UserTerminalEnum;
  21. use app\common\model\PayConfig;
  22. /**
  23. * 微信配置类
  24. * Class WeChatConfigService
  25. * @package app\common\service
  26. */
  27. class WeChatConfigService
  28. {
  29. /**
  30. * 获取小程序配置
  31. * @return array
  32. */
  33. public static function getMnpConfig()
  34. {
  35. $config = [
  36. 'app_id' => ConfigService::get('mini_program', 'app_id',''),
  37. 'secret' => ConfigService::get('mini_program', 'app_secret','' ),
  38. 'mch_id' => ConfigService::get('mini_program', 'mch_id'),
  39. 'key' => ConfigService::get('mini_program', 'key'),
  40. 'response_type' => 'array',
  41. 'log' => [
  42. 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
  43. 'channels' => [
  44. // 测试环境
  45. 'dev' => [
  46. 'driver' => 'daily',
  47. 'path' => app()->getRuntimePath() . 'easywechat.log',
  48. 'level' => 'debug',
  49. ],
  50. // 生产环境
  51. 'prod' => [
  52. 'driver' => 'daily',
  53. 'path' => app()->getRuntimePath() . 'easywechat.log',
  54. 'level' => 'info',
  55. ],
  56. ],
  57. ],
  58. ];
  59. return $config;
  60. }
  61. /**
  62. * 获取微信公众号配置
  63. * @return array
  64. */
  65. public static function getOaConfig()
  66. {
  67. $config = [
  68. 'app_id' => ConfigService::get('official_account', 'app_id',''),
  69. 'secret' => ConfigService::get('official_account', 'app_secret',''),
  70. 'mch_id' => ConfigService::get('official_account', 'mch_id'),
  71. 'key' => ConfigService::get('official_account', 'key'),
  72. 'token' => ConfigService::get('official_account', 'token',''),
  73. 'response_type' => 'array',
  74. 'log' => [
  75. 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
  76. 'channels' => [
  77. // 测试环境
  78. 'dev' => [
  79. 'driver' => 'daily',
  80. 'path' => app()->getRuntimePath() . 'easywechat.log',
  81. 'level' => 'debug',
  82. ],
  83. // 生产环境
  84. 'prod' => [
  85. 'driver' => 'daily',
  86. 'path' => app()->getRuntimePath() . 'easywechat.log',
  87. 'level' => 'info',
  88. ],
  89. ],
  90. ],
  91. ];
  92. return $config;
  93. }
  94. /**
  95. * 获取微信开放平台应用配置
  96. * @return array
  97. */
  98. public static function getOpConfig()
  99. {
  100. $config = [
  101. 'app_id' => ConfigService::get('op', 'app_id'),
  102. 'secret' => ConfigService::get('op', 'secret'),
  103. 'response_type' => 'array',
  104. 'log' => [
  105. 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
  106. 'channels' => [
  107. // 测试环境
  108. 'dev' => [
  109. 'driver' => 'daily',
  110. 'path' => app()->getRuntimePath() . 'easywechat.log',
  111. 'level' => 'debug',
  112. ],
  113. // 生产环境
  114. 'prod' => [
  115. 'driver' => 'daily',
  116. 'path' => app()->getRuntimePath() . 'easywechat.log',
  117. 'level' => 'info',
  118. ],
  119. ],
  120. ],
  121. ];
  122. return $config;
  123. }
  124. //根据用户客户端不同获取不同的微信配置
  125. public static function getWechatConfigByTerminal($terminal)
  126. {
  127. switch ($terminal) {
  128. case UserTerminalEnum::WECHAT_MMP:
  129. $appid = ConfigService::get('mini_program', 'app_id');
  130. $secret = ConfigService::get('mini_program', 'app_secret');
  131. $notify_url = (string) url('pay/notifyMnp', [], false, true);
  132. break;
  133. case UserTerminalEnum::WECHAT_OA:
  134. case UserTerminalEnum::PC:
  135. case UserTerminalEnum::H5:
  136. $appid = ConfigService::get('official_account', 'app_id');
  137. $secret = ConfigService::get('official_account', 'app_secret');
  138. $notify_url = (string) url('pay/notifyOa', [], false, true);
  139. break;
  140. case UserTerminalEnum::ANDROID:
  141. case UserTerminalEnum::IOS:
  142. $appid = ConfigService::get('open_platform', 'app_id');
  143. $secret = ConfigService::get('open_platform', 'app_secret');
  144. $notify_url = (string) url('pay/notifyApp', [], false, true);
  145. break;
  146. default:
  147. $appid = '';
  148. $secret = '';
  149. }
  150. $pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->findOrEmpty()->toArray();
  151. //写入文件
  152. $save_path = app()->getRuntimePath() . 'certificates';
  153. file_exists($save_path) || mkdir($save_path, 0775, true);
  154. $apiclient_cert = $pay['config']['apiclient_cert'] ?? '';
  155. $apiclient_key = $pay['config']['apiclient_key'] ?? '';
  156. $wechat_public_cert = $pay['config']['wechat_public_cert'] ?? '';
  157. $prefix = implode('-', [
  158. "{$save_path}/sid",
  159. $pay['config']['interface_version'] ?? '',
  160. $pay['config']['merchant_type'] ?? '',
  161. ]);
  162. $cert_path = $prefix . '-' . md5($apiclient_cert) . '.pem';
  163. $key_path = $prefix . '-' . md5($apiclient_key) . '.pem';
  164. $wechat_public_path = $prefix . '-' . md5($wechat_public_cert) . '.pem';
  165. file_exists($cert_path) || file_put_contents($cert_path, $apiclient_cert);
  166. file_exists($key_path) || file_put_contents($key_path, $apiclient_key);
  167. file_exists($wechat_public_path) || file_put_contents($wechat_public_path, $wechat_public_cert);
  168. $log = request()->isCli() ? '-command' : '';
  169. $config = [
  170. 'app_id' => $appid,
  171. 'secret' => $secret,
  172. 'mch_id' => $pay['config']['mch_id'] ?? '',
  173. 'key' => $pay['config']['pay_sign_key'] ?? '',
  174. 'cert_path' => $cert_path,
  175. 'key_path' => $key_path,
  176. 'interface_version' => $pay['config']['interface_version'] ?? '',
  177. 'merchant_type' => $pay['config']['merchant_type'] ?? '',
  178. 'response_type' => 'array',
  179. 'wechat_public_serial' => $pay['config']['wechat_public_serial'] ?? '',
  180. 'wechat_public_cert' => $wechat_public_cert,
  181. 'log' => [
  182. 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
  183. 'channels' => [
  184. // 测试环境
  185. 'dev' => [
  186. 'driver' => 'daily',
  187. 'path' => app()->getRuntimePath() . 'easywechat' . $log . '.log',
  188. 'level' => 'debug',
  189. ],
  190. // 生产环境
  191. 'prod' => [
  192. 'driver' => 'daily',
  193. 'path' => app()->getRuntimePath() . 'easywechat' . $log . '.log',
  194. 'level' => 'info',
  195. ],
  196. ],
  197. ],
  198. 'notify_url' => $notify_url
  199. ];
  200. return $config;
  201. }
  202. }