AgentSiteLogic.php 13 KB

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