小说推广数据系统

MpDailyReport.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\AdsDailyReport;
  5. use App\Models\UserAuthorization;
  6. use App\Services\HttpService;
  7. use App\Services\MpDailyReportService;
  8. use Illuminate\Console\Command;
  9. class MpDailyReport extends Command
  10. {
  11. protected $signature = 'MpDailyReport {type?} {date?}'; // type 1:按年抓取(date示例:2021) 2:按指定日期(date示例:2021-01-01)3:抓取昨日数据
  12. protected $description = '微信广告主日报表';
  13. protected $type;
  14. protected $dateTime;
  15. protected $month;
  16. protected $page;
  17. protected $time;
  18. protected $date = false;
  19. protected $pageSize = 200;
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. }
  24. public function handle()
  25. {
  26. \DB::connection()->disableQueryLog();
  27. $this->time = date('m-d H:i:s');
  28. $this->info($this->time . ' 开始整理');
  29. $this->type = $this->argument('type') ? $this->argument('type') : 2;
  30. $this->dateTime = $this->argument('date') ? $this->argument('date') : null;
  31. $this->getAccount();
  32. }
  33. public function getAccount()
  34. {
  35. try {
  36. if($this->type==1) {
  37. for ($month = 1; $month <= 12; $month++) {
  38. $days = getDaysByMonth($this->dateTime, $month);
  39. foreach ($days as $day) {
  40. if($day > date("Y-m-d"))
  41. continue 2;
  42. $this->date = $day;
  43. $this->getReport();
  44. }
  45. }
  46. } elseif ($this->type==2) {
  47. if($this->date === false) {
  48. if(is_null($this->dateTime)) {
  49. $this->date = date('Y-m-d');
  50. } else {
  51. $this->date = $this->dateTime;
  52. }
  53. }
  54. $this->getReport();
  55. } elseif($this->type==3) {
  56. $this->date = date('Y-m-d', strtotime('-1 days'));
  57. $this->getReport();
  58. }
  59. } catch(\Exception $e) {
  60. Log::logError('广告账号信息获取异常', [
  61. 'msg' => $e->getMessage(),
  62. 'line' => $e->getLine()
  63. ], 'AccountGetWithPer');
  64. }
  65. return true;
  66. }
  67. public function getReport()
  68. {
  69. $this->info($this->date);
  70. try {
  71. $this->page = 1;
  72. do{
  73. # 获取账号信息
  74. $list = UserAuthorization::getAccountList($this->page, $this->pageSize);
  75. $total = $list->count();
  76. $this->info('本次待拉取的账号数量'.$total.'【'.$this->time.'】');
  77. foreach($list as $account) {
  78. $params = [
  79. 'access_token' => $account->access_token,
  80. 'timestamp' => time(),
  81. 'nonce' => md5(uniqid('', true)),
  82. 'account_id' => $account->account_id,
  83. // 'level' => 'REPORT_LEVEL_ADVERTISER_WECHAT',
  84. 'date_range' => [
  85. 'start_date' => $this->date,
  86. 'end_date' => $this->date,
  87. ],
  88. 'order_by' => [
  89. [
  90. 'sort_field' => 'view_count',
  91. 'sort_type' => 'ASCENDING'
  92. ]
  93. ],
  94. 'fields' => [
  95. 'view_count','view_user_count','date','account_id','cost', 'valid_click_count',
  96. 'follow_count','scan_follow_count','scan_follow_user_count'
  97. ]
  98. ];
  99. if(2 == $account->launch_type) {# adq
  100. $params['level'] = 'REPORT_LEVEL_ADVERTISER';
  101. } else { # mp
  102. $params['level'] = 'REPORT_LEVEL_ADVERTISER_WECHAT';
  103. }
  104. foreach ($params as $key => $value) {
  105. if (!is_string($value)) {
  106. $params[$key] = json_encode($value);
  107. }
  108. }
  109. $requestUri = MpDailyReportService::API_BASE_URL . MpDailyReportService::DAILY_REPORT . '?' . http_build_query($params);
  110. $response = HttpService::httpGet($requestUri);
  111. if($response === false)
  112. Log::logError('MP日推广数据获取失败' . $requestUri, $params, 'MpDailyReport');
  113. $responseData = json_decode($response, true);
  114. if(isset($responseData['code']) && $responseData['code']==0) {
  115. $list = [];
  116. if(isset($responseData['data']['list'])) {
  117. $list = $responseData['data']['list'];
  118. }
  119. foreach ($list as $item) {
  120. $insertData = [
  121. 'exp_pv' => isset($item['view_count']) ? $item['view_count'] : 0,
  122. 'exp_uv' => isset($item['view_user_count']) ? $item['view_user_count'] : 0,
  123. 'ref_date' => $item['date'],
  124. 'account_id' => $item['account_id'],
  125. 'paid' => isset($item['cost']) ? $item['cost'] : 0,
  126. 'clk_pv' => isset($item['valid_click_count']) ? $item['valid_click_count'] : 0,
  127. 'app_id' => $account->wechat_account_id,
  128. 'follow_uv' => isset($item['follow_count']) ? $item['follow_count'] : 0,
  129. 'scan_follow_count' => isset($item['scan_follow_count']) ? $item['scan_follow_count'] : 0
  130. ];
  131. # 数据写入
  132. AdsDailyReport::updateOrCreate(['app_id' => $account->wechat_account_id, 'ref_date' => $item['date']], $insertData);
  133. }
  134. } else {
  135. Log::logInfo('MP广告主数据获取异常: 【'.$requestUri.'】', $responseData, 'MPExceptionAccount');
  136. }
  137. }
  138. $this->page++;
  139. } while ($total == $this->pageSize);
  140. } catch (\Exception $e) {
  141. Log::logError('MP广告主数据获取失败', [
  142. 'msg' => $e->getMessage(),
  143. 'line' => $e->getLine()
  144. ], 'MpDailyReport');
  145. return false;
  146. }
  147. return true;
  148. }
  149. }