forked from easy-swoole/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasySwooleEvent.php
64 lines (54 loc) · 1.84 KB
/
EasySwooleEvent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/5/28
* Time: 下午6:33
*/
namespace EasySwoole\EasySwoole;
use App\Utility\TrackerManager;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Message\Status;
use EasySwoole\Http\Message\Stream;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Trace\Bean\Tracker;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
}
public static function mainServerCreate(EventRegister $register)
{
// TODO: Implement mainServerCreate() method.
}
public static function onRequest(Request $request, Response $response): bool
{
//不建议在这拦截请求,可增加一个控制器基类进行拦截
//如果真要拦截,判断之后return false即可
$code = $request->getRequestParam('code');
if (0/*empty($code)验证失败*/){
$data = Array(
"code" => Status::CODE_BAD_REQUEST,
"result" => [],
"msg" => '验证失败'
);
$response->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$response->withHeader('Content-type', 'application/json;charset=utf-8');
$response->withStatus(Status::CODE_BAD_REQUEST);
return false;
}
return true;
}
public static function afterRequest(Request $request, Response $response): void
{
$responseMsg = $response->getBody()->__toString();
Logger::getInstance()->console("响应内容:".$responseMsg);
//响应状态码:
var_dump($response->getStatusCode());
}
}