新建exception文件夾,新增文件Handle.php文件
<?php
namespace App\service\exception;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* 不用記錄日志的異常
* @var string[]
*/
public $dontReport = [
HttpException::class
];
public function report(Throwable $exception)
{
parent::report($exception);
}
public function render(Request $request, Throwable $exception) : Response
{
if ($exception instanceof HttpException) {
return $exception->getResponse();
}
if ($request->expectsJson()) {
if (!$this->_debug) {
$json = ['code' => 500, 'msg' => '服務(wù)器錯(cuò)誤!'];
return new Response(200, ['Content-Type' => 'application/json'],
json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}
return parent::render($request, $exception);
}
}
新增文件HttpException.php文件
<?php
namespace App\service\exception;
use support\Response;
use Throwable;
class HttpException extends \RuntimeException
{
protected $response = null;
public function __construct($message = "", $code = 0, $header=[],Throwable $previous = null)
{
$this->response = json(json_decode($message,true));
parent::__construct($message, $code, $previous);
}
public function getResponse(){
return $this->response;
}
}
新增Jump.php
<?php
namespace App\service\common;
use App\service\exception\HttpException;
trait Jump
{
static protected $return_code = [
1001 => '請求成功',
1002 => '你想做什么呢', //非法的請求方式 非ajax
1003 => '請求參數(shù)錯(cuò)誤', //如參數(shù)不完整,類型不正確
1004 => '請先登陸再訪問', //未登錄 或者 未授權(quán)
1005 => '請求授權(quán)不符', ////非法的請求 無授權(quán)查看
1006 => '數(shù)據(jù)加載失敗', //
1007 => '拒絕請求',//禁止訪問的方法
1008 => '當(dāng)前訪問路由不存在',//miss路由
1009 => '驗(yàn)證碼錯(cuò)誤!',
1010 => '數(shù)據(jù)不存在', //
1020 => '驗(yàn)證碼輸入不正確', //
1021 => '用戶賬號或密碼錯(cuò)誤', //
1022 => '用戶賬號被禁用', //
1030 => '數(shù)據(jù)操作失敗', //
1031 => '成功移除數(shù)據(jù)',
1032 => '請輸入管理密碼',
1033 => '管理密碼錯(cuò)誤',
1034 => '郵件發(fā)送成功!',
1035 => '郵件發(fā)送失敗!',
1040 => '網(wǎng)站未開放!',
1041 => '未開放注冊!',
1042 => '接口未開放!',
1043 => '結(jié)算功能未開放!',
1044 => '退款功能未開放!',
];
protected $back = [];//返回的數(shù)據(jù)
/**
* Notes:錯(cuò)誤返回方式
* User: Administrator
* DateTime: 2022/3/30 22:20
* @param string $message
* @param int $code
* @param int $status
* @param string $type
* @param array $header
*/
protected function error($message = '', int $code = 412, int $status = 0, $header = [])
{
$res_msg = self::$return_code;
$message = is_string($message) && !empty($message) ? $message : $res_msg[$message] ?? "未定義消息!";
$data = [
'data' => $this->back,
'msg'=>$message,
'code' => $code,
'status' => $status,
'time' => time()
];
throw new HttpException(json_encode($data, JSON_UNESCAPED_UNICODE),$code,$header);
}
/**
* Notes:成功返回方式
* User: Administrator
* DateTime: 2022/3/30 22:21
* @param string $message
* @param int $code
* @param int $status
* @param string $type
* @param array $header
*/
protected function success($message = '', int $code = 200, int $status=1,string $type = 'json', $header = [])
{
$message = is_string($message) && !empty($message) ? $message : self::$return_code[$message] ?? self::$return_code[1001];
$data = [
'msg'=>$message,
'data' => $this->back,
'code' => $code,
'status' => $status,
'time' => time()
];
throw new HttpException( json_encode($data, JSON_UNESCAPED_UNICODE), $code, $header);
}
/**
* Notes:返回?cái)?shù)據(jù) 不做跳轉(zhuǎn)
* User: Administrator
* DateTime: 2022/3/30 22:21
* @param string $message
* @param int $code
* @param array $data
* @return array
*/
protected function back(string $message, int $code = 0, $data = [] ) :array
{
$back = [
'message' => $message, 'code' => $code, 'data' => $data
];
return $back;
}
}
調(diào)用的時(shí)候use Jump即可
$this->error();
$this-success();
有什么問題,大家可以提下,互相切磋探討