|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
|
|
|
|
class Sms
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|