MexServiceProvider.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Module\Mex\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Console\Scheduling\Schedule;
  5. use Illuminate\Support\Facades\Event;
  6. use Illuminate\Support\Facades\Schedule as ScheduleFacade;
  7. /**
  8. * Mex模块服务提供者
  9. *
  10. * 负责注册Mex模块的服务、命令、计划任务等
  11. */
  12. class MexServiceProvider extends ServiceProvider
  13. {
  14. /**
  15. * 注册服务
  16. */
  17. public function register(): void
  18. {
  19. // 注册配置文件
  20. $this->mergeConfigFrom(
  21. __DIR__ . '/../Config/mex.php',
  22. 'mex'
  23. );
  24. // 注册单例服务 ;不注册服务
  25. // $this->registerServices();
  26. }
  27. /**
  28. * 启动服务
  29. */
  30. public function boot(): void
  31. {
  32. // 注册命令
  33. $this->registerCommands();
  34. // 注册Mex定时任务
  35. $this->registerSchedules();
  36. // 注册事件监听器
  37. $this->registerEventListeners();
  38. // 发布配置文件
  39. $this->publishes([
  40. __DIR__ . '/../Config/mex.php' => config_path('mex.php'),
  41. ], 'mex-config');
  42. }
  43. /**
  44. * 注册命令
  45. */
  46. protected function registerCommands(): void
  47. {
  48. if ($this->app->runningInConsole()) {
  49. $this->commands([
  50. \App\Module\Mex\Commands\MexUserBuyItemMatchCommand::class,
  51. \App\Module\Mex\Commands\MexUserSellItemMatchCommand::class,
  52. \App\Module\Mex\Commands\MexTestCommand::class,
  53. \App\Module\Mex\Commands\TestMultiCurrencyCommand::class,
  54. \App\Module\Mex\Commands\TestOpenHandlerCommand::class,
  55. \App\Module\Mex\Commands\FixMissingTransactionRecordsCommand::class,
  56. \App\Module\Mex\Commands\GenerateDailyPriceTrendsCommand::class,
  57. \App\Module\Mex\Commands\TestConfigCommand::class,
  58. ]);
  59. }
  60. }
  61. /**
  62. * 注册事件监听器
  63. */
  64. protected function registerEventListeners(): void
  65. {
  66. Event::listen(
  67. \App\Module\Mex\Events\OrderCreatedEvent::class,
  68. \App\Module\Mex\Listeners\OrderCreatedListener::class
  69. );
  70. Event::listen(
  71. \App\Module\Mex\Events\OrderCompletedEvent::class,
  72. [\App\Module\Mex\Listeners\OrderCreatedListener::class, 'handleCompleted']
  73. );
  74. Event::listen(
  75. \App\Module\Mex\Events\TransactionCreatedEvent::class,
  76. \App\Module\Mex\Listeners\TransactionCreatedListener::class
  77. );
  78. }
  79. /**
  80. * 注册Mex相关的定时任务
  81. *
  82. * 将原本在 routes/console.php 中的Mex调度配置迁移到此处
  83. */
  84. protected function registerSchedules(): void
  85. {
  86. // 在应用完全启动后注册定时任务
  87. $this->app->booted(function () {
  88. // 每天凌晨00:05生成每日价格趋势数据
  89. ScheduleFacade::command('mex:generate-daily-trends')
  90. ->dailyAt('00:05')
  91. ->description('生成交易所每日价格趋势数据');
  92. // 注释掉的匹配任务(可根据需要启用)
  93. // php artisan mex:user-sell-item-match
  94. // ScheduleFacade::command('mex:user-sell-item-match')
  95. // ->everyMinute()
  96. // ->onOneServer()
  97. // ->description('用户卖出物品匹配');
  98. // php artisan mex:user-sell-item-match
  99. // ScheduleFacade::command('mex:user-buy-item-match')
  100. // ->everyFiveMinutes()
  101. // ->onOneServer()
  102. // ->description('用户买入物品匹配');
  103. });
  104. }
  105. }