FacebookServerEvent.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Jobs;
  3. use App\Game\Services\FacebookEventService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Log;
  10. class FacebookServerEvent implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. protected string $eventName;
  14. protected string $eventId;
  15. protected $userId;
  16. protected $value;
  17. protected ?string $currency;
  18. protected array $extraCustomData;
  19. protected string $pixelId;
  20. protected string $accessToken;
  21. public $tries = 3;
  22. public $timeout = 30;
  23. /**
  24. * @param string $eventName
  25. * @param string $eventId
  26. * @param int|string $userId
  27. * @param float|int|null $value
  28. * @param string|null $currency
  29. * @param string $pixelId
  30. * @param string $accessToken
  31. * @param array $extraCustomData
  32. */
  33. public function __construct(
  34. string $eventName,
  35. string $eventId,
  36. $userId,
  37. $value = null,
  38. ?string $currency = null,
  39. string $pixelId = '',
  40. string $accessToken = '',
  41. array $extraCustomData = []
  42. ) {
  43. $this->eventName = $eventName;
  44. $this->eventId = $eventId;
  45. $this->userId = $userId;
  46. $this->value = $value;
  47. $this->currency = $currency;
  48. $this->extraCustomData = $extraCustomData;
  49. $this->pixelId = $pixelId;
  50. $this->accessToken = $accessToken;
  51. }
  52. public function handle(): void
  53. {
  54. try {
  55. // 队列里没有 Request,上报时不依赖 Request,只依赖渠道配置中的 PixelID
  56. FacebookEventService::trackEvent(
  57. $this->eventName,
  58. $this->eventId,
  59. $this->userId,
  60. $this->value,
  61. $this->currency,
  62. $this->pixelId,
  63. $this->accessToken,
  64. $this->extraCustomData
  65. );
  66. } catch (\Throwable $e) {
  67. Log::error('FacebookServerEvent job error', [
  68. 'event_name' => $this->eventName,
  69. 'event_id' => $this->eventId,
  70. 'user_id' => $this->userId,
  71. 'message' => $e->getMessage(),
  72. ]);
  73. }
  74. }
  75. }