disableQueryLog(); $this->time = date('m-d H:i:s'); $this->info($this->time . ' 开始整理'); $this->type = $this->argument('type') ? $this->argument('type') : 2; $this->dateTime = $this->argument('date') ? $this->argument('date') : null; $this->getAccount(); } public function getAccount() { try { if($this->type==1) { for ($month = 1; $month <= 12; $month++) { $days = getDaysByMonth($this->dateTime, $month); foreach ($days as $day) { if($day > date("Y-m-d")) continue 2; $this->date = $day; $this->getReport(); } } } elseif ($this->type==2) { if($this->date === false) { if(is_null($this->dateTime)) { $this->date = date('Y-m-d'); } else { $this->date = $this->dateTime; } } $this->getReport(); } elseif($this->type==3) { $this->date = date('Y-m-d', strtotime('-1 days')); $this->getReport(); } } catch(\Exception $e) { Log::logError('广告账号信息获取异常', [ 'msg' => $e->getMessage(), 'line' => $e->getLine() ], 'AccountGetWithPer'); } return true; } public function getReport() { $this->info($this->date); try { $this->page = 1; do{ # 获取账号信息 $list = UserAuthorization::getAccountList($this->page, $this->pageSize); $total = $list->count(); $this->info('本次待拉取的账号数量'.$total.'【'.$this->time.'】'); foreach($list as $account) { $params = [ 'access_token' => $account->access_token, 'timestamp' => time(), 'nonce' => md5(uniqid('', true)), 'account_id' => $account->account_id, // 'level' => 'REPORT_LEVEL_ADVERTISER_WECHAT', 'date_range' => [ 'start_date' => $this->date, 'end_date' => $this->date, ], 'order_by' => [ [ 'sort_field' => 'view_count', 'sort_type' => 'ASCENDING' ] ], 'fields' => [ 'view_count','view_user_count','date','account_id','cost', 'valid_click_count', 'follow_count','scan_follow_count','scan_follow_user_count' ] ]; if(2 == $account->launch_type) {# adq $params['level'] = 'REPORT_LEVEL_ADVERTISER'; } else { # mp $params['level'] = 'REPORT_LEVEL_ADVERTISER_WECHAT'; } foreach ($params as $key => $value) { if (!is_string($value)) { $params[$key] = json_encode($value); } } $requestUri = MpDailyReportService::API_BASE_URL . MpDailyReportService::DAILY_REPORT . '?' . http_build_query($params); $response = HttpService::httpGet($requestUri); if($response === false) Log::logError('MP日推广数据获取失败' . $requestUri, $params, 'MpDailyReport'); $responseData = json_decode($response, true); if(isset($responseData['code']) && $responseData['code']==0) { $list = []; if(isset($responseData['data']['list'])) { $list = $responseData['data']['list']; } foreach ($list as $item) { $insertData = [ 'exp_pv' => isset($item['view_count']) ? $item['view_count'] : 0, 'exp_uv' => isset($item['view_user_count']) ? $item['view_user_count'] : 0, 'ref_date' => $item['date'], 'account_id' => $item['account_id'], 'paid' => isset($item['cost']) ? $item['cost'] : 0, 'clk_pv' => isset($item['valid_click_count']) ? $item['valid_click_count'] : 0, 'app_id' => $account->wechat_account_id, 'follow_uv' => isset($item['follow_count']) ? $item['follow_count'] : 0, 'scan_follow_count' => isset($item['scan_follow_count']) ? $item['scan_follow_count'] : 0 ]; # 数据写入 AdsDailyReport::updateOrCreate(['app_id' => $account->wechat_account_id, 'ref_date' => $item['date']], $insertData); } } else { Log::logInfo('MP广告主数据获取异常: 【'.$requestUri.'】', $responseData, 'MPExceptionAccount'); } } $this->page++; } while ($total == $this->pageSize); } catch (\Exception $e) { Log::logError('MP广告主数据获取失败', [ 'msg' => $e->getMessage(), 'line' => $e->getLine() ], 'MpDailyReport'); return false; } return true; } }