- 插件管理:集成 qcloudsms 腾讯云短信发送插件,配置应用AppID/AppKey、短信签名及各业务模板ID(登录、修改密码、重置密码、支付密码、订单核销通知) - 业务封装:新增 SmsService 统一业务短信服务,实现防刷限流、冷却控制、短信验证码发送与防重放校验,支持完工订单自动下发客户核销码 - 接口层:恢复并升级 Sms 控制器,并在 Auth/MasterAuth/Security 控制器中支持手机验证码登录、找回登录密码及短信验证设置/修改支付密码 - 小程序端:用户端与师傅端登录页、账号安全中心、支付密码组件全面适配短信验证码交互,优化用户找回密码与支付密码安全操作流程 - 部署脚本:优化 deploy_all.py 打包剪枝逻辑,全局过滤深层 .git 目录并规范打包相对路径
224 行
5.8 KiB
PHP
可执行文件
224 行
5.8 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace app\common\library;
|
|
|
|
use fast\Random;
|
|
use think\Db;
|
|
use think\Hook;
|
|
|
|
/**
|
|
* 短信验证码类
|
|
*/
|
|
class Sms
|
|
{
|
|
|
|
/**
|
|
* 验证码有效时长
|
|
* @var int
|
|
*/
|
|
protected static $expire = 300;
|
|
|
|
/**
|
|
* 最大允许检测的次数
|
|
* @var int
|
|
*/
|
|
protected static $maxCheckNums = 10;
|
|
|
|
/**
|
|
* 最近一次发送失败原因
|
|
* @var mixed
|
|
*/
|
|
protected static $lastError = '';
|
|
|
|
/**
|
|
* 获取最近一次发送失败原因
|
|
* @return mixed
|
|
*/
|
|
public static function getLastError()
|
|
{
|
|
return self::$lastError;
|
|
}
|
|
|
|
/**
|
|
* 从短信插件提取错误信息
|
|
*/
|
|
protected static function captureAddonError()
|
|
{
|
|
self::$lastError = '';
|
|
try {
|
|
if (function_exists('get_addon_instance')) {
|
|
$addon = get_addon_instance('qcloudsms');
|
|
if ($addon && method_exists($addon, 'getError')) {
|
|
self::$lastError = $addon->getError();
|
|
}
|
|
}
|
|
} catch (\Throwable $e) {
|
|
self::$lastError = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取最后一次手机发送的数据
|
|
*
|
|
* @param int $mobile 手机号
|
|
* @param string $event 事件
|
|
* @return Sms
|
|
*/
|
|
public static function get($mobile, $event = 'default')
|
|
{
|
|
$sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
|
|
->order('id', 'DESC')
|
|
->find();
|
|
Hook::listen('sms_get', $sms, null, true);
|
|
return $sms ?: null;
|
|
}
|
|
|
|
/**
|
|
* 发送验证码
|
|
*
|
|
* @param int $mobile 手机号
|
|
* @param int $code 验证码,为空时将自动生成6位数字
|
|
* @param string $event 事件
|
|
* @return boolean
|
|
*/
|
|
public static function send($mobile, $code = null, $event = 'default')
|
|
{
|
|
self::$lastError = '';
|
|
$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]);
|
|
$result = Hook::listen('sms_send', $sms, null, true);
|
|
if (!$result) {
|
|
self::captureAddonError();
|
|
$sms->delete();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 发送通知
|
|
*
|
|
* @param mixed $mobile 手机号,多个以,分隔
|
|
* @param string $msg 消息内容
|
|
* @param string $template 消息模板
|
|
* @param array $templateParams 模板参数列表
|
|
* @return boolean
|
|
*/
|
|
public static function notice($mobile, $msg = '', $template = null, array $templateParams = [])
|
|
{
|
|
self::$lastError = '';
|
|
$params = [
|
|
'mobile' => $mobile,
|
|
'msg' => $msg,
|
|
'template' => $template,
|
|
];
|
|
if (!empty($templateParams)) {
|
|
$params['params'] = array_values($templateParams);
|
|
}
|
|
$result = Hook::listen('sms_notice', $params, null, true);
|
|
if (!$result) {
|
|
self::captureAddonError();
|
|
}
|
|
return (bool)$result;
|
|
}
|
|
|
|
/**
|
|
* 校验验证码
|
|
*
|
|
* @param int $mobile 手机号
|
|
* @param int $code 验证码
|
|
* @param string $event 事件
|
|
* @param boolean $flush 验证成功是否删除
|
|
* @return boolean
|
|
*/
|
|
public static function check($mobile, $code, $event = 'default', $flush = false)
|
|
{
|
|
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;
|
|
}
|
|
|
|
// 验证码已过期
|
|
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 (!hash_equals((string)$sms['code'], (string)$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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 清空指定手机号验证码
|
|
*
|
|
* @param int $mobile 手机号
|
|
* @param string $event 事件
|
|
* @return boolean
|
|
*/
|
|
public static function flush($mobile, $event = 'default')
|
|
{
|
|
\app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
|
|
->delete();
|
|
Hook::listen('sms_flush');
|
|
return true;
|
|
}
|
|
}
|