修复短信发送失败问题
This commit is contained in:
@@ -10,6 +10,7 @@ import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -109,7 +110,7 @@ public class TencentSmsController {
|
||||
|
||||
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
|
||||
// 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
|
||||
String signName = "腾讯云";
|
||||
String signName = "深圳市壹键通讯科技";
|
||||
req.setSignName(signName);
|
||||
|
||||
/* 模板 ID: 必须填写已审核通过的模板 ID */
|
||||
@@ -118,7 +119,7 @@ public class TencentSmsController {
|
||||
req.setTemplateId(templateId);
|
||||
|
||||
/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
|
||||
String[] templateParamSet = {"{" + code + "}"};
|
||||
String[] templateParamSet = {code};
|
||||
req.setTemplateParamSet(templateParamSet);
|
||||
|
||||
/* 下发手机号码,采用 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)
|
||||
* 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
|
||||
*/
|
||||
String randomString = RandomStringUtils.randomAlphanumeric(32);
|
||||
long now = System.currentTimeMillis();
|
||||
long expireAt = now + Duration.ofMinutes(LOGIN_CODE_TTL).toMillis();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("code", code);
|
||||
map.put("sms", res.getRequestId());
|
||||
map.put("verifyKey", randomString);
|
||||
map.put("sentAt", now);
|
||||
map.put("expireAt", expireAt);
|
||||
//4.保存验证码到Redis,并且设置有效期5分钟
|
||||
if (!sent) {
|
||||
redisTemplate.opsForValue().set(phone, map, Duration.ofMinutes(5));
|
||||
SendStatus sendStatus = res.getSendStatusSet()[0];
|
||||
if ("Ok".equalsIgnoreCase(sendStatus.getCode())) {
|
||||
String randomString = RandomStringUtils.randomAlphanumeric(32);
|
||||
long now = System.currentTimeMillis();
|
||||
long expireAt = now + Duration.ofMinutes(LOGIN_CODE_TTL).toMillis();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("sms", sendStatus.getMessage());
|
||||
map.put("verifyKey", randomString);
|
||||
map.put("sentAt", now);
|
||||
map.put("expireAt", expireAt);
|
||||
Map<String, Object> codeMap = new HashMap<>(map);
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
return Result.error().message(e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user