Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
services
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tencent
services
Commits
28669e4a
Commit
28669e4a
authored
Jul 25, 2018
by
王召彬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
手机验证码 工具类
parent
b8d02185
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
8 deletions
+89
-8
src/Common/Lib/Smscode.php
+89
-8
No files found.
src/Common/Lib/Smscode.php
View file @
28669e4a
<?php
<?php
namespace
Hdll\Services\Common\Lib
;
namespace
Hdll\Services\Common\Lib
;
use
Flc\Dysms\Client
;
use
Swoft\App
;
use
Flc\Dysms\Request\SendSms
;
use
Hdll\Services\Common\Entity\User
;
/**
/**
* 手机验证码 工具类
* 手机验证码 工具类
* 使用:
* 发送验证码:
* $err = (new Smscode)->send($mobile); // $mobile 接收验证码的手机号
* if($err) {
* throw .... // 错误处理
* }
* 校验验证码:
* $check = (new Smscode)->check($mobile,$smscode); // 前台传入的手机号和验证码
* if(!$check) {
* throw .... // 错误处理
* }
*
*/
*/
class
Smscode
class
Smscode
{
{
protected
$storeId
;
protected
$sellerId
;
public
function
__construct
()
{
$this
->
sellerId
=
App
::
getBean
(
User
::
class
)
->
getId
();
}
/**
* 发送验证码,可指定短信签名和使用哪个短信模块
*
* @param integer $mobile
* @param [type] $signName
* @param [type] $templateCode
* @return null|string 返回错误信息,正确时返回null
*/
public
function
send
(
int
$mobile
,
$signName
=
null
,
$templateCode
=
null
)
{
$key
=
$this
->
getKey
(
$mobile
);
$val
=
cache
()
->
get
(
$key
);
if
(
$val
)
{
list
(,
$time
,
$count
)
=
explode
(
','
,
$val
);
if
(
$count
>=
3
)
{
return
'验证码发送次数超限,请稍后再试'
;
}
if
(
time
()
-
$time
<
60
)
{
return
'发送验证码请求过于频繁'
;
}
$count
+=
1
;
}
else
{
$count
=
1
;
}
$vcode
=
mt_rand
(
10000
,
99999
);
$alisms
=
new
Alisms
(
$signName
??
Alisms
::
SN_01
,
$templateCode
??
Alisms
::
TPL_01
,
[
'code'
=>
$vcode
]
);
$response
=
$alisms
->
send
(
$mobile
);
if
(
$response
->
Code
==
'OK'
)
{
$vdata
=
sprintf
(
"%d,%d,%d"
,
$vcode
,
time
(),
$count
);
$result
=
cache
()
->
set
(
$key
,
$vdata
,
300
);
// 验证码有效期5分钟
if
(
!
$result
)
{
return
'验证码存储失败'
;
}
}
else
{
return
'验证码发送失败:'
.
$response
->
Message
;
}
}
/**
* 校验验证码
*
* @param integer $mobile
* @param integer $smscode
* @return bool 验证码正确时返回true
*/
public
function
check
(
int
$mobile
,
int
$smscode
)
{
$key
=
$this
->
getKey
(
$mobile
);
$val
=
cache
()
->
get
(
$key
);
if
(
$val
)
{
list
(
$vcode
,,
)
=
explode
(
','
,
$val
);
if
(
$vcode
==
$smscode
)
{
return
true
;
}
}
return
false
;
}
private
function
getKey
(
$mobile
)
{
return
'smscode-'
.
md5
(
$this
->
sellerId
.
'|'
.
$mobile
);
}
}
}
\ No newline at end of file
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