123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- /**
- * GameController.php UTF-8
- *
- *
- * @date : 2018/5/2 14:06
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace api\sdk\controller;
- use api\common\controller\V2ApiBaseController;
- use huo\controller\common\HuoCookie;
- use huo\controller\common\HuoSession;
- use huo\controller\game\Game;
- use huo\controller\game\GameCache;
- use huo\controller\member\Member;
- use huo\controller\member\MemCache;
- use huo\model\log\MemLoginLogModel;
- use huo\model\member\MemoauthModel;
- use huolib\constant\GameConst;
- use huolib\status\GameStatus;
- use huolib\tool\SimpleSec;
- use huolib\tool\StrUtils;
- use think\Config;
- class GameController extends V2ApiBaseController {
- function _initialize() {
- parent::_initialize();
- Config::set('default_return_type', 'html');
- /* Modified by chenbingling BEGIN 2021/6/1 ISSUES:#14890 处理游戏URL携带token与当前浏览器存储 MTT不一致问题 */
- $_url_token = $this->request->param('token/s', '');
- $_mtt_token = HuoCookie::getMemToken();
- if (!empty($_url_token) && !empty($_mtt_token) && $_url_token != $_mtt_token) {
- $_param = $this->request->param();
- $_param['token'] = $_mtt_token;
- $_param_str = StrUtils::createLinkString($_param);
- $_base_url = $this->request->baseUrl();
- $_url = $this->request->root(true);
- $_url = StrUtils::getUrl($_url.$_base_url).$_param_str;
- $this->redirect($_url);
- }
- /* END 2021/6/1 ISSUES:#14890 */
- }
- /**
- * 游戏地址 父frame
- * http://doc.1tsdk.com/138?page_id=3204
- * 【域名】/game/game/:game_id/agent/:agent_id
- *
- * 【域名】/game
- *
- * @param int $game_id
- * @param int $agent_id
- *
- * @return mixed
- */
- public function index($game_id = 0, $agent_id = 0) {
- $_game_id = $game_id;
- $_agent_id = $agent_id;
- if (empty($_game_id)) {
- $_game_id = $_agent_id = get_val($this->rq_data, 'game_id', 0);
- }
- if (empty($_game_id)) {
- $_code = GameStatus::GAME_ID_EMPTY;
- $this->error(GameStatus::getMsg($_code), [], $_code);
- }
- if (empty($_agent_id)) {
- $_agent_id = get_val($this->rq_data, 'agent_id', 0);
- }
- if (!empty($_agent_id)) {
- (new HuoSession($this->mem_id, $_game_id))->setAgentId($agent_id);
- }
- $_game_info = GameCache::ins()->getInfoByAppId($_game_id);
- if (empty($_game_info)) {
- $_code = GameStatus::GAME_NOT_EXISTS;
- $this->error(GameStatus::getMsg($_code), [], $_code);
- }
- $this->assign('down_url', $this->request->url(true));
- $this->assign('game_name', $_game_info['name']);
- $this->assign('game_icon', $_game_info['icon']);
- $_ext_info = !empty($_game_info['ext_info']) ? $_game_info['ext_info'] : [];
- $_login_back_img = !empty($_ext_info['login_back_img']) ? cmf_get_image_url($_ext_info['login_back_img'])
- : '';
- $this->assign('login_back_img', $_login_back_img);
- $this->assign('game_id', $_game_id);
- $this->assign('agent_id', $_agent_id);
- $_game_class = new Game();
- $_game_url = $_game_class->getH5GameCpUrl($_game_id);
- $_request_url = $this->request->url(true);
- /* 判断游戏地址是否为HTTPS开头 */
- if ('https' == parse_url($_game_url, PHP_URL_SCHEME)) {
- /* 重定向HTTPS域名 */
- if ('http' == parse_url($_request_url, PHP_URL_SCHEME)) {
- $_request_url = preg_replace("/^http/", "https", $_request_url);
- $this->redirect($_request_url);
- }
- } else {
- /* 重定向HTTP域名 */
- if ('https' == parse_url($_request_url, PHP_URL_SCHEME)) {
- $_request_url = preg_replace("/^https/", "http", $_request_url);
- $this->redirect($_request_url);
- }
- }
- /* 判断是否登陆 */
- if (empty($this->mem_id)) {
- /* 未登陆 显示登陆页面 */
- $this->assign('game_id', $_game_id);
- $this->assign('agent_id', $_agent_id);
- return $this->fetch('game/login');
- } else {
- $_mem_data = MemCache::ins()->getInfoById($this->mem_id);
- $_mg_data = (new Member())->memGameLogin($_mem_data, $game_id);
- $_mem_data['mg_mem_id'] = $_mg_data['id'];
- $this->insertLoginLog($_mem_data, $_game_id);
- }
- $_open_id = (new MemoauthModel())->getOpenidByMemId('', $this->mem_id);
- /*微信环境,设置微信登陆的open_id,为切换账号后支付做准备*/
- if (!empty($_open_id) && $this->request->isWeixin()) {
- $_from = (new MemoauthModel())->getTypeByMemId($this->mem_id);
- if (in_array($_from, ['weixin', 'mp'])) {
- HuoCookie::setOpenId($_open_id);
- }
- }
- $_map['game_id'] = $_game_id;
- $_map['agent_id'] = $_agent_id;
- $_map['token'] = $this->token;
- $_play_url = H5ISITE.'/'.'play?'.StrUtils::createLinkString($_map);
- $this->assign('play_url', $_play_url);
- $_data = (new Member())->getMemInfo($this->mem_id);
- $_game_data = (new Game())->getDetail($_game_id);
- $this->assign('userinfo', $_data);
- $this->assign('game', $_game_data['data']);
- return $this->fetch('game/index');
- }
- /**
- * H5-游戏地址校验登陆页
- * http://doc.1tsdk.com/138?page_id=3152
- * 【域名】/play/game/:game_id/agent/:agent_id
- * 【域名】/play
- *
- * @param int $game_id
- * @param int $agent_id
- *
- * @return mixed
- */
- public function play($game_id = 0, $agent_id = 0) {
- $_game_id = $game_id;
- $_agent_id = $agent_id;
- if (empty($game_id)) {
- $_game_id = get_val($this->rq_data, 'game_id', 0);
- }
- if (empty($_game_id)) {
- $_code = GameStatus::GAME_ID_EMPTY;
- $this->error(GameStatus::getMsg($_code), [], $_code);
- }
- if (empty($agent_id)) {
- $_agent_id = get_val($this->rq_data, 'agent_id', 0);
- }
- if (!empty($_agent_id)) {
- (new HuoSession($this->mem_id, $_game_id))->setAgentId($_agent_id);
- }
- /* 拼接游戏地址 */
- $_game_class = new Game();
- $_play_url = $_game_class->getPlayUrl($_game_id);
- if (is_numeric($_play_url)) {
- $this->error(GameStatus::getMsg($_play_url), [], $_play_url);
- }
- $_play_url = htmlspecialchars_decode($_play_url);
- $_app_key = $_game_class->getAppkey($_game_id);
- /* 判断是否登陆 */
- if (empty($this->mem_id)) {
- $_game_info = GameCache::ins()->getInfoByAppId($_game_id);
- if (empty($_game_info)) {
- $_code = GameStatus::GAME_NOT_EXISTS;
- $this->error(GameStatus::getMsg($_code), [], $_code);
- }
- $this->assign('down_url', $this->request->url(true));
- $this->assign('game_name', $_game_info['name']);
- $this->assign('game_icon', $_game_info['icon']);
- $_ext_info = !empty($_game_info['ext_info']) ? $_game_info['ext_info'] : [];
- $_login_back_img = !empty($_ext_info['login_back_img']) ? cmf_get_image_url($_ext_info['login_back_img'])
- : '';
- $this->assign('login_back_img', $_login_back_img);
- /* 未登陆 显示登陆页面 */
- $this->assign('game_id', $_game_id);
- $this->assign('agent_id', $_agent_id);
- return $this->fetch('game/login');
- }
- $_mem_data = MemCache::ins()->getInfoById($this->mem_id);
- $_mg_data = (new Member())->memGameLogin($_mem_data, $game_id);
- $_param['app_id'] = $game_id;
- $_param['mem_id'] = !empty($_mg_data['id']) ? $_mg_data['id'] : $this->mem_id;
- $_token = session_id();
- if (empty($_token)) {
- $_token = HuoCookie::getMemToken();
- }
- $_param['user_token'] = SimpleSec::encode($_token, config('CPAUTHCODE'));
- $_param_str = StrUtils::createLinkString($_param);
- $_sign = md5($_param_str.'&app_key='.$_app_key);
- $_param_str = $_param_str.'&sign='.$_sign;
- return redirect(StrUtils::getUrl($_play_url).$_param_str);
- }
- /**
- * 插入登陆记录
- *
- */
- public function insertLoginLog($mem_data, $game_id) {
- /* 插入mem_game */
- $_data['mem_id'] = $mem_data['id'];
- $_data['mg_mem_id'] = $mem_data['mg_mem_id'];
- $_data['agent_id'] = $mem_data['agent_id'];
- $_data['agent_game'] = '';
- $_data['ip'] = request()->ip();
- $_data['from'] = GameConst::GAME_H5;
- $_data['app_id'] = $game_id;
- $_data['reg_time'] = get_val($mem_data, 'create_time', 0);
- $_data['create_time'] = time();
- $_data['reg_app_id'] = $mem_data['app_id'];
- return (new MemLoginLogModel())->insertLog($_data);
- }
- }
|