GameController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * GameController.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/2 14:06
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace api\sdk\controller;
  13. use api\common\controller\V2ApiBaseController;
  14. use huo\controller\common\HuoCookie;
  15. use huo\controller\common\HuoSession;
  16. use huo\controller\game\Game;
  17. use huo\controller\game\GameCache;
  18. use huo\controller\member\Member;
  19. use huo\controller\member\MemCache;
  20. use huo\model\log\MemLoginLogModel;
  21. use huo\model\member\MemoauthModel;
  22. use huolib\constant\GameConst;
  23. use huolib\status\GameStatus;
  24. use huolib\tool\SimpleSec;
  25. use huolib\tool\StrUtils;
  26. use think\Config;
  27. class GameController extends V2ApiBaseController {
  28. function _initialize() {
  29. parent::_initialize();
  30. Config::set('default_return_type', 'html');
  31. /* Modified by chenbingling BEGIN 2021/6/1 ISSUES:#14890 处理游戏URL携带token与当前浏览器存储 MTT不一致问题 */
  32. $_url_token = $this->request->param('token/s', '');
  33. $_mtt_token = HuoCookie::getMemToken();
  34. if (!empty($_url_token) && !empty($_mtt_token) && $_url_token != $_mtt_token) {
  35. $_param = $this->request->param();
  36. $_param['token'] = $_mtt_token;
  37. $_param_str = StrUtils::createLinkString($_param);
  38. $_base_url = $this->request->baseUrl();
  39. $_url = $this->request->root(true);
  40. $_url = StrUtils::getUrl($_url.$_base_url).$_param_str;
  41. $this->redirect($_url);
  42. }
  43. /* END 2021/6/1 ISSUES:#14890 */
  44. }
  45. /**
  46. * 游戏地址 父frame
  47. * http://doc.1tsdk.com/138?page_id=3204
  48. * 【域名】/game/game/:game_id/agent/:agent_id
  49. *
  50. * 【域名】/game
  51. *
  52. * @param int $game_id
  53. * @param int $agent_id
  54. *
  55. * @return mixed
  56. */
  57. public function index($game_id = 0, $agent_id = 0) {
  58. $_game_id = $game_id;
  59. $_agent_id = $agent_id;
  60. if (empty($_game_id)) {
  61. $_game_id = $_agent_id = get_val($this->rq_data, 'game_id', 0);
  62. }
  63. if (empty($_game_id)) {
  64. $_code = GameStatus::GAME_ID_EMPTY;
  65. $this->error(GameStatus::getMsg($_code), [], $_code);
  66. }
  67. if (empty($_agent_id)) {
  68. $_agent_id = get_val($this->rq_data, 'agent_id', 0);
  69. }
  70. if (!empty($_agent_id)) {
  71. (new HuoSession($this->mem_id, $_game_id))->setAgentId($agent_id);
  72. }
  73. $_game_info = GameCache::ins()->getInfoByAppId($_game_id);
  74. if (empty($_game_info)) {
  75. $_code = GameStatus::GAME_NOT_EXISTS;
  76. $this->error(GameStatus::getMsg($_code), [], $_code);
  77. }
  78. $this->assign('down_url', $this->request->url(true));
  79. $this->assign('game_name', $_game_info['name']);
  80. $this->assign('game_icon', $_game_info['icon']);
  81. $_ext_info = !empty($_game_info['ext_info']) ? $_game_info['ext_info'] : [];
  82. $_login_back_img = !empty($_ext_info['login_back_img']) ? cmf_get_image_url($_ext_info['login_back_img'])
  83. : '';
  84. $this->assign('login_back_img', $_login_back_img);
  85. $this->assign('game_id', $_game_id);
  86. $this->assign('agent_id', $_agent_id);
  87. $_game_class = new Game();
  88. $_game_url = $_game_class->getH5GameCpUrl($_game_id);
  89. $_request_url = $this->request->url(true);
  90. /* 判断游戏地址是否为HTTPS开头 */
  91. if ('https' == parse_url($_game_url, PHP_URL_SCHEME)) {
  92. /* 重定向HTTPS域名 */
  93. if ('http' == parse_url($_request_url, PHP_URL_SCHEME)) {
  94. $_request_url = preg_replace("/^http/", "https", $_request_url);
  95. $this->redirect($_request_url);
  96. }
  97. } else {
  98. /* 重定向HTTP域名 */
  99. if ('https' == parse_url($_request_url, PHP_URL_SCHEME)) {
  100. $_request_url = preg_replace("/^https/", "http", $_request_url);
  101. $this->redirect($_request_url);
  102. }
  103. }
  104. /* 判断是否登陆 */
  105. if (empty($this->mem_id)) {
  106. /* 未登陆 显示登陆页面 */
  107. $this->assign('game_id', $_game_id);
  108. $this->assign('agent_id', $_agent_id);
  109. return $this->fetch('game/login');
  110. } else {
  111. $_mem_data = MemCache::ins()->getInfoById($this->mem_id);
  112. $_mg_data = (new Member())->memGameLogin($_mem_data, $game_id);
  113. $_mem_data['mg_mem_id'] = $_mg_data['id'];
  114. $this->insertLoginLog($_mem_data, $_game_id);
  115. }
  116. $_open_id = (new MemoauthModel())->getOpenidByMemId('', $this->mem_id);
  117. /*微信环境,设置微信登陆的open_id,为切换账号后支付做准备*/
  118. if (!empty($_open_id) && $this->request->isWeixin()) {
  119. $_from = (new MemoauthModel())->getTypeByMemId($this->mem_id);
  120. if (in_array($_from, ['weixin', 'mp'])) {
  121. HuoCookie::setOpenId($_open_id);
  122. }
  123. }
  124. $_map['game_id'] = $_game_id;
  125. $_map['agent_id'] = $_agent_id;
  126. $_map['token'] = $this->token;
  127. $_play_url = H5ISITE.'/'.'play?'.StrUtils::createLinkString($_map);
  128. $this->assign('play_url', $_play_url);
  129. $_data = (new Member())->getMemInfo($this->mem_id);
  130. $_game_data = (new Game())->getDetail($_game_id);
  131. $this->assign('userinfo', $_data);
  132. $this->assign('game', $_game_data['data']);
  133. return $this->fetch('game/index');
  134. }
  135. /**
  136. * H5-游戏地址校验登陆页
  137. * http://doc.1tsdk.com/138?page_id=3152
  138. * 【域名】/play/game/:game_id/agent/:agent_id
  139. * 【域名】/play
  140. *
  141. * @param int $game_id
  142. * @param int $agent_id
  143. *
  144. * @return mixed
  145. */
  146. public function play($game_id = 0, $agent_id = 0) {
  147. $_game_id = $game_id;
  148. $_agent_id = $agent_id;
  149. if (empty($game_id)) {
  150. $_game_id = get_val($this->rq_data, 'game_id', 0);
  151. }
  152. if (empty($_game_id)) {
  153. $_code = GameStatus::GAME_ID_EMPTY;
  154. $this->error(GameStatus::getMsg($_code), [], $_code);
  155. }
  156. if (empty($agent_id)) {
  157. $_agent_id = get_val($this->rq_data, 'agent_id', 0);
  158. }
  159. if (!empty($_agent_id)) {
  160. (new HuoSession($this->mem_id, $_game_id))->setAgentId($_agent_id);
  161. }
  162. /* 拼接游戏地址 */
  163. $_game_class = new Game();
  164. $_play_url = $_game_class->getPlayUrl($_game_id);
  165. if (is_numeric($_play_url)) {
  166. $this->error(GameStatus::getMsg($_play_url), [], $_play_url);
  167. }
  168. $_play_url = htmlspecialchars_decode($_play_url);
  169. $_app_key = $_game_class->getAppkey($_game_id);
  170. /* 判断是否登陆 */
  171. if (empty($this->mem_id)) {
  172. $_game_info = GameCache::ins()->getInfoByAppId($_game_id);
  173. if (empty($_game_info)) {
  174. $_code = GameStatus::GAME_NOT_EXISTS;
  175. $this->error(GameStatus::getMsg($_code), [], $_code);
  176. }
  177. $this->assign('down_url', $this->request->url(true));
  178. $this->assign('game_name', $_game_info['name']);
  179. $this->assign('game_icon', $_game_info['icon']);
  180. $_ext_info = !empty($_game_info['ext_info']) ? $_game_info['ext_info'] : [];
  181. $_login_back_img = !empty($_ext_info['login_back_img']) ? cmf_get_image_url($_ext_info['login_back_img'])
  182. : '';
  183. $this->assign('login_back_img', $_login_back_img);
  184. /* 未登陆 显示登陆页面 */
  185. $this->assign('game_id', $_game_id);
  186. $this->assign('agent_id', $_agent_id);
  187. return $this->fetch('game/login');
  188. }
  189. $_mem_data = MemCache::ins()->getInfoById($this->mem_id);
  190. $_mg_data = (new Member())->memGameLogin($_mem_data, $game_id);
  191. $_param['app_id'] = $game_id;
  192. $_param['mem_id'] = !empty($_mg_data['id']) ? $_mg_data['id'] : $this->mem_id;
  193. $_token = session_id();
  194. if (empty($_token)) {
  195. $_token = HuoCookie::getMemToken();
  196. }
  197. $_param['user_token'] = SimpleSec::encode($_token, config('CPAUTHCODE'));
  198. $_param_str = StrUtils::createLinkString($_param);
  199. $_sign = md5($_param_str.'&app_key='.$_app_key);
  200. $_param_str = $_param_str.'&sign='.$_sign;
  201. return redirect(StrUtils::getUrl($_play_url).$_param_str);
  202. }
  203. /**
  204. * 插入登陆记录
  205. *
  206. */
  207. public function insertLoginLog($mem_data, $game_id) {
  208. /* 插入mem_game */
  209. $_data['mem_id'] = $mem_data['id'];
  210. $_data['mg_mem_id'] = $mem_data['mg_mem_id'];
  211. $_data['agent_id'] = $mem_data['agent_id'];
  212. $_data['agent_game'] = '';
  213. $_data['ip'] = request()->ip();
  214. $_data['from'] = GameConst::GAME_H5;
  215. $_data['app_id'] = $game_id;
  216. $_data['reg_time'] = get_val($mem_data, 'create_time', 0);
  217. $_data['create_time'] = time();
  218. $_data['reg_app_id'] = $mem_data['app_id'];
  219. return (new MemLoginLogModel())->insertLog($_data);
  220. }
  221. }