gateway_new.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. define('IN_WEB', true);
  3. class GatewayNew {
  4. public function start($aRequest) {
  5. //$calStart = oo::timeReleased();
  6. if(isset($aRequest['saToken']) && isset($aRequest['uid'])) {
  7. if(!isset($aRequest['saToken'])) {
  8. return json_encode(['code'=>errorCode::UNAUTHORIZED_REQUEST,'msg'=>'token empty']);
  9. }else{
  10. $token = oo::commonOprRedis('common')->get(okeys::UserToken($aRequest['uid']));
  11. if($token != $aRequest['saToken'] && !IS_DEBUF){
  12. oo::logs()->debug3(["ts"=>date("H:i:s"),"uid"=>$aRequest['uid'],'cacheToke'=>$token,'postToken'=>$aRequest['saToken']],'tokenerr.log');
  13. return json_encode(['code'=>errorCode::API_TIME_EXPIRED,'msg'=>'token error']);
  14. }
  15. }
  16. }else if(isset($aRequest['uid'])){
  17. oo::logs()->debug3(["ts"=>date("H:i:s"),"uid"=>$aRequest['uid'],'postToken'=>$aRequest['saToken']],'tokenerr2.log');
  18. }
  19. if (!is_array($aRequest)) {
  20. return json_encode(['code'=>errorCode::REQUEST_PARAM_ERROR,'msg'=>'request is wrong']);
  21. }
  22. $aRequest['version'] = isset($aRequest['version']) ? $aRequest['version'] : '1.0.0';
  23. if (!empty($aRequest['mod'])) {
  24. $class = preg_replace("/[^a-zA-Z]/", '', trim($aRequest['mod']));
  25. $class = ucfirst($class);
  26. } else {
  27. $class = 'Main';
  28. }
  29. /* 获得 act 名 */
  30. if ( ! empty($aRequest['act'])) {
  31. $function = preg_replace("/[^a-zA-Z]/", '', trim($aRequest['act']));
  32. if ($function == 'list') {
  33. $function = 'listFunction';
  34. } elseif ( $function == 'main') {
  35. $function = 'mainFunction';
  36. }
  37. } else {
  38. $function = 'page';
  39. }
  40. if(!empty($aRequest['frombg'])){
  41. return json_encode(['code'=>-1,'msg'=>'background api forbidden']);
  42. }
  43. //只保留几个响应时间比较长的key
  44. $actKey = $aRequest['mod'].'_'.$aRequest['act'];
  45. $superStoreKeys = ['Friend_SynchronizeFb', 'Friend_receiveGifts', 'Friend_giftList', 'User_enemyList'];
  46. $apiFile = 'services';
  47. if(isset($aRequest['superKey']) && in_array($actKey, $superStoreKeys)) {
  48. $ret = oo::commonOprRedis('statistics')->get(okeys::superKeys($aRequest['uid'],$class,$function,$aRequest['superKey']));
  49. if($ret){
  50. return $ret;
  51. }
  52. }
  53. if(isset($aRequest['service'])){
  54. $apiFile = 'services_2';
  55. }
  56. if($apiFile == 'services'){
  57. //oo::logs()->debug3(['req' => $aRequest], 'oldapi'); //记录哪些老接口还在被调用
  58. return json_encode(['code'=>-1,'msg'=>'old api forbidden']); //老接口停止调用
  59. }
  60. $file = WWWROOT . 'api/'.$apiFile.'/' . $class . '.php';
  61. if(!in_array($file, get_included_files()) && !class_exists($class)){
  62. if (!is_file($file)) {
  63. return json_encode(['code'=>errorCode::REQUEST_PARAM_ERROR,'msg'=>'no file services']);
  64. }
  65. include_once $file;
  66. }
  67. $obj = new $class();
  68. if (!method_exists($obj, $function)) {
  69. return json_encode(['code'=>errorCode::REQUEST_PARAM_ERROR,'msg'=>'no function:'.$function.'in '.$class.$apiFile]);
  70. }
  71. //记录所有api请求记录,未响应的也记录
  72. oo::commonOprModel('statistics')->apiRequestLog('gateway_'.$class, $function);
  73. $ts1 = oo::getMsectime();
  74. //奖励等部分接口请求频率限制
  75. $lock = true;
  76. if(in_array($apiFile, ['services', 'services_2'])){
  77. //$confs = oo::cfg('cmdlimit');
  78. //$lock = true;
  79. //if(!empty($confs[$apiFile]) && in_array((string)$aRequest['mod'].'.'.(string)$aRequest['act'], $confs[$apiFile])){
  80. // $lock = funs::concurrentLock($aRequest['uid'], $apiFile.(string)$aRequest['mod'].(string)$aRequest['act'], 1);
  81. //}
  82. $lock = funs::concurrentLock($aRequest['uid'], $apiFile.(string)$aRequest['mod'].(string)$aRequest['act'], 1);
  83. if(!$lock){ //高频请求
  84. usleep(100000); //延迟100毫秒
  85. }
  86. }
  87. $result = $obj->$function($aRequest);
  88. //echo $result;
  89. if(!$lock){ //高频请求记录详细信息,分析客户重复请求行为
  90. //!in_array($aRequest['mod'].'_'.$aRequest['act'],['Level_build','Goods_list']) && oo::logs()->debug3(['req' => $aRequest, 'rsp' => $result], 'frequentapi.log');
  91. //exit(oo::response(errorCode::API_FREQUENT_REQUESTS));
  92. }
  93. $ts2 = oo::getMsectime();
  94. if(isset($aRequest['superKey']) && in_array($actKey, $superStoreKeys)) {
  95. oo::commonOprRedis('statistics')->setex(okeys::superKeys($aRequest['uid'],$class,$function,$aRequest['superKey']),$result,20);
  96. $lastApi=["mod_act"=>$aRequest['mod'].'_'.$aRequest['act'],"request"=>$aRequest,"result"=>$result,"ts1"=>$ts1,"ts2"=>$ts2];
  97. //oo::commonOprRedis('statistics')->setex(okeys::lastApiReturn($aRequest['uid']),json_encode($lastApi),60*10);//记录最后请求接口
  98. }
  99. if(!in_array($apiFile, ['services', 'services_2'])){
  100. return $result;
  101. }
  102. if($ts2-$ts1>2000){//超时长接口
  103. 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');
  104. }
  105. return $result;
  106. }
  107. }