From 21e571e237af7af86210fbe964910281cd3a4e5a Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 24 Apr 2025 01:45:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChouBox.Code/Other/SMSBLL.cs | 35 +++++++++++++------ .../TencentSMSSendVerificationCode.cs | 32 +++++++++++++++-- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/ChouBox.Code/Other/SMSBLL.cs b/ChouBox.Code/Other/SMSBLL.cs index 267fee9..55551a6 100644 --- a/ChouBox.Code/Other/SMSBLL.cs +++ b/ChouBox.Code/Other/SMSBLL.cs @@ -53,8 +53,6 @@ public class SMSBLL : ChouBoxCodeBase var dailyCountKey = $"VerificationCodeDailyCount:{phone}:{DateTime.Now:yyyyMMdd}"; var dailyCount = RedisCache.StringGet(dailyCountKey) ?? 0; - - // 获取重试次数 var retryCountKey = $"VerificationCode:{phone}{RETRY_COUNT_SUFFIX}"; var retryCount = RedisCache.StringGet(retryCountKey) ?? 0; @@ -103,18 +101,26 @@ public class SMSBLL : ChouBoxCodeBase try { TencentSMSSendVerificationCode tencentSMSSendVerificationCode = new TencentSMSSendVerificationCode(smsConfig, phone); - var result = await tencentSMSSendVerificationCode.SendVerificationCode(verificationCode.ToString(), 5); - if (!result) + bool smsSucceeded = true; + string errorMessage = "验证码发送失败"; + + try { + var result = await tencentSMSSendVerificationCode.SendVerificationCode(verificationCode.ToString(), 5); + if (!result) + { + smsSucceeded = false; + } + } + catch (Exception smsEx) + { + smsSucceeded = false; + errorMessage = smsEx.Message; // 记录发送失败日志 - Logger.LogError($"验证码发送失败,手机号:{phone},IP:{ipAddress}"); - throw new CustomException("验证码发送失败"); + Logger.LogError($"验证码发送异常,手机号:{phone},IP:{ipAddress},错误:{smsEx.Message}"); } - // 发送成功,存入Redis,5分钟有效期 - await RedisCache.StringSetAsync(redisKey, verificationCode.ToString(), TimeSpan.FromMinutes(5)); - - // 更新重试次数和锁定时间 + // 无论成功失败都更新重试次数和锁定时间 retryCount++; var nextWaitSeconds = GetWaitSeconds(retryCount); @@ -130,6 +136,15 @@ public class SMSBLL : ChouBoxCodeBase var expireTime = endOfDay - DateTime.Now; await RedisCache.StringSetAsync(dailyCountKey, dailyCount.ToString(), expireTime); + // 如果短信发送失败,抛出异常 + if (!smsSucceeded) + { + throw new CustomException(errorMessage); + } + + // 发送成功,存入Redis,5分钟有效期 + await RedisCache.StringSetAsync(redisKey, verificationCode.ToString(), TimeSpan.FromMinutes(5)); + // 保存到数据库 var verificationRecord = new UserVerificationCodes { diff --git a/ChouBox.Code/TencentCloudExtend/TencentSMSSendVerificationCode.cs b/ChouBox.Code/TencentCloudExtend/TencentSMSSendVerificationCode.cs index 490e676..62e030c 100644 --- a/ChouBox.Code/TencentCloudExtend/TencentSMSSendVerificationCode.cs +++ b/ChouBox.Code/TencentCloudExtend/TencentSMSSendVerificationCode.cs @@ -124,12 +124,39 @@ public class TencentSMSSendVerificationCode(TencentSMSConfig tencentSMSConfig, s Console.WriteLine(AbstractModel.ToJsonString(resp)); + // 检查短信发送状态 + if (resp.SendStatusSet != null && resp.SendStatusSet.Length > 0) + { + foreach (var status in resp.SendStatusSet) + { + if (status.Code != "Ok") + { + // 检查特定错误码并提供更友好的错误信息 + if (status.Code == "LimitExceeded.PhoneNumberSameContentDailyLimit" || status.Code == "LimitExceeded.PhoneNumberDailyLimit") + { + throw new Exception($"您的手机号今日接收该内容的短信次数已达上限,请明天再试"); + } + else if (status.Code == "LimitExceeded.PhoneNumberThirtySecondLimit") + { + throw new Exception($"您的手机号短信请求过于频繁,请30秒后再试"); + } + else + { + throw new Exception($"短信发送失败:手机号 {status.PhoneNumber},错误代码 {status.Code},错误信息 {status.Message}"); + } + } + } + } + else + { + throw new Exception("短信发送失败:未收到发送状态"); + } + } catch (Exception e) { - Console.WriteLine(e.ToString()); - return false; + throw; // 重新抛出异常,而不是返回false } //Console.Read(); return true; @@ -137,4 +164,3 @@ public class TencentSMSSendVerificationCode(TencentSMSConfig tencentSMSConfig, s } } - \ No newline at end of file