header(Config::get('cookie.admin_token_name','')), ); $adminInfo = $this->adminParseToken($token); $request->adminInfo = $adminInfo; return $next($request); } /** * 获取Admin授权信息 * @param $token * @param int $expires * @param string $prefix * @return array * @throws \Psr\SimpleCache\InvalidArgumentException */ public static function adminParseToken($token): array { $redis = Cache::store('redis'); try { if (!$token || !$redis->has($token) || !($cacheToken = $redis->get($token))) throw new AuthException('请重新登录', -909); [$adminInfo, $type] = Admin::parseToken($token); Cache::store('redis')->set($cacheToken['token'], $cacheToken, $cacheToken['exp']); } catch (ExpiredException $e) { list($headb64, $bodyb64, $cryptob64) = explode('.', $token); $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)); $type = $payload->jti->type; $adminInfo = Admin::where('id', $payload->jti->id)->find(); if (!$adminInfo) { $redis->delete($token); throw new AuthException('登录超时,请重新登录!', -909); } $redis->set($cacheToken['token'], $cacheToken, $cacheToken['exp']); } catch (\Throwable $e) { $redis->delete($token); throw new AuthException('登录超时,请重新登录!', -909); } catch (\Throwable $e) { throw new AuthException('登录超时,请重新登录!', -909); } if (!isset($adminInfo) || !$adminInfo || !$adminInfo->id) { $redis->delete($token); throw new AuthException('登录超时,请重新登录!', -909); } $adminInfo->type = $type; return $adminInfo->toArray(); } }