框架升级及修复升级后的bug
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace app\common\library;
|
||||
|
||||
use fast\Random;
|
||||
use think\Db;
|
||||
use think\Hook;
|
||||
|
||||
/**
|
||||
@@ -26,8 +27,8 @@ class Sms
|
||||
/**
|
||||
* 获取最后一次手机发送的数据
|
||||
*
|
||||
* @param int $mobile 手机号
|
||||
* @param string $event 事件
|
||||
* @param int $mobile 手机号
|
||||
* @param string $event 事件
|
||||
* @return Sms
|
||||
*/
|
||||
public static function get($mobile, $event = 'default')
|
||||
@@ -42,14 +43,14 @@ class Sms
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
* @param int $mobile 手机号
|
||||
* @param int $code 验证码,为空时将自动生成4位数字
|
||||
* @param string $event 事件
|
||||
* @param int $mobile 手机号
|
||||
* @param int $code 验证码,为空时将自动生成6位数字
|
||||
* @param string $event 事件
|
||||
* @return boolean
|
||||
*/
|
||||
public static function send($mobile, $code = null, $event = 'default')
|
||||
{
|
||||
$code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
|
||||
$code = is_null($code) ? Random::numeric(config('fastadmin.sms_captcha_length') ?: 6) : $code;
|
||||
$time = time();
|
||||
$ip = request()->ip();
|
||||
$sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
|
||||
@@ -64,9 +65,9 @@ class Sms
|
||||
/**
|
||||
* 发送通知
|
||||
*
|
||||
* @param mixed $mobile 手机号,多个以,分隔
|
||||
* @param string $msg 消息内容
|
||||
* @param string $template 消息模板
|
||||
* @param mixed $mobile 手机号,多个以,分隔
|
||||
* @param string $msg 消息内容
|
||||
* @param string $template 消息模板
|
||||
* @return boolean
|
||||
*/
|
||||
public static function notice($mobile, $msg = '', $template = null)
|
||||
@@ -83,34 +84,81 @@ class Sms
|
||||
/**
|
||||
* 校验验证码
|
||||
*
|
||||
* @param int $mobile 手机号
|
||||
* @param int $code 验证码
|
||||
* @param string $event 事件
|
||||
* @param int $mobile 手机号
|
||||
* @param int $code 验证码
|
||||
* @param string $event 事件
|
||||
* @param boolean $flush 验证成功是否删除
|
||||
* @return boolean
|
||||
*/
|
||||
public static function check($mobile, $code, $event = 'default')
|
||||
public static function check($mobile, $code, $event = 'default', $flush = false)
|
||||
{
|
||||
$time = time() - self::$expire;
|
||||
$sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
|
||||
->order('id', 'DESC')
|
||||
->find();
|
||||
if ($sms) {
|
||||
if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
|
||||
$correct = $code == $sms['code'];
|
||||
if (!$correct) {
|
||||
$sms->times = $sms->times + 1;
|
||||
$sms->save();
|
||||
return false;
|
||||
} else {
|
||||
$result = Hook::listen('sms_check', $sms, null, true);
|
||||
return $result;
|
||||
}
|
||||
} else {
|
||||
// 过期则清空该手机验证码
|
||||
self::flush($mobile, $event);
|
||||
if (empty($mobile) || empty($code)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$expireTime = time() - self::$expire;
|
||||
|
||||
// 开启事务
|
||||
Db::startTrans();
|
||||
|
||||
try {
|
||||
// 使用行锁查询最新的验证码记录
|
||||
$sms = \app\common\model\Sms::where([
|
||||
'mobile' => $mobile,
|
||||
'event' => $event
|
||||
])
|
||||
->order('id', 'desc')
|
||||
->lock(true) // 行锁
|
||||
->find();
|
||||
|
||||
// 验证码记录不存在
|
||||
if (!$sms) {
|
||||
Db::rollback();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
||||
// 验证码已过期
|
||||
if ($sms['createtime'] <= $expireTime) {
|
||||
self::flush($mobile, $event);
|
||||
Db::rollback();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 验证次数已超限
|
||||
if ($sms['times'] >= self::$maxCheckNums) {
|
||||
Db::rollback();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 先增加验证次数(无论验证成功失败都计数)
|
||||
Db::name('sms')
|
||||
->where('id', $sms['id'])
|
||||
->setInc('times');
|
||||
|
||||
// 验证码不匹配
|
||||
if ($code != $sms['code']) {
|
||||
Db::commit(); // 提交次数增加
|
||||
return false;
|
||||
}
|
||||
|
||||
// 删除验证码
|
||||
if ($flush) {
|
||||
self::flush($mobile, $event);
|
||||
}
|
||||
|
||||
// 验证成功,提交事务
|
||||
Db::commit();
|
||||
|
||||
// 触发验证成功钩子
|
||||
Hook::listen('sms_check', $sms);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
// 记录错误日志
|
||||
\think\Log::record('SMS验证异常: ' . $e->getMessage(), 'error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -118,8 +166,8 @@ class Sms
|
||||
/**
|
||||
* 清空指定手机号验证码
|
||||
*
|
||||
* @param int $mobile 手机号
|
||||
* @param string $event 事件
|
||||
* @param int $mobile 手机号
|
||||
* @param string $event 事件
|
||||
* @return boolean
|
||||
*/
|
||||
public static function flush($mobile, $event = 'default')
|
||||
|
||||
Reference in New Issue
Block a user