短信-腾讯云-resp日志

This commit is contained in:
khalil
2025-04-29 20:15:48 +08:00
parent e34d4a9353
commit 2af2491534
2 changed files with 20 additions and 23 deletions

View File

@@ -9,11 +9,13 @@ import com.accompany.core.exception.ServiceException;
import com.accompany.sms.config.TencentSmsConfig;
import com.accompany.sms.service.TencentSmsService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
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 lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -37,7 +39,7 @@ public class TencentSmsServiceImpl implements TencentSmsService {
private SmsClient smsClient;
@Override
public SendStatus sendSmsCode(String mobile, String app, String code) throws TencentCloudSDKException {
public SendStatus sendSmsCode(String mobile, String app, String code) {
log.info("tencent mobile : {}, app : {}, code : {}", mobile, app, code);
String appId = tencentSmsConfig.getAppId();
Map<String, TencentSmsConfig.AppConfig> apps = tencentSmsConfig.getApps();
@@ -75,8 +77,8 @@ public class TencentSmsServiceImpl implements TencentSmsService {
* @return
* @throws TencentCloudSDKException
*/
@Nullable
private SendStatus send(String mobile, String appId, String signName, String templateId, String[] templateParamSet) throws TencentCloudSDKException {
@SneakyThrows
private SendStatus send(String mobile, String appId, String signName, String templateId, String[] templateParamSet) {
/* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
* 你可以直接查询SDK源码确定接口有哪些属性可以设置
* 属性可能是基本类型,也可能引用了另一个数据结构
@@ -106,19 +108,17 @@ public class TencentSmsServiceImpl implements TencentSmsService {
req.setPhoneNumberSet(phoneNumberSet);
/* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
* 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
log.info("SendSmsRequest : {}", JSON.toJSONString(req));
SendSmsResponse res = smsClient.SendSms(req);
if (res != null) {
log.info("SendSmsResponse : {}", JSONObject.toJSON(res));
/* 当出现以下错误码时,快速解决方案参考
* [FailedOperation.SignatureIncorrectOrUnapproved](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.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
* [FailedOperation.TemplateIncorrectOrUnapproved](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.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
* [UnauthorizedOperation.SmsSdkAppIdVerifyFail](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.9Aunauthorizedoperation.smssdkappidverifyfail-.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)
*/
return res.getSendStatusSet()[0];
}
return null;
log.info("SendSmsResponse : {}", JSON.toJSONString(res));
/* 当出现以下错误码时,快速解决方案参考
* [FailedOperation.SignatureIncorrectOrUnapproved](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.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
* [FailedOperation.TemplateIncorrectOrUnapproved](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.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
* [UnauthorizedOperation.SmsSdkAppIdVerifyFail](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.9Aunauthorizedoperation.smssdkappidverifyfail-.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)
*/
return res.getSendStatusSet()[0];
}
@Override

View File

@@ -5,6 +5,7 @@ import com.accompany.sms.enums.SmsTypeEnum;
import com.accompany.sms.service.TencentSmsService;
import com.accompany.sms.service.impl.TencentSmsServiceImpl;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
@@ -28,20 +29,16 @@ public class TencentSmsStrategy extends SmsStrategy {
return SmsTypeEnum.TENCENT.name().toLowerCase();
}
@SneakyThrows
@Override
public SmsResponse doSendSmsCode(SmsContext context) {
SmsResponse response = new SmsResponse();
String mobile = context.getMobile();
String app = context.getApp();
String code = context.getCode();
try {
SendStatus sendStatus = tencentSmsService.sendSmsCode(mobile, app, code);
response.setCode(sendStatus.getCode());
response.setMessage(sendStatus.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
SendStatus sendStatus = tencentSmsService.sendSmsCode(mobile, app, code);
response.setCode(sendStatus.getCode());
response.setMessage(sendStatus.getMessage());
return response;
}