AgentSiteLogic.php 13 KB

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