35 lines
1.1 KiB
PHP
35 lines
1.1 KiB
PHP
<?php
|
|
namespace app\middleware;
|
|
|
|
use ReflectionClass;
|
|
use Webman\MiddlewareInterface;
|
|
use Webman\Http\Response;
|
|
use Webman\Http\Request;
|
|
|
|
class AuthCheck implements MiddlewareInterface
|
|
{
|
|
public function process(Request $request, callable $handler) : Response
|
|
{
|
|
// 通过反射获取控制器哪些方法不需要登录
|
|
$controller = new ReflectionClass($request->controller);
|
|
$noNeedLogin = $controller->getDefaultProperties()['noNeedLogin'] ?? [];
|
|
|
|
// 访问的方法需要登录
|
|
if (!in_array($request->action, $noNeedLogin)) {
|
|
try {
|
|
$uid = \Tinywan\Jwt\JwtToken::getCurrentId();
|
|
if ($uid != 0) {
|
|
$request->uid = $uid;
|
|
return $handler($request);
|
|
}
|
|
}catch (\Exception $e) {
|
|
return Response(json_encode(['code' => 0, 'msg' => $e->getMessage()], 256));
|
|
}
|
|
}else{
|
|
return $handler($request);
|
|
}
|
|
|
|
// 不需要登录,请求继续向洋葱芯穿越
|
|
return $handler($request);
|
|
}
|
|
} |