Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵增煜
/
tzt-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8f1c3e96
authored
Nov 21, 2024
by
赵增煜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
短信时间限制
parent
9131d414
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
13 deletions
+32
-13
app/Admin/Controllers/AuthController.php
+16
-6
app/Api/Controllers/UserController.php
+16
-7
No files found.
app/Admin/Controllers/AuthController.php
View file @
8f1c3e96
...
...
@@ -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
];
...
...
app/Api/Controllers/UserController.php
View file @
8f1c3e96
...
...
@@ -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
];
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment