<?php
/**
 * User: richard
 */

use \GatewayWorker\Lib\Gateway;
require_once "./GatewayWorker/Function/Init.php";

class ApiCrazyWorker extends Init {
    public function Gateway($client_id, $data){
        $ts1 = oo::getMsectime();
        if(!isset($data['uid'], $data['mod'], $data['act'])){
            $res = json_encode([
                'cmd'  => ocmd::$send['Api_Gateway'],
                'code' => -1,
                'data' => [],
                'msg'  => '参数错误',
                'httpIdentifier' => $data['httpIdentifier'],
            ]);
            Gateway::sendToClient($client_id,$this->encryp(json_encode($res)));
            return;
        }

        if(IS_DEBUF){ //客户端抱怨请求连不上的问题，记录每次请求日志
            oo::logs()->debug3([
                'time'   => date("Y-m-d H:i:s"),
                "uid"    => $data['uid'],
                "mod"    => $data['mod'],
                "act"    => $data['act'],
                'status' => 'wait',
            ], 'wsapi_req.log');
        }

        $class = ucfirst(preg_replace("/[^a-zA-Z]/", '', trim($data['mod'])));
        $function = preg_replace("/[^a-zA-Z]/", '', trim($data['act']));
        $file = WWWROOT . "api/services_2/{$class}.php";

        if($function == "list"){
            $function = "listFunction";
        }
        if($function == "main"){
            $function = "mainFunction";
        }

        if (!is_file($file)) {
            $res = json_encode([
                'cmd' => ocmd::$send['Api_Gateway'],
                'code'=>-1,
                'data' => [],
                'msg'=>'no file services.',
                'httpIdentifier' => $data['httpIdentifier'],
            ]);
            Gateway::sendToClient($client_id,$this->encryp($res));
            return;
        }
        if(!in_array($file, get_included_files()) && !class_exists($class)){
            include_once $file;
        }
        $obj = new $class();
        if (!method_exists($obj, $function)) {
            $res = json_encode([
                'cmd' => ocmd::$send['Api_Gateway'],
                'code'=>-1,
                'data' => [],
                'msg'=>'act参数错误',
                'httpIdentifier' => $data['httpIdentifier'],
            ]);
            Gateway::sendToClient($client_id,$this->encryp(json_encode($res)));
            return;
        }
        unset($data['cmd']);
        $result = $obj->$function($data);
        //Gateway::sendToUid($client_id, $this->encryp($result));
        $sendArr = [
            'mod'  => $data['mod'],
            'act'  => $data['act'],
            'cmd'  => ocmd::$send['Api_Gateway'],
            'data' => (array)json_decode($result, true), //先decode
            'httpIdentifier' => $data['httpIdentifier'],
        ];
        $ts2 = oo::getMsectime();
        if($ts2-$ts1 > 2000){//超时长接口
            oo::logs()->debug3([
                'time'=>date("Y-m-d H:i:s"),
                "uid"=>$data['uid'],
                "expend_ts"=>($ts2-$ts1),
                "mod"=>$data['mod'],
                "act"=>$data['act']
            ], 'wsapi_timeout.log');
        }

        if(IS_DEBUF){ //客户端抱怨请求连不上的问题，记录每次请求日志
            oo::logs()->debug3([
                'time'   => date("Y-m-d H:i:s"),
                "uid"    => $data['uid'],
                "mod"    => $data['mod'],
                "act"    => $data['act'],
                'status' => 'ok',
            ], 'wsapi_req.log');
        }

        Gateway::sendToClient($client_id,$this->encryp(json_encode($sendArr,JSON_NUMERIC_CHECK)));
    }
}
