功能说明

该函数通过腾讯云短信服务发送验证码,核心流程包括:

  1. 加载腾讯云短信 SDK
  2. 获取系统配置参数
  3. 根据发送类型选择签名和模板
  4. 调用短信发送接口
  5. 处理发送结果

关键问题分析

  1. 模板选择逻辑缺陷

    $templateId_dl = $configpri['tencent_sms_templateCode'];
    $templateId_hw = $configpri['tencent_sms_templateCode']; // 同个配置项
    
    • 问题:中国大陆与国际模板使用相同配置值
    • 风险:无法区分不同地区的短信模板
    • 建议:配置项应分离
      $templateId_dl = $configpri['tencent_sms_templateCode_dl'];
      $templateId_hw = $configpri['tencent_sms_templateCode_hw'];
      
  2. 错误处理机制不足

    $arr = json_decode($result, TRUE);
    if($arr['result']==0 && $arr['errmsg']=='OK'){...}
    
    • 问题:未处理 JSON 解析失败或字段缺失
    • 风险:可能触发未定义索引错误
    • 建议:增加防御性检查
      if(empty($arr) || !isset($arr['result'])) {
          $rs['code'] = 1003;
          $rs['msg'] = '解析服务端响应失败';
          return $rs;
      }
      
  3. 日志记录功能缺失

    // file_put_contents(...); 当前被注释
    
    • 建议:启用日志并优化记录内容
      $logContent = date('Y-m-d H:i:s')." | 手机号:{$mobile} | 响应:".$result;
      file_put_contents($logPath, $logContent.PHP_EOL, FILE_APPEND);
      

优化后代码建议

function sendCodeByTencentSms($nationCode, $mobile, $code) {
    require_once CMF_ROOT."sdk/tencentSms/index.php";
    $configpri = getConfigPri();
    
    // 基础配置
    $appid = $configpri['tencent_sms_appid'];
    $appkey = $configpri['tencent_sms_appkey'];
    
    // 签名配置
    $smsSign_dl = $configpri['tencent_sms_signName'];
    $smsSign_hw = $configpri['tencent_sms_hw_signName'];
    
    // 模板配置(分离国际/国内)
    $templateId_dl = $configpri['tencent_sms_templateCode_dl'] ?? '';
    $templateId_hw = $configpri['tencent_sms_templateCode_hw'] ?? '';
    
    // 发送类型决策
    $tencent_sendcode_type = $configpri['tencent_sendcode_type'] ?? 0;
    if ($tencent_sendcode_type == 1) {
        $smsSign = $smsSign_dl;
        $templateId = $templateId_dl;
    } elseif ($tencent_sendcode_type == 2) {
        $smsSign = $smsSign_hw;
        $templateId = $templateId_hw;
    } else {
        $smsSign = ($nationCode == 86) ? $smsSign_dl : $smsSign_hw;
        $templateId = ($nationCode == 86) ? $templateId_dl : $templateId_hw;
    }
    
    // 发送短信
    $sender = new \Qcloud\Sms\SmsSingleSender($appid, $appkey);
    $params = [$code];
    $result = $sender->sendWithParam($nationCode, $mobile, $templateId, $params, $smsSign, "", "");
    
    // 日志记录(启用)
    $logPath = API_ROOT.'/../log/sendCode_tencent_'.date('Y-m-d').'.log';
    $logData = json_encode(['mobile'=>$mobile, 'response'=>$result]);
    file_put_contents($logPath, date('[Y-m-d H:i:s]').$logData.PHP_EOL, FILE_APPEND);
    
    // 增强结果处理
    $rs = ['code' => 1002, 'msg' => '发送失败'];
    $arr = json_decode($result, true);
    
    if (json_last_error() !== JSON_ERROR_NONE) {
        $rs['msg'] = '响应数据格式异常';
        return $rs;
    }
    
    if (isset($arr['result']) && $arr['result'] == 0) {
        $rs['code'] = 0;
        $rs['msg'] = '发送成功';
    } elseif (isset($arr['errmsg'])) {
        $rs['msg'] = $arr['errmsg'];
    }
    
    return $rs;
}

数学约束说明

在短信发送场景中,需满足:
成功率=成功次数总尝试次数×100% \text{成功率} = \frac{\text{成功次数}}{\text{总尝试次数}} \times 100\% 成功率=总尝试次数成功次数×100%
优化后代码通过:

  1. 防御性编程降低错误率
  2. 详细日志支持故障分析
  3. 配置分离提升可维护性

建议在实际部署前进行边界值测试,特别是针对:

  • 非常规国家代码(如+1、+44)
  • 空模板ID场景
  • 高并发请求场景
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐