mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-21 17:44:27 +08:00
更新后端基础
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 认证失败(账号或密码错误)
|
||||
*/
|
||||
class AuthFailedException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '账号或者密码错误', int $bizCode = 400, mixed $data = null)
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
<?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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 权限不足
|
||||
*/
|
||||
class ForbiddenException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '无访问权限', int $bizCode = 403, mixed $data = null)
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
/**
|
||||
* 未认证或登录过期
|
||||
*/
|
||||
class UnauthorizedException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '登录状态已过期', int $bizCode = 401, mixed $data = null)
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,23 @@
|
||||
|
||||
namespace app\exceptions;
|
||||
|
||||
use Webman\Exception\BusinessException;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
|
||||
/**
|
||||
* 参数校验异常
|
||||
* 最常用的异常类型,用于参数验证、业务规则验证等
|
||||
*
|
||||
* 示例:
|
||||
* throw new ValidationException('优惠券和会员不可叠加使用');
|
||||
* throw new ValidationException('手机号格式不正确');
|
||||
* throw new ValidationException('金额必须大于0');
|
||||
*/
|
||||
class ValidationException extends BusinessException
|
||||
{
|
||||
public function __construct(string $message = '参数校验失败', int $bizCode = 422, mixed $data = null)
|
||||
public function __construct(string $message = '参数校验失败', int $bizCode = 422, array $data = [])
|
||||
{
|
||||
parent::__construct($message, $bizCode, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user