From 2c8ad072cb2861276d5ec4dedc60fa6f90587ce6 Mon Sep 17 00:00:00 2001 From: filesite Date: Sat, 8 Oct 2022 22:32:53 +0800 Subject: [PATCH] http status support for errors --- controller/ApiController.php | 19 +++++++++++++------ controller/Controller.php | 25 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/controller/ApiController.php b/controller/ApiController.php index 6732dfa..e14a0cf 100644 --- a/controller/ApiController.php +++ b/controller/ApiController.php @@ -10,6 +10,13 @@ use Gregwar\Captcha\CaptchaBuilder; Class ApiController extends Controller { protected $version = '1.0'; + protected $httpStatus = array( + 'notLogined' => 401, + 'notPurchased' => 402, + 'notAllowed' => 403, + 'notFound' => 404, + 'systemError' => 500, + ); //show api list public function actionIndex() { @@ -39,7 +46,7 @@ Class ApiController extends Controller { if ($this->isUserLogined() == false) { $err = '没登陆或登陆已过期!'; - return $this->renderJson(compact('code', 'msg', 'err', 'data')); + return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); } $scanner = new DirScanner(); @@ -92,7 +99,7 @@ Class ApiController extends Controller { if ($this->isUserLogined() == false) { $err = '没登陆或登陆已过期!'; - return $this->renderJson(compact('code', 'msg', 'err', 'data')); + return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); } @@ -145,7 +152,7 @@ Class ApiController extends Controller { if ($this->isUserLogined() == false) { $err = '没登陆或登陆已过期!'; - return $this->renderJson(compact('code', 'msg', 'err', 'data')); + return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); } @@ -192,7 +199,7 @@ Class ApiController extends Controller { if ($this->isUserLogined() == false) { $err = '没登陆或登陆已过期!'; - return $this->renderJson(compact('code', 'msg', 'err', 'data')); + return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); } @@ -237,7 +244,7 @@ Class ApiController extends Controller { if ($this->isUserLogined() == false) { $err = '没登陆或登陆已过期!'; - return $this->renderJson(compact('code', 'msg', 'err', 'data')); + return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); } @@ -285,7 +292,7 @@ Class ApiController extends Controller { if ($this->isUserLogined() == false) { $err = '没登陆或登陆已过期!'; - return $this->renderJson(compact('code', 'msg', 'err', 'data')); + return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); } diff --git a/controller/Controller.php b/controller/Controller.php index efc1662..77ce9fa 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -79,13 +79,36 @@ Class Controller { } //render json data - protected function renderJson($data) { + protected function renderJson($data, $httpStatus = 200) { if (!empty(FSC::$app['config']['debug'])) { $end_time = microtime(true); $data['page_time_cost'] = ceil( ($end_time - FSC::$app['start_time']) * 1000 ); //ms } header("Content-Type: application/json; charset=utf-8"); + if ($httpStatus != 200 && is_numeric($httpStatus)) { + $title = "HTTP/1.0 {$httpStatus} Internal Server Error"; + switch($httpStatus) { + case 401: + $title = "HTTP/1.0 {$httpStatus} 未授权"; + break; + case 402: + $title = "HTTP/1.0 {$httpStatus} 未购买"; + break; + case 403: + $title = "HTTP/1.0 {$httpStatus} 禁止访问"; + break; + case 404: + $title = "HTTP/1.0 {$httpStatus} 不存在"; + break; + case 500: + $title = "HTTP/1.0 {$httpStatus} 系统错误"; + break; + } + + header($title, true, $httpStatus); + } + echo json_encode($data); exit; }