Last active
April 23, 2020 08:21
-
-
Save GithubMrxia/5266c6099047d3757af42b500c1a5469 to your computer and use it in GitHub Desktop.
短信验证码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Libs\Sms; | |
use App\Libs\Sms\Sms; | |
use App\Libs\RedisLib; | |
class SmsVerificationCode | |
{ | |
protected $seedLimit = 60; | |
protected $expire = 300; | |
public function send(string $phone, string $key, string $smsContent = '', int $expire = 300) | |
{ | |
$key = self::getKey($phone, $key); | |
if ($this->speedLimit($key)) { | |
return $this->respFail('请不要在60秒内重复发送验证码'); | |
} | |
$verificationCode = self::generateVerificationCode(); | |
$sms = new Sms(); | |
$msg = "验证码:{$verificationCode},您正在登录北极洲灵活用工后台,非本人使用,请立即联系客服。"; | |
if ($smsContent) { | |
$msg = sprintf($smsContent, $verificationCode); | |
} | |
$res = $sms->sendSms($phone, $msg); | |
if ($res['respCode'] != '00000') { | |
return $this->respFail($res['respMsg']); | |
} | |
$content = $verificationCode . ',' . $expire; | |
RedisLib::set($key, $content, $expire); | |
$data = [ | |
'expire' => $expire, | |
'expire_at' => date('YmdHis', time() + $expire) | |
]; | |
return $this->respSuc($data); | |
} | |
/** | |
* 验证验证码 | |
* | |
* @param string $phone | |
* @param string $key | |
* @param string $verificationCode | |
* @return array | |
*/ | |
public function verifyVerificationCode(string $phone, string $key, string $verificationCode) | |
{ | |
$key = self::getKey($phone, $key); | |
$content = RedisLib::get($key); | |
if (!$content) { | |
return $this->respFail('验证码错误或已失效'); | |
} | |
$contentArr = explode(',', $content); | |
$redisVerificationCode = $contentArr[0]; | |
$res = $verificationCode == $redisVerificationCode; | |
if ($res) { | |
RedisLib::del($key); | |
return $this->respSuc(); | |
} | |
return $this->respFail('验证码不正确'); | |
} | |
/** | |
* 生成验证码 | |
* | |
* @return int | |
*/ | |
protected static function generateVerificationCode() | |
{ | |
if (env('APP_DEBUG') && !in_array(env('APP_ENV'), ['prd'])) { | |
return '123456'; | |
} | |
return mt_rand(100000, 999999); | |
} | |
public function speedLimit(string $redisKey) | |
{ | |
if ($content = RedisLib::get($redisKey)) { | |
$contentArr = explode(',', $content); | |
$expire = array_key_exists(1, $contentArr) ? $contentArr[1] : $this->expire; | |
$ttl = RedisLib::ttl($redisKey); | |
return $expire - $ttl <= $this->seedLimit; | |
} | |
return false; | |
} | |
public static function getKey(string $phone, string $key) | |
{ | |
return 'v:code:' . $phone . ':' . $key; | |
} | |
protected function respSuc(array $data = []) | |
{ | |
return [ | |
'code' => '00000', | |
'msg' => '成功', | |
'data' => $data | |
]; | |
} | |
protected function respFail(string $msg) | |
{ | |
return [ | |
'code' => '10000', | |
'msg' => $msg | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment