DownController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * Down.php UTF-8
  4. * IOS 游戏落地页下载游戏
  5. *
  6. * @date : 2018/3/24 17:02
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : ouzhongfu <ozf@huosdk.com>
  10. * @version : HUOSDK 7.2
  11. * Add by wuyonghong 2018/3/27 2018/3/27 ISSUES:5283 IOS 广告
  12. */
  13. namespace api\down\controller;
  14. use ads\Agent;
  15. use api\common\controller\V2ApiBaseController;
  16. use Endroid\QrCode\QrCode;
  17. use huo\controller\agent\AgentCache;
  18. use huo\logic\agent\AgentGameLogic;
  19. use huo\logic\game\GameLogic;
  20. use huo\model\game\CategoryModel;
  21. use think\Cookie;
  22. use think\Exception;
  23. use think\Log;
  24. use think\Db;
  25. class DownController extends V2ApiBaseController {
  26. function _initialize() {
  27. parent::_initialize();
  28. }
  29. /**
  30. * ios下载落地页
  31. * http://doc.1tsdk.com/43?page_id=3012
  32. *
  33. * @return mixed
  34. */
  35. function index() {
  36. $_app_id = $this->request->param('app_id/s', 0);
  37. $_client_id = $this->request->param('client_id/s', 0);
  38. $_apple_id = $this->request->param('apple_id/s', '');
  39. $_id_str = $this->request->param('id/s', 0);
  40. $_game_id = $this->request->param('game_id/d', 0);
  41. $_lp_id = $this->request->param('lp_id/s', 0); /*landing_page_id 此处缩写为lp_id*/
  42. if (!empty($_app_id) || !empty($_apple_id)) {
  43. /* 表示ios中获取cookie */
  44. return $this->sdkOpen($_app_id, $_client_id, $_apple_id);
  45. }
  46. /* Modified by linjiebin BEGIN 2018/4/21 ISSUES:5569 安卓 广告-落地页 */
  47. $_agent_down_url = '';
  48. if (!empty($_id_str)) {
  49. $_agent_game_id = intval(base_convert($_id_str, 36, 10));
  50. $_agent_game_info = $this->getAgInfo($_agent_game_id);
  51. if (empty($_agent_game_info)) {
  52. $this->error('参数错误');
  53. }
  54. $_rdata['agent_id']=$_agent_game_info['agent_id'];
  55. $_ads_agent_class = new Agent();
  56. $_ads_agent_class->setAgentGameId($_agent_game_id);
  57. $_ag_code = $_ads_agent_class->genAgentCode();
  58. $_game_id = $_agent_game_info['app_id'];
  59. $_agent_down_url = DOWNSITE.$_agent_game_info['package_url'];
  60. } else {
  61. $_ag_code = 0;
  62. }
  63. $_where = ['id' => $_game_id];
  64. $_game_info = (new GameLogic())->getDetail($_where);
  65. $_rdata['ag_code'] = $_ag_code;
  66. $_rdata['gameid'] = $_game_info['game_id'];
  67. $_rdata['classify'] = $_game_info['classify'];
  68. $_rdata['icon'] = $_game_info['icon'];
  69. $_rdata['gamename'] = $_game_info['gamename'];
  70. $_rdata['type'] = (new CategoryModel())->getNameByIds($_game_info['type']);
  71. $_rdata['size'] = $_game_info['size'];
  72. $_rdata['down_cnt'] = $_game_info['down_cnt'];
  73. $_rdata['star_cnt'] = $_game_info['star_cnt'];
  74. $_rdata['hot'] = $_game_info['hot_order'];
  75. $_rdata['ios_url'] = '';
  76. $_rdata['and_url'] = '';
  77. if (3 == $_rdata['classify']) {
  78. $_rdata['and_url'] = isset($_game_info['down_url']) ? $_game_info['down_url'] : '';
  79. $_rdata['and_url'] = empty($_agent_down_url) ? $_rdata['and_url'] : $_agent_down_url;
  80. } else {
  81. $_rdata['ios_url'] = !empty($_game_info['down_url']) ? $_game_info['down_url'] : '';
  82. }
  83. /* END 2018/4/21 ISSUES:5569 */
  84. $_rdata['gift_cnt'] = $_game_info['gift_cnt'];
  85. $_rdata['package_name'] = $_game_info['package_name'];
  86. $_rdata['desc'] = $_game_info['desc'];
  87. $_rdata['image'] = json_encode($_game_info['image']);
  88. $_rdata['version'] = $_game_info['version'];
  89. $_rdata['qr_code'] = $this->getImageUrl($_id_str, $_rdata['icon']);
  90. $this->assign($_rdata);
  91. Cookie::set('huoagentcode', $_ag_code);
  92. /* Modified by guxiannong BEGIN 2018/7/31 ISSUES:5842 买量系统 */
  93. if (!empty($_agent_game_id)) {
  94. $_temp_data = $this->getTemp($_agent_game_id);
  95. if (!empty($_temp_data)) {
  96. $this->assign("src_url", $_temp_data['src_url']);
  97. return $this->fetch($_temp_data['access_url']);
  98. }
  99. }
  100. if (!empty($_lp_id)) {
  101. $_lp_data = $this->getLandpageData($_lp_id);
  102. $this->assign("src_url", $_lp_data['src_url']);
  103. return $this->fetch($_lp_data['access_url']);
  104. }
  105. /* END 2018/7/31 ISSUES:5842 */
  106. return $this->fetch();
  107. }
  108. /**
  109. * http://doc.1tsdk.com/43?page_id=3019
  110. * ios免越狱信任页面
  111. *
  112. * @return mixed
  113. */
  114. public function trust() {
  115. $this->assign('embedded_url', DOWNIP.'embedded.mobileprovision');
  116. return $this->fetch();
  117. }
  118. /**
  119. * 通过
  120. *
  121. * @param int $ag_id 渠道游戏ID
  122. *
  123. * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
  124. */
  125. function getAgInfo($ag_id) {
  126. if (empty($ag_id)) {
  127. return '';
  128. }
  129. try {
  130. $_rdata = (new AgentCache())->getAgInfoByAgId($ag_id);
  131. } catch (Exception $_e) {
  132. Log::write('getAgentGame error:'.$_e->getMessage(), 'error');
  133. $_rdata = false;
  134. }
  135. return $_rdata;
  136. }
  137. /**
  138. * 游戏中初始化后打开页面
  139. *
  140. * @param int $app_id
  141. * @param int $client_id
  142. * @param string $apple_id
  143. */
  144. public function sdkOpen($app_id = 0, $client_id = 0, $apple_id = '') {
  145. $_ag_code = Cookie::get('huoagentcode');
  146. $_ads_agent_class = new Agent();
  147. $_ag_id = $_ads_agent_class->getIdByCode($_ag_code);
  148. $_agentgame = '';
  149. if (!empty($_ag_id)) {
  150. $_agentgame = (new AgentGameLogic())->getAgentGameById($_ag_id);
  151. }
  152. $_rt_string = 'agentgame='.$_agentgame;
  153. if (!empty($app_id)) {
  154. $_url = 'h'.DOCDOMAIN.$app_id.$client_id.'://ag?'.$_rt_string;
  155. } else {
  156. $_url = 'h'.DOCDOMAIN.$apple_id.'://ag?'.$_rt_string;
  157. }
  158. header('Location:'.$_url);
  159. exit;
  160. }
  161. public function getImageUrl($src_ag_id = '', $icon_url = '') {
  162. $_root_path = CMF_ROOT.'public';
  163. $_img_path = '/upload/qrcode/'.$src_ag_id.'.png';
  164. $_destination = $_root_path.$_img_path;
  165. if (file_exists($_destination)) {
  166. return STATICSITE.$_img_path;
  167. }
  168. $_path = dirname($_destination);
  169. if (false == is_dir($_path)) {
  170. mkdir($_path, 0755, true);
  171. }
  172. $_url = SDKSITE.'/down/down?id='.$src_ag_id;
  173. // $_url = url('down/down/index', ['id' => $src_ag_id], false, SDKSITE);
  174. $qrCode = new QrCode($_url);
  175. $qrCode->setSize(300);
  176. $qrCode->setWriterByName('png');
  177. $qrCode->setMargin(10);
  178. $qrCode->setEncoding('UTF-8');
  179. $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
  180. $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
  181. $_len = strlen(STATICSITE);
  182. $_icon_path = substr($icon_url, $_len);
  183. if (is_file($_root_path.$_icon_path)) {
  184. $qrCode->setLogoPath($_root_path.$_icon_path);
  185. }
  186. $qrCode->setLogoWidth(50);
  187. $qrCode->setValidateResult(false);
  188. $qrCode->writeFile($_destination);
  189. return STATICSITE.$_img_path;
  190. }
  191. /* **** 买量相关 start ***** */
  192. /**
  193. * 浏览次数统计
  194. */
  195. public function visitLog() {
  196. /*1 获取agent_id跟app_id,ip*/
  197. $_agent_id = request()->param('agent_id', 0);
  198. $_app_id = request()->param('app_id', 0);
  199. $_ip = request()->ip();
  200. /*2 插入记录表 promotion_visit_log*/
  201. $_log_data['agent_id'] = $_agent_id;
  202. $_log_data['app_id'] = $_app_id;
  203. $_log_data['ip'] = $_ip;
  204. $_log_data['create_time'] = time();
  205. $_rs = Db::name('promotion_visit_log')->insert($_log_data);
  206. /*3 插入每日记录 */
  207. $_check_map['agent_id'] = $_agent_id;
  208. $_check_map['app_id'] = $_app_id;
  209. $_check_map['ip'] = $_ip;
  210. $_check = Db::name('promotion_visit_log')->where($_check_map)->count();
  211. /*3.1 查看promotion_day_log中是否有记录*/
  212. $_day_check_map = array();
  213. $_day_check_map['agent_id'] = $_agent_id;
  214. $_day_check_map['app_id'] = $_app_id;
  215. $_day_check_map['date'] = date('Y-m-d');
  216. $_day_check = Db::name('promotion_day_log')->where($_day_check_map)->find();
  217. if (empty($_day_check)) {
  218. /*3.2 不存在则插入*/
  219. $_inster_data['agent_id'] = $_agent_id;
  220. $_inster_data['app_id'] = $_app_id;
  221. $_inster_data['date'] = date('Y-m-d');
  222. $_inster_data['visit_cnt'] = 1;
  223. $_inster_data['distinct_visit_cnt'] = 1;
  224. $_rrs = Db::name('promotion_day_log')->insert($_inster_data);
  225. } else {
  226. if (is_object($_day_check)) {
  227. $_day_check = $_day_check->toArray();
  228. }
  229. /*3.3 存在则修改*/
  230. if ($_check <= 1) {
  231. $_day_data['distinct_visit_cnt'] = $_day_check['distinct_visit_cnt'] + 1;
  232. }
  233. $_day_data['visit_cnt'] = $_day_check['visit_cnt'] + 1;
  234. $_rrs = Db::name('promotion_day_log')->where($_day_check_map)->update($_day_data);
  235. }
  236. }
  237. /**
  238. * 下载次数统计
  239. */
  240. public function downLog() {
  241. /*1 获取agent_id跟app_id,ip*/
  242. $_agent_id = request()->param('agent_id', 0);
  243. $_app_id = request()->param('app_id', 0);
  244. $_ip = request()->ip();
  245. /*2 插入记录表 promotion_down_log*/
  246. $_log_data['agent_id'] = $_agent_id;
  247. $_log_data['app_id'] = $_app_id;
  248. $_log_data['ip'] = $_ip;
  249. $_log_data['create_time'] = time();
  250. $_rs = Db::name('promotion_down_log')->insert($_log_data);
  251. /*3 插入每日记录 */
  252. $_check_map = array();
  253. $_check_map['agent_id'] = $_agent_id;
  254. $_check_map['app_id'] = $_app_id;
  255. $_check_map['ip'] = $_ip;
  256. $_check = Db::name('promotion_down_log')->where($_check_map)->count();
  257. /*3.1 查看promotion_day_log中是否有记录*/
  258. $_day_check_map['agent_id'] = $_agent_id;
  259. $_day_check_map['app_id'] = $_app_id;
  260. $_day_check_map['date'] = date('Y-m-d');
  261. $_day_check = Db::name('promotion_day_log')->where($_day_check_map)->find();
  262. if (empty($_day_check)) {
  263. /*3.2 不存在则插入*/
  264. $_inster_data['agent_id'] = $_agent_id;
  265. $_inster_data['app_id'] = $_app_id;
  266. $_inster_data['date'] = date('Y-m-d');
  267. $_inster_data['down_cnt'] = 1;
  268. $_inster_data['distinct_down_cnt'] = 1;
  269. $_rrs = Db::name('promotion_day_log')->insert($_inster_data);
  270. } else {
  271. if (is_object($_day_check)) {
  272. $_day_check = $_day_check->toArray();
  273. }
  274. /*3.3 存在则修改*/
  275. if ($_check <= 1) {
  276. $_day_data['distinct_down_cnt'] = $_day_check['distinct_down_cnt'] + 1;
  277. }
  278. $_day_data['down_cnt'] = $_day_check['down_cnt'] + 1;
  279. $_rrs = Db::name('promotion_day_log')->where($_day_check_map)->update($_day_data);
  280. }
  281. }
  282. /**
  283. * 获取模板
  284. *
  285. * @param $agent_game_id
  286. *
  287. * @return array|string
  288. */
  289. public function getTemp($agent_game_id) {
  290. if (empty($agent_game_id)) {
  291. return '';
  292. }
  293. $_ag_map['id'] = $agent_game_id;
  294. $_agent_game_data = Db::name('agent_game')->where($_ag_map)->find();
  295. if (empty($_agent_game_data)) {
  296. return '';
  297. }
  298. $_lp_map['agent_id'] = $_agent_game_data['agent_id'];
  299. $_lp_map['app_id'] = $_agent_game_data['app_id'];
  300. $_access_url = Db::name('promotion_detail')->where($_lp_map)->value('access_url');
  301. if (empty($_access_url)) {
  302. return '';
  303. }
  304. $_landing_page_id = Db::name('promotion_detail')->where($_lp_map)->value('landing_page_id');
  305. if (empty($_landing_page_id)) {
  306. return '';
  307. }
  308. $_src_map['id'] = $_landing_page_id;
  309. $_glp_data_src_url = Db::name('game_landing_page')->where($_src_map)->value('src_url');
  310. if (empty($_glp_data_src_url)) {
  311. return '';
  312. }
  313. $_rdata = [
  314. 'access_url' => "down/".$_access_url."/index",
  315. 'src_url' => $_glp_data_src_url,
  316. ];
  317. return $_rdata;
  318. }
  319. /**
  320. * 获取落地页信息
  321. *
  322. * @param int $lp_id
  323. *
  324. * @return array|string
  325. */
  326. public function getLandpageData($lp_id = 0) {
  327. if (empty($lp_id)) {
  328. return '';
  329. }
  330. $_src_map['id'] = $lp_id;
  331. $_glp_data = Db::name('game_landing_page')->where($_src_map)->find();
  332. if (empty($_glp_data)) {
  333. return '';
  334. }
  335. $_rdata = [
  336. 'access_url' => "down/".$_glp_data['app_id'].'/'.$_glp_data['id']."/index",
  337. 'src_url' => $_glp_data['src_url'],
  338. ];
  339. return $_rdata;
  340. }
  341. /* **** 买量相关 end ***** */
  342. }