AgentSiteLogic.php 12 KB

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