header('Authorization')); if ($plainToken === '') { return response()->json(['code' => HttpStatus::AUTHORIZATION_HEADER_NOT_FOUND, 'timestamp' => time(), 'msg' => '请先登录', 'data' => []]); } $token = AgentToken::query() ->where('token_hash', hash('sha256', $plainToken)) ->where('expires_at', '>', now()) ->first(); $agent = $token ? Agent::query()->find($token->agent_id) : null; if (!$token || !$agent || (int) $agent->status !== Agent::STATUS_ENABLED) { return response()->json(['code' => HttpStatus::AUTHORIZATION_HEADER_NOT_FOUND, 'timestamp' => time(), 'msg' => '登录已失效', 'data' => []]); } if (!$token->last_used_at || $token->last_used_at->lt(now()->subMinute())) { $token->last_used_at = now(); $token->save(); } $request->attributes->set('agent', $agent); $request->attributes->set('agent_token', $token); return $next($request); } }