AgentSiteLogic.php 14 KB

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