TransferServiceProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Module\Transfer;
  3. use App\Module\Transfer\Commands\TransferProcessCommand;
  4. use App\Module\Transfer\Commands\TransferStatsCommand;
  5. use App\Module\Transfer\Commands\TransferCallbackCommand;
  6. use App\Module\Transfer\Commands\TransferCleanCommand;
  7. use App\Module\Transfer\Commands\InsertTransferAdminMenuCommand;
  8. use App\Module\Transfer\Commands\FeeStatisticsCommand;
  9. use App\Module\Transfer\Commands\TestFeeTransferCommand;
  10. use Illuminate\Support\ServiceProvider;
  11. use Illuminate\Support\Facades\Schedule;
  12. /**
  13. * Transfer模块服务提供者
  14. */
  15. class TransferServiceProvider extends ServiceProvider
  16. {
  17. /**
  18. * 注册服务
  19. */
  20. public function register(): void
  21. {
  22. // 注册配置文件
  23. $this->mergeConfigFrom(
  24. __DIR__ . '/Config/transfer.php',
  25. 'transfer'
  26. );
  27. // 注册服务绑定
  28. $this->registerServices();
  29. }
  30. /**
  31. * 启动服务
  32. */
  33. public function boot(): void
  34. {
  35. // 发布配置文件
  36. $this->publishes([
  37. __DIR__ . '/Config/transfer.php' => config_path('transfer.php'),
  38. ], 'transfer-config');
  39. // 注册命令
  40. $this->registerCommands();
  41. // 注册定时任务
  42. $this->registerSchedules();
  43. // 注册事件监听器
  44. $this->registerEventListeners();
  45. // 注册中间件
  46. $this->registerMiddleware();
  47. }
  48. /**
  49. * 注册服务绑定
  50. */
  51. protected function registerServices(): void
  52. {
  53. // 注册Transfer服务
  54. $this->app->singleton('transfer', function () {
  55. return new \App\Module\Transfer\Services\TransferService();
  56. });
  57. // 注册外部API服务
  58. $this->app->singleton('transfer.api', function () {
  59. return new \App\Module\Transfer\Services\ExternalApiService();
  60. });
  61. }
  62. /**
  63. * 注册命令
  64. */
  65. protected function registerCommands(): void
  66. {
  67. if ($this->app->runningInConsole()) {
  68. $this->commands([
  69. TransferProcessCommand::class,
  70. TransferStatsCommand::class,
  71. TransferCallbackCommand::class,
  72. TransferCleanCommand::class,
  73. InsertTransferAdminMenuCommand::class,
  74. FeeStatisticsCommand::class,
  75. TestFeeTransferCommand::class,
  76. ]);
  77. }
  78. }
  79. /**
  80. * 注册定时任务
  81. */
  82. protected function registerSchedules(): void
  83. {
  84. // 在应用完全启动后注册定时任务
  85. $this->app->booted(function () {
  86. // 每天22:00执行手续费统计
  87. Schedule::command('transfer:fee-statistics')
  88. ->dailyAt('22:00')
  89. ->description('Transfer模块手续费每日统计')
  90. ->withoutOverlapping() // 防止重复执行
  91. ->runInBackground(); // 后台运行
  92. });
  93. }
  94. /**
  95. * 注册事件监听器
  96. */
  97. protected function registerEventListeners(): void
  98. {
  99. // Transfer模块使用事件驱动架构
  100. // 事件监听器将在需要时自动注册
  101. }
  102. /**
  103. * 注册中间件
  104. */
  105. protected function registerMiddleware(): void
  106. {
  107. // Transfer模块不直接提供外部API
  108. // 通过OpenAPI模块进行对外开放和集成
  109. }
  110. /**
  111. * 获取提供的服务
  112. */
  113. public function provides(): array
  114. {
  115. return [
  116. 'transfer',
  117. 'transfer.api',
  118. ];
  119. }
  120. }