浏览代码

增加适配器模式

PC-202304251453\Administrator 3 月之前
父节点
当前提交
782efef5fc

+ 133 - 0
app/adapter/ToutiaoAdapter.php

@@ -0,0 +1,133 @@
+<?php
+
+namespace app\adapter;
+
+// 头条-适配器
+use GuzzleHttp\Client;
+
+class ToutiaoAdapter
+{
+    // 获取子账号列表
+    public function getSubAccounts($pmid)
+    {
+
+    }
+
+    // 刷新 token
+    public function refreshToken($pmid)
+    {
+
+    }
+
+    // 获取小时消耗
+    public function getHourlyCost($advertiserId, $accessToken, $date): array
+    {
+        $page = 1;
+        $data = [];
+        do {
+            $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
+            $request_data = [
+                'advertiser_id'=>$advertiserId,
+                'dimensions'=>json_encode(["cdp_project_id","cdp_project_name","ad_platform_cdp_project_action_track_url","ad_platform_cdp_project_download_url","stat_time_hour"]),
+                'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","convert_cnt"]),
+                'filters'=>'[{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
+                'start_time'=>$date,
+                'end_time'=>$date,
+                'order_by'=>json_encode([]),
+                'page'=>$page,
+                'page_size'=>100,
+            ];
+            $options = [
+                "headers" => [
+                    'Access-Token' => $accessToken,
+                    'Content-Type' => 'application/json',
+                ]
+            ];
+            $url = $url."?".http_build_query($request_data);
+
+            $httpClient = new Client(['timeout' => 10]);
+            $res = $httpClient->request('GET', $url, $options);
+            $result = json_decode($res->getBody(), true);
+
+            if (empty($result['data']['rows'])) {
+                break;
+            }
+
+            $data = array_merge($data, $result['data']['rows']);
+            $totalPage = $result['data']['page_info']['total_page'] ?? 1;
+            $page++;
+        } while ($page <= $totalPage);
+
+        return $data;
+    }
+
+    // 获取媒体消耗
+    public function getVideoCost($advertiserId, $accessToken, $date): array
+    {
+        $page = 1;
+        $data = [];
+        do {
+            $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
+
+            $request_data = [
+                'advertiser_id'=>$advertiserId,
+                'dimensions'=>json_encode(["ad_platform_material_name","material_id","cdp_project_id","cdp_project_name","stat_time_day", "ad_platform_cdp_project_action_track_url"]),
+                'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","active","active_register","active_pay","valid_play","download_finish_cnt","total_play", "stat_pay_amount"]),
+                'filters'=>'[{"field":"image_mode","type":1,"operator":0,"values":["5","15"]},{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
+                'start_time'=>$date,
+                'end_time'=>$date,
+                'order_by'=>json_encode([]),
+                'page'=>1,
+                'page_size'=>100,
+            ];
+
+            $options = [
+                "headers" => [
+                    'Access-Token' => $accessToken,
+                    'Content-Type' => 'application/json',
+                ]
+            ];
+            $url = $url."?".http_build_query($request_data);
+
+            $httpClient = new Client(['timeout' => 10]);
+            $res = $httpClient->request('GET', $url, $options);
+            $result = json_decode($res->getBody(), true);
+
+            if (empty($result['data']['rows'])) {
+                break;
+            }
+
+            $data = array_merge($data, $result['data']['rows']);
+            $totalPage = $result['data']['page_info']['total_page'] ?? 1;
+            $page++;
+        } while ($page <= $totalPage);
+
+        return $data;
+    }
+
+    public function getVideo($advertiserId, $accessToken, $materialIds): array
+    {
+
+        $url = 'https://api.oceanengine.com/open_api/2/file/video/get/';
+        $request_data = [
+            'advertiser_id'=>$advertiserId,
+            'filtering' => json_encode([
+                "material_ids" => $materialIds,
+            ]),
+        ];
+
+        $options = [
+            "headers" => [
+                'Access-Token' => $accessToken,
+                'Content-Type' => 'application/json',
+            ]
+        ];
+        $url = $url."?".http_build_query($request_data);
+
+        $httpClient = new Client(['timeout' => 10]);
+        $res = $httpClient->request('GET', $url, $options);
+        $result = json_decode($res->getBody(), true);
+
+        return $result['data']['list'] ?? [];
+    }
+}

+ 2 - 43
app/v1/logic/tool/advertCost/TtCostHourLogic.php

@@ -2,6 +2,7 @@
 
 namespace app\v1\logic\tool\advertCost;
 
+use app\adapter\ToutiaoAdapter;
 use GuzzleHttp\Client;
 use support\think\Db;
 
@@ -21,55 +22,13 @@ class TtCostHourLogic extends BaseAdvertLogic
             if(!$accessToken) continue;
 
             // 获取消耗
-            $dataList = $this->getTtCost($account['advertiser_id'], $accessToken);
+            $dataList = (new ToutiaoAdapter)->getHourlyCost($account['advertiser_id'], $accessToken, $this->date);
 
             // 整理数据入库
             $this->organizeDataList($account, $dataList);
         }
     }
 
-    // 获取媒体消耗
-    protected function getTtCost($advertiserId, $accessToken): array
-    {
-        $page = 1;
-        $data = [];
-        do {
-            $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
-            $request_data = [
-                'advertiser_id'=>$advertiserId,
-                'dimensions'=>json_encode(["cdp_project_id","cdp_project_name","ad_platform_cdp_project_action_track_url","ad_platform_cdp_project_download_url","stat_time_hour"]),
-                'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","convert_cnt"]),
-                'filters'=>'[{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
-                'start_time'=>$this->date,
-                'end_time'=>$this->date,
-                'order_by'=>json_encode([]),
-                'page'=>$page,
-                'page_size'=>100,
-            ];
-            $options = [
-                "headers" => [
-                    'Access-Token' => $accessToken,
-                    'Content-Type' => 'application/json',
-                ]
-            ];
-            $url = $url."?".http_build_query($request_data);
-
-            $httpClient = new Client(['timeout' => 10]);
-            $res = $httpClient->request('GET', $url, $options);
-            $result = json_decode($res->getBody(), true);
-
-            if (empty($result['data']['rows'])) {
-                break;
-            }
-
-            $data = array_merge($data, $result['data']['rows']);
-            $totalPage = $result['data']['page_info']['total_page'] ?? 1;
-            $page++;
-        } while ($page <= $totalPage);
-
-        return $data;
-    }
-
     // 整理入库数据列表
     protected function organizeDataList($account, $dataList): void
     {

+ 3 - 72
app/v1/logic/tool/advertCost/TtCostVideoLogic.php

@@ -2,6 +2,7 @@
 
 namespace app\v1\logic\tool\advertCost;
 
+use app\adapter\ToutiaoAdapter;
 use GuzzleHttp\Client;
 use support\think\Db;
 
@@ -21,7 +22,7 @@ class TtCostVideoLogic extends BaseAdvertLogic
             if(!$accessToken) continue;
 
             // 获取消耗
-            $dataList = $this->getTtVideoCost($account['advertiser_id'], $accessToken);
+            $dataList = (new ToutiaoAdapter)->getVideoCost($account['advertiser_id'], $accessToken, $this->date);
 
             // 数据加工,获取素材信息
             if($dataList){
@@ -33,76 +34,6 @@ class TtCostVideoLogic extends BaseAdvertLogic
         }
     }
 
-    // 获取媒体消耗
-    protected function getTtVideoCost($advertiserId, $accessToken): array
-    {
-        $page = 1;
-        $data = [];
-        do {
-            $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
-
-            $request_data = [
-                'advertiser_id'=>$advertiserId,
-                'dimensions'=>json_encode(["ad_platform_material_name","material_id","cdp_project_id","cdp_project_name","stat_time_day", "ad_platform_cdp_project_action_track_url"]),
-                'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","active","active_register","active_pay","valid_play","download_finish_cnt","total_play", "stat_pay_amount"]),
-                'filters'=>'[{"field":"image_mode","type":1,"operator":0,"values":["5","15"]},{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
-                'start_time'=>$this->date,
-                'end_time'=>$this->date,
-                'order_by'=>json_encode([]),
-                'page'=>1,
-                'page_size'=>100,
-            ];
-
-            $options = [
-                "headers" => [
-                    'Access-Token' => $accessToken,
-                    'Content-Type' => 'application/json',
-                ]
-            ];
-            $url = $url."?".http_build_query($request_data);
-
-            $httpClient = new Client(['timeout' => 10]);
-            $res = $httpClient->request('GET', $url, $options);
-            $result = json_decode($res->getBody(), true);
-
-            if (empty($result['data']['rows'])) {
-                break;
-            }
-
-            $data = array_merge($data, $result['data']['rows']);
-            $totalPage = $result['data']['page_info']['total_page'] ?? 1;
-            $page++;
-        } while ($page <= $totalPage);
-
-        return $data;
-    }
-
-    protected function getTtVideo($advertiserId, $accessToken, $materialIds): array
-    {
-
-        $url = 'https://api.oceanengine.com/open_api/2/file/video/get/';
-        $request_data = [
-            'advertiser_id'=>$advertiserId,
-            'filtering' => json_encode([
-                "material_ids" => $materialIds,
-            ]),
-        ];
-
-        $options = [
-            "headers" => [
-                'Access-Token' => $accessToken,
-                'Content-Type' => 'application/json',
-            ]
-        ];
-        $url = $url."?".http_build_query($request_data);
-
-        $httpClient = new Client(['timeout' => 10]);
-        $res = $httpClient->request('GET', $url, $options);
-        $result = json_decode($res->getBody(), true);
-
-        return $result['data']['list'] ?? [];
-    }
-
     protected function getTtVideoNameMap($advertiserId, $accessToken, $dataList): array
     {
         if(!$dataList) return $dataList;
@@ -116,7 +47,7 @@ class TtCostVideoLogic extends BaseAdvertLogic
         $videoIdList = array_chunk($videoMaterialIds, 100);
         $videoList = [];
         foreach ($videoIdList as $videoIds) {
-            $videos = $this->getTtVideo($advertiserId, $accessToken, $videoIds);
+            $videos = (new ToutiaoAdapter())->getVideo($advertiserId, $accessToken, $videoIds);
             $videoList = array_merge($videoList, $videos);
         }