MyQEE\Server\WorkerAPI::onRequest PHP Method

onRequest() public method

HTTP 接口请求处理的方法
public onRequest ( Swoole\Http\Request $request, Swoole\Http\Response $response )
$request Swoole\Http\Request
$response Swoole\Http\Response
    public function onRequest($request, $response)
    {
        $this->request = $request;
        $this->response = $response;
        $response->header('Content-Type', 'application/json');
        $response->end('{"status":"error","code":0,"msg":"verify fail."}');
        try {
            if (!$this->verify($request)) {
                $response->status(401);
            }
            $uri = $this->uri();
            $file = __DIR__ . '/../../../../api/' . $uri . (substr($uri, -1) === '/' ? 'index' : '') . '.php';
            $this->debug("request api: {$file}");
            if (!is_file($file)) {
                throw new \Exception('can not found api', 1);
            }
            $rs = (include $file);
            if (!$rs) {
                throw new \Exception('api result empty', 2);
            } elseif (is_string($rs)) {
                $rs = ['data' => $rs, 'status' => 'success'];
            } elseif (!is_array($rs)) {
                $rs = (array) $rs;
            }
            if (!isset($rs['status'])) {
                $rs['status'] = 'success';
            }
            $response->end(json_encode($rs, JSON_UNESCAPED_UNICODE));
        } catch (\Exception $e) {
            $response->status(500);
            $response->end(json_encode(['status' => 'error', 'code' => -$e->getCode(), 'msg' => $e->getMessage()], JSON_UNESCAPED_UNICODE));
        }
        $this->request = null;
        $this->response = null;
    }