作者 刘朕
1 个管道 的构建 通过 耗费 0 秒

合并分支 'liuzhen' 到 'master'

验证调试



查看合并请求 !77
<?php
use \think\Db;
use app\nsms\nsms;
/**
* 传json字符串
* @param $code
* @param array $data
* @param string $msg
* @param int $errorCode
* @return \think\response\Json
*/
function writeJson($data = [], $code = 0, $msg = 'ok', $errorCode = 200)
{
$data = [
'code' => $code,
'data' => $data,
'msg' => $msg,
'time' => time()
];
return json($data, $errorCode);
}
if (!function_exists('generate_user_token')) {
/**
* 生成token
... ...
... ... @@ -2,6 +2,7 @@
namespace app\api\library;
use app\api\library\Exception\BaseException;
use Exception;
use think\exception\Handle;
... ... @@ -10,28 +11,53 @@ use think\exception\Handle;
*/
class ExceptionHandle extends Handle
{
private $code;
private $msg;
private $errorCode;
public function render(Exception $e)
{
// 在生产环境下返回code信息
if (!\think\Config::get('app_debug')) {
$statuscode = $code = 500;
$msg = 'An error occurred';
// 验证异常
if ($e instanceof \think\exception\ValidateException) {
$code = 0;
$statuscode = 200;
$msg = $e->getError();
}
// Http异常
if ($e instanceof \think\exception\HttpException) {
$statuscode = $code = $e->getStatusCode();
}
return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
if($e instanceof BaseException){
$this->code = $e->code;
$this->msg = $e->msg;
$this->errorCode = $e->errCode;
}elseif($e instanceof \think\exception\ValidateException){
$this->code = -2;
$this->errorCode = 200;
$this->msg = $e->getError();
}
else{
if(config('app_debug')){
return parent::render($e);
}else{
$this->code = 500;
$this->msg ='服务器内部异常';
$this->errorCode = 500;
}
}
return writeJson([],$this->code,$this->msg,$this->errorCode);
// $url = $_SERVER['SERVER_NAME'];
// switch ($url){
// case config("environment.local_url"):
// $this->code = $this->errorCode = 500;
// $this->msg = 'An error occurred';
// // 验证异常
// if ($e instanceof \think\exception\ValidateException) {
// $this->code = -2;
// $this->errorCode = 200;
// $this->msg = $e->getError();
// }
// // Http异常
// else if($e instanceof BaseException){
// $this->code = $e->code;
// $this->msg = $e->msg;
// $this->errorCode = $e->errCode;
// }
// return json(['code' => $this->code, 'msg' => $this->msg, 'time' => time(), 'data' => null], $this->errorCode);
// }
//其它此交由系统处理
return parent::render($e);
// return parent::render($e);
}
}
... ...
... ... @@ -28,6 +28,7 @@ class BaseValidate extends Validate{
$result = $this->check($params);
}
if(!$result){
throw new ValidateException($this->error);
}else{
return $params;
... ...