Explorar o código

Merge commit 'aedb2d717e53eea70edf84465651e993e75d8513' into dev

ith5 hai 5 meses
pai
achega
a722fe64eb

+ 2 - 2
app/v1/controller/advert/AgentSiteController.php

@@ -102,8 +102,8 @@ class AgentSiteController extends BaseController
     public function ttPushNewEvent(Request $request): Response
     {
         $data = $request->post();
-        $result = $this->logic->ttPushNewEvent($data);
-        return $this->success($result);
+        $this->logic->ttPushNewEvent($data);
+        return $this->success();
     }
 
     /**

+ 202 - 175
app/v1/logic/advert/AgentSiteLogic.php

@@ -54,15 +54,6 @@ class AgentSiteLogic extends BaseLogic
         return $data;
     }
 
-    /**
-     * 根据广告位IDS集合获取广告位数据  
-     */
-    public function getAgentSiteListBySiteIds($site_ids = [])
-    {
-        $data = $this->model->whereIn('id', $site_ids)->select()->toArray();
-        return $data;
-    }
-
 
     /**
      * 导出分包标识数据
@@ -89,9 +80,9 @@ class AgentSiteLogic extends BaseLogic
     /**
      * 获取头条推送的access_token
      */
-    public function getTtAccessToken($advertiser_id)
+    public function getTtAccessToken($advertiserId)
     {
-        $data = Db::connect('db_advert')->table('ad_jrtt_account')->where('advertiser_id', $advertiser_id)->where('status', 1)->value('access_token');
+        $data = Db::connect('db_advert')->table('ad_jrtt_account')->where('advertiser_id', $advertiserId)->where('status', 1)->value('access_token');
         return $data;
     }
 
@@ -100,197 +91,240 @@ class AgentSiteLogic extends BaseLogic
      */
     public function getTtMediaConfig()
     {
-        $data = Db::connect('db_advert')->table('media_list')->where('state', 1)->where('name','今日头条')->select()->toArray();
-        return $data;
+        return Db::connect('db_advert')->table('media_list')->where('id', 1)->find();
     }
 
     /**
      * 获取头条媒体配置
      */
-    public function getTtAssetid($data = [])
+    public function getTtAssetId($data = [])
     {
-        $data = Db::connect('db_advert_log')->table('ad_jrtt_asset')->where('game_id', $data['game_id'])->where('advertiser_id', $data['advertiser_id'])->value('assets_id');
-        return $data;
+        return Db::connect('db_advert_log')->table('ad_jrtt_asset')->where('game_id', $data['game_id'])->where('advertiser_id', $data['advertiser_id'])->value('assets_id');
     }
 
     /**
      * 头条推送事件
      */
-    public function ttPushNewEvent($data = [])
+    public function ttPushNewEvent($data = []): void
     {
+        // 检查广告位是否传入
+        if(empty($data['site_ids'])){
+            throw new ApiException('请选择广告位');
+        }
+
         // 获取头条的游戏
         $toutiaoGameData = (new GamePackageLogic())->getToutiaoGames();
 
         // 广告位ID集合
-        $site_ids = $data['site_ids'];
-        // 平台的游戏ID
-        $game_id = $data['game_id'];
-        // 分包标识
-        $fb = $data['fb'];
-        // 推送转化跟踪
-        $zh = $data['zh'];
-        // 头条广告主ID
-        $advertiser_id = $data['advertiser_id'];
-        // 成功数
-        $succ = $succ2 = 0;
-        // 失败数
-        $fail = $fail2 = 0;
+        $siteIds = $data['site_ids'];
+        $gamePackageId = $data['game_package_id'];
+        $fb = $data['fb']; // 分包标识
+        $zh = $data['zh']; // 推送转化跟踪
+        $advertiserId = $data['advertiser_id']; // 头条广告主ID
+        $gameId = $toutiaoGameData[$gamePackageId]['game_id'] ?? 0;
 
-        // 广告位数据
-        $agentSiteList = [];
+        // 检查游戏是否为头条媒体的母包
+        if(!$gamePackageId || !$toutiaoGameData[$gamePackageId]){
+            throw new ApiException('请选择推送的头条母包');
+        }
 
-        // 错误数组
-        $error = [];
+        $appId = $toutiaoGameData[$gamePackageId]['tt_appid'] ?? 0;
+        if(!$appId){
+            throw new ApiException('母包管理没有设置头条APPID');
+        }
 
-        // 检查广告位是否传入
-        if(empty($site_ids)){
-            throw new ApiException('请选择广告位');
+        $packageId = $toutiaoGameData[$gamePackageId]['tt_package_id'] ?? "";
+        if(!$packageId){
+            throw new ApiException('母包管理没有设置对应的主包名');
         }
 
         // 获取广告位数据
-        $agentSiteList =  $this->getAgentSiteListBySiteIds($site_ids);
-        if(!$agentSiteList){
-            throw new ApiException('广告位数据错误');
+        $agentSiteMap =  $this->model->where(['id'=>$siteIds, 'media_id'=>1])->column('agent_id', 'id');
+        if(count($agentSiteMap) != count($siteIds)){
+            throw new ApiException('请选择头条媒体的广告位');
         }
-        $agentSiteList = array_column($agentSiteList, null, 'id');
-        
-        // 检查游戏是否为头条媒体的母包
-        if(!$game_id || !$toutiaoGameData[$game_id]){
-            throw new ApiException('请选择推送的头条游戏');
+
+        // 获取头条的access_token
+        $accessToken = $this->getTtAccessToken($advertiserId);
+        if(!$accessToken){
+            throw new ApiException('推送头条的-access_token获取失败');
         }
-        // 检查广告位是否为头条媒体的广告位
-        $siteData = (new AgentSiteLogic())->getAgentSiteOptions();
-        $siteData = array_column($siteData, null, 'id');
-        $site_ids = explode(',', $site_ids);
-        foreach($site_ids as $site_id){
-            if($siteData[$site_id]['media_id']!=1){
-                throw new ApiException('请选择头条媒体的广告位');
-            }
+
+        $getTtAssetAppMap = $this->getTtAssetMap($advertiserId, $accessToken);
+        $assetId = $getTtAssetAppMap[$appId] ?? 0;
+        if(!$assetId){
+            throw new ApiException('媒体账户后台没有设置对应APP资产');
         }
 
         // 推送头条分包
         if($fb){
              // 头条游戏平台
-             $os = $toutiaoGameData[$game_id]['game_os'];
-             if($os == 2){
-                throw new ApiException('IOS不能推送分包');
+             $os = $toutiaoGameData[$gamePackageId]['game_os'];
+             if($os!=1){
+                throw new ApiException('只有安卓可以推送分包');
              }
-             // 获取头条的access_token
-                $access_token = $this->getTtAccessToken($advertiser_id);
-               
-                if(!$access_token){
-                    throw new ApiException('推送头条的-access_token获取失败');
-                }
-                $header = [
-                    'Access-Token' => $access_token,
-                    'Content-Type' => 'application/json',
-                ];
-                $list = [];
-                
-                foreach ($site_ids as $val) {
-                
-                    $list[] = [
-                        // 渠道ID = 平台游戏ID_渠道ID_广告位ID
-                        'channel_id' => $game_id.'_'.$agentSiteList[$val]['agent_id'].'_'.$val,
-                        // 备注 = 平台游戏名称-广告位ID
-                        'remark'     => $toutiaoGameData[$game_id]['name'].'-'.$val,
-                    ];
-
-                    //记录提交信息
-                    $values = [
-                        'game_id'       => $game_id,
-                        'agent_id'      => $agentSiteList[$val]['agent_id'],
-                        'site_id'       => $val,
-                        'advertiser_id' => $advertiser_id,
-                        'package_id'    => $toutiaoGameData[$game_id]['tt_package_id'],
-                        'remark'        => $toutiaoGameData[$game_id]['name'].'-'.$val,
-                    ];
-
-                    
-                    Db::connect('db_advert_log')->table('ad_jrtt_channel_package')->insert($values);
-                   
-                }
-                // 推送给头条数据
-                $pushData = [
-                    'account_id' => $advertiser_id,
-                    'package_id' => $toutiaoGameData[$game_id]['tt_package_id'],
-                    'channel_list' => $list,
-                    'mode' => 'Manual',
-                ];
-                 $url = 'https://ad.oceanengine.com/open_api/2/tools/app_management/extend_package/create/';
-                // 发送请求
 
-                $client = new Client();
-                $headers = [
-                    'Access-Token' => $access_token,
-                    'Content-Type'  => 'application/json'
+            $channelList = [];
+            $channelListLog = [];
+            foreach ($siteIds as $siteId) {
+                $agentId = $agentSiteMap[$siteId] ?? 0;
+                $channelId = $gameId.'_'.$agentId.'_'.$siteId;
+                $channelList[] = [
+                    'channel_id' => $channelId,
+                    'remark'     => $toutiaoGameData[$gamePackageId]['name'].'-'.$siteId,
                 ];
-
-                // 发起 POST 请求
-                $response = $client->post($url, [
-                    'headers' => $headers,
-                    'json'    => $pushData, // 如果是 application/json
-                ]);
-
-                // 获取响应内容
-                $body = $response->getBody()->getContents();
-                $result = json_decode($body, true);
-               
-                 if($result['message']=='OK'){
-                    $succ = "成功";
-                 } else {
-                    $error[] = "请求:".json_encode($pushData, 256) ."结果:".json_encode($result, 256);
-                    $succ = '失败';
-                 }
+                //记录提交信息
+                $channelListLog[] = [
+                    'game_id'       => $gameId,
+                    'agent_id'      => $agentId,
+                    'site_id'       => $siteId,
+                    'advertiser_id' => $advertiserId,
+                    'package_id'    => $toutiaoGameData[$gamePackageId]['tt_package_id'],
+                    'remark'        => $toutiaoGameData[$gamePackageId]['name'].'-'.$siteId,
+                ];
+            }
+            Db::connect('db_advert_log')->table('ad_jrtt_channel_package')->insertAll($channelListLog);
+            // Todo 推送分包
+            $result = $this->sendTtPushPackage($advertiserId, $accessToken, $packageId, $channelList);
+            if($result['message']!='OK'){
+                throw new ApiException('推送头条分包失败');
+            }
         }
+
         // 推送转化跟踪
         if($zh){
-            // assetId
-             $asset_id  = $this->getTtAssetid(['game_id'=>$game_id,'advertiser_id'=>$advertiser_id]);
-             if(!$asset_id){    
-                throw new ApiException('推送头条的-asset_id获取失败');
-             }
-             $mediaArr = $this->getTtMediaConfig();
+             $mediaInfo = $this->getTtMediaConfig();
             
-             foreach ($site_ids as $val) {
-                $site_id = $val;
-                $agent_id = $agentSiteList[$val]['agent_id'];
-                $group_name = $toutiaoGameData[$game_id]['name'].'_'.$val;
-                // $error 长度
-                if(count($error)>0){
-                    $fail2++;
-                    continue;
-                }
-
-                if($toutiaoGameData[$game_id]['ios_appid']>0){
-                    $apiurl = $mediaArr[0]['iosurl'];
-                    $downloadurl = 'https://itunes.apple.com/cn/app/id'.$toutiaoGameData[$game_id]['ios_appid'];
+             foreach ($siteIds as $siteId) {
+                $agentId = $agentSiteMap[$siteId] ?? 0;
+                $channelId = $gameId.'_'.$agentId.'_'.$siteId;
+                $groupName = $toutiaoGameData[$gamePackageId]['name'].'_'.$siteId;
+
+                if($toutiaoGameData[$gamePackageId]['ios_appid']>0){
+                    $clickUrl = $mediaInfo['iosurl'];
+                    $downloadUrl = 'https://itunes.apple.com/cn/app/id'.$toutiaoGameData[$gamePackageId]['ios_appid'];
                 } else {
-                    $apiurl = $mediaArr[0]['andurl'];
-                    $downloadurl = 'https://apps.bytesfield.com/download/extend/cur/'.$toutiaoGameData[$game_id]['tt_package_id'].'/'.$toutiaoGameData[$game_id]['id'].'_'.$agent_id.'_'.$val;
+                    $clickUrl = $mediaInfo['andurl'];
+                    $downloadUrl = 'https://apps.bytesfield.com/download/extend/cur/'.$toutiaoGameData[$gamePackageId]['tt_package_id'].'/'.$channelId;
                 }
-                $yfinfo = $game_id.'_'.$agent_id.'_'.$site_id;
-                $click_url = str_replace('__YFINFO__', $yfinfo, $apiurl);
-                $values = [
-                    'game_id'       => $game_id,
-                    'gid'           => $game_id,
-                    'agent_id'      => $agent_id,
-                    'site_id'       => $site_id,
-                    'advertiser_id' => $advertiser_id,
-                    'assets_id'     => $asset_id,
-                    'download_url'  => $downloadurl,
-                    'click_url'     => $click_url,
-                    'group_name'    => $group_name,
+
+                $clickUrl = str_replace('__SITE__', $channelId, $clickUrl);
+
+                $apiResponse = $this->sendTtPushTrackUrl($advertiserId, $accessToken, $assetId, $downloadUrl, $clickUrl, $groupName);
+
+                $log = [
+                    'game_package_id' => $gamePackageId,
+                    'game_id'       => $gameId,
+                    'agent_id'      => $agentId,
+                    'site_id'       => $siteId,
+                    'advertiser_id' => $advertiserId,
+                    'assets_id'     => $getTtAssetAppMap['assets_id'],
+                    'download_url'  => $downloadUrl,
+                    'click_url'     => $clickUrl,
+                    'group_name'    => $groupName,
+                    'api_response'  => $apiResponse,
                 ];
-                $result = Db::connect('db_advert_log')->table('ad_jrtt_asset')->insert($values);
-                if($result){
-                    $succ2 ++;
-                } else {
-                    $fail2 ++;
-                }
-            }
+                Db::connect('db_advert_log')->table('ad_jrtt_track_list')->save($log);
+             }
         }
-        return ['message' => "分包推送".$succ.",转化推送成功".$succ2.",失败".$fail2];
+    }
+
+    // 推送分包
+    protected function sendTtPushPackage($advertiserId, $accessToken, $packageId, $channelList)
+    {
+        // 推送给头条数据
+        $pushData = [
+            'account_id' => $advertiserId,
+            'package_id' => $packageId,
+            'channel_list' => $channelList,
+            'mode' => 'Manual',
+        ];
+        $url = 'https://ad.oceanengine.com/open_api/2/tools/app_management/extend_package/create/';
+        // 发送请求
+
+        $client = new Client();
+        $headers = [
+            'Access-Token' => $accessToken,
+        ];
+
+        // 发起 POST 请求
+        $response = $client->post($url, [
+            'headers' => $headers,
+            'json'    => $pushData, // 如果是 application/json
+        ]);
+
+        // 获取响应内容
+        $body = $response->getBody()->getContents();
+
+        return json_decode($body, true);
+    }
+
+    // 推送监测链接
+    protected function sendTtPushTrackUrl($advertiserId, $accessToken, $assetId, $downloadUrl, $clickUrl, $groupName): string
+    {
+        $pushData = json_encode([
+            'advertiser_id' => $advertiserId,
+            'assets_id'     => $assetId + 0,
+            'download_url'  => $downloadUrl,
+            'track_url_groups'  => [
+                [
+                    'action_track_url' => $clickUrl,
+                    'track_url' => $clickUrl,
+                    'track_url_group_name' => $groupName,
+                ],
+            ],
+        ]);
+        $url = "https://ad.oceanengine.com/open_api/2/event_manager/track_url/create/";
+        // 发送请求
+
+        $client = new Client();
+        $headers = [
+            'Access-Token' => $accessToken,
+        ];
+
+        // 发起 POST 请求
+        $response = $client->post($url, [
+            'headers' => $headers,
+            'json'    => $pushData, // 如果是 application/json
+        ]);
+
+        // 获取响应内容
+        return $response->getBody()->getContents();
+    }
+
+    // 获取广告账户资产信息
+    protected function getTtAssetMap($advertiserId, $accessToken): array
+    {
+        $headers = [
+            'Access-Token: '.$accessToken,
+            'Content-Type' => 'application/json'
+        ];
+        $url = "https://api.oceanengine.com/open_api/2/tools/event/all_assets/list/?advertiser_id={$advertiserId}";
+        $client = new Client();
+        // 发起 POST 请求
+        $response = $client->get($url, [
+            'headers' => $headers,
+        ]);
+        // 获取响应内容
+        $result = $response->getBody()->getContents();
+        $result = json_decode($result, true);
+        $appMap = [];
+        if($result['message']=='OK'){
+            $assetIds = array_column($result['data']['asset_list'], "asset_id");
+            // Todo 获取资产详情
+            $url = "https://api.oceanengine.com/open_api/2/tools/event/all_assets/detail/?advertiser_id={$advertiserId}&asset_ids=".json_encode($assetIds);
+            $client = new Client();
+            // 发起 POST 请求
+            $response = $client->get($url, [
+                'headers' => $headers,
+            ]);
+            // 获取响应内容
+            $result = $response->getBody()->getContents();
+            $result = json_decode($result, true);
+            $assetList = $result['data']['asset_list'] ?? [];
+            $appMap = array_column($assetList, null, 'app_id');
+        }
+        return $appMap;
     }
 
     /**
@@ -304,34 +338,27 @@ class AgentSiteLogic extends BaseLogic
         // 广告位信息:游戏ID_渠道ID_广告位ID
         $siteInfo = $game['id'].'_'.$data['agent_id'].'_'.$data['site_id'];
 
-
-
         // 包名
         $package_name = $game['package_name'];
 
         // 根据媒体ID读取监测链接
         $media_info = $this->mediaListLogic->read($data['media_id']);
 
-
-
-       
-
-
         // 点击监测链接
-        $click_url = '';
+        $clickUrl = '';
         if($media_info && $game['os'] ==1){
-            $click_url = $media_info['andurl'];
+            $clickUrl = $media_info['andurl'];
         } else if($media_info && $game['os'] ==2){
-            $click_url = $media_info['iosurl'];
+            $clickUrl = $media_info['iosurl'];
             // appid
             $appid = $game['ios_appid'];
             $download_url = 'https://itunes.apple.com/cn/app/id'.$game['ios_appid'];
         } else if($media_info && ($game['os'] ==3 || $game['os'] ==4)){
-            $click_url = $media_info['xyxurl'];
+            $clickUrl = $media_info['xyxurl'];
             // 小游戏路径参数
             $wxgamepro ="?media_id=".$siteInfo."&ext_channel=".$media_info['channel_name'].$media_info['appleturl'];
         }
-        $click_url = str_replace('__SITE__', $siteInfo, $click_url);
+        $clickUrl = str_replace('__SITE__', $siteInfo, $clickUrl);
 
 
         return [
@@ -339,7 +366,7 @@ class AgentSiteLogic extends BaseLogic
             'appid' => $appid??'',
             'download_url' => $download_url??'',
             'package_name' => $package_name,
-            'click_url' => $baseUrl.$click_url,
+            'click_url' => $baseUrl.$clickUrl,
             'wxgamepro' => $wxgamepro ?? "",
         ];
     }

+ 1 - 1
app/v1/logic/advert/GamePackageLogic.php

@@ -41,7 +41,7 @@ class GamePackageLogic extends BaseLogic
         $data = $this->model->where('media_id', 1)->select()->toArray();
         $data = $this->trandformListColumn($data, ['game']);
         // 以game_id为key,value为数组
-        $data = array_column($data, null, 'game_id');
+        $data = array_column($data, null, 'id');
         return $data;
     }
 }