修复短信发送失败问题

This commit is contained in:
2025-08-05 20:32:07 +08:00
parent 0fb5189f4e
commit 66480121b4

View File

@@ -10,6 +10,7 @@ import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient; import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@@ -109,7 +110,7 @@ public class TencentSmsController {
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */ /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
// 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看 // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
String signName = "腾讯云"; String signName = "深圳市壹键通讯科技";
req.setSignName(signName); req.setSignName(signName);
/* 模板 ID: 必须填写已审核通过的模板 ID */ /* 模板 ID: 必须填写已审核通过的模板 ID */
@@ -118,7 +119,7 @@ public class TencentSmsController {
req.setTemplateId(templateId); req.setTemplateId(templateId);
/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */ /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
String[] templateParamSet = {"{" + code + "}"}; String[] templateParamSet = {code};
req.setTemplateParamSet(templateParamSet); req.setTemplateParamSet(templateParamSet);
/* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号] /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
@@ -155,20 +156,26 @@ public class TencentSmsController {
* [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F) * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
* 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms) * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
*/ */
String randomString = RandomStringUtils.randomAlphanumeric(32); SendStatus sendStatus = res.getSendStatusSet()[0];
long now = System.currentTimeMillis(); if ("Ok".equalsIgnoreCase(sendStatus.getCode())) {
long expireAt = now + Duration.ofMinutes(LOGIN_CODE_TTL).toMillis(); String randomString = RandomStringUtils.randomAlphanumeric(32);
Map<String, Object> map = new HashMap<>(); long now = System.currentTimeMillis();
map.put("code", code); long expireAt = now + Duration.ofMinutes(LOGIN_CODE_TTL).toMillis();
map.put("sms", res.getRequestId()); Map<String, Object> map = new HashMap<>();
map.put("verifyKey", randomString); map.put("sms", sendStatus.getMessage());
map.put("sentAt", now); map.put("verifyKey", randomString);
map.put("expireAt", expireAt); map.put("sentAt", now);
//4.保存验证码到Redis,并且设置有效期5分钟 map.put("expireAt", expireAt);
if (!sent) { Map<String, Object> codeMap = new HashMap<>(map);
redisTemplate.opsForValue().set(phone, map, Duration.ofMinutes(5)); codeMap.put("code", randomString);
if (!sent) {
//4.保存验证码到Redis,并且设置有效期5分钟
redisTemplate.opsForValue().set(phone, codeMap, Duration.ofMinutes(5));
}
return Result.ok().data(map);
} else {
return Result.error().message("短信发送失败");
} }
return Result.ok().data(map);
} catch (TencentCloudSDKException e) { } catch (TencentCloudSDKException e) {
e.printStackTrace(); e.printStackTrace();
return Result.error().message(e.getMessage()); return Result.error().message(e.getMessage());