123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\AdsDailyReport;
- use App\Models\UserAuthorization;
- use App\Services\HttpService;
- use App\Services\MpDailyReportService;
- use Illuminate\Console\Command;
- class MpDailyReport extends Command
- {
- protected $signature = 'MpDailyReport {type?} {date?}'; // type 1:按年抓取(date示例:2021) 2:按指定日期(date示例:2021-01-01)3:抓取昨日数据
- protected $description = '微信广告主日报表';
- protected $type;
- protected $dateTime;
- protected $month;
- protected $page;
- protected $time;
- protected $date = false;
- protected $pageSize = 200;
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- \DB::connection()->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;
- }
- }
|