CpController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * CpController.php UTF-8
  4. * CP玩家校验
  5. *
  6. * @date : 2018/10/13 17:52
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace mini\sdk\controller;
  13. use huo\controller\common\HuoSession;
  14. use huo\controller\game\Game;
  15. use huo\controller\member\Member;
  16. use huolib\tool\SimpleSec;
  17. use huomp\controller\member\MemberOut;
  18. use huomp\model\game\GameMiniModel;
  19. use think\Controller;
  20. class CpController extends Controller {
  21. private $mg_mem_id;
  22. private $mem_id;
  23. private $app_id;
  24. private $user_token;
  25. private $sign;
  26. private $app_key;
  27. function _initialize() {
  28. parent::_initialize();
  29. }
  30. private function cpReturn($status = '0', $msg = '请求参数错误') {
  31. $_rdata = array(
  32. 'status' => $status,
  33. 'msg' => $msg
  34. );
  35. echo json_encode($_rdata);
  36. exit;
  37. }
  38. /**
  39. * CP登陆用户验证
  40. * http://doc.1tsdk.com/138?page_id=2953
  41. * 【域名】/cp/user/check
  42. *
  43. */
  44. public function check() {
  45. /* 1 查询是否具有访问权限 */
  46. $_rs = $this->checkAuth();
  47. if (false == $_rs) {
  48. $this->cpReturn('100', '没有接口访问权限');
  49. }
  50. $_url_data = $this->request->param();
  51. $this->app_id = get_val($_url_data, 'app_id');
  52. $this->mg_mem_id = get_val($_url_data, 'mem_id');
  53. $this->user_token = get_val($_url_data, 'user_token');
  54. $this->sign = get_val($_url_data, 'sign');
  55. /* 0 检查参数 */
  56. $this->checkParam();
  57. /* 11 校验APPID */
  58. $this->checkAppid();
  59. /* 13 校验user_token */
  60. $this->getSession();
  61. /* 15 校验玩家 */
  62. $this->checkUser();
  63. /* 12 校验签名 */
  64. $this->verifySign();
  65. /* 16 检查访问次数 */
  66. $this->checkCnt();
  67. $this->cpReturn('1', '验证成功');
  68. }
  69. /**
  70. * @return bool
  71. */
  72. private function checkAppid() {
  73. $_app_key = (new Game())->getAppKey($this->app_id);
  74. if (empty($_app_key)) {
  75. $this->cpReturn('11', '游戏ID(app_id)错误');
  76. }
  77. $this->app_key = $_app_key;
  78. return true;
  79. }
  80. /**
  81. * @return bool
  82. */
  83. private function checkUser() {
  84. //$_mg_mem_id = (new HuoSession($this->mem_id, $this->app_id))->getMgMemId();
  85. $_mg_mem_id = (new MemberOut())->getMgMemId($this->app_id, $this->mem_id);
  86. if ($_mg_mem_id != $this->mg_mem_id) {
  87. $this->cpReturn('15', '玩家未登陆');
  88. }
  89. return true;
  90. }
  91. /**
  92. * 1 校验参数
  93. */
  94. private function checkParam() {
  95. if (empty($this->app_id) || $this->app_id < 0) {
  96. $this->cpReturn('0', '请求参数为空 app_id');
  97. }
  98. if (empty($this->mg_mem_id) || $this->mg_mem_id < 0) {
  99. $this->cpReturn('0', '请求参数为空 mem_id');
  100. }
  101. if (empty($this->user_token)) {
  102. $this->cpReturn('0', '请求参数为空 user_token');
  103. }
  104. if (empty($this->sign)) {
  105. $this->cpReturn('0', '请求参数为空 sign');
  106. }
  107. }
  108. /**
  109. * 校验权限
  110. *
  111. * @return bool
  112. */
  113. private function checkAuth() {
  114. // $this->cpReturn('100','没有接口访问权限');
  115. return true;
  116. }
  117. /**
  118. * 检查次数
  119. *
  120. * @return bool
  121. */
  122. private function checkCnt() {
  123. // $this->cpReturn('16','访问太频繁,超过访问次数');
  124. $_cnt = HuoSession::getCpCheckCnt($this->user_token);
  125. if (empty($_cnt)) {
  126. $_cnt = 0;
  127. }
  128. $_cnt++;
  129. HuoSession::setCpCheckCnt($this->user_token, $_cnt);
  130. return true;
  131. }
  132. /*12 校验签名 */
  133. private function verifySign() {
  134. $_signstr = "app_id=".$this->app_id."&mem_id=".$this->mg_mem_id."&user_token=".$this->user_token."&app_key="
  135. .$this->app_key;
  136. $_verify_sign = md5($_signstr);
  137. if ($this->sign != $_verify_sign) {
  138. $this->cpReturn('12', '签名校验不通过');
  139. }
  140. return true;
  141. }
  142. /**
  143. * @return bool
  144. */
  145. private function getSession() {
  146. $_user_token = $this->user_token;
  147. if (empty($_user_token)) {
  148. $this->cpReturn('0', '请求参数为空 user_token');
  149. }
  150. $_session_id = Simplesec::decode($_user_token, config('CPAUTHCODE'));
  151. if (empty($_session_id)) {
  152. $this->cpReturn('13', 'user_token错误');
  153. }
  154. $_se_app_id = HuoSession::getAppId($_session_id);
  155. if ($_se_app_id != $this->app_id) {
  156. $this->cpReturn('11', '游戏ID(app_id)错误');
  157. }
  158. $_wx_id = (new GameMiniModel())->getMpIdByAppId($_se_app_id);
  159. $this->mem_id = (new Member())->getMemIdByToken($_session_id, $_wx_id);
  160. if (empty($this->mem_id)) {
  161. $this->cpReturn('13', 'user_token错误');
  162. }
  163. return true;
  164. }
  165. }