You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Models;
use Illuminate\Support\Facades\Cache;
class Sms extends SoftDeletesModel
{
/**
* 检查IP是否被锁定
*
* @param string $ip 客户端IP地址
* @param string $type 方法类型 (bind_mobile/check_mobile)
* @return array ['locked' => bool, 'message' => string]
*/
public static function checkIpLock($ip, $type = 'bind_mobile')
{
$ipLockKey = 'sms_ip_lock_' . $type . '_' . $ip;
if (Cache::has($ipLockKey)) {
$lockTime = Cache::get($ipLockKey);
$remainingTime = $lockTime - time();
if ($remainingTime > 0) {
$minutes = ceil($remainingTime / 60);
return [
'locked' => true,
'message' => "访问过于频繁,请{$minutes}分钟后再试"
];
}
}
return ['locked' => false, 'message' => ''];
}
/**
* 记录验证码错误
*
* @param string $ip 客户端IP地址
* @param string $type 方法类型 (bind_mobile/check_mobile)
* @return array ['locked' => bool, 'message' => string]
*/
public static function recordError($ip, $type = 'bind_mobile')
{
$errorKey = 'sms_error_count_' . $type . '_' . $ip;
$ipLockKey = 'sms_ip_lock_' . $type . '_' . $ip;
$errorCount = Cache::get($errorKey, 0) + 1;
Cache::put($errorKey, $errorCount, 3600); // 1小时过期
// 如果错误次数达到10次锁定IP
if ($errorCount >= 10) {
Cache::put($ipLockKey, time() + 3600, 3600); // 锁定1小时
Cache::forget($errorKey); // 清除错误计数
return [
'locked' => true,
'message' => '验证码错误次数过多请1小时后再试'
];
}
return ['locked' => false, 'message' => '验证码错误'];
}
/**
* 清除错误计数
*
* @param string $ip 客户端IP地址
* @param string $type 方法类型 (bind_mobile/check_mobile)
*/
public static function clearError($ip, $type = 'bind_mobile')
{
$errorKey = 'sms_error_count_' . $type . '_' . $ip;
Cache::forget($errorKey);
}
}