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;
}