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

合并分支 'liuzhen' 到 'master'

验证调试



查看合并请求 !77
1 <?php 1 <?php
2 use \think\Db; 2 use \think\Db;
3 use app\nsms\nsms; 3 use app\nsms\nsms;
  4 +
  5 +
  6 +/**
  7 + * 传json字符串
  8 + * @param $code
  9 + * @param array $data
  10 + * @param string $msg
  11 + * @param int $errorCode
  12 + * @return \think\response\Json
  13 + */
  14 +function writeJson($data = [], $code = 0, $msg = 'ok', $errorCode = 200)
  15 +{
  16 + $data = [
  17 + 'code' => $code,
  18 + 'data' => $data,
  19 + 'msg' => $msg,
  20 + 'time' => time()
  21 + ];
  22 + return json($data, $errorCode);
  23 +}
  24 +
4 if (!function_exists('generate_user_token')) { 25 if (!function_exists('generate_user_token')) {
5 /** 26 /**
6 * 生成token 27 * 生成token
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace app\api\library; 3 namespace app\api\library;
4 4
  5 +use app\api\library\Exception\BaseException;
5 use Exception; 6 use Exception;
6 use think\exception\Handle; 7 use think\exception\Handle;
7 8
@@ -10,28 +11,53 @@ use think\exception\Handle; @@ -10,28 +11,53 @@ use think\exception\Handle;
10 */ 11 */
11 class ExceptionHandle extends Handle 12 class ExceptionHandle extends Handle
12 { 13 {
13 - 14 + private $code;
  15 + private $msg;
  16 + private $errorCode;
14 public function render(Exception $e) 17 public function render(Exception $e)
15 { 18 {
16 // 在生产环境下返回code信息 19 // 在生产环境下返回code信息
17 - if (!\think\Config::get('app_debug')) {  
18 - $statuscode = $code = 500;  
19 - $msg = 'An error occurred';  
20 - // 验证异常  
21 - if ($e instanceof \think\exception\ValidateException) {  
22 - $code = 0;  
23 - $statuscode = 200;  
24 - $msg = $e->getError();  
25 - }  
26 - // Http异常  
27 - if ($e instanceof \think\exception\HttpException) {  
28 - $statuscode = $code = $e->getStatusCode(); 20 + if($e instanceof BaseException){
  21 + $this->code = $e->code;
  22 + $this->msg = $e->msg;
  23 + $this->errorCode = $e->errCode;
  24 + }elseif($e instanceof \think\exception\ValidateException){
  25 + $this->code = -2;
  26 + $this->errorCode = 200;
  27 + $this->msg = $e->getError();
29 } 28 }
30 - return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode); 29 + else{
  30 + if(config('app_debug')){
  31 + return parent::render($e);
  32 + }else{
  33 + $this->code = 500;
  34 + $this->msg ='服务器内部异常';
  35 + $this->errorCode = 500;
31 } 36 }
32 37
  38 + }
  39 + return writeJson([],$this->code,$this->msg,$this->errorCode);
  40 +// $url = $_SERVER['SERVER_NAME'];
  41 +// switch ($url){
  42 +// case config("environment.local_url"):
  43 +// $this->code = $this->errorCode = 500;
  44 +// $this->msg = 'An error occurred';
  45 +// // 验证异常
  46 +// if ($e instanceof \think\exception\ValidateException) {
  47 +// $this->code = -2;
  48 +// $this->errorCode = 200;
  49 +// $this->msg = $e->getError();
  50 +// }
  51 +// // Http异常
  52 +// else if($e instanceof BaseException){
  53 +// $this->code = $e->code;
  54 +// $this->msg = $e->msg;
  55 +// $this->errorCode = $e->errCode;
  56 +// }
  57 +// return json(['code' => $this->code, 'msg' => $this->msg, 'time' => time(), 'data' => null], $this->errorCode);
  58 +// }
33 //其它此交由系统处理 59 //其它此交由系统处理
34 - return parent::render($e); 60 +// return parent::render($e);
35 } 61 }
36 62
37 } 63 }
@@ -28,6 +28,7 @@ class BaseValidate extends Validate{ @@ -28,6 +28,7 @@ class BaseValidate extends Validate{
28 $result = $this->check($params); 28 $result = $this->check($params);
29 } 29 }
30 if(!$result){ 30 if(!$result){
  31 +
31 throw new ValidateException($this->error); 32 throw new ValidateException($this->error);
32 }else{ 33 }else{
33 return $params; 34 return $params;