disableQueryLog(); $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0; $this->info('本次查询的账号对应键值为:' . $this->accountLabel); $this->getPromoteList(); } private function getPromoteList() { # 获取账号列表 $fields = ['ma_app_id', 'app_id', 'service_type']; if($this->accountLabel == 1) { $this->platformId = 3; } else { $this->platformId = 1; } $accountList = VpAccount::getAccountList($this->platformId, $fields, []); foreach ($accountList as $item) { $maAppId = isset($item->ma_app_id) ? $item->ma_app_id : null; $appId = isset($item->app_id) ? $item->app_id : null; $this->info('本次处理账号appId【'.$appId.'】'); if(empty($maAppId) || empty($appId)) { Log::logError('账号数据存在异常', $item, 'YouZiPlayletPromoteList'); continue; } $serviceType = isset($item->service_type) ? $item->service_type : 0; $this->info('该账号ServiceType【'.$serviceType.'】'); if(!in_array($serviceType, YouZiService::ACCOUNT_SERVICE_TYPE)) { Log::logError('ServiceType非法', $item, 'YouZiPlayletPromoteList'); continue; } $this->getCampaign($appId, $maAppId, $serviceType); } } public function getCampaign($appId, $maAppId, $serviceType, $retry=0) { try { $requestUri = YouZiService::BASE_URI . YouZiService::CAMPAIGN_LIST_URI; $this->page = 1; do{ $this->info('公众号:'.$appId); $this->info('当前页码数:'.$this->page); $this->info('每页显示条数:'.$this->perPage); $param = [ 'dbSorted' => array([ 'fieldName' => 'createdTs', 'order' => 'ASC' ]), 'page' => $this->page, 'limit' => $this->perPage, 'beginCreatedTs' => '', 'endCreatedTs' => '' ]; # 获取令牌并拼装Header $accessToken = YouZiService::getAccessToken($this->accountLabel); if(empty($accessToken) && $retry < 5) { # 清除已缓存的AccessToken RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel); $retry++; $this->getCampaign($appId, $maAppId, $serviceType, $retry); } $headers = [ 'accesstoken: ' . $accessToken, 'maappid: ' . $maAppId, 'mpappid: ' . $appId ]; # 获取列表 $response = HttpService::httpPost($requestUri, json_encode($param), true, $headers); $responseData = json_decode($response, True); if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) { # 清除已缓存的AccessToken RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel); $retry++; $this->getCampaign($appId, $maAppId, $serviceType, $retry); } # 是否存在合法数据 $totalPage = isset($responseData['data']['totalPage']) ? $responseData['data']['totalPage'] : 0; $data = isset($responseData['data']['items']) ? $responseData['data']['items'] : []; $platformId = $this->platformId; # 检出数据 foreach ($data as $datum) { $enable = 1; $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0; if($isDeleted) $enable = -1; $playletActivityId = isset($datum['playletActivityId']) ? $datum['playletActivityId'] : null; if(empty($playletActivityId)) { Log::logError('短剧活动ID获取异常', $datum, 'YouZiPlayletPromoteList'); return false; } // appID/platformId $platformCreatedTime = isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null; $requestTimestamp = strtotime($platformCreatedTime); if($requestTimestamp === false ) $requestTimestamp = time(); $year = date('Y', $requestTimestamp); $playletName = isset($datum['name']) ? $datum['name'] : null; $playletPromoteDate = getDateFromPlayletTitle($year, $playletName); if($playletPromoteDate < $platformCreatedTime) { // 兼容跨年 $playletPromoteDate = getDateFromPlayletTitle($year+1, $playletName); } $insertData = [ 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null, 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null, 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null, 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null, 'ma_app_id' => isset($datum['maAppId']) ? $datum['maAppId'] : null, 'dp_admin_id' => isset($datum['dpAdminId']) ? $datum['dpAdminId'] : null, 'promote_date' => $playletPromoteDate, 'name' => $playletName, 'enable' => $enable, 'playlet_id' => isset($datum['playletId']) ? $datum['playletId'] : null, 'playlet_title' => isset($datum['playletTitle']) ? $datum['playletTitle'] : null, 'playlet_cover_img' => isset($datum['playletCoverImg']) ? $datum['playletCoverImg'] : null, 'playlet_info' => isset($datum['playlet']) ? json_encode($datum['playlet']) : null, 'mp_follow_tip_type' => isset($datum['mpFollowTipType']) ? $datum['mpFollowTipType'] : null, 'start_episode_index' => isset($datum['startEpisodeIndex']) ? $datum['startEpisodeIndex'] : null, 'mp_follow_tip_episode_index' => isset($datum['mpFollowTipEpisodeIndex']) ? $datum['mpFollowTipEpisodeIndex'] : null, 'cost' => isset($datum['cost']) ? $datum['cost'] : null, 'copy_id' => isset($datum['copyId']) ? $datum['copyId'] : null, 'pay_money' => isset($datum['payMoney']) ? $datum['payMoney'] : null, 'today_pay_money' => isset($datum['todayPayMoney']) ? $datum['todayPayMoney'] : null, 'view_pv' => isset($datum['viewPv']) ? $datum['viewPv'] : null, 'view_uv' => isset($datum['viewUv']) ? $datum['viewUv'] : null, 'pay_uv' => isset($datum['payUv']) ? $datum['payUv'] : null, 'pay_pv' => isset($datum['payPv']) ? $datum['payPv'] : null, 'today_pay_pv' => isset($datum['todayPayPv']) ? $datum['todayPayPv'] : null, 'today_pay_uv' => isset($datum['todayPayUv']) ? $datum['todayPayUv'] : null, 'subscribe_count' => isset($datum['subscribeCount']) ? $datum['subscribeCount'] : null ]; # 判断是否存在数据,存在则不更新推广日期 $isExist = VpPlayletCampaign::where('platform_id', $platformId)->where('app_id', $appId)->where('playlet_activity_id', $playletActivityId)->exists(); if($isExist) unset($insertData['promote_date']); VpPlayletCampaign::updateOrCreate( ['platform_id'=>$platformId, 'app_id' => $appId, 'playlet_activity_id' => $playletActivityId], $insertData ); } $this->page++; $this->info('本次获取数据条数'.$totalPage); } while($this->page <= $totalPage); } catch (\Exception $e) { Log::logError('短剧推广列表获取异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'YouZiPlayletPromoteList'); return false; } return true; } }