feat: admin login
This commit is contained in:
45
app/Exception/Handler/AdminExceptionHandler.php
Normal file
45
app/Exception/Handler/AdminExceptionHandler.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Exception\AdminException;
|
||||
use App\Lib\AdminReturn;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class AdminExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
public function __construct(protected AdminReturn $return)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* admin控制器异常处理
|
||||
* @param Throwable $throwable
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($throwable instanceof AdminException) {
|
||||
$result = $this->return->error($throwable->getMessage(),[],$throwable->getCode());
|
||||
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
|
||||
return $response->withHeader("Content-Type", "application/json")
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
// 交给下一个异常处理器
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
63
app/Exception/Handler/ValidationDataExceptionHandler.php
Normal file
63
app/Exception/Handler/ValidationDataExceptionHandler.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Lib\AdminReturn;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpServer\Request;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
use Hyperf\Validation\ValidationException;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
|
||||
class ValidationDataExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
public function __construct(protected AdminReturn $adminReturn)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证器异常处理
|
||||
* @param Throwable $throwable
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($throwable instanceof ValidationException) {
|
||||
// 格式化输出
|
||||
$request = new Request();
|
||||
$url = $request->path();
|
||||
|
||||
$urlArr = explode('/',$url);
|
||||
|
||||
if ($urlArr[0] == 'admin') {
|
||||
$result = $this->adminReturn->error($throwable->validator->errors()->first());
|
||||
}else{
|
||||
//todo api
|
||||
$result = $this->adminReturn->error($throwable->validator->errors()->first());
|
||||
}
|
||||
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
|
||||
if (!is_array($result)){
|
||||
return $response->withHeader("Content-Type", "application/json")
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream($result));
|
||||
}
|
||||
|
||||
return $response->withHeader("Content-Type", "application/json")
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
// 交给下一个异常处理器
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user