Browse Source

http status support for errors

master
filesite 2 years ago
parent
commit
2c8ad072cb
  1. 19
      controller/ApiController.php
  2. 25
      controller/Controller.php

19
controller/ApiController.php

@ -10,6 +10,13 @@ use Gregwar\Captcha\CaptchaBuilder;
Class ApiController extends Controller { Class ApiController extends Controller {
protected $version = '1.0'; protected $version = '1.0';
protected $httpStatus = array(
'notLogined' => 401,
'notPurchased' => 402,
'notAllowed' => 403,
'notFound' => 404,
'systemError' => 500,
);
//show api list //show api list
public function actionIndex() { public function actionIndex() {
@ -39,7 +46,7 @@ Class ApiController extends Controller {
if ($this->isUserLogined() == false) { if ($this->isUserLogined() == false) {
$err = '没登陆或登陆已过期!'; $err = '没登陆或登陆已过期!';
return $this->renderJson(compact('code', 'msg', 'err', 'data')); return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']);
} }
$scanner = new DirScanner(); $scanner = new DirScanner();
@ -92,7 +99,7 @@ Class ApiController extends Controller {
if ($this->isUserLogined() == false) { if ($this->isUserLogined() == false) {
$err = '没登陆或登陆已过期!'; $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) { if ($this->isUserLogined() == false) {
$err = '没登陆或登陆已过期!'; $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) { if ($this->isUserLogined() == false) {
$err = '没登陆或登陆已过期!'; $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) { if ($this->isUserLogined() == false) {
$err = '没登陆或登陆已过期!'; $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) { if ($this->isUserLogined() == false) {
$err = '没登陆或登陆已过期!'; $err = '没登陆或登陆已过期!';
return $this->renderJson(compact('code', 'msg', 'err', 'data')); return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']);
} }

25
controller/Controller.php

@ -79,13 +79,36 @@ Class Controller {
} }
//render json data //render json data
protected function renderJson($data) { protected function renderJson($data, $httpStatus = 200) {
if (!empty(FSC::$app['config']['debug'])) { if (!empty(FSC::$app['config']['debug'])) {
$end_time = microtime(true); $end_time = microtime(true);
$data['page_time_cost'] = ceil( ($end_time - FSC::$app['start_time']) * 1000 ); //ms $data['page_time_cost'] = ceil( ($end_time - FSC::$app['start_time']) * 1000 ); //ms
} }
header("Content-Type: application/json; charset=utf-8"); 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); echo json_encode($data);
exit; exit;
} }

Loading…
Cancel
Save