mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
优化短信发送的异常处理
--ALiYunSmsService.java 优化异常处理
This commit is contained in:
+19
-25
@@ -3,14 +3,13 @@ package xiaozhi.modules.sms.service.imp;
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.modules.sms.service.SmsService;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
|
||||
@@ -22,34 +21,25 @@ public class ALiYunSmsService implements SmsService {
|
||||
|
||||
@Override
|
||||
public void sendVerificationCodeSms(String phone, String VerificationCode) {
|
||||
Client client = createClient();
|
||||
String SignName = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_SIGN_NAME.getValue(),true);
|
||||
String TemplateCode = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_SMS_CODE_TEMPLATE_CODE.getValue(),true);
|
||||
try {
|
||||
Client client = createClient();
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||
.setSignName(SignName)
|
||||
.setTemplateCode(TemplateCode)
|
||||
.setPhoneNumbers(phone)
|
||||
.setTemplateParam(String.format("{\"code\":\"%s\"}", VerificationCode));
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
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));
|
||||
}
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
log.info("发送短信响应的requestID: {}", sendSmsResponse.getBody().getRequestId());
|
||||
} catch (Exception e) {
|
||||
// 错误 message
|
||||
log.error(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
throw new RenException("短信发送失败");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -58,19 +48,23 @@ public class ALiYunSmsService implements SmsService {
|
||||
/**
|
||||
* 创建阿里云连接
|
||||
* @return 返回连接对象
|
||||
* @throws Exception
|
||||
*/
|
||||
private Client createClient() throws Exception {
|
||||
private Client createClient(){
|
||||
String ACCESS_KEY_ID = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_ACCESS_KEY_ID.getValue(),true);
|
||||
String ACCESS_KEY_SECRET = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_ACCESS_KEY_SECRET.getValue(),true);
|
||||
Config config = new Config()
|
||||
.setAccessKeyId(ACCESS_KEY_ID)
|
||||
.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||
// 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com
|
||||
config.endpoint = "dysmsapi.aliyuncs.com";
|
||||
|
||||
return new Client(config);
|
||||
try {
|
||||
Config config = new Config()
|
||||
.setAccessKeyId(ACCESS_KEY_ID)
|
||||
.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||
// 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com
|
||||
config.endpoint = "dysmsapi.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}catch (Exception e){
|
||||
// 错误 message
|
||||
log.error(e.getMessage());
|
||||
throw new RenException("短信连接建立失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user