CpController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\sdk\controller\v8;
  13. use huo\controller\common\HuoSession;
  14. use huo\controller\game\Game;
  15. use huo\controller\member\Member;
  16. use huoIdentify\controller\Identify;
  17. use huolib\constant\DeviceTypeConst;
  18. use huolib\tool\SimpleSec;
  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. $_data = [];
  32. /* Modified by chenbingling BEGIN 2019/12/5 ISSUES:10837 新增实名认证信息返回 */
  33. if (1 == $status && !empty($this->mem_id)) {
  34. $_data = (new Identify())->getIdentifyByMemId($this->mem_id, true, $this->app_id);
  35. }
  36. /* END 2019/12/5 ISSUES:10837 */
  37. $_rdata = array(
  38. 'status' => $status,
  39. 'msg' => $msg,
  40. 'data' => $_data
  41. );
  42. echo json_encode($_rdata);
  43. exit;
  44. }
  45. /**
  46. * CP登陆用户验证
  47. * http://doc.1tsdk.com/138?page_id=2953
  48. * 【域名】/cp/user/check
  49. *
  50. */
  51. public function check() {
  52. /* 1 查询是否具有访问权限 */
  53. $_rs = $this->checkAuth();
  54. $_url_data = $this->request->param();
  55. $this->app_id = get_val($_url_data, 'app_id');
  56. $this->mg_mem_id = get_val($_url_data, 'mem_id');
  57. $this->user_token = get_val($_url_data, 'user_token');
  58. $this->sign = get_val($_url_data, 'sign');
  59. /* 0 检查参数 */
  60. $this->checkParam();
  61. /* 11 校验APPID */
  62. $this->checkAppid();
  63. /* 13 校验user_token */
  64. $this->getSession();
  65. /* 15 校验玩家 */
  66. $this->checkUser();
  67. /* 12 校验签名 */
  68. $this->verifySign();
  69. /* 16 检查访问次数 */
  70. $this->checkCnt();
  71. $this->cpReturn('1', '验证成功');
  72. }
  73. /**
  74. * @return bool
  75. */
  76. private function checkAppid() {
  77. $_app_key = (new Game())->getAppKey($this->app_id);
  78. if (empty($_app_key)) {
  79. $this->cpReturn('11', '游戏ID(app_id)错误a');
  80. }
  81. $this->app_key = $_app_key;
  82. return true;
  83. }
  84. /**
  85. * @return bool
  86. */
  87. private function checkUser() {
  88. $_mg_mem_id = (new HuoSession($this->mem_id, $this->app_id))->getMgMemId();
  89. if ($_mg_mem_id != $this->mg_mem_id) {
  90. $this->cpReturn('15', '玩家未登陆');
  91. }
  92. return true;
  93. }
  94. /**
  95. * 1 校验参数
  96. */
  97. private function checkParam() {
  98. if (empty($this->app_id) || $this->app_id < 0) {
  99. $this->cpReturn('0', '请求参数为空 app_id');
  100. }
  101. if (empty($this->mg_mem_id) || $this->mg_mem_id < 0) {
  102. $this->cpReturn('0', '请求参数为空 mem_id');
  103. }
  104. if (empty($this->user_token)) {
  105. $this->cpReturn('0', '请求参数为空 user_token');
  106. }
  107. if (empty($this->sign)) {
  108. $this->cpReturn('0', '请求参数为空 sign');
  109. }
  110. }
  111. /**
  112. * 校验权限
  113. *
  114. * @return bool
  115. */
  116. private function checkAuth() {
  117. // $this->cpReturn('100','没有接口访问权限');
  118. return true;
  119. }
  120. /**
  121. * 检查次数
  122. *
  123. * @return bool
  124. */
  125. private function checkCnt() {
  126. // $this->cpReturn('16','访问太频繁,超过访问次数');
  127. $_cnt = HuoSession::getCpCheckCnt($this->user_token);
  128. if (empty($_cnt)) {
  129. $_cnt = 0;
  130. }
  131. $_cnt++;
  132. HuoSession::setCpCheckCnt($this->user_token, $_cnt);
  133. return true;
  134. }
  135. /*12 校验签名 */
  136. private function verifySign() {
  137. $_signstr = "app_id=".$this->app_id."&mem_id=".$this->mg_mem_id."&user_token=".$this->user_token."&app_key="
  138. .$this->app_key;
  139. $_verify_sign = md5($_signstr);
  140. if ($this->sign != $_verify_sign) {
  141. $this->cpReturn('12', '签名校验不通过');
  142. }
  143. return true;
  144. }
  145. /**
  146. * @return bool
  147. */
  148. private function getSession() {
  149. $_user_token = $this->user_token;
  150. if (empty($_user_token)) {
  151. $this->cpReturn('0', '请求参数为空 user_token');
  152. }
  153. $_session_id = Simplesec::decode($_user_token, config('CPAUTHCODE'));
  154. if (empty($_session_id)) {
  155. $this->cpReturn('13', 'user_token错误');
  156. }
  157. $_device_type = DeviceTypeConst::DEVICE_TYPE_WAP;
  158. $_mem_obj = new Member();
  159. $_mem_id = $_mem_obj->getMemIdByToken($_session_id, $_device_type);
  160. if (empty($_mem_id)) {
  161. $_mem_id = $_mem_obj->getMemIdByToken($_session_id, DeviceTypeConst::DEVICE_TYPE_PC);
  162. if (empty($_mem_id)) {
  163. $_mem_id = $_mem_obj->getMemIdByToken($_session_id, DeviceTypeConst::DEVICE_TYPE_WEIXIN);
  164. if (empty($_mem_id)) {
  165. $this->cpReturn('13', 'user_token错误'.$_session_id);
  166. }
  167. }
  168. }
  169. $this->mem_id = $_mem_id;
  170. return true;
  171. }
  172. }