ttl($key); if (!empty($ttl) && $expire - $ttl < $limitSecond) { throw new BusinessException("请求过于频繁"); } } public static function getSmsCode(string $key, int $ttl): string { $redis = RedisService::getRedisInstance(); $redis->del($key); $code = Str::random(6, 1); $redis->set($key, $code, 'ex', $ttl); return $code; } public static function compareSmsCode(string $codeCacheKey, $code) { $redis = RedisService::getRedisInstance(); $cacheCode = $redis->get($codeCacheKey); if (empty($cacheCode) || $code != $cacheCode) { throw new BusinessException("验证码错误"); } $redis->del($codeCacheKey); } public static function getSmsKey($mobile): string { return BusinessCacheKey::SITE_SMS_CODE['key'] . ":" . $mobile; } }