zhaojs
2023-08-04 73e8924ae3a93d4b6a69579ccc464cc599af2bd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
 
namespace app\api\library;
 
use Exception;
use think\exception\Handle;
 
/**
 * 自定义API模块的错误显示
 */
class ExceptionHandle extends Handle
{
 
    public function __construct($message = "", $code = 0, $data = [])
    {
        $this->message = $message;
        $this->code = $code;
        $this->data = $data;
    }
 
    public function render(Exception $e)
    {
        $msg = $e->getMessage();
        $code = 0;
        // 验证异常
        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' => $this->data], 200);
    }
 
}