AgentSiteLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace app\v1\logic\advert;
  3. use app\v1\logic\advert\GamePackageLogic;
  4. use app\v1\logic\center\GameLogic;
  5. use plugin\saiadmin\basic\BaseLogic;
  6. use plugin\saiadmin\exception\ApiException;
  7. use plugin\saiadmin\utils\Helper;
  8. use app\v1\model\advert\AgentSite;
  9. use GuzzleHttp\Client;
  10. use Illuminate\Support\Facades\Http;
  11. use plugin\saiadmin\service\OpenSpoutWriter;
  12. use support\think\Db;
  13. /**
  14. * 广告位列表逻辑层
  15. */
  16. class AgentSiteLogic extends BaseLogic
  17. {
  18. protected $mediaListLogic;
  19. protected $gameLogic;
  20. /**
  21. * 构造函数
  22. */
  23. public function __construct()
  24. {
  25. $this->model = new AgentSite();
  26. $this->gameLogic = new GameLogic();
  27. $this->mediaListLogic = new MediaListLogic();
  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. * 导出分包标识数据
  47. */
  48. public function exportGamePackageKs($data = [])
  49. {
  50. $file_name = $data['title'].'_标识数据_'.date('YmdHis').'.xlsx';
  51. $header = $data['title']=='快手分包' ? ['渠道','备注'] : ['备注','渠道'];
  52. $data = array_map(function($item){
  53. return [
  54. 'agent_id' => explode(',', $item)[0],
  55. 'remark' => explode(',', $item)[1]
  56. ];
  57. }, $data['data']);
  58. $writer = new OpenSpoutWriter($file_name);
  59. $writer->setWidth([15, 15]);
  60. $writer->setHeader($header);
  61. $writer->setData($data);
  62. $file_path = $writer->returnFile();
  63. return response()->download($file_path, urlencode($file_name));
  64. }
  65. /**
  66. * 获取头条推送的access_token
  67. */
  68. public function getTtAccessToken($advertiserId)
  69. {
  70. $data = Db::connect('db_advert')->table('ad_jrtt_account_list')->alias('a')
  71. ->join('ad_jrtt_account b', 'a.pmid=b.id', 'left')
  72. ->where('a.advertiser_id', $advertiserId)
  73. ->value('access_token');
  74. return $data;
  75. }
  76. /**
  77. * 获取头条媒体配置
  78. */
  79. public function getTtMediaConfig()
  80. {
  81. return Db::connect('db_advert')->table('media_list')->where('id', 1)->find();
  82. }
  83. /**
  84. * 获取头条媒体配置
  85. */
  86. public function getTtAssetId($data = [])
  87. {
  88. return Db::connect('db_advert_log')->table('ad_jrtt_asset')->where('game_id', $data['game_id'])->where('advertiser_id', $data['advertiser_id'])->value('assets_id');
  89. }
  90. /**
  91. * 头条推送事件
  92. */
  93. public function ttPushNewEvent($data = []): void
  94. {
  95. // 检查广告位是否传入
  96. if(empty($data['site_ids'])){
  97. throw new ApiException('请选择广告位');
  98. }
  99. // 获取头条的游戏
  100. $toutiaoGameData = (new GamePackageLogic())->getToutiaoGames();
  101. // 广告位ID集合
  102. $siteIds = explode(",", $data['site_ids']);
  103. $gamePackageId = $data['game_package_id'];
  104. $fb = $data['fb']; // 分包标识
  105. $zh = $data['zh']; // 推送转化跟踪
  106. $advertiserId = $data['advertiser_id']; // 头条广告主ID
  107. // 检查游戏是否为头条媒体的母包
  108. if(!$gamePackageId || empty($toutiaoGameData[$gamePackageId])){
  109. throw new ApiException('请选择推送的头条母包');
  110. }
  111. $gameId = $toutiaoGameData[$gamePackageId]['game_id'] ?? 0;
  112. $appId = $toutiaoGameData[$gamePackageId]['tt_appid'] ?? 0;
  113. if(!$appId){
  114. throw new ApiException('母包管理没有设置头条APPID');
  115. }
  116. $packageId = $toutiaoGameData[$gamePackageId]['tt_package_id'] ?? "";
  117. if(!$packageId){
  118. throw new ApiException('母包管理没有设置对应的主包名');
  119. }
  120. // 获取广告位数据
  121. $agentSiteMap = $this->model->where(['id'=>$siteIds, 'media_id'=>1])->column('agent_id', 'id');
  122. if(count($agentSiteMap) != count($siteIds)){
  123. throw new ApiException('请选择头条媒体的广告位');
  124. }
  125. // 获取头条的access_token
  126. $accessToken = $this->getTtAccessToken($advertiserId);
  127. if(!$accessToken){
  128. throw new ApiException('推送头条的-access_token获取失败');
  129. }
  130. $getTtAssetAppMap = $this->getTtAssetMap($advertiserId, $accessToken);
  131. $assetId = $getTtAssetAppMap[$appId] ?? 0;
  132. if(!$assetId){
  133. throw new ApiException('媒体账户后台没有设置对应APP资产');
  134. }
  135. // 推送头条分包
  136. if($fb){
  137. // 头条游戏平台
  138. $os = $toutiaoGameData[$gamePackageId]['game_os'];
  139. if($os!=1){
  140. throw new ApiException('只有安卓可以推送分包');
  141. }
  142. $channelList = [];
  143. $channelListLog = [];
  144. foreach ($siteIds as $siteId) {
  145. $agentId = $agentSiteMap[$siteId] ?? 0;
  146. $channelId = $gameId.'_'.$agentId.'_'.$siteId;
  147. $channelList[] = [
  148. 'channel_id' => $channelId,
  149. 'remark' => $toutiaoGameData[$gamePackageId]['name'].'-'.$siteId,
  150. ];
  151. //记录提交信息
  152. $channelListLog[] = [
  153. 'game_id' => $gameId,
  154. 'agent_id' => $agentId,
  155. 'site_id' => $siteId,
  156. 'advertiser_id' => $advertiserId,
  157. 'package_id' => $toutiaoGameData[$gamePackageId]['tt_package_id'],
  158. 'remark' => $toutiaoGameData[$gamePackageId]['name'].'-'.$siteId,
  159. ];
  160. }
  161. Db::connect('db_advert_log')->table('ad_jrtt_channel_package')->insertAll($channelListLog);
  162. // Todo 推送分包
  163. $result = $this->sendTtPushPackage($advertiserId, $accessToken, $packageId, $channelList);
  164. if($result['message']!='OK'){
  165. throw new ApiException('推送头条分包失败');
  166. }
  167. }
  168. // 推送转化跟踪
  169. if($zh){
  170. $mediaInfo = $this->getTtMediaConfig();
  171. foreach ($siteIds as $siteId) {
  172. $agentId = $agentSiteMap[$siteId] ?? 0;
  173. $channelId = $gameId.'_'.$agentId.'_'.$siteId;
  174. $groupName = $toutiaoGameData[$gamePackageId]['name'].'_'.$siteId;
  175. if($toutiaoGameData[$gamePackageId]['ios_appid']>0){
  176. $clickUrl = $mediaInfo['iosurl'];
  177. $downloadUrl = 'https://itunes.apple.com/cn/app/id'.$toutiaoGameData[$gamePackageId]['ios_appid'];
  178. } else {
  179. $clickUrl = $mediaInfo['andurl'];
  180. $downloadUrl = 'https://apps.bytesfield.com/download/extend/cur/'.$toutiaoGameData[$gamePackageId]['tt_package_id'].'/'.$channelId;
  181. }
  182. $clickUrl = str_replace('__SITE__', $channelId, $clickUrl);
  183. $apiResponse = $this->sendTtPushTrackUrl($advertiserId, $accessToken, $assetId, $downloadUrl, $clickUrl, $groupName);
  184. $log = [
  185. 'game_package_id' => $gamePackageId,
  186. 'game_id' => $gameId,
  187. 'agent_id' => $agentId,
  188. 'site_id' => $siteId,
  189. 'advertiser_id' => $advertiserId,
  190. 'assets_id' => $getTtAssetAppMap['assets_id'],
  191. 'download_url' => $downloadUrl,
  192. 'click_url' => $clickUrl,
  193. 'group_name' => $groupName,
  194. 'api_response' => $apiResponse,
  195. ];
  196. Db::connect('db_advert_log')->table('ad_jrtt_track_list')->save($log);
  197. }
  198. }
  199. }
  200. // 推送分包
  201. protected function sendTtPushPackage($advertiserId, $accessToken, $packageId, $channelList)
  202. {
  203. // 推送给头条数据
  204. $pushData = [
  205. 'account_id' => $advertiserId,
  206. 'package_id' => $packageId,
  207. 'channel_list' => $channelList,
  208. 'mode' => 'Manual',
  209. ];
  210. $url = 'https://ad.oceanengine.com/open_api/2/tools/app_management/extend_package/create/';
  211. // 发送请求
  212. $client = new Client();
  213. $headers = [
  214. 'Access-Token' => $accessToken,
  215. ];
  216. // 发起 POST 请求
  217. $response = $client->post($url, [
  218. 'headers' => $headers,
  219. 'json' => $pushData, // 如果是 application/json
  220. ]);
  221. // 获取响应内容
  222. $body = $response->getBody()->getContents();
  223. return json_decode($body, true);
  224. }
  225. // 推送监测链接
  226. protected function sendTtPushTrackUrl($advertiserId, $accessToken, $assetId, $downloadUrl, $clickUrl, $groupName): string
  227. {
  228. $pushData = json_encode([
  229. 'advertiser_id' => $advertiserId,
  230. 'assets_id' => $assetId + 0,
  231. 'download_url' => $downloadUrl,
  232. 'track_url_groups' => [
  233. [
  234. 'action_track_url' => $clickUrl,
  235. 'track_url' => $clickUrl,
  236. 'track_url_group_name' => $groupName,
  237. ],
  238. ],
  239. ]);
  240. $url = "https://ad.oceanengine.com/open_api/2/event_manager/track_url/create/";
  241. // 发送请求
  242. $client = new Client();
  243. $headers = [
  244. 'Access-Token' => $accessToken,
  245. ];
  246. // 发起 POST 请求
  247. $response = $client->post($url, [
  248. 'headers' => $headers,
  249. 'json' => $pushData, // 如果是 application/json
  250. ]);
  251. // 获取响应内容
  252. return $response->getBody()->getContents();
  253. }
  254. // 获取广告账户资产信息
  255. protected function getTtAssetMap($advertiserId, $accessToken): array
  256. {
  257. $headers = [
  258. 'Access-Token' => $accessToken,
  259. 'Content-Type' => 'application/json'
  260. ];
  261. $url = "https://api.oceanengine.com/open_api/2/tools/event/all_assets/list/?advertiser_id={$advertiserId}";
  262. $client = new Client();
  263. // 发起 POST 请求
  264. $response = $client->get($url, [
  265. 'headers' => $headers,
  266. ]);
  267. // 获取响应内容
  268. $result = $response->getBody()->getContents();
  269. $result = json_decode($result, true);
  270. $appMap = [];
  271. if($result['message']=='OK'){
  272. $assetIds = array_column($result['data']['asset_list'], "asset_id");
  273. // Todo 获取资产详情
  274. $url = "https://api.oceanengine.com/open_api/2/tools/event/all_assets/detail/?advertiser_id={$advertiserId}&asset_ids=".json_encode($assetIds);
  275. $client = new Client();
  276. // 发起 POST 请求
  277. $response = $client->get($url, [
  278. 'headers' => $headers,
  279. ]);
  280. // 获取响应内容
  281. $result = $response->getBody()->getContents();
  282. $result = json_decode($result, true);
  283. $assetList = $result['data']['asset_list'] ?? [];
  284. $appMap = array_column($assetList, null, 'app_id');
  285. }
  286. return $appMap;
  287. }
  288. /**
  289. * 联调生成参数
  290. */
  291. public function linkDebugGenerateParams($game,$data)
  292. {
  293. // .env中获取
  294. $baseUrl = env('WATCH_LINK_BASE_API');
  295. // 广告位信息:游戏ID_渠道ID_广告位ID
  296. $siteInfo = $game['id'].'_'.$data['agent_id'].'_'.$data['site_id'];
  297. // 包名
  298. $package_name = $game['package_name'];
  299. // 根据媒体ID读取监测链接
  300. $media_info = $this->mediaListLogic->read($data['media_id']);
  301. // 点击监测链接
  302. $clickUrl = '';
  303. if($media_info && $game['os'] ==1){
  304. $clickUrl = $media_info['andurl'];
  305. } else if($media_info && $game['os'] ==2){
  306. $clickUrl = $media_info['iosurl'];
  307. // appid
  308. $appid = $game['ios_appid'];
  309. $download_url = 'https://itunes.apple.com/cn/app/id'.$game['ios_appid'];
  310. } else if($media_info && ($game['os'] ==3 || $game['os'] ==4)){
  311. $clickUrl = $media_info['xyxurl'];
  312. // 小游戏路径参数
  313. $wxgamepro ="?media_id=".$siteInfo."&ext_channel=".$media_info['channel_name'].$media_info['appleturl'];
  314. }
  315. $clickUrl = str_replace('__SITE__', $siteInfo, $clickUrl);
  316. return [
  317. 'site_info' => $siteInfo,
  318. 'appid' => $appid??'',
  319. 'download_url' => $download_url??'',
  320. 'package_name' => $package_name,
  321. 'click_url' => $baseUrl.$clickUrl,
  322. 'wxgamepro' => $wxgamepro ?? "",
  323. ];
  324. }
  325. public function getSiteAuth(): array
  326. {
  327. $siteList = $this->model->select();
  328. $siteMap = [];
  329. foreach ($siteList as $site) {
  330. $siteMap[$site['id']] = $site;
  331. }
  332. return $siteMap;
  333. }
  334. }