From e66ab647e14937afebb8242984ce25484b34d102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Sat, 17 May 2025 10:57:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=9F=AD=E4=BF=A1=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=20--ALiY?= =?UTF-8?q?unSmsService.java=20=E4=BC=98=E5=8C=96=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/service/imp/ALiYunSmsService.java | 44 ++++++++----------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java b/main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java index 0a73bf68..b39b24c8 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/sms/service/imp/ALiYunSmsService.java @@ -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("短信连接建立失败"); + } } }