| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- // +----------------------------------------------------------------------
- // | saiadmin [ saiadmin快速开发框架 ]
- // +----------------------------------------------------------------------
- // | Author: your name
- // +----------------------------------------------------------------------
- namespace app\v1\logic\advert;
- use app\v1\logic\gameLog\GamePackageLogic;
- use plugin\saiadmin\basic\BaseLogic;
- use plugin\saiadmin\exception\ApiException;
- use plugin\saiadmin\utils\Helper;
- use app\v1\model\advert\AgentSite;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\Http;
- use plugin\saiadmin\service\OpenSpoutWriter;
- use support\think\Db;
- /**
- * 广告位列表逻辑层
- */
- class AgentSiteLogic extends BaseLogic
- {
- protected $mediaListLogic;
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->model = new AgentSite();
- $this->mediaListLogic = new MediaListLogic();
- }
- /**
- * 获取广告位options
- */
- public function getAgentSiteOptions()
- {
- $data = $this->model->select()->toArray();
- return $data;
- }
- /**
- * 获取头条账号列表
- */
- public function getTtAccountList()
- {
- $data = Db::connect('db_advert')->table('ad_jrtt_account_list')->where('status', 1)->select()->toArray();
- return $data;
- }
- /**
- * 根据广告位IDS集合获取广告位数据
- */
- public function getAgentSiteListBySiteIds($site_ids = [])
- {
- $data = $this->model->whereIn('id', $site_ids)->select()->toArray();
- return $data;
- }
- /**
- * 导出分包标识数据
- */
- public function exportGamePackageKs($data = [])
- {
- $file_name = $data['title'].'_标识数据_'.date('YmdHis').'.xlsx';
- $header = $data['title']=='快手分包' ? ['渠道','备注'] : ['备注','渠道'];
- $data = array_map(function($item){
- return [
- 'agent_id' => explode(',', $item)[0],
- 'remark' => explode(',', $item)[1]
- ];
- }, $data['data']);
-
- $writer = new OpenSpoutWriter($file_name);
- $writer->setWidth([15, 15]);
- $writer->setHeader($header);
- $writer->setData($data);
- $file_path = $writer->returnFile();
- return response()->download($file_path, urlencode($file_name));
- }
- /**
- * 获取头条推送的access_token
- */
- public function getTtAccessToken($advertiser_id)
- {
- $data = Db::connect('db_advert')->table('ad_jrtt_account')->where('advertiser_id', $advertiser_id)->where('status', 1)->value('access_token');
- return $data;
- }
- /**
- * 获取头条媒体配置
- */
- public function getTtMediaConfig()
- {
- $data = Db::connect('db_advert')->table('media_list')->where('state', 1)->where('name','今日头条')->select()->toArray();
- return $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;
- }
- /**
- * 头条推送事件
- */
- public function ttPushNewEvent($data = [])
- {
- // 获取头条的游戏
- $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;
- // 广告位数据
- $agentSiteList = [];
- // 错误数组
- $error = [];
- // 检查广告位是否传入
- if(empty($site_ids)){
- throw new ApiException('请选择广告位');
- }
- // 获取广告位数据
- $agentSiteList = $this->getAgentSiteListBySiteIds($site_ids);
- if(!$agentSiteList){
- throw new ApiException('广告位数据错误');
- }
- $agentSiteList = array_column($agentSiteList, null, 'id');
- print_r('agentSiteList');
- print_r($agentSiteList);
-
- // 检查游戏是否为头条媒体的母包
- if(!$game_id || !$toutiaoGameData[$game_id]){
- throw new ApiException('请选择推送的头条游戏');
- }
- // 检查广告位是否为头条媒体的广告位
- $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('请选择头条媒体的广告位');
- }
- }
- // 推送头条分包
- if($fb){
- // 头条游戏平台
- $os = $toutiaoGameData[$game_id]['game_os'];
- if($os == 2){
- throw new ApiException('IOS不能推送分包');
- }
- // 获取头条的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'
- ];
- // 发起 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 = '失败';
- }
- }
- // 推送转化跟踪
- 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();
- print_r('mediaArr');
- print_r($mediaArr);
-
- 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'];
- } 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;
- }
- $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,
- ];
- $result = Db::connect('db_advert_log')->table('ad_jrtt_asset')->insert($values);
- if($result){
- $succ2 ++;
- } else {
- $fail2 ++;
- }
- }
- }
- return ['message' => "分包推送".$succ.",转化推送成功".$succ2.",失败".$fail2];
- }
- /**
- * 联调生成参数
- */
- public function linkDebugGenerateParams($game,$data)
- {
- // .env中获取
- $baseUrl = env('WATCH_LINK_BASE_API');
- // 广告位信息:游戏ID_渠道ID_广告位ID
- $siteInfo = $game['id'].'_'.$data['agent_id'].'_'.$data['site_id'];
- // appid
- $appid = $game['ios_appid'];
- // 包名
- $package_name = $game['package_name'];
- // 点击监测链接
- $click_url = '';
- // 根据媒体ID读取监测链接
- $media_info = $this->mediaListLogic->read($data['media_id']);
-
- if($media_info && $game['os'] ==1){
- $click_url = $media_info['andurl'];
- $click_url = str_replace('__SITE__', $siteInfo, $click_url);
- } else if($media_info && $game['os'] ==2){
- $click_url = $media_info['iosurl'];
- $click_url = str_replace('__SITE__', $siteInfo, $click_url);
- } else if($media_info && ($game['os'] ==3 || $game['os'] ==4)){
- $click_url = $media_info['xyxurl'];
- // $media_info['xyxurl'] 可能没有值
- if($click_url = $media_info['xyxurl']){
- $click_url = str_replace('__YFINFO__', $siteInfo, $click_url);
- }
- }
- // 小游戏路径参数
- $wxgamepro ="?media_id=".$siteInfo."&ext_channel=".$media_info['channel_name'].$media_info['appleturl'];
- return [
- 'site_info' => $siteInfo,
- 'appid' => $appid,
- 'package_name' => $package_name,
- 'click_url' => $baseUrl.$click_url,
- 'wxgamepro' => $wxgamepro,
- ];
- }
- }
|