You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.7 KiB
63 lines
1.7 KiB
<?php
|
|
declare (strict_types=1);
|
|
/**
|
|
* 工程基类
|
|
* @since 2017/02/28 创建
|
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
|
*/
|
|
|
|
namespace app\controller\api;
|
|
|
|
use app\BaseController;
|
|
use app\util\ReturnCode;
|
|
use think\facade\Env;
|
|
use think\Response;
|
|
|
|
class Base extends BaseController {
|
|
|
|
private $debug = [];
|
|
protected $userInfo = [];
|
|
|
|
public function _initialize() {
|
|
// $this->userInfo = ''; 这部分初始化用户信息可以参考admin模块下的Base去自行处理
|
|
}
|
|
|
|
public function buildSuccess(array $data = [], string $msg = '操作成功', int $code = ReturnCode::SUCCESS): Response {
|
|
$return = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
if (Env::get('APP_DEBUG') && $this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
|
|
return json($return);
|
|
}
|
|
|
|
public function buildFailed(int $code, string $msg = '操作失败', array $data = []): Response {
|
|
$return = ['code' => $code, 'msg' => $msg, 'data' => $data];
|
|
if (Env::get('APP_DEBUG') && $this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
// 根据 code 映射 HTTP 状态码
|
|
switch($code) {
|
|
case ReturnCode::AUTH_ERROR:
|
|
case ReturnCode::ACCESS_TOKEN_TIMEOUT:
|
|
$httpCode = 401;
|
|
break;
|
|
case ReturnCode::INVALID:
|
|
$httpCode = 404;
|
|
//ReturnCode::NO_PERMISSION => 403,
|
|
default :
|
|
$httpCode = 400;
|
|
};
|
|
return json($return, $httpCode);
|
|
}
|
|
|
|
protected function debug($data): void {
|
|
if ($data) {
|
|
$this->debug[] = $data;
|
|
}
|
|
}
|
|
}
|
|
|