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 '短信发送失败,请稍后重试或联系客服'; } }