|
|
@@ -0,0 +1,295 @@
|
|
|
+<?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
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new AgentSite();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取广告位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];
|
|
|
+ }
|
|
|
+}
|