Commit 8f1c3e96 by 赵增煜

短信时间限制

parent 9131d414
......@@ -138,11 +138,20 @@ public function getLoginSmsCode(Request $request)
}
// 检查当天发送次数
$today = date('Y-m-d');
$smsCountKey = "sms_count_{$phone}_{$today}";
$smsCount = cache()->get($smsCountKey, 0);
if ($smsCount >= 10) {
return response()->json(['error' => '今天该手机号已达到发送次数上限']);
// $today = date('Y-m-d');
// $smsCountKey = "sms_count_{$phone}_{$today}";
// $smsCount = cache()->get($smsCountKey, 0);
// if ($smsCount >= 10) {
// return response()->json(['error' => '今天该手机号已达到发送次数上限']);
// }
// 获取当前时间戳
$currentTime = time();
// 检查1分钟内是否已发送
$lastSendTimeKey = "last_sms_time_{$phone}";
$lastSendTime = cache()->get($lastSendTimeKey, 0);
if ($currentTime - $lastSendTime < 60) {
return response()->json(['error' => '请等待60秒后再试']);
}
// 生成短信验证码
......@@ -151,7 +160,8 @@ public function getLoginSmsCode(Request $request)
// 存储验证码和有效期(10分钟)
cache()->put("sms_verification_code_{$phone}", $verificationCode, 600); // 600秒 = 10分钟
// 设置当天发送次数的过期时间为当天结束
cache()->put($smsCountKey, $smsCount + 1, strtotime('tomorrow') - time()); // 设置为明天0点过期
// cache()->put($smsCountKey, $smsCount + 1, strtotime('tomorrow') - time()); // 设置为明天0点过期
cache()->put($lastSendTimeKey, $currentTime, 60);
$templateName = 'verification_code';
$templateData = ['code' => $verificationCode];
......
......@@ -71,12 +71,20 @@ public function smsCode(Request $request)
if (! preg_match('/^1[3-9]\d{9}$/', $mobile)) {
return response()->json(['error' => '手机号格式不正确']);
}
// TODO 增加手机验证码发送频率限制
$today = date('Y-m-d');
$smsCountKey = "sms_count_{$mobile}_{$today}";
$smsCount = cache()->get($smsCountKey, 0);
if ($smsCount >= 10) {
return $this->failed('今天该手机号已达到发送次数上限');
// // 增加手机验证码发送频率限制
// $today = date('Y-m-d');
// $smsCountKey = "sms_count_{$mobile}_{$today}";
// $smsCount = cache()->get($smsCountKey, 0);
// if ($smsCount >= 10) {
// return $this->failed('今天该手机号已达到发送次数上限');
// }
// 获取当前时间戳
$currentTime = time();
// 检查1分钟内是否已发送
$lastSendTimeKey = "last_sms_time_{$mobile}";
$lastSendTime = cache()->get($lastSendTimeKey, 0);
if ($currentTime - $lastSendTime < 60) {
return $this->failed('请等待60秒后再试');
}
// 检查手机号在医师或者药店表中是否存在
......@@ -97,7 +105,8 @@ public function smsCode(Request $request)
// 存储验证码和有效期(10分钟)
cache()->put("sms_verification_code_{$mobile}", $verificationCode, 600); // 600秒 = 10分钟
cache()->put($smsCountKey, $smsCount + 1, strtotime('tomorrow') - time());
// cache()->put($smsCountKey, $smsCount + 1, strtotime('tomorrow') - time());
cache()->put($lastSendTimeKey, $currentTime, 60);
$templateName = 'verification_code';
$templateData = ['code' => $verificationCode];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment