初始化

main
gzydong 2020-11-14 23:05:45 +08:00
parent 7dafc96462
commit 2a7e886a70
8 changed files with 25 additions and 29 deletions

View File

@ -2,9 +2,7 @@
namespace App\Bootstrap; namespace App\Bootstrap;
use App\Service\SocketFDService;
use Hyperf\Framework\Bootstrap\ServerStartCallback; use Hyperf\Framework\Bootstrap\ServerStartCallback;
use Hashids\Hashids;
use Swoole\Timer; use Swoole\Timer;
use Hyperf\Redis\Redis; use Hyperf\Redis\Redis;

View File

@ -1,23 +1,15 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Exception\Handler; namespace App\Exception\Handler;
use App\Exception\ValidateException;
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Throwable; use Throwable;
use Phper666\JWTAuth\Exception\TokenValidException;
class AppExceptionHandler extends ExceptionHandler class AppExceptionHandler extends ExceptionHandler
{ {
/** /**
@ -32,22 +24,16 @@ class AppExceptionHandler extends ExceptionHandler
public function handle(Throwable $throwable, ResponseInterface $response) public function handle(Throwable $throwable, ResponseInterface $response)
{ {
// 判断是否是验证器异常类
if($throwable instanceof ValidateException){
return $response;
}
if($throwable instanceof TokenValidException){
return $response;
}
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile())); $this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
$this->logger->error($throwable->getTraceAsString()); $this->logger->error($throwable->getTraceAsString());
return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.')); return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
} }
/**
* @param Throwable $throwable
* @return bool
*/
public function isValid(Throwable $throwable): bool public function isValid(Throwable $throwable): bool
{ {
return true; return true;

View File

@ -16,6 +16,8 @@ class JwtAuthExceptionHandler extends ExceptionHandler
{ {
public function handle(Throwable $throwable, ResponseInterface $response) public function handle(Throwable $throwable, ResponseInterface $response)
{ {
// echo $throwable->getMessage();
// 判断被捕获到的异常是希望被捕获的异常 // 判断被捕获到的异常是希望被捕获的异常
if ($throwable instanceof TokenValidException) { if ($throwable instanceof TokenValidException) {
// 格式化输出 // 格式化输出
@ -27,7 +29,6 @@ class JwtAuthExceptionHandler extends ExceptionHandler
// 阻止异常冒泡 // 阻止异常冒泡
$this->stopPropagation(); $this->stopPropagation();
return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withStatus(401)->withBody(new SwooleStream($data)); return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withStatus(401)->withBody(new SwooleStream($data));
} }
@ -36,6 +37,9 @@ class JwtAuthExceptionHandler extends ExceptionHandler
/** /**
* 判断该异常处理器是否要对该异常进行处理 * 判断该异常处理器是否要对该异常进行处理
*
* @param Throwable $throwable
* @return bool
*/ */
public function isValid(Throwable $throwable): bool public function isValid(Throwable $throwable): bool
{ {

View File

@ -16,6 +16,11 @@ use Throwable;
*/ */
class ValidateExceptionHandler extends ExceptionHandler class ValidateExceptionHandler extends ExceptionHandler
{ {
/**
* @param Throwable $throwable
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function handle(Throwable $throwable, ResponseInterface $response) public function handle(Throwable $throwable, ResponseInterface $response)
{ {
// 判断被捕获到的异常是希望被捕获的异常 // 判断被捕获到的异常是希望被捕获的异常
@ -38,6 +43,9 @@ class ValidateExceptionHandler extends ExceptionHandler
/** /**
* 判断该异常处理器是否要对该异常进行处理 * 判断该异常处理器是否要对该异常进行处理
*
* @param Throwable $throwable
* @return bool
*/ */
public function isValid(Throwable $throwable): bool public function isValid(Throwable $throwable): bool
{ {

View File

@ -10,7 +10,7 @@ class Hash
* @param string $value * @param string $value
* @return string * @return string
*/ */
public static function make($value) public static function make(string $value)
{ {
return password_hash($value, PASSWORD_DEFAULT); return password_hash($value, PASSWORD_DEFAULT);
} }
@ -22,7 +22,7 @@ class Hash
* @param string $hashedValue * @param string $hashedValue
* @return bool * @return bool
*/ */
public static function check($value, $hashedValue) public static function check(string $value, string $hashedValue)
{ {
return password_verify($value, $hashedValue); return password_verify($value, $hashedValue);
} }

View File

@ -43,7 +43,7 @@ class DbQueryExecutedListener implements ListenerInterface
} }
/** /**
* @param QueryExecuted $event * @param object|QueryExecuted $event
*/ */
public function process(object $event) public function process(object $event)
{ {

View File

@ -13,7 +13,7 @@ class Response
* @Inject * @Inject
* @var ResponseInterface|mixed * @var ResponseInterface|mixed
*/ */
protected $response; private $response;
/** /**
* @param $data * @param $data

View File

@ -13,9 +13,9 @@ return [
'handler' => [ 'handler' => [
'http' => [ 'http' => [
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class, Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
App\Exception\Handler\AppExceptionHandler::class,
App\Exception\Handler\ValidateExceptionHandler::class,
App\Exception\Handler\JwtAuthExceptionHandler::class, App\Exception\Handler\JwtAuthExceptionHandler::class,
App\Exception\Handler\ValidateExceptionHandler::class,
App\Exception\Handler\AppExceptionHandler::class,
], ],
], ],
]; ];