mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-03-27 23:20:17 +08:00
fix : update cors and admin user cache
This commit is contained in:
@@ -4,10 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Aspect;
|
||||
|
||||
use App\Annotation\ResponseFormat;
|
||||
use App\Common\Trait\AdminUserTrait;
|
||||
use App\Common\Trait\ClientIpTrait;
|
||||
use App\Lib\Log\Logger;
|
||||
use App\Lib\Return\AdminReturn;
|
||||
use App\Model\AdminUser;
|
||||
use App\Model\AdminUserOperationLog;
|
||||
use Hyperf\Context\Context;
|
||||
@@ -18,7 +18,7 @@ use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
use Hyperf\Di\Exception\Exception;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
|
||||
#[Aspect]
|
||||
#[Aspect(priority: 1)]
|
||||
class AdminReturnLogAspect extends AbstractAspect
|
||||
{
|
||||
use ClientIpTrait;
|
||||
@@ -29,7 +29,7 @@ class AdminReturnLogAspect extends AbstractAspect
|
||||
* @var array|\class-string[]
|
||||
*/
|
||||
public array $classes = [
|
||||
AdminReturn::class,
|
||||
ResponseFormat::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@ use Hyperf\HttpServer\Request;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
|
||||
#[Aspect]
|
||||
#[Aspect(priority: 0)]
|
||||
class ResponseFormatAspect extends AbstractAspect
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -51,4 +51,16 @@ trait AdminUserTrait
|
||||
|
||||
return $userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $adminId
|
||||
* @return void
|
||||
*/
|
||||
public function updateAdminUserInfo(int $adminId): void
|
||||
{
|
||||
$key = RedisKey::getAdminUserInfoKey($adminId);
|
||||
if ($this->redis->with()->exists($key)) $this->redis->with()->del($key);
|
||||
if (Context::has($key)) Context::destroy($key);
|
||||
$this->getAdminUserInfo($adminId);
|
||||
}
|
||||
}
|
||||
@@ -12,25 +12,50 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Constants\ResultCode;
|
||||
use App\Exception\ErrException;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\MessageInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class AppExceptionHandler extends ExceptionHandler
|
||||
class AppExceptionHandler extends BaseErrExceptionHandler
|
||||
{
|
||||
public function __construct(protected StdoutLoggerInterface $logger)
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @param ResponseInterface $response
|
||||
* @return MessageInterface|ResponseInterface
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface
|
||||
{
|
||||
$this->logger->error()->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
|
||||
$this->logger->error()->error($throwable->getTraceAsString());
|
||||
$throwable = $this->modifyException($throwable);
|
||||
return $this->handlerResponse($throwable,$response);
|
||||
}
|
||||
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @return ErrException
|
||||
*/
|
||||
protected function modifyException(Throwable $throwable): ErrException
|
||||
{
|
||||
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
|
||||
$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.'));
|
||||
return New ErrException(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()),ResultCode::ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
|
||||
use App\Lib\Log\Logger;
|
||||
use App\Lib\Return\AdminReturn;
|
||||
use App\Lib\Return\ApiReturn;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
@@ -20,6 +21,7 @@ abstract class BaseErrExceptionHandler extends ExceptionHandler
|
||||
public function __construct(
|
||||
private readonly Request $request,
|
||||
private readonly ContainerInterface $container,
|
||||
protected readonly Logger $logger
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
||||
44
app/Exception/Handler/MainHttpExceptionHandler.php
Normal file
44
app/Exception/Handler/MainHttpExceptionHandler.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Constants\ResultCode;
|
||||
use App\Exception\ErrException;
|
||||
use Hyperf\HttpMessage\Exception\HttpException;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class MainHttpExceptionHandler extends BaseErrExceptionHandler
|
||||
{
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($throwable instanceof HttpException) {
|
||||
$msg = match ($throwable->getStatusCode()) {
|
||||
404 => 'The route does not exist.',
|
||||
405 => 'The HTTP method not allowed.',
|
||||
default => $throwable->getMessage(),
|
||||
};
|
||||
$throwable = new ErrException($msg,ResultCode::ERROR);
|
||||
return $this->handlerResponse($throwable,$response);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return $throwable instanceof HttpException;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Constants\ResultCode;
|
||||
use App\Exception\ErrException;
|
||||
use Hyperf\Validation\ValidationException;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
@@ -19,9 +21,11 @@ class ValidationExceptionHandler extends BaseErrExceptionHandler
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
return $throwable instanceof ValidationException
|
||||
? $this->handlerResponse($throwable, $response)
|
||||
: $response;
|
||||
if ($throwable instanceof ValidationException) {
|
||||
$throwable = new ErrException($throwable->validator->errors()->first(),ResultCode::ERROR);
|
||||
return $this->handlerResponse($throwable,$response);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
35
app/Middleware/CorsMiddleware.php
Normal file
35
app/Middleware/CorsMiddleware.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Middleware;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Hyperf\Context\Context;
|
||||
|
||||
class CorsMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$response = Context::get(ResponseInterface::class);
|
||||
$response = $response->withHeader('Access-Control-Allow-Origin', '*')
|
||||
->withHeader('Access-Control-Allow-Credentials', 'true')
|
||||
->withHeader('Access-Control-Allow-Headers', '*') // Headers 可以根据实际情况进行改写。
|
||||
->withHeader('Access-Control-Request-Headers', '*')
|
||||
->withHeader('Access-Control-Allow-Methods', '*')
|
||||
->withHeader('Access-Control-Max-Age', 1800);
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
if ($request->getMethod() == 'OPTIONS') {
|
||||
return $response;
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,8 @@ class PermissionService extends BaseAdminService
|
||||
|
||||
if (!$this->adminUserRepository->updateById($userInfo->id, $data)) throw new ErrException('更新失败');
|
||||
|
||||
$this->updateAdminUserInfo($this->adminId);
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,8 @@ class UserService extends BaseAdminService
|
||||
|
||||
if (!$res) throw new ErrException('修改失败');
|
||||
|
||||
$this->updateAdminUserInfo($this->adminId);
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user