Skip to content

Commit 1b5b946

Browse files
committed
增加参数验证处理器,处理用户退出时跳转的问题
1 parent bc897f1 commit 1b5b946

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ REDIS_PORT=6379
1616
REDIS_DB=0
1717

1818
WS_URL=ws://127.0.0.1:9502/ws
19+
APP_URL=http://127.0.0.1:9501
1920

2021
STORAGE_IMG_URL=http://127.0.0.1:9501/storage/upload/
2122
STORAGE_FILE_URL=http://127.0.0.1:9501/file/upload/

app/Controller/Http/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function getApplication()
169169
*/
170170
public function signOut()
171171
{
172-
return $this->response->withCookie(new Cookie('IM_TOKEN', ''))->redirect('index/login');
172+
return $this->response->withCookie(new Cookie('IM_TOKEN', ''))->redirect(env('APP_URL').'/index/login');
173173
}
174174

175175
/**
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://doc.hyperf.io
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace App\Exception\Handler;
14+
15+
use App\Exception\InputException;
16+
use Hyperf\Contract\StdoutLoggerInterface;
17+
use Hyperf\ExceptionHandler\ExceptionHandler;
18+
use Hyperf\HttpMessage\Stream\SwooleStream;
19+
use Hyperf\Utils\Codec\Json;
20+
use Psr\Http\Message\ResponseInterface;
21+
use Throwable;
22+
23+
class InputExceptionHandler extends ExceptionHandler
24+
{
25+
/**
26+
* @var StdoutLoggerInterface
27+
*/
28+
protected $logger;
29+
30+
public function __construct(StdoutLoggerInterface $logger)
31+
{
32+
$this->logger = $logger;
33+
}
34+
35+
/**
36+
* @param \Throwable $throwable
37+
* @param \Psr\Http\Message\ResponseInterface $response
38+
*
39+
* @return \Psr\Http\Message\ResponseInterface
40+
*/
41+
public function handle(Throwable $throwable, ResponseInterface $response)
42+
{
43+
if ($throwable instanceof InputException) {
44+
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
45+
$this->logger->error($throwable->getTraceAsString());
46+
$data = Json::encode([
47+
'code' => -1,
48+
'msg' => $throwable->getMessage(),
49+
'data' => NULL
50+
], JSON_UNESCAPED_UNICODE);
51+
$this->stopPropagation();
52+
return $response->withStatus(200)
53+
->withAddedHeader('content-type', 'application/json; charset=utf-8')
54+
->withBody(new SwooleStream($data));
55+
}
56+
return $response;
57+
}
58+
59+
public function isValid(Throwable $throwable) : bool
60+
{
61+
return true;
62+
}
63+
}

config/autoload/exceptions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types=1);
3+
declare(strict_types = 1);
44
/**
55
* This file is part of Hyperf.
66
*
@@ -10,10 +10,14 @@
1010
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
1111
*/
1212

13+
use App\Exception\Handler\AppExceptionHandler;
14+
use App\Exception\Handler\InputExceptionHandler;
15+
1316
return [
1417
'handler' => [
1518
'http' => [
16-
App\Exception\Handler\AppExceptionHandler::class,
19+
AppExceptionHandler::class,
20+
InputExceptionHandler::class
1721
],
1822
],
1923
];

0 commit comments

Comments
 (0)