AgentSiteLogic.php 13 KB

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