AgentSiteLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /**
  23. * 构造函数
  24. */
  25. public function __construct()
  26. {
  27. $this->model = new AgentSite();
  28. }
  29. /**
  30. * 获取广告位options
  31. */
  32. public function getAgentSiteOptions()
  33. {
  34. $data = $this->model->select()->toArray();
  35. return $data;
  36. }
  37. /**
  38. * 获取头条账号列表
  39. */
  40. public function getTtAccountList()
  41. {
  42. $data = Db::connect('db_advert')->table('ad_jrtt_account_list')->where('status', 1)->select()->toArray();
  43. return $data;
  44. }
  45. /**
  46. * 根据广告位IDS集合获取广告位数据
  47. */
  48. public function getAgentSiteListBySiteIds($site_ids = [])
  49. {
  50. $data = $this->model->whereIn('id', $site_ids)->select()->toArray();
  51. return $data;
  52. }
  53. /**
  54. * 导出分包标识数据
  55. */
  56. public function exportGamePackageKs($data = [])
  57. {
  58. $file_name = $data['title'].'_标识数据_'.date('YmdHis').'.xlsx';
  59. $header = $data['title']=='快手分包' ? ['渠道','备注'] : ['备注','渠道'];
  60. $data = array_map(function($item){
  61. return [
  62. 'agent_id' => explode(',', $item)[0],
  63. 'remark' => explode(',', $item)[1]
  64. ];
  65. }, $data['data']);
  66. $writer = new OpenSpoutWriter($file_name);
  67. $writer->setWidth([15, 15]);
  68. $writer->setHeader($header);
  69. $writer->setData($data);
  70. $file_path = $writer->returnFile();
  71. return response()->download($file_path, urlencode($file_name));
  72. }
  73. /**
  74. * 获取头条推送的access_token
  75. */
  76. public function getTtAccessToken($advertiser_id)
  77. {
  78. $data = Db::connect('db_advert')->table('ad_jrtt_account')->where('advertiser_id', $advertiser_id)->where('status', 1)->value('access_token');
  79. return $data;
  80. }
  81. /**
  82. * 获取头条媒体配置
  83. */
  84. public function getTtMediaConfig()
  85. {
  86. $data = Db::connect('db_advert')->table('media_list')->where('state', 1)->where('name','今日头条')->select()->toArray();
  87. return $data;
  88. }
  89. /**
  90. * 获取头条媒体配置
  91. */
  92. public function getTtAssetid($data = [])
  93. {
  94. $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');
  95. return $data;
  96. }
  97. /**
  98. * 头条推送事件
  99. */
  100. public function ttPushNewEvent($data = [])
  101. {
  102. // 获取头条的游戏
  103. $toutiaoGameData = (new GamePackageLogic())->getToutiaoGames();
  104. // 广告位ID集合
  105. $site_ids = $data['site_ids'];
  106. // 平台的游戏ID
  107. $game_id = $data['game_id'];
  108. // 分包标识
  109. $fb = $data['fb'];
  110. // 推送转化跟踪
  111. $zh = $data['zh'];
  112. // 头条广告主ID
  113. $advertiser_id = $data['advertiser_id'];
  114. // 成功数
  115. $succ = $succ2 = 0;
  116. // 失败数
  117. $fail = $fail2 = 0;
  118. // 广告位数据
  119. $agentSiteList = [];
  120. // 错误数组
  121. $error = [];
  122. // 检查广告位是否传入
  123. if(empty($site_ids)){
  124. throw new ApiException('请选择广告位');
  125. }
  126. // 获取广告位数据
  127. $agentSiteList = $this->getAgentSiteListBySiteIds($site_ids);
  128. if(!$agentSiteList){
  129. throw new ApiException('广告位数据错误');
  130. }
  131. $agentSiteList = array_column($agentSiteList, null, 'id');
  132. print_r('agentSiteList');
  133. print_r($agentSiteList);
  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. print_r('mediaArr');
  220. print_r($mediaArr);
  221. foreach ($site_ids as $val) {
  222. $site_id = $val;
  223. $agent_id = $agentSiteList[$val]['agent_id'];
  224. $group_name = $toutiaoGameData[$game_id]['name'].'_'.$val;
  225. // $error 长度
  226. if(count($error)>0){
  227. $fail2++;
  228. continue;
  229. }
  230. if($toutiaoGameData[$game_id]['ios_appid']>0){
  231. $apiurl = $mediaArr[0]['iosurl'];
  232. $downloadurl = 'https://itunes.apple.com/cn/app/id'.$toutiaoGameData[$game_id]['ios_appid'];
  233. } else {
  234. $apiurl = $mediaArr[0]['andurl'];
  235. $downloadurl = 'https://apps.bytesfield.com/download/extend/cur/'.$toutiaoGameData[$game_id]['tt_package_id'].'/'.$toutiaoGameData[$game_id]['id'].'_'.$agent_id.'_'.$val;
  236. }
  237. $yfinfo = $game_id.'_'.$agent_id.'_'.$site_id;
  238. $click_url = str_replace('__YFINFO__', $yfinfo, $apiurl);
  239. $values = [
  240. 'game_id' => $game_id,
  241. 'gid' => $game_id,
  242. 'agent_id' => $agent_id,
  243. 'site_id' => $site_id,
  244. 'advertiser_id' => $advertiser_id,
  245. 'assets_id' => $asset_id,
  246. 'download_url' => $downloadurl,
  247. 'click_url' => $click_url,
  248. 'group_name' => $group_name,
  249. ];
  250. $result = Db::connect('db_advert_log')->table('ad_jrtt_asset')->insert($values);
  251. if($result){
  252. $succ2 ++;
  253. } else {
  254. $fail2 ++;
  255. }
  256. }
  257. }
  258. return ['message' => "分包推送".$succ.",转化推送成功".$succ2.",失败".$fail2];
  259. }
  260. }