jwtService = new JwtService(); } public function process(Request $request, callable $handler): Response { // 通过反射获取控制器 $controller = new ReflectionClass($request->controller); $noNeedJwt = $controller->getDefaultProperties()['noNeedJwt'] ?? []; // 除外 if (in_array($request->action, $noNeedJwt)) { return $handler($request); } else { try { $authHeader = $request->header('Authorization'); if (!$authHeader || !preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) { $body = [ 'status' => 'error', 'code' => 401, 'message' => '未授权', 'data' => [], ]; return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']); } $token = $matches[1]; $request->user = $this->jwtService->verifyToken($token); // 验证权限 -- start // $user = \app\mzl\modules\permission\model\User::find($request->user->user_id); // if (!$user || !$user->hasPermission($request->path())) { // $body = [ // 'status' => 'error', // 'code' => 403, // 'message' => '无权限访问', // 'data' => [], // ]; // return response(json_encode($body, JSON_UNESCAPED_UNICODE), 403, ['Content-Type' => 'application/json']); // } // 验证权限 -- end } catch (\Throwable $e) { $body = [ 'status' => 'error', 'code' => 401, 'message' => $e->getMessage(), 'data' => [], ]; return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']); } return $handler($request); } } }