123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace App\Console\Commands\Star;
- use App\Models\JuxingAdAccount;
- use App\Models\JxStarLiveTaskList;
- use App\Support\Log;
- use App\Support\RedisModel;
- use Illuminate\Console\Command;
- use kwaiSDK\JuXing;
- class LiveTaskDataSync extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'Star:LiveTaskDataSync';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '星直播任务列表接口';
- protected $limit = 100;
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- $this->info(date('H:i') . ' 开始执行');
- $this->start();
- $this->info(date('H:i') . ' 结束执行');
- return 0;
- }
- public function start()
- {
- $curTime = time();
- while (true) {
- if ((time() - $curTime) >= 60) break;
- $dataJson = RedisModel::rPop(JuxingAdAccount::JUXING_STAR_LIVE_SYNC_ADVID_LIST);
- if (empty($dataJson)) {
- sleep(2);
- continue;
- }
- $data = json_decode($dataJson, true);
- if (!isset($data['advertiserId'], $data['stTime'], $data['enTime'])) {
- # 输出错误日志
- Log::error('数据参数不合法', $data, 'Star:LiveTaskDataSync');
- continue;
- }
- $this->getTaskData($data);
- }
- }
- public function getTaskData($data)
- {
- $advertiserId = $data['advertiserId'];
- $stTime = $data['stTime'];
- $enTime = $data['enTime'];
- $accessToken = JuxingAdAccount::getAccessToken($advertiserId);
- $juxing = new JuXing($accessToken);
- $param = [
- 'advertiser_id' => $advertiserId,
- 'start_time' => $stTime,
- 'end_time' => $enTime
- ];
- $listGenerator = $this->generator($juxing, $param);
- foreach ($listGenerator as $list) {
- foreach ($list as $datum) {
- $this->allotmentData($advertiserId, $datum);
- }
- }
- }
- public function generator($juxing, $param)
- {
- $failRecord = [];
- $pageNumber = 1;
- $totalPage = 1;
- do {
- $param['page_num'] = $pageNumber;
- $param['page_size'] = $this->limit;
- $rst = $juxing->investData()->starLiveTaskList($param);
- if (isset($rst['code']) && ($rst['code'] == 0)) {
- yield $rst['data']['star_live_task_detail_resp'];
- $totalPage = ceil($rst['data']['total']/$this->limit);
- $pageNumber ++;
- } else {
- if (isset($failRecord[$pageNumber])) {
- $failRecord[$pageNumber] ++;
- } else {
- $failRecord[$pageNumber] = 0;
- }
- if ($failRecord[$pageNumber] > 5) {
- Log::error('请求接口错误', $param, 'Star:LiveTaskDataSync');
- break;
- }
- }
- // 控频
- sleep(1);
- } while($pageNumber <= $totalPage);
- }
- public function allotmentData($advertiserId, $datum)
- {
- # 插入任务入库队列
- $taskItem = [
- 'advertiser_id' => $advertiserId,
- 'data' => $datum
- ];
- RedisModel::lPush(JxStarLiveTaskList::JUXING_STAR_LIVE_TASK_INDB_LIST, json_encode($taskItem));
- # 输出记录日志
- // Log::info('入库任务数据入队列成功', $taskItem, 'Star:LiveTaskDataSync');
- # 插入获取订单队列
- $taskId = $datum['task_id'] ?? null;
- $getOrderItem = [
- 'advertiser_id' => $advertiserId,
- 'task_id' => $taskId
- ];
- RedisModel::lPush(JxStarLiveTaskList::JUXING_STAR_LIVE_ORDER_SYNC_TASKID_LIST, json_encode($getOrderItem));
- # 输出记录日志
- // Log::info('获取星直播订单入队列成功', $getOrderItem, 'Star:LiveTaskDataSync');
- }
- }
|