mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-21 01:14:31 +08:00
更新基础架构
This commit is contained in:
16
app/exceptions/AuthFailedException.php
Normal file
16
app/exceptions/AuthFailedException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 认证失败(账号或密码错误)
|
||||
*/
|
||||
class AuthFailedException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '账号或者密码错误', int $bizCode = 400, mixed $data = null)
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
49
app/exceptions/BusinessException.php
Normal file
49
app/exceptions/BusinessException.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 业务基础异常
|
||||
*
|
||||
* 说明:
|
||||
* - 继承 webman 的 BusinessException,让框架自动捕获并渲染
|
||||
* - 重写 render() 以对齐前端期望字段:code/message/data
|
||||
*/
|
||||
class BusinessException extends \support\exception\BusinessException
|
||||
{
|
||||
public function __construct(string $message = '', int $bizCode = 500, array $data = [])
|
||||
{
|
||||
parent::__construct($message, $bizCode);
|
||||
$this->data($data);
|
||||
}
|
||||
|
||||
public function getBizCode(): int
|
||||
{
|
||||
return (int)$this->getCode();
|
||||
}
|
||||
|
||||
// 保持与 webman BusinessException 方法签名兼容
|
||||
public function getData(): array
|
||||
{
|
||||
return parent::getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义渲染
|
||||
* - json 请求:返回 {code,message,data}
|
||||
* - 非 json:返回文本
|
||||
*/
|
||||
public function render(\Webman\Http\Request $request): ?\Webman\Http\Response
|
||||
{
|
||||
if ($request->expectsJson()) {
|
||||
return json([
|
||||
'code' => $this->getBizCode() ?: 500,
|
||||
'message' => $this->getMessage(),
|
||||
'data' => $this->getData(),
|
||||
]);
|
||||
}
|
||||
return new \Webman\Http\Response(200, [], $this->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
16
app/exceptions/ForbiddenException.php
Normal file
16
app/exceptions/ForbiddenException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 权限不足
|
||||
*/
|
||||
class ForbiddenException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '无访问权限', int $bizCode = 403, mixed $data = null)
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
16
app/exceptions/UnauthorizedException.php
Normal file
16
app/exceptions/UnauthorizedException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 未认证或登录过期
|
||||
*/
|
||||
class UnauthorizedException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '登录状态已过期', int $bizCode = 401, mixed $data = null)
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
16
app/exceptions/ValidationException.php
Normal file
16
app/exceptions/ValidationException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 参数校验异常
|
||||
*/
|
||||
class ValidationException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '参数校验失败', int $bizCode = 422, mixed $data = null)
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user