UrsPromotionServiceProvider.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Module\UrsPromotion\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Console\Scheduling\Schedule;
  5. use App\Module\Farm\Events\CropHarvestedEvent;
  6. use App\Module\UrsPromotion\Listeners\CropHarvestedListener;
  7. /**
  8. * URS推广模块服务提供者
  9. */
  10. class UrsPromotionServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * 事件与监听器映射
  14. *
  15. * @var array
  16. */
  17. protected $listen = [
  18. CropHarvestedEvent::class => [
  19. CropHarvestedListener::class,
  20. ],
  21. ];
  22. /**
  23. * 注册服务
  24. */
  25. public function register(): void
  26. {
  27. // 注册命令
  28. if ($this->app->runningInConsole()) {
  29. $this->commands([
  30. \App\Module\UrsPromotion\Commands\TestUrsProfitCommand::class,
  31. \App\Module\UrsPromotion\Commands\TestUrsTalentCommand::class,
  32. \App\Module\UrsPromotion\Commands\InsertUrsPromotionAdminMenuCommand::class,
  33. \App\Module\UrsPromotion\Commands\UrsPromotionIntegrationTestCommand::class,
  34. \App\Module\UrsPromotion\Commands\TestFarmIntegrationCommand::class,
  35. \App\Module\UrsPromotion\Commands\TestPromotionRewardCommand::class,
  36. ]);
  37. }
  38. }
  39. /**
  40. * 启动服务
  41. */
  42. public function boot(): void
  43. {
  44. // 注册事件监听器
  45. $events = $this->app['events'];
  46. foreach ($this->listen as $event => $listeners) {
  47. foreach ($listeners as $listener) {
  48. $events->listen($event, $listener);
  49. }
  50. }
  51. // 加载API路由
  52. if (file_exists(__DIR__ . '/../Routes/api.php')) {
  53. require __DIR__ . '/../Routes/api.php';
  54. }
  55. // 注册定时任务
  56. $this->app->booted(function () {
  57. $schedule = $this->app->make(Schedule::class);
  58. // 每小时更新一次达人等级
  59. $schedule->command('urs:update-talents')->hourly();
  60. // 每天清理过期数据
  61. $schedule->command('urs:clean-data')->daily();
  62. });
  63. }
  64. }