GameTestController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * GameTestController.php UTF-8
  4. * 游戏测试
  5. *
  6. * @date : 2018/5/10 14:02
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace api\sdk\controller;
  13. use huo\controller\game\Game;
  14. use huolib\tool\StrUtils;
  15. use think\Config;
  16. use think\Controller;
  17. use think\Log;
  18. class GameTestController extends Controller {
  19. private $mem_id;
  20. private $app_id;
  21. private $user_token;
  22. private $sign;
  23. private $app_key;
  24. function _initialize() {
  25. parent::_initialize();
  26. Config::set('default_return_type', 'html');
  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. * H5-游戏demo地址
  38. * http://doc.1tsdk.com/138?page_id=3216
  39. * 【域名】/game/test
  40. */
  41. public function index() {
  42. /* 1 查询是否具有访问权限 */
  43. $_rs = $this->checkAuth();
  44. $_url_data = $this->request->param();
  45. Log::write($_url_data, Log::LOG);//记录请求数据
  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. /* 11 校验APPID */
  53. $this->checkAppid();
  54. /* 12 校验签名 */
  55. $this->verifySign();
  56. $this->assign('app_id', $this->app_id);
  57. $this->assign('mem_id', $this->mem_id);
  58. return $this->fetch('game/test');
  59. }
  60. /**
  61. * @return bool
  62. */
  63. private function checkAppid() {
  64. $_app_key = (new Game())->getAppKey($this->app_id);
  65. if (empty($_app_key)) {
  66. $this->cpReturn('11', '游戏ID(app_id)错误');
  67. }
  68. $this->app_key = $_app_key;
  69. return true;
  70. }
  71. /**
  72. * 1 校验参数
  73. */
  74. private function checkParam() {
  75. if (empty($this->app_id) || $this->app_id < 0) {
  76. $this->cpReturn('0', '请求参数为空 app_id');
  77. }
  78. if (empty($this->mem_id) || $this->mem_id < 0) {
  79. $this->cpReturn('0', '请求参数为空 mem_id');
  80. }
  81. if (empty($this->user_token)) {
  82. $this->cpReturn('0', '请求参数为空 user_token');
  83. }
  84. if (empty($this->sign)) {
  85. $this->cpReturn('0', '请求参数为空 sign');
  86. }
  87. }
  88. /**
  89. * 校验权限
  90. *
  91. * @return bool
  92. */
  93. private function checkAuth() {
  94. // $this->cpReturn('100','没有接口访问权限');
  95. return true;
  96. }
  97. /*12 校验签名 */
  98. private function verifySign() {
  99. $_param['app_id'] = $this->app_id;
  100. $_param['mem_id'] = $this->mem_id;
  101. $_param['user_token'] = $this->user_token;
  102. $_param_str = StrUtils::createLinkString($_param);
  103. $_verify_sign = md5($_param_str.'&app_key='.$this->app_key);
  104. if ($this->sign != $_verify_sign) {
  105. $this->cpReturn('12', '签名校验不通过');
  106. }
  107. return true;
  108. }
  109. }