* @version : HuoMp 1.0 */ namespace mini\sdk\controller; use huo\controller\common\HuoSession; use huo\controller\game\Game; use huo\controller\member\Member; use huolib\tool\SimpleSec; use huomp\controller\member\MemberOut; use huomp\model\game\GameMiniModel; use think\Controller; class CpController extends Controller { private $mg_mem_id; 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(); if (false == $_rs) { $this->cpReturn('100', '没有接口访问权限'); } $_url_data = $this->request->param(); $this->app_id = get_val($_url_data, 'app_id'); $this->mg_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(); /* 11 校验APPID */ $this->checkAppid(); /* 13 校验user_token */ $this->getSession(); /* 15 校验玩家 */ $this->checkUser(); /* 12 校验签名 */ $this->verifySign(); /* 16 检查访问次数 */ $this->checkCnt(); $this->cpReturn('1', '验证成功'); } /** * @return bool */ private function checkAppid() { $_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(); $_mg_mem_id = (new MemberOut())->getMgMemId($this->app_id, $this->mem_id); if ($_mg_mem_id != $this->mg_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->mg_mem_id) || $this->mg_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 = HuoSession::getCpCheckCnt($this->user_token); if (empty($_cnt)) { $_cnt = 0; } $_cnt++; HuoSession::setCpCheckCnt($this->user_token, $_cnt); return true; } /*12 校验签名 */ private function verifySign() { $_signstr = "app_id=".$this->app_id."&mem_id=".$this->mg_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错误'); } $_se_app_id = HuoSession::getAppId($_session_id); if ($_se_app_id != $this->app_id) { $this->cpReturn('11', '游戏ID(app_id)错误'); } $_wx_id = (new GameMiniModel())->getMpIdByAppId($_se_app_id); $this->mem_id = (new Member())->getMemIdByToken($_session_id, $_wx_id); if (empty($this->mem_id)) { $this->cpReturn('13', 'user_token错误'); } return true; } }