CpController.php 4.6 KB

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