EasyWeChat.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Merlin
  5. * Date: 2021/1/26
  6. * Time: 11:07
  7. */
  8. namespace app\common\facades;
  9. use app\common\helpers\Url;
  10. use app\common\models\AccountWechats;
  11. use app\framework\EasyWechat\MiniProgram\AppCode;
  12. use app\framework\EasyWechat\OfficialAccount\Material;
  13. use app\framework\EasyWechat\Payment\Order;
  14. use app\framework\EasyWechat\Payment\TransferV3;
  15. use app\framework\EasyWechat\Work\Client;
  16. use app\framework\EasyWechat\Work\ContactWayClient;
  17. use EasyWeChat\MiniProgram\Application as MiniProgram;
  18. use EasyWeChat\OfficialAccount\Application as OfficialAccount;
  19. use EasyWeChat\OpenPlatform\Application as OpenPlatform;
  20. use EasyWeChat\Payment\Application as Payment;
  21. use EasyWeChat\Work\Application as Work;
  22. use Illuminate\Support\Facades\Facade;
  23. class EasyWeChat extends Facade
  24. {
  25. /**
  26. * 默认为 Server.
  27. *
  28. * @return string
  29. */
  30. public static function getFacadeAccessor()
  31. {
  32. return new OfficialAccount();
  33. }
  34. /**
  35. * @return \EasyWeChat\OfficialAccount\Application
  36. */
  37. public static function officialAccount(array $config = [])
  38. {
  39. //独立版
  40. if (config('APP_Framework') == 'platform') {
  41. $default_config = [
  42. 'app_id' => \Setting::get('plugin.wechat')['app_id'],// AppID
  43. 'secret' => \Setting::get('plugin.wechat')['app_secret'], // AppSecret
  44. 'token' => \Setting::get('plugin.wechat.token'),// Token
  45. 'aes_key' => \Setting::get('plugin.wechat.aes_key'),// EncodingAESKey,安全模式与兼容模式下请一定要填写!!!
  46. ];
  47. }
  48. //微擎版
  49. else{
  50. $account = AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid);
  51. $default_config = [
  52. 'app_id' => $account['key'], // AppID
  53. 'secret' => $account['secret'], // AppSecret
  54. 'token' => $account['token'], // Token
  55. 'aes_key' => $account['encodingaeskey'], // EncodingAESKey,兼容与安全模式下请一定要填写!!!
  56. ];
  57. }
  58. $OfficialAccount = new OfficialAccount(array_merge($default_config,$config));
  59. $OfficialAccount->rebind('material',function ($app) {
  60. return new Material($app);
  61. });
  62. return $OfficialAccount;
  63. }
  64. /**
  65. * @return \EasyWeChat\Work\Application
  66. */
  67. public static function work(array $config = [])
  68. {
  69. $Work = new Work($config);
  70. $Work->rebind('external_contact',function ($app) {
  71. return new Client($app);
  72. });
  73. $Work->rebind('contact_way',function ($app) {
  74. return new ContactWayClient($app);
  75. });
  76. $Work->rebind('user', function ($app) {
  77. return new \app\framework\EasyWechat\Work\User($app);
  78. });
  79. $Work->rebind('department', function ($app) {
  80. return new \app\framework\EasyWechat\Work\Department($app);
  81. });
  82. return $Work;
  83. }
  84. /**
  85. * @return \EasyWeChat\Payment\Application
  86. */
  87. public static function payment(array $config = [])
  88. {
  89. $pay = \Setting::get('shop.pay');
  90. $default_config = [
  91. 'app_id' => $pay['weixin_appid'],
  92. 'mch_id' => $pay['weixin_mchid'],
  93. 'key' => $pay['weixin_apisecret'],
  94. 'cert_path' => $pay['weixin_cert'],
  95. 'key_path' => $pay['weixin_key'],
  96. 'notify_url' => Url::shopSchemeUrl('payment/wechat/notifyUrl.php'),
  97. ];
  98. $Payment = new Payment(array_merge($default_config,$config));
  99. $Payment->rebind('order',function ($app) {
  100. return new Order($app);
  101. });
  102. $Payment->rebind('transfer_v3',function ($app) {
  103. return new TransferV3($app);
  104. });
  105. return $Payment;
  106. }
  107. /**
  108. * @return \EasyWeChat\MiniProgram\Application
  109. */
  110. public static function miniProgram(array $config = [])
  111. {
  112. $MiniProgram = new MiniProgram($config);
  113. $MiniProgram->rebind('app_code',function ($app) {
  114. return new AppCode($app);
  115. });
  116. return $MiniProgram;
  117. }
  118. /**
  119. * @return \EasyWeChat\OpenPlatform\Application
  120. */
  121. public static function openPlatform(array $config = [])
  122. {
  123. return new OpenPlatform($config);
  124. }
  125. }