AgentSiteLogic.php 14 KB

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