Açıklama Yok

LiveTaskDataSync.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Console\Commands\Star;
  3. use App\Models\JuxingAdAccount;
  4. use App\Models\JxStarLiveTaskList;
  5. use App\Support\Log;
  6. use App\Support\RedisModel;
  7. use Illuminate\Console\Command;
  8. use kwaiSDK\JuXing;
  9. class LiveTaskDataSync extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'Star:LiveTaskDataSync';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '星直播任务列表接口';
  23. protected $limit = 100;
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $this->info(date('H:i') . ' 开始执行');
  41. $this->start();
  42. $this->info(date('H:i') . ' 结束执行');
  43. return 0;
  44. }
  45. public function start()
  46. {
  47. $curTime = time();
  48. while (true) {
  49. if ((time() - $curTime) >= 60) break;
  50. $dataJson = RedisModel::rPop(JuxingAdAccount::JUXING_STAR_LIVE_SYNC_ADVID_LIST);
  51. if (empty($dataJson)) {
  52. sleep(2);
  53. continue;
  54. }
  55. $data = json_decode($dataJson, true);
  56. if (!isset($data['advertiserId'], $data['stTime'], $data['enTime'])) {
  57. # 输出错误日志
  58. Log::error('数据参数不合法', $data, 'Star:LiveTaskDataSync');
  59. continue;
  60. }
  61. $this->getTaskData($data);
  62. }
  63. }
  64. public function getTaskData($data)
  65. {
  66. $advertiserId = $data['advertiserId'];
  67. $stTime = $data['stTime'];
  68. $enTime = $data['enTime'];
  69. $accessToken = JuxingAdAccount::getAccessToken($advertiserId);
  70. $juxing = new JuXing($accessToken);
  71. $param = [
  72. 'advertiser_id' => $advertiserId,
  73. 'start_time' => $stTime,
  74. 'end_time' => $enTime
  75. ];
  76. $listGenerator = $this->generator($juxing, $param);
  77. foreach ($listGenerator as $list) {
  78. foreach ($list as $datum) {
  79. $this->allotmentData($advertiserId, $datum);
  80. }
  81. }
  82. }
  83. public function generator($juxing, $param)
  84. {
  85. $failRecord = [];
  86. $pageNumber = 1;
  87. $totalPage = 1;
  88. do {
  89. $param['page_num'] = $pageNumber;
  90. $param['page_size'] = $this->limit;
  91. $rst = $juxing->investData()->starLiveTaskList($param);
  92. if (isset($rst['code']) && ($rst['code'] == 0)) {
  93. yield $rst['data']['star_live_task_detail_resp'];
  94. $totalPage = ceil($rst['data']['total']/$this->limit);
  95. $pageNumber ++;
  96. } else {
  97. if (isset($failRecord[$pageNumber])) {
  98. $failRecord[$pageNumber] ++;
  99. } else {
  100. $failRecord[$pageNumber] = 0;
  101. }
  102. if ($failRecord[$pageNumber] > 5) {
  103. Log::error('请求接口错误', $param, 'Star:LiveTaskDataSync');
  104. break;
  105. }
  106. }
  107. // 控频
  108. sleep(1);
  109. } while($pageNumber <= $totalPage);
  110. }
  111. public function allotmentData($advertiserId, $datum)
  112. {
  113. # 插入任务入库队列
  114. $taskItem = [
  115. 'advertiser_id' => $advertiserId,
  116. 'data' => $datum
  117. ];
  118. RedisModel::lPush(JxStarLiveTaskList::JUXING_STAR_LIVE_TASK_INDB_LIST, json_encode($taskItem));
  119. # 输出记录日志
  120. // Log::info('入库任务数据入队列成功', $taskItem, 'Star:LiveTaskDataSync');
  121. # 插入获取订单队列
  122. $taskId = $datum['task_id'] ?? null;
  123. $getOrderItem = [
  124. 'advertiser_id' => $advertiserId,
  125. 'task_id' => $taskId
  126. ];
  127. RedisModel::lPush(JxStarLiveTaskList::JUXING_STAR_LIVE_ORDER_SYNC_TASKID_LIST, json_encode($getOrderItem));
  128. # 输出记录日志
  129. // Log::info('获取星直播订单入队列成功', $getOrderItem, 'Star:LiveTaskDataSync');
  130. }
  131. }