AgentSiteLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: your name
  6. // +----------------------------------------------------------------------
  7. namespace app\v1\logic\advert;
  8. use app\v1\logic\advert\GamePackageLogic;
  9. use app\v1\logic\center\GameLogic;
  10. use plugin\saiadmin\basic\BaseLogic;
  11. use plugin\saiadmin\exception\ApiException;
  12. use plugin\saiadmin\utils\Helper;
  13. use app\v1\model\advert\AgentSite;
  14. use GuzzleHttp\Client;
  15. use Illuminate\Support\Facades\Http;
  16. use plugin\saiadmin\service\OpenSpoutWriter;
  17. use support\think\Db;
  18. /**
  19. * 广告位列表逻辑层
  20. */
  21. class AgentSiteLogic extends BaseLogic
  22. {
  23. protected $mediaListLogic;
  24. protected $gameLogic;
  25. /**
  26. * 构造函数
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new AgentSite();
  31. $this->gameLogic = new GameLogic();
  32. $this->mediaListLogic = new MediaListLogic();
  33. }
  34. /**
  35. * 获取广告位options
  36. */
  37. public function getAgentSiteOptions()
  38. {
  39. $data = $this->model->select()->toArray();
  40. return $data;
  41. }
  42. /**
  43. * 获取头条账号列表
  44. */
  45. public function getTtAccountList()
  46. {
  47. $data = Db::connect('db_advert')->table('ad_jrtt_account_list')->where('status', 1)->select()->toArray();
  48. return $data;
  49. }
  50. /**
  51. * 根据广告位IDS集合获取广告位数据
  52. */
  53. public function getAgentSiteListBySiteIds($site_ids = [])
  54. {
  55. $data = $this->model->whereIn('id', $site_ids)->select()->toArray();
  56. return $data;
  57. }
  58. /**
  59. * 导出分包标识数据
  60. */
  61. public function exportGamePackageKs($data = [])
  62. {
  63. $file_name = $data['title'].'_标识数据_'.date('YmdHis').'.xlsx';
  64. $header = $data['title']=='快手分包' ? ['渠道','备注'] : ['备注','渠道'];
  65. $data = array_map(function($item){
  66. return [
  67. 'agent_id' => explode(',', $item)[0],
  68. 'remark' => explode(',', $item)[1]
  69. ];
  70. }, $data['data']);
  71. $writer = new OpenSpoutWriter($file_name);
  72. $writer->setWidth([15, 15]);
  73. $writer->setHeader($header);
  74. $writer->setData($data);
  75. $file_path = $writer->returnFile();
  76. return response()->download($file_path, urlencode($file_name));
  77. }
  78. /**
  79. * 获取头条推送的access_token
  80. */
  81. public function getTtAccessToken($advertiser_id)
  82. {
  83. $data = Db::connect('db_advert')->table('ad_jrtt_account')->where('advertiser_id', $advertiser_id)->where('status', 1)->value('access_token');
  84. return $data;
  85. }
  86. /**
  87. * 获取头条媒体配置
  88. */
  89. public function getTtMediaConfig()
  90. {
  91. $data = Db::connect('db_advert')->table('media_list')->where('state', 1)->where('name','今日头条')->select()->toArray();
  92. return $data;
  93. }
  94. /**
  95. * 获取头条媒体配置
  96. */
  97. public function getTtAssetid($data = [])
  98. {
  99. $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');
  100. return $data;
  101. }
  102. /**
  103. * 头条推送事件
  104. */
  105. public function ttPushNewEvent($data = [])
  106. {
  107. // 获取头条的游戏
  108. $toutiaoGameData = (new GamePackageLogic())->getToutiaoGames();
  109. // 广告位ID集合
  110. $site_ids = $data['site_ids'];
  111. // 平台的游戏ID
  112. $game_id = $data['game_id'];
  113. // 分包标识
  114. $fb = $data['fb'];
  115. // 推送转化跟踪
  116. $zh = $data['zh'];
  117. // 头条广告主ID
  118. $advertiser_id = $data['advertiser_id'];
  119. // 成功数
  120. $succ = $succ2 = 0;
  121. // 失败数
  122. $fail = $fail2 = 0;
  123. // 广告位数据
  124. $agentSiteList = [];
  125. // 错误数组
  126. $error = [];
  127. // 检查广告位是否传入
  128. if(empty($site_ids)){
  129. throw new ApiException('请选择广告位');
  130. }
  131. // 获取广告位数据
  132. $agentSiteList = $this->getAgentSiteListBySiteIds($site_ids);
  133. if(!$agentSiteList){
  134. throw new ApiException('广告位数据错误');
  135. }
  136. $agentSiteList = array_column($agentSiteList, null, 'id');
  137. // 检查游戏是否为头条媒体的母包
  138. if(!$game_id || !$toutiaoGameData[$game_id]){
  139. throw new ApiException('请选择推送的头条游戏');
  140. }
  141. // 检查广告位是否为头条媒体的广告位
  142. $siteData = (new AgentSiteLogic())->getAgentSiteOptions();
  143. $siteData = array_column($siteData, null, 'id');
  144. $site_ids = explode(',', $site_ids);
  145. foreach($site_ids as $site_id){
  146. if($siteData[$site_id]['media_id']!=1){
  147. throw new ApiException('请选择头条媒体的广告位');
  148. }
  149. }
  150. // 推送头条分包
  151. if($fb){
  152. // 头条游戏平台
  153. $os = $toutiaoGameData[$game_id]['game_os'];
  154. if($os == 2){
  155. throw new ApiException('IOS不能推送分包');
  156. }
  157. // 获取头条的access_token
  158. $access_token = $this->getTtAccessToken($advertiser_id);
  159. if(!$access_token){
  160. throw new ApiException('推送头条的-access_token获取失败');
  161. }
  162. $header = [
  163. 'Access-Token' => $access_token,
  164. 'Content-Type' => 'application/json',
  165. ];
  166. $list = [];
  167. foreach ($site_ids as $val) {
  168. $list[] = [
  169. // 渠道ID = 平台游戏ID_渠道ID_广告位ID
  170. 'channel_id' => $game_id.'_'.$agentSiteList[$val]['agent_id'].'_'.$val,
  171. // 备注 = 平台游戏名称-广告位ID
  172. 'remark' => $toutiaoGameData[$game_id]['name'].'-'.$val,
  173. ];
  174. //记录提交信息
  175. $values = [
  176. 'game_id' => $game_id,
  177. 'agent_id' => $agentSiteList[$val]['agent_id'],
  178. 'site_id' => $val,
  179. 'advertiser_id' => $advertiser_id,
  180. 'package_id' => $toutiaoGameData[$game_id]['tt_package_id'],
  181. 'remark' => $toutiaoGameData[$game_id]['name'].'-'.$val,
  182. ];
  183. Db::connect('db_advert_log')->table('ad_jrtt_channel_package')->insert($values);
  184. }
  185. // 推送给头条数据
  186. $pushData = [
  187. 'account_id' => $advertiser_id,
  188. 'package_id' => $toutiaoGameData[$game_id]['tt_package_id'],
  189. 'channel_list' => $list,
  190. 'mode' => 'Manual',
  191. ];
  192. $url = 'https://ad.oceanengine.com/open_api/2/tools/app_management/extend_package/create/';
  193. // 发送请求
  194. $client = new Client();
  195. $headers = [
  196. 'Access-Token' => $access_token,
  197. 'Content-Type' => 'application/json'
  198. ];
  199. // 发起 POST 请求
  200. $response = $client->post($url, [
  201. 'headers' => $headers,
  202. 'json' => $pushData, // 如果是 application/json
  203. ]);
  204. // 获取响应内容
  205. $body = $response->getBody()->getContents();
  206. $result = json_decode($body, true);
  207. if($result['message']=='OK'){
  208. $succ = "成功";
  209. } else {
  210. $error[] = "请求:".json_encode($pushData, 256) ."结果:".json_encode($result, 256);
  211. $succ = '失败';
  212. }
  213. }
  214. // 推送转化跟踪
  215. if($zh){
  216. // assetId
  217. $asset_id = $this->getTtAssetid(['game_id'=>$game_id,'advertiser_id'=>$advertiser_id]);
  218. if(!$asset_id){
  219. throw new ApiException('推送头条的-asset_id获取失败');
  220. }
  221. $mediaArr = $this->getTtMediaConfig();
  222. foreach ($site_ids as $val) {
  223. $site_id = $val;
  224. $agent_id = $agentSiteList[$val]['agent_id'];
  225. $group_name = $toutiaoGameData[$game_id]['name'].'_'.$val;
  226. // $error 长度
  227. if(count($error)>0){
  228. $fail2++;
  229. continue;
  230. }
  231. if($toutiaoGameData[$game_id]['ios_appid']>0){
  232. $apiurl = $mediaArr[0]['iosurl'];
  233. $downloadurl = 'https://itunes.apple.com/cn/app/id'.$toutiaoGameData[$game_id]['ios_appid'];
  234. } else {
  235. $apiurl = $mediaArr[0]['andurl'];
  236. $downloadurl = 'https://apps.bytesfield.com/download/extend/cur/'.$toutiaoGameData[$game_id]['tt_package_id'].'/'.$toutiaoGameData[$game_id]['id'].'_'.$agent_id.'_'.$val;
  237. }
  238. $yfinfo = $game_id.'_'.$agent_id.'_'.$site_id;
  239. $click_url = str_replace('__YFINFO__', $yfinfo, $apiurl);
  240. $values = [
  241. 'game_id' => $game_id,
  242. 'gid' => $game_id,
  243. 'agent_id' => $agent_id,
  244. 'site_id' => $site_id,
  245. 'advertiser_id' => $advertiser_id,
  246. 'assets_id' => $asset_id,
  247. 'download_url' => $downloadurl,
  248. 'click_url' => $click_url,
  249. 'group_name' => $group_name,
  250. ];
  251. $result = Db::connect('db_advert_log')->table('ad_jrtt_asset')->insert($values);
  252. if($result){
  253. $succ2 ++;
  254. } else {
  255. $fail2 ++;
  256. }
  257. }
  258. }
  259. return ['message' => "分包推送".$succ.",转化推送成功".$succ2.",失败".$fail2];
  260. }
  261. /**
  262. * 联调生成参数
  263. */
  264. public function linkDebugGenerateParams($game,$data)
  265. {
  266. // .env中获取
  267. $baseUrl = env('WATCH_LINK_BASE_API');
  268. // 广告位信息:游戏ID_渠道ID_广告位ID
  269. $siteInfo = $game['id'].'_'.$data['agent_id'].'_'.$data['site_id'];
  270. // 包名
  271. $package_name = $game['package_name'];
  272. // 根据媒体ID读取监测链接
  273. $media_info = $this->mediaListLogic->read($data['media_id']);
  274. // 点击监测链接
  275. $click_url = '';
  276. if($media_info && $game['os'] ==1){
  277. $click_url = $media_info['andurl'];
  278. } else if($media_info && $game['os'] ==2){
  279. $click_url = $media_info['iosurl'];
  280. // appid
  281. $appid = $game['ios_appid'];
  282. $download_url = 'https://itunes.apple.com/cn/app/id'.$game['ios_appid'];
  283. } else if($media_info && ($game['os'] ==3 || $game['os'] ==4)){
  284. $click_url = $media_info['xyxurl'];
  285. // 小游戏路径参数
  286. $wxgamepro ="?media_id=".$siteInfo."&ext_channel=".$media_info['channel_name'].$media_info['appleturl'];
  287. }
  288. $click_url = str_replace('__SITE__', $siteInfo, $click_url);
  289. return [
  290. 'site_info' => $siteInfo,
  291. 'appid' => $appid??'',
  292. 'download_url' => $download_url??'',
  293. 'package_name' => $package_name,
  294. 'click_url' => $baseUrl.$click_url,
  295. 'wxgamepro' => $wxgamepro ?? "",
  296. ];
  297. }
  298. }