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); } }