3 changed files with 83 additions and 75 deletions
@ -1,60 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace app\model; |
|
||||
|
|
||||
use think\Model; |
|
||||
|
|
||||
class WechatSms extends Model |
|
||||
{ |
|
||||
|
|
||||
/** |
|
||||
* @return int |
|
||||
*/ |
|
||||
public static function getCode($wechat_user_id) |
|
||||
{ |
|
||||
$code = rand(000000,999999); |
|
||||
|
|
||||
$result = self::getValidCode($code,$wechat_user_id); |
|
||||
if ($result) { |
|
||||
return self::getCode($wechat_user_id); |
|
||||
} |
|
||||
|
|
||||
return $code; |
|
||||
} |
|
||||
|
|
||||
public static function getValidCode($code,$wechat_user_id) |
|
||||
{ |
|
||||
$where = [ |
|
||||
['code', '=', $code], |
|
||||
['wechat_user_id', '=', $wechat_user_id], |
|
||||
['create_time', '>=', time() - (10*60)], |
|
||||
]; |
|
||||
|
|
||||
return (new self())->where($where)->value('wechat_user_id'); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
public static function getValidUser($wechat_user_id) |
|
||||
{ |
|
||||
$where = [ |
|
||||
['wechat_user_id', '=', $wechat_user_id], |
|
||||
['create_time', '>=', time() - 60], |
|
||||
]; |
|
||||
|
|
||||
return (new self())->where($where)->value('create_time'); |
|
||||
} |
|
||||
|
|
||||
public static function addSmsCode($wechat_user_id,$code): bool |
|
||||
{ |
|
||||
|
|
||||
$data = [ |
|
||||
'wechat_user_id' => $wechat_user_id, |
|
||||
'code' => $code, |
|
||||
'create_time' => time() |
|
||||
]; |
|
||||
|
|
||||
return (new self())->save($data); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
||||
@ -0,0 +1,75 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\util; |
||||
|
|
||||
|
use fast\FuncException; |
||||
|
use think\facade\Cache; |
||||
|
|
||||
|
class AuthCodeUtil |
||||
|
{ |
||||
|
//redis连接 |
||||
|
private $cache; |
||||
|
//验证码的保存秒数,600秒,即:10分钟 |
||||
|
private $ttl = 600; |
||||
|
|
||||
|
//构造 |
||||
|
public function __construct($cache) |
||||
|
{ |
||||
|
$this->cache = $cache; |
||||
|
} |
||||
|
|
||||
|
//生成code保存到redis,并返回code |
||||
|
public function setCode($mobile) |
||||
|
{ |
||||
|
$key = "Auth:" . $mobile; |
||||
|
$code = $this->newAuthCode(); |
||||
|
$this->cache->set($key, $code, $this->ttl); |
||||
|
return $code; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 校对验证码 |
||||
|
* @param $mobile |
||||
|
* @param $code |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public function verifyCode($mobile, $code): bool |
||||
|
{ |
||||
|
$key = "Auth:" . $mobile; |
||||
|
$cacheCode = $this->cache->get($key); |
||||
|
if ($cacheCode == $code) { |
||||
|
return true; |
||||
|
} else { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @return int |
||||
|
*/ |
||||
|
public function newAuthCode(): int |
||||
|
{ |
||||
|
return rand(100000, 999999); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $mobile |
||||
|
* @return bool |
||||
|
* @throws FuncException |
||||
|
*/ |
||||
|
public function setRate($mobile): bool |
||||
|
{ |
||||
|
$key = "Auth:" . $mobile; |
||||
|
$cacheCode = $this->cache->get($key); |
||||
|
|
||||
|
if ($cacheCode) { |
||||
|
$ttl = Cache::store('redis')->ttl($key); |
||||
|
if (($this->ttl - $ttl) < 60) { |
||||
|
throw new FuncException('请超过60秒之后再发送短信'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue