* @version : HUOSDK 8.0 */ namespace api\apple\controller\v8; use huo\controller\common\HuoSession; use huo\controller\game\Game; use huolib\tool\SimpleSec; use think\Config; use think\Controller; use think\Session; class CpController extends Controller { private $mem_id; private $app_id; private $user_token; private $sign; private $app_key; function _initialize() { parent::_initialize(); } private function cpReturn($status = '0', $msg = '请求参数错误') { $_rdata = array( 'status' => $status, 'msg' => $msg ); echo json_encode($_rdata); exit; } /** * CP登陆用户验证 * http://doc.1tsdk.com/138?page_id=2953 * 【域名】/cp/user/check * */ public function check() { /* 1 查询是否具有访问权限 */ $_rs = $this->checkAuth(); $_url_data = $this->request->param(); $this->app_id = get_val($_url_data, 'app_id'); $this->mem_id = get_val($_url_data, 'mem_id'); $this->user_token = get_val($_url_data, 'user_token'); $this->sign = get_val($_url_data, 'sign'); /* 0 检查参数 */ $this->checkParam(); /* 13 校验user_token */ $this->getSession(); /* 15 校验玩家 */ $this->checkUser(); /* 11 校验APPID */ $this->checkAppid(); /* 12 校验签名 */ $this->verifySign(); /* 16 检查访问次数 */ $this->checkCnt(); $this->cpReturn('1', '验证成功'); } /** * @return bool */ private function checkAppid() { $_se_app_id = Session::get('app_id', 'app'); if ($_se_app_id != $this->app_id) { $this->cpReturn('11', '游戏ID(app_id)错误'); } $_app_key = (new Game())->getAppKey($this->app_id); if (empty($_app_key)) { $this->cpReturn('11', '游戏ID(app_id)错误'); } $this->app_key = $_app_key; return true; } /** * @return bool */ private function checkUser() { $_mg_mem_id = (new HuoSession($this->mem_id, $this->app_id))->getMgMemId(); if ($_mg_mem_id != $this->mem_id) { $this->cpReturn('15', '玩家未登陆'); } return true; } /** * 1 校验参数 */ private function checkParam() { if (empty($this->app_id) || $this->app_id < 0) { $this->cpReturn('0', '请求参数为空 app_id'); } if (empty($this->mem_id) || $this->mem_id < 0) { $this->cpReturn('0', '请求参数为空 mem_id'); } if (empty($this->user_token)) { $this->cpReturn('0', '请求参数为空 user_token'); } if (empty($this->sign)) { $this->cpReturn('0', '请求参数为空 sign'); } } /** * 校验权限 * * @return bool */ private function checkAuth() { // $this->cpReturn('100','没有接口访问权限'); return true; } /** * 检查次数 * * @return bool */ private function checkCnt() { // $this->cpReturn('16','访问太频繁,超过访问次数'); $_cnt = Session::get('cnt', 'cp'); if (empty($_cnt)) { $_cnt = 0; } $_cnt++; Session::set('cnt', $_cnt, 'cp'); return true; } /*12 校验签名 */ private function verifySign() { $_signstr = "app_id=".$this->app_id."&mem_id=".$this->mem_id."&user_token=".$this->user_token."&app_key=" .$this->app_key; $_verify_sign = md5($_signstr); if ($this->sign != $_verify_sign) { $this->cpReturn('12', '签名校验不通过'); } return true; } /** * @return bool */ private function getSession() { $_user_token = $this->user_token; if (empty($_user_token)) { $this->cpReturn('0', '请求参数为空 user_token'); } $_session_id = Simplesec::decode($_user_token, config('CPAUTHCODE')); if (empty($_session_id)) { $this->cpReturn('13', 'user_token错误'); } $_config['id'] = $_session_id; Config::set('session.id', $_session_id); Session::boot(); if (!Session::get('mem_id', 'mem')) { $this->cpReturn('13', 'user_token错误'); } return true; } }