feat(sms): 集成腾讯云短信插件并打通双端短信验证码业务链路
- 插件管理:集成 qcloudsms 腾讯云短信发送插件,配置应用AppID/AppKey、短信签名及各业务模板ID(登录、修改密码、重置密码、支付密码、订单核销通知) - 业务封装:新增 SmsService 统一业务短信服务,实现防刷限流、冷却控制、短信验证码发送与防重放校验,支持完工订单自动下发客户核销码 - 接口层:恢复并升级 Sms 控制器,并在 Auth/MasterAuth/Security 控制器中支持手机验证码登录、找回登录密码及短信验证设置/修改支付密码 - 小程序端:用户端与师傅端登录页、账号安全中心、支付密码组件全面适配短信验证码交互,优化用户找回密码与支付密码安全操作流程 - 部署脚本:优化 deploy_all.py 打包剪枝逻辑,全局过滤深层 .git 目录并规范打包相对路径
这个提交包含在:
@@ -16,7 +16,7 @@ class Sms
|
||||
* 验证码有效时长
|
||||
* @var int
|
||||
*/
|
||||
protected static $expire = 120;
|
||||
protected static $expire = 300;
|
||||
|
||||
/**
|
||||
* 最大允许检测的次数
|
||||
@@ -24,6 +24,39 @@ class Sms
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后一次手机发送的数据
|
||||
*
|
||||
@@ -50,12 +83,14 @@ class Sms
|
||||
*/
|
||||
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;
|
||||
}
|
||||
@@ -65,19 +100,27 @@ class Sms
|
||||
/**
|
||||
* 发送通知
|
||||
*
|
||||
* @param mixed $mobile 手机号,多个以,分隔
|
||||
* @param string $msg 消息内容
|
||||
* @param string $template 消息模板
|
||||
* @param mixed $mobile 手机号,多个以,分隔
|
||||
* @param string $msg 消息内容
|
||||
* @param string $template 消息模板
|
||||
* @param array $templateParams 模板参数列表
|
||||
* @return boolean
|
||||
*/
|
||||
public static function notice($mobile, $msg = '', $template = null)
|
||||
public static function notice($mobile, $msg = '', $template = null, array $templateParams = [])
|
||||
{
|
||||
self::$lastError = '';
|
||||
$params = [
|
||||
'mobile' => $mobile,
|
||||
'msg' => $msg,
|
||||
'template' => $template
|
||||
'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;
|
||||
}
|
||||
|
||||
@@ -137,7 +180,7 @@ class Sms
|
||||
->setInc('times');
|
||||
|
||||
// 验证码不匹配
|
||||
if ($code != $sms['code']) {
|
||||
if (!hash_equals((string)$sms['code'], (string)$code)) {
|
||||
Db::commit(); // 提交次数增加
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library;
|
||||
|
||||
use think\Db;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* 统一业务短信服务:频次限流、密码验证码发送/校验、完工核销码短信下发
|
||||
*/
|
||||
class SmsService
|
||||
{
|
||||
const COOLDOWN_SECONDS = 60;
|
||||
const MAX_DAILY_PER_MOBILE = 10;
|
||||
const MAX_HOURLY_PER_IP = 20;
|
||||
|
||||
const ALLOWED_EVENTS = [
|
||||
'user_changepwd',
|
||||
'user_resetpwd',
|
||||
'user_paypwd',
|
||||
'master_changepwd',
|
||||
'master_resetpwd',
|
||||
'master_paypwd',
|
||||
'changepwd',
|
||||
'resetpwd',
|
||||
'paypwd',
|
||||
];
|
||||
|
||||
/**
|
||||
* 规范化并校验中国大陆 11 位手机号
|
||||
*/
|
||||
public static function normalizeMobile($mobile)
|
||||
{
|
||||
$mobile = preg_replace('/[^\d]/', '', trim((string)$mobile));
|
||||
if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) {
|
||||
throw new \InvalidArgumentException('手机号格式不正确');
|
||||
}
|
||||
return $mobile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验发送频次限制并发送 6 位数字短信验证码
|
||||
*
|
||||
* @param string $mobile 手机号
|
||||
* @param string $event 事件标识
|
||||
* @param string $ip 请求 IP
|
||||
* @return array ['cooldown' => int, 'expire_seconds' => int]
|
||||
*/
|
||||
public static function sendVerifyCode($mobile, $event, $ip = '')
|
||||
{
|
||||
$mobile = self::normalizeMobile($mobile);
|
||||
$event = trim((string)$event);
|
||||
if (!in_array($event, self::ALLOWED_EVENTS, true)) {
|
||||
throw new \InvalidArgumentException('不支持的短信事件类型');
|
||||
}
|
||||
|
||||
$now = time();
|
||||
$last = Sms::get($mobile, $event);
|
||||
if ($last) {
|
||||
$elapsed = $now - (int)$last['createtime'];
|
||||
if ($elapsed < self::COOLDOWN_SECONDS) {
|
||||
$wait = self::COOLDOWN_SECONDS - $elapsed;
|
||||
throw new \RuntimeException("发送过于频繁,请 {$wait} 秒后再试");
|
||||
}
|
||||
}
|
||||
|
||||
$dayCount = (int)\app\common\model\Sms::where('mobile', $mobile)
|
||||
->where('createtime', '>=', $now - 86400)
|
||||
->count();
|
||||
if ($dayCount >= self::MAX_DAILY_PER_MOBILE) {
|
||||
throw new \RuntimeException('该手机号今日短信发送次数已达上限,请明天再试');
|
||||
}
|
||||
|
||||
if ($ip !== '') {
|
||||
$ipCount = (int)\app\common\model\Sms::where('ip', $ip)
|
||||
->where('createtime', '>=', $now - 3600)
|
||||
->count();
|
||||
if ($ipCount >= self::MAX_HOURLY_PER_IP) {
|
||||
throw new \RuntimeException('当前网络请求验证码过于频繁,请稍后再试');
|
||||
}
|
||||
}
|
||||
|
||||
$sent = Sms::send($mobile, null, $event);
|
||||
if (!$sent) {
|
||||
$errMsg = self::formatAddonError(Sms::getLastError());
|
||||
throw new \RuntimeException($errMsg);
|
||||
}
|
||||
|
||||
return [
|
||||
'cooldown' => self::COOLDOWN_SECONDS,
|
||||
'expire_seconds' => 300,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验短信验证码(默认校验成功后立即失效,防重放)
|
||||
*/
|
||||
public static function verifyCode($mobile, $code, $event, $flush = true)
|
||||
{
|
||||
$mobile = self::normalizeMobile($mobile);
|
||||
$code = trim((string)$code);
|
||||
if (!preg_match('/^\d{4,6}$/', $code)) {
|
||||
throw new \InvalidArgumentException('请输入有效的短信验证码');
|
||||
}
|
||||
$event = trim((string)$event);
|
||||
if (!in_array($event, self::ALLOWED_EVENTS, true)) {
|
||||
throw new \InvalidArgumentException('不支持的短信事件类型');
|
||||
}
|
||||
|
||||
if (!Sms::check($mobile, $code, $event, $flush)) {
|
||||
throw new \RuntimeException('短信验证码不正确或已过期');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 师傅完工后向订单客户联系手机下发 6 位验收核销码短信
|
||||
* 注意:绝不向调用方/师傅端暴露或抛出异常阻断完工主流程
|
||||
*
|
||||
* @param array $order 订单记录
|
||||
* @return array ['sent' => bool, 'mobile_mask' => string, 'reason' => string]
|
||||
*/
|
||||
public static function sendOrderVerifyCode(array $order)
|
||||
{
|
||||
$orderId = (int)($order['id'] ?? 0);
|
||||
$code = trim((string)($order['verify_code'] ?? ''));
|
||||
if ($code === '') {
|
||||
return ['sent' => false, 'mobile_mask' => '', 'reason' => '核销码为空'];
|
||||
}
|
||||
|
||||
// 优先发送到订单客户联系手机(order.mobile),异常或空时兜底取下单账号手机
|
||||
$targetMobile = preg_replace('/[^\d]/', '', trim((string)($order['mobile'] ?? '')));
|
||||
if (!preg_match('/^1[3-9]\d{9}$/', $targetMobile) && !empty($order['user_id'])) {
|
||||
$userMobile = (string)Db::name('user')->where('id', (int)$order['user_id'])->value('mobile');
|
||||
$targetMobile = preg_replace('/[^\d]/', '', trim($userMobile));
|
||||
}
|
||||
|
||||
if (!preg_match('/^1[3-9]\d{9}$/', $targetMobile)) {
|
||||
Log::warning("[SmsService::sendOrderVerifyCode] 订单#{$orderId} 缺少有效客户手机号,跳过核销码短信下发");
|
||||
return ['sent' => false, 'mobile_mask' => '', 'reason' => '客户手机号无效'];
|
||||
}
|
||||
|
||||
$maskedMobile = substr_replace($targetMobile, '****', 3, 4);
|
||||
|
||||
try {
|
||||
$sent = Sms::notice($targetMobile, $code, 'order_verify', [$code]);
|
||||
if (!$sent) {
|
||||
$reason = self::formatAddonError(Sms::getLastError());
|
||||
Log::warning("[SmsService::sendOrderVerifyCode] 订单#{$orderId} 下发核销码短信失败 mobile={$maskedMobile} reason={$reason}");
|
||||
return ['sent' => false, 'mobile_mask' => $maskedMobile, 'reason' => $reason];
|
||||
}
|
||||
Log::info("[SmsService::sendOrderVerifyCode] 订单#{$orderId} 已向客户 {$maskedMobile} 下发核销码短信");
|
||||
return ['sent' => true, 'mobile_mask' => $maskedMobile, 'reason' => ''];
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("[SmsService::sendOrderVerifyCode] 订单#{$orderId} 下发核销码短信异常: " . $e->getMessage());
|
||||
return ['sent' => false, 'mobile_mask' => $maskedMobile, 'reason' => '短信发送异常'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将插件底层错误格式化为可读提示(脱敏不透传英文协议与密钥等内部细节)
|
||||
*/
|
||||
public static function formatAddonError($rawError)
|
||||
{
|
||||
if (is_string($rawError) && $rawError !== '') {
|
||||
if (strpos($rawError, '未配置') !== false) {
|
||||
return '短信服务尚未配置完成,请联系平台管理员';
|
||||
}
|
||||
if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $rawError) && !preg_match('/[A-Za-z]{4,}/', $rawError)) {
|
||||
return $rawError;
|
||||
}
|
||||
return '短信发送失败,请稍后重试';
|
||||
}
|
||||
if (is_array($rawError)) {
|
||||
$code = (int)($rawError['result'] ?? 0);
|
||||
if ($code === 1014) {
|
||||
return '短信模板审核中或未生效,请稍后再试';
|
||||
}
|
||||
if (in_array($code, [1013, 1022, 1023, 1024, 1025], true)) {
|
||||
return '验证码发送过于频繁,请稍后再试';
|
||||
}
|
||||
if ($code === 1016) {
|
||||
return '手机号格式不正确,请核对后重试';
|
||||
}
|
||||
if ($code === -1 || $code === -2) {
|
||||
return '短信通道连接超时,请稍后重试';
|
||||
}
|
||||
if ($code > 0) {
|
||||
return '短信服务暂不可用,请稍后重试或联系客服';
|
||||
}
|
||||
}
|
||||
return '短信发送失败,请稍后重试或联系客服';
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户