ReportDy.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Jobs;
  3. use App\Consts\ErrorConst;
  4. use App\Libs\Utils;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldBeUnique;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\DB;
  13. use Throwable;
  14. class ReportDy implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. /**
  18. * 任务可尝试次数
  19. *
  20. * @var int
  21. */
  22. public $tries = 5; // 重试次数
  23. public $timeout = 120; // 超时秒数
  24. public $backoff = 3; // 重试任务前等待的秒数
  25. public $params = [];
  26. public $url = 'https://analytics.oceanengine.com/api/v2/conversion'; // 上报url
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @param $params
  31. */
  32. public function __construct($params)
  33. {
  34. $this->params = $params;
  35. $this->onQueue('{ReportDy}');
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. dLog('reportDy')->info('开始执行上报抖音队列', $this->params);
  45. $clickid = getProp($this->params, 'clickid');
  46. $event_type = getProp($this->params, 'event_type');
  47. if (!$clickid || !$event_type) {
  48. dLog('reportDy')->info('抖音上报参数错误', $this->params);
  49. return ;
  50. }
  51. $trade_no = '';
  52. if (isset($this->params['trade_no'])) {
  53. $trade_no = $this->params['trade_no'];
  54. unset($this->params['trade_no']);
  55. }
  56. try {
  57. $client = new Client(['verify' => false]);
  58. $response = $client->post($this->url, ['json' => $this->params]);
  59. $response_json = $response->getBody()->getContents();
  60. $result = json_decode($response_json, true);
  61. $update_data = [
  62. 'event_type' => $event_type,
  63. 'callback_result' => isset($result['message']) ? $result['message'] : '回参有误',
  64. 'callback_response' => $response_json,
  65. 'updated_at' => date('Y-m-d H:i:s')
  66. ];
  67. if ($trade_no) $update_data['trade_no'] = $trade_no;
  68. DB::beginTransaction();
  69. // 写入抖音广告主投放回调日志表
  70. $boolen = DB::table('dy_report_logs')->where('clickid', $clickid)->where('event_type', '')->update($update_data);
  71. if (!$boolen) {
  72. DB::rollBack();
  73. dLog('reportDy')->info('写入抖音广告主投放回调日志表失败: ', $update_data);
  74. Utils::throwError(ErrorConst::REPORT_FAILED);
  75. }
  76. // 如果是充值事件则更新派单级别回传上报总数
  77. if ($event_type == 'active_pay') {
  78. $send_order_id = DB::table('dy_report_logs')->where('clickid', $clickid)->value('send_order_id');
  79. $boolen1 = DB::table('send_orders')->where('id', $send_order_id)->increment('report_post_num');
  80. if (!$boolen1) {
  81. DB::rollBack();
  82. dLog('reportDy')->info('派单表更新回传上报总数失败: ', $this->params);
  83. Utils::throwError(ErrorConst::REPORT_FAILED);
  84. }
  85. }
  86. DB::commit();
  87. dLog('reportDy')->info('上报结果: ', $result);
  88. }catch (\Exception $e) {
  89. dLog('reportDy')->info('上报异常: ', ['error'=>$e->getMessage()]);
  90. }
  91. }
  92. public function failed(Throwable $exception)
  93. {
  94. dLog('reportDy')->info('上报抖音队列失败', ['error'=>$exception->getMessage()]);
  95. }
  96. }