GdtCostVideoLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace app\v1\logic\tool\advertCost;
  3. use GuzzleHttp\Client;
  4. use support\think\Db;
  5. class GdtCostVideoLogic extends BaseAdvertLogic
  6. {
  7. // 执行正文
  8. protected function initStart(): void
  9. {
  10. $tokenMap = $this->getGdtTokenMap();
  11. $accountList = $this->getGdtAccountList();
  12. // 循环执行
  13. foreach ($accountList as $account)
  14. {
  15. $accessToken = $tokenMap[$account['pmid']] ?? "";
  16. if(!$accessToken) continue;
  17. // 获取消耗
  18. $dataList = $this->getGdtVideoCost($account['advertiser_id'], $accessToken);
  19. // 数据加工,获取素材信息
  20. if($dataList){
  21. $videoMap = $this->getGdtVideoNameMap($account['advertiser_id'], $accessToken, $dataList);
  22. // 整理数据入库
  23. $this->organizeDataList($account, $dataList, $videoMap);
  24. }
  25. }
  26. }
  27. // 获取媒体消耗
  28. protected function getGdtVideoCost($advertiserId, $accessToken): array
  29. {
  30. $page = 1;
  31. $data = [];
  32. do {
  33. $nonce = md5(time().rand(00000, 99999));
  34. $url = 'https://api.e.qq.com/v3.0/daily_reports/get?access_token='.$accessToken.'&timestamp='.time().'&nonce='.$nonce;
  35. $request_data = [
  36. 'account_id' => $advertiserId,
  37. 'level' => 'REPORT_LEVEL_MATERIAL_VIDEO',
  38. 'date_range' => json_encode(['start_date'=>$this->date,'end_date'=>$this->date]),
  39. 'group_by' => json_encode(['date','video_id','adgroup_id']),
  40. 'fields' => json_encode(['date','video_id','adgroup_id','adgroup_name', 'view_count','valid_click_count','cost', 'activated_count','download_count','first_pay_count','video_outer_play_count','video_outer_play100_count']),
  41. 'page' => $page,
  42. 'page_size' => 1000,
  43. ];
  44. $url = $url."&".http_build_query($request_data);
  45. $httpClient = new Client(['timeout' => 10]);
  46. $res = $httpClient->request('GET', $url);
  47. $result = json_decode($res->getBody(), true);
  48. if (empty($result['data']['list'])) {
  49. break;
  50. }
  51. $data = array_merge($data, $result['data']['list']);
  52. $totalPage = $result['data']['page_info']['total_page'] ?? 1;
  53. $page++;
  54. } while ($page <= $totalPage);
  55. return $data;
  56. }
  57. // 获取素材id信息 $mediaIds 最多100个
  58. protected function getGdtVideo($advertiserId, $accessToken, $videoIds): array
  59. {
  60. $nonce = md5(time().rand(00000, 99999));
  61. $url = 'https://api.e.qq.com/v3.0/videos/get?access_token='.$accessToken.'&timestamp='.time().'&nonce='.$nonce;
  62. $request_data = [
  63. 'account_id' => $advertiserId,
  64. 'filtering' => json_encode([
  65. [
  66. 'field' => 'media_id',
  67. 'operator' => 'IN',
  68. 'values' => $videoIds,
  69. ]
  70. ]),
  71. ];
  72. $url = $url."&".http_build_query($request_data);
  73. $httpClient = new Client(['timeout' => 10]);
  74. $res = $httpClient->request('GET', $url);
  75. $result = json_decode($res->getBody(), true);
  76. return $result['data']['list'] ?? [];
  77. }
  78. protected function getGdtVideoNameMap($advertiserId, $accessToken, $dataList)
  79. {
  80. if(!$dataList) return $dataList;
  81. $videoMaterialIds = array_column($dataList, "video_id");
  82. // 判断是否超过100条,切割处理
  83. $videoIdList = array_chunk($videoMaterialIds, 100);
  84. $videoList = [];
  85. foreach ($videoIdList as $videoIds) {
  86. $videos = $this->getGdtVideo($advertiserId, $accessToken, $videoIds);
  87. $videoList = array_merge($videoList, $videos);
  88. }
  89. $videoData = [];
  90. if($videoList) {
  91. foreach ($videoList as $val){
  92. $videoData[$val['video_id']] = $val;
  93. }
  94. }
  95. return $videoData;
  96. }
  97. // 整理入库数据列表
  98. protected function organizeDataList($account, $dataList, $videoMap): void
  99. {
  100. if(!$dataList) return;
  101. $db = Db::connect('db_advert');
  102. // 返点率
  103. $fandianRate = ($account['son_fandian']>1) ? (1/$account['son_fandian']) : $account['son_fandian'];
  104. foreach ($dataList as $val) {
  105. $cost = $val['cost']/100;
  106. if($cost==0) continue;
  107. $adId = $val['adgroup_id'];
  108. $adName = $val['adgroup_name'];
  109. $materialId = $val['video_id'];
  110. $materialName = $videoMap[$materialId]["description"] ?? "";
  111. $materialUrl = $videoMap[$materialId]["preview_url"] ?? "";
  112. $materialImg = $videoMap[$materialId]['key_frame_image_url'] ?? "";
  113. // Todo 从素材名称中获取归属人
  114. $authorId = $this->getAuthorId($materialName);
  115. // Todo 从广告名称中拆分归因数据
  116. $siteInfo = $this->getSiteInfo($adName);
  117. if(!$siteInfo['game_id'] || empty($this->siteMap[$siteInfo['site_id']])) continue;
  118. $materialData = [
  119. 'ad_id' => $adId,
  120. 'game_id' => $siteInfo['game_id'],
  121. 'agent_id'=> $siteInfo['agent_id'],
  122. 'site_id' => $siteInfo['site_id'],
  123. 'media_id' => $this->siteMap[$siteInfo['site_id']]['media_id'] ?? 0,
  124. 'auth_id' => $this->siteMap[$siteInfo['site_id']]['auth_id'] ?? 0,
  125. 'material_name' => $materialName,
  126. 'material_url' => $materialUrl,
  127. 'material_img' => $materialImg,
  128. 'ori_money' => $cost,
  129. 'money' => $cost*$fandianRate,
  130. 'show' => $val['view_count'],
  131. 'click' => $val['valid_click_count'],
  132. 'total_play' => $val['video_outer_play_count'],
  133. 'valid_play' => $val['video_outer_play100_count'],
  134. 'download' => $val['download_count'],
  135. 'active' => $val['activated_count'],
  136. 'pay_count' => $val['first_pay_count'],
  137. 'author_id' => $authorId
  138. ];
  139. $where = [
  140. 'tdate' => $this->date,
  141. 'ad_id' => $adId,
  142. 'material_id' => $materialId,
  143. 'advertiser_id' => $account['advertiser_id'],
  144. ];
  145. $findId= $db->table($this->materialTable)->where($where)->value("id");
  146. if($findId) {
  147. $materialData['id'] = $findId;
  148. }
  149. // 保存数据
  150. $db->table($this->materialTable)->save(array_merge($materialData, $where));
  151. }
  152. }
  153. }