TransferServiceProvider.php 3.4 KB

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