gateway.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. define('IN_WEB', true);
  3. include(dirname(__FILE__) . "/../lib/ProtocolsEvent.php");//解码
  4. $str = $_REQUEST['param'];
  5. if(isset($str)&&!empty($str)){
  6. ProtocolsEvent::apiDecode($str);
  7. }
  8. include (dirname(__FILE__)."/../com.php");//核心
  9. if(IS_DEBUF){
  10. $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
  11. $allow_origin = array('http://wiki.superant.cc','http://apiwiki.superant.cc');
  12. if(in_array($origin,$allow_origin)){
  13. header('Access-Control-Allow-Origin:'.$origin);
  14. }else{
  15. header('Access-Control-Allow-Origin:*');//允许跨域
  16. }
  17. }else{
  18. header('Access-Control-Allow-Origin:*');//允许跨域
  19. }
  20. class gateway {
  21. public function __construct($aRequest) {
  22. //$calStart = oo::timeReleased();
  23. if(isset($aRequest['saToken']) && isset($aRequest['uid'])) {
  24. if(!isset($aRequest['saToken'])) {
  25. die(json_encode(['code'=>errorCode::UNAUTHORIZED_REQUEST,'msg'=>'token empty']));
  26. }else{
  27. $token = oo::commonOprRedis('common')->get(okeys::UserToken($aRequest['uid']));
  28. if($token != $aRequest['saToken']){
  29. oo::logs()->debug3(["ts"=>date("H:i:s"),"uid"=>$aRequest['uid'],'cacheToke'=>$token,'postToken'=>$aRequest['saToken']],'tokenerr.log');
  30. die(json_encode(['code'=>errorCode::API_TIME_EXPIRED,'msg'=>'token error']));
  31. }
  32. }
  33. }else if(isset($aRequest['uid'])){
  34. oo::logs()->debug3(["ts"=>date("H:i:s"),"uid"=>$aRequest['uid'],'postToken'=>$aRequest['saToken']],'tokenerr2.log');
  35. }
  36. if (!is_array($aRequest)) {
  37. exit('request is wrong.');
  38. }
  39. $aRequest['version'] = isset($aRequest['version']) ? $aRequest['version'] : '1.0.0';
  40. if (!empty($aRequest['mod'])) {
  41. $class = preg_replace("/[^a-zA-Z]/", '', trim($aRequest['mod']));
  42. $class = ucfirst($class);
  43. } else {
  44. $class = 'Main';
  45. }
  46. /* 获得 act 名 */
  47. if ( ! empty($aRequest['act'])) {
  48. $function = preg_replace("/[^a-zA-Z]/", '', trim($aRequest['act']));
  49. if ($function == 'list') {
  50. $function = 'listFunction';
  51. } elseif ( $function == 'main') {
  52. $function = 'mainFunction';
  53. }
  54. } else {
  55. $function = 'page';
  56. }
  57. if(!empty($aRequest['frombg'])){
  58. $apiFile = 'background';
  59. $ip = oo::getIp();
  60. if(!in_array($ip,oo::$config['adminIpList'])){
  61. die(json_encode(['code'=>-1,'ip'=>$ip,'msg'=>'IP ERROR']));
  62. }
  63. }else{
  64. $apiFile = 'services';
  65. if(isset($aRequest['superKey'])) {
  66. $ret = oo::commonOprRedis('statistics')->get(okeys::superKeys($aRequest['uid'],$class,$function,$aRequest['superKey']));
  67. if($ret){
  68. echo $ret;
  69. exit(0);
  70. }
  71. }
  72. if(isset($aRequest['service'])){
  73. $apiFile = 'services_2';
  74. }
  75. }
  76. $file = WWWROOT . 'api/'.$apiFile.'/' . $class . '.php';
  77. if (!is_file($file)) {
  78. exit('no file services.');
  79. }
  80. include $file;
  81. $obj = new $class();
  82. if (!method_exists($obj, $function)) {
  83. exit('no function:'.$function.'in '.$class.$apiFile);
  84. }
  85. $ts1 = oo::getMsectime();
  86. //奖励等部分接口请求频率限制
  87. $lock = true;
  88. if(in_array($apiFile, ['services', 'services_2'])){
  89. //$confs = oo::cfg('cmdlimit');
  90. //$lock = true;
  91. //if(!empty($confs[$apiFile]) && in_array((string)$aRequest['mod'].'.'.(string)$aRequest['act'], $confs[$apiFile])){
  92. // $lock = funs::concurrentLock($aRequest['uid'], $apiFile.(string)$aRequest['mod'].(string)$aRequest['act'], 1);
  93. //}
  94. $lock = funs::concurrentLock($aRequest['uid'], $apiFile.(string)$aRequest['mod'].(string)$aRequest['act'], 1);
  95. if(!$lock){ //高频请求
  96. usleep(100000); //延迟100毫秒
  97. }
  98. }
  99. $result = $obj->$function($aRequest);
  100. echo $result;
  101. if(!$lock){ //高频请求记录详细信息,分析客户重复请求行为
  102. !in_array($aRequest['mod'].'_'.$aRequest['act'],['Level_build','Goods_list']) && oo::logs()->debug3(['req' => $aRequest, 'rsp' => $result], 'frequentapi.log');
  103. //exit(oo::response(errorCode::API_FREQUENT_REQUESTS));
  104. }
  105. $ts2 = oo::getMsectime();
  106. if(isset($aRequest['superKey'])) {
  107. oo::commonOprRedis('statistics')->setex(okeys::superKeys($aRequest['uid'],$class,$function,$aRequest['superKey']),$result,60);
  108. $lastApi=["mod_act"=>$aRequest['mod'].'_'.$aRequest['act'],"request"=>$aRequest,"result"=>$result,"ts1"=>$ts1,"ts2"=>$ts2];
  109. oo::commonOprRedis('statistics')->setex(okeys::lastApiReturn($aRequest['uid']),json_encode($lastApi),60*10);//记录最后请求接口
  110. }
  111. if(!in_array($apiFile, ['services', 'services_2'])){
  112. exit(0);
  113. }
  114. if($ts2-$ts1>2000){//超时长接口
  115. oo::logs()->debug3(['time'=>date("Y-m-d H:i:s"),"uid"=>$aRequest['uid'],"expend_ts"=>($ts2-$ts1),"mod"=>$aRequest['mod'],"act"=>$aRequest['act']], 'expend_ts_http.log');
  116. }
  117. exit(0);
  118. }
  119. }
  120. //$_REQUEST = json_decode(file_get_contents('php://input'), true);
  121. new gateway($_REQUEST);