优化短信发送的异常处理

--ALiYunSmsService.java 优化异常处理
This commit is contained in:
剑雨
2025-05-17 10:57:02 +08:00
parent 2741e11078
commit e66ab647e1
@@ -3,14 +3,13 @@ package xiaozhi.modules.sms.service.imp;
import com.aliyun.dysmsapi20170525.Client; import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse; import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config; import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions; import com.aliyun.teautil.models.RuntimeOptions;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import xiaozhi.common.constant.Constant; import xiaozhi.common.constant.Constant;
import xiaozhi.common.exception.RenException;
import xiaozhi.modules.sms.service.SmsService; import xiaozhi.modules.sms.service.SmsService;
import xiaozhi.modules.sys.service.SysParamsService; import xiaozhi.modules.sys.service.SysParamsService;
@@ -22,34 +21,25 @@ public class ALiYunSmsService implements SmsService {
@Override @Override
public void sendVerificationCodeSms(String phone, String VerificationCode) { public void sendVerificationCodeSms(String phone, String VerificationCode) {
Client client = createClient();
String SignName = sysParamsService.getValue(Constant.SysMSMParam String SignName = sysParamsService.getValue(Constant.SysMSMParam
.ALIYUN_SMS_SIGN_NAME.getValue(),true); .ALIYUN_SMS_SIGN_NAME.getValue(),true);
String TemplateCode = sysParamsService.getValue(Constant.SysMSMParam String TemplateCode = sysParamsService.getValue(Constant.SysMSMParam
.ALIYUN_SMS_SMS_CODE_TEMPLATE_CODE.getValue(),true); .ALIYUN_SMS_SMS_CODE_TEMPLATE_CODE.getValue(),true);
try { try {
Client client = createClient();
SendSmsRequest sendSmsRequest = new SendSmsRequest() SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName(SignName) .setSignName(SignName)
.setTemplateCode(TemplateCode) .setTemplateCode(TemplateCode)
.setPhoneNumbers(phone) .setPhoneNumbers(phone)
.setTemplateParam(String.format("{\"code\":\"%s\"}", VerificationCode)); .setTemplateParam(String.format("{\"code\":\"%s\"}", VerificationCode));
RuntimeOptions runtime = new RuntimeOptions(); RuntimeOptions runtime = new RuntimeOptions();
try { // 复制代码运行请自行打印 API 的返回值
// 复制代码运行请自行打印 API 的返回值 SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime); log.info("发送短信响应的requestID: {}", sendSmsResponse.getBody().getRequestId());
System.out.println(sendSmsResponse.getBody().getRequestId());
System.out.println(sendSmsResponse.getBody().getRequestId());
} catch (TeaException error) {
// 错误 message
log.error(error.getMessage());
// 诊断地址
log.error(error.getData().get("Recommend").toString());
log.error(Common.assertAsString(error.message));
}
} catch (Exception e) { } catch (Exception e) {
// 错误 message // 错误 message
log.error(e.getMessage()); log.error(e.getMessage());
throw new RuntimeException(e); throw new RenException("短信发送失败");
} }
} }
@@ -58,19 +48,23 @@ public class ALiYunSmsService implements SmsService {
/** /**
* 创建阿里云连接 * 创建阿里云连接
* @return 返回连接对象 * @return 返回连接对象
* @throws Exception
*/ */
private Client createClient() throws Exception { private Client createClient(){
String ACCESS_KEY_ID = sysParamsService.getValue(Constant.SysMSMParam String ACCESS_KEY_ID = sysParamsService.getValue(Constant.SysMSMParam
.ALIYUN_SMS_ACCESS_KEY_ID.getValue(),true); .ALIYUN_SMS_ACCESS_KEY_ID.getValue(),true);
String ACCESS_KEY_SECRET = sysParamsService.getValue(Constant.SysMSMParam String ACCESS_KEY_SECRET = sysParamsService.getValue(Constant.SysMSMParam
.ALIYUN_SMS_ACCESS_KEY_SECRET.getValue(),true); .ALIYUN_SMS_ACCESS_KEY_SECRET.getValue(),true);
Config config = new Config() try {
.setAccessKeyId(ACCESS_KEY_ID) Config config = new Config()
.setAccessKeySecret(ACCESS_KEY_SECRET); .setAccessKeyId(ACCESS_KEY_ID)
// 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com .setAccessKeySecret(ACCESS_KEY_SECRET);
config.endpoint = "dysmsapi.aliyuncs.com"; // 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config); return new Client(config);
}catch (Exception e){
// 错误 message
log.error(e.getMessage());
throw new RenException("短信连接建立失败");
}
} }
} }