From 6fa7ff22ca4cedfe783ce180b0b7e66c54ed0397 Mon Sep 17 00:00:00 2001 From: yaooo <272523191@qq.com> Date: Mon, 13 Nov 2023 10:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=99=BB=E5=BD=95token?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../http/middleware/LoginJwtMiddleware.php | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 app/api/http/middleware/LoginJwtMiddleware.php diff --git a/app/api/http/middleware/LoginJwtMiddleware.php b/app/api/http/middleware/LoginJwtMiddleware.php new file mode 100644 index 00000000..a7cfc838 --- /dev/null +++ b/app/api/http/middleware/LoginJwtMiddleware.php @@ -0,0 +1,59 @@ +header('token'); + //判断接口是否免登录 + $isNotNeedLogin = $request->controllerObject->isNotNeedLogin(); + + //不直接判断$isNotNeedLogin结果,使不需要登录的接口通过,为了兼容某些接口可以登录或不登录访问 + if (empty($token) && !$isNotNeedLogin) { + return JsonService::fail('请求参数缺token', [], 0, 0); + } + + if (!$isNotNeedLogin) { + try { + $userInfo = JwtTokenService::parseToken($token); + $userInfo['user_id'] = $userInfo['uid'] ?? 0; + $request->userInfo = $userInfo; + $request->userId = $userInfo['uid'] ?? 0; + } catch (\Exception $e) { + return JsonService::fail($e->getMessage(), [], 0, 0); + } + if (empty($userInfo['user_id'])) { + return JsonService::fail('用户登录异常', [], 0, 0); + } + } + return $next($request); + } + +} \ No newline at end of file