agent_id */ class AuthTokenMiddleware implements MiddlewareInterface { public function process(Request $request, callable $handler): Response { $token = $request->header('auth-token'); $token = $token !== null ? trim((string) $token) : ''; if ($token === '') { throw new ApiException('Please provide auth-token', ReturnCode::UNAUTHORIZED); } try { $decoded = JwtToken::verify(1, $token); } catch (JwtTokenExpiredException $e) { throw new ApiException('auth-token expired', ReturnCode::TOKEN_INVALID); } catch (JwtTokenException $e) { throw new ApiException('auth-token invalid', ReturnCode::TOKEN_INVALID); } catch (\Throwable $e) { throw new ApiException('auth-token format invalid', ReturnCode::TOKEN_INVALID); } $extend = $decoded['extend'] ?? []; if ((string) ($extend['plat'] ?? '') !== 'api_auth_token') { throw new ApiException('auth-token invalid', ReturnCode::TOKEN_INVALID); } $agentId = trim((string) ($extend['agent_id'] ?? '')); if ($agentId === '') { throw new ApiException('auth-token invalid', ReturnCode::TOKEN_INVALID); } // 单次 Redis:token → agent_id,与 JWT 内 agent_id 一致即有效(减少一次按 agent 取当前 token 的往返) $agentIdFromStore = AuthTokenCache::getAgentIdByToken($token); if ($agentIdFromStore === null || $agentIdFromStore !== $agentId) { throw new ApiException('auth-token invalid or expired', ReturnCode::TOKEN_INVALID); } $agent = SystemUser::where('agent_id', $agentId)->find(); if (!$agent || (int) ($agent->status ?? 0) !== 1) { throw new ApiException('Invalid agent_id', ReturnCode::FORBIDDEN); } if (empty($agent->dept_id) || (int) $agent->dept_id <= 0) { throw new ApiException('Agent channel is not configured', ReturnCode::FORBIDDEN); } $request->agent_id = $agentId; $request->agent_admin_id = (int) $agent->id; $request->agent_dept_id = (int) $agent->dept_id; return $handler($request); } }