From 1b2462a55e70c9db1cdab10e80f31475e2921c2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=8F=E7=A5=A5?= <14281818+xue-hongxiang@user.noreply.gitee.com> Date: Tue, 17 Dec 2024 20:04:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=B4=E4=BF=AE=E4=BA=BA=E5=91=98=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8D=95=E6=95=B0=E9=87=8F=E5=92=8C=E6=BB=A1=E6=84=8F?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/business/sms/AliYunSmsConfig.java | 39 --------- .../business/sms/AliYunSmsManager.java | 72 ----------------- .../modules/business/sms/SmsConfig.java | 27 +++++++ .../modules/business/sms/SmsService.java | 80 +++++++++---------- src/main/resources/application-dev.yml | 2 +- src/main/resources/application.yml | 4 +- 6 files changed, 71 insertions(+), 153 deletions(-) delete mode 100644 src/main/java/org/springblade/modules/business/sms/AliYunSmsConfig.java delete mode 100644 src/main/java/org/springblade/modules/business/sms/AliYunSmsManager.java create mode 100644 src/main/java/org/springblade/modules/business/sms/SmsConfig.java diff --git a/src/main/java/org/springblade/modules/business/sms/AliYunSmsConfig.java b/src/main/java/org/springblade/modules/business/sms/AliYunSmsConfig.java deleted file mode 100644 index 7568e96..0000000 --- a/src/main/java/org/springblade/modules/business/sms/AliYunSmsConfig.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.springblade.modules.business.sms; - -import com.aliyun.teaopenapi.models.*; -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -/** - * @author xueyaoxuan - */ -@Data -@Component -@ConfigurationProperties(prefix = "sms.aliyun") -public class AliYunSmsConfig { - - private String accessKeyId; - private String accessKeySecret; - private String signName; - private boolean isPushSms; - - /** - * 使用AK&SK初始化账号Client - * - * @return Client - * @throws Exception - */ - public com.aliyun.dysmsapi20170525.Client createClient() throws Exception { - Config config = new Config() - // AccessKey ID - .setAccessKeyId(this.getAccessKeyId()) - // AccessKey Secret - .setAccessKeySecret(this.getAccessKeySecret()); - // 访问的域名 - config.endpoint = "dysmsapi.aliyuncs.com"; - return new com.aliyun.dysmsapi20170525.Client(config); - } - -} - diff --git a/src/main/java/org/springblade/modules/business/sms/AliYunSmsManager.java b/src/main/java/org/springblade/modules/business/sms/AliYunSmsManager.java deleted file mode 100644 index e3b08d6..0000000 --- a/src/main/java/org/springblade/modules/business/sms/AliYunSmsManager.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.springblade.modules.business.sms; - -import cn.hutool.core.util.StrUtil; -import com.aliyun.dysmsapi20170525.Client; -import com.aliyun.dysmsapi20170525.models.SendSmsRequest; -import com.aliyun.dysmsapi20170525.models.SendSmsResponse; -import lombok.extern.slf4j.Slf4j; -import org.springblade.core.log.exception.ServiceException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.List; -import java.util.stream.Collectors; - -@Slf4j -@Component -public class AliYunSmsManager { - - @Autowired - private AliYunSmsConfig aliYunSmsConfig; - - public String sendSms(List mobiles, String message, String code) { - SendSmsResponse sendSmsResponse = null; - try{ - //调用阿里云api手机号上限1000 - if (mobiles.size()>1000){ - throw new ServiceException("发送短信手机号上限!"); - } - //检验手机号格式 - mobiles.forEach(mobile->{ - if (StrUtil.isAllEmpty(mobile)){ - throw new ServiceException("手机号格式错误!"); - } - }); - sendSmsResponse = sendALiYunSms(mobiles.stream().collect(Collectors.joining(",")), - message, code); - // - if (200 == sendSmsResponse.getStatusCode()) { - // 发送短信成功,返回短信码进行redis存储 - return message; - } - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - /** - * 发送阿里云短信 - * - * @param mobiles 手机号列表 - * @param message json格式的模板参数 - * @param code 阿里云短信模板code - * @return - * @throws Exception - */ - private SendSmsResponse sendALiYunSms(String mobiles, String message, String code) throws Exception { - - //初始化Client对象 - Client client = aliYunSmsConfig.createClient(); - - //构建请求参数 - SendSmsRequest sendSmsRequest = new SendSmsRequest() - .setPhoneNumbers(mobiles) - .setSignName(aliYunSmsConfig.getSignName()) - .setTemplateCode(code) - .setTemplateParam(message); - //发送短信 - return client.sendSms(sendSmsRequest); - - } -} diff --git a/src/main/java/org/springblade/modules/business/sms/SmsConfig.java b/src/main/java/org/springblade/modules/business/sms/SmsConfig.java new file mode 100644 index 0000000..c510aff --- /dev/null +++ b/src/main/java/org/springblade/modules/business/sms/SmsConfig.java @@ -0,0 +1,27 @@ +package org.springblade.modules.business.sms; + + +import com.aliyun.dysmsapi20170525.Client; +import com.aliyun.teaopenapi.models.Config; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SmsConfig { + + @Value("${sms.access-key}") + private String ACCESS_KEY_ID; + + @Value("${sms.secret-key}") + private String ACCESS_KEY_SECRET; + + + @Bean + public Client ClientConfig() throws Exception { + + Config config = new Config().setAccessKeyId(ACCESS_KEY_ID) + .setAccessKeySecret(ACCESS_KEY_SECRET); + return new Client(config); + } +} diff --git a/src/main/java/org/springblade/modules/business/sms/SmsService.java b/src/main/java/org/springblade/modules/business/sms/SmsService.java index 1042c34..f158f22 100644 --- a/src/main/java/org/springblade/modules/business/sms/SmsService.java +++ b/src/main/java/org/springblade/modules/business/sms/SmsService.java @@ -1,14 +1,17 @@ package org.springblade.modules.business.sms; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; +import com.aliyun.dysmsapi20170525.Client; import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.dysmsapi20170525.models.SendSmsResponse; -import com.aliyuncs.CommonRequest; -import com.aliyuncs.CommonResponse; -import com.aliyuncs.DefaultAcsClient; +import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody; +import com.aliyun.teautil.models.RuntimeOptions; + import com.aliyuncs.exceptions.ClientException; -import com.aliyuncs.profile.DefaultProfile; -import com.aliyuncs.profile.IClientProfile; + +import lombok.AllArgsConstructor; + import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -16,51 +19,48 @@ import java.util.HashMap; import java.util.Map; @Component +@AllArgsConstructor public class SmsService { + //private static String ACCESS_KEY_ID = "LTAI5tHsaR8S4nFH3nhchQ3Z"; + //private static String ACCESS_KEY_SECRET = "PWFmEByyD5YrjMlAyioZZqLtMGxZLu"; + //private static String SIGN_NAME = "阿里云短信测试"; + //private static String TEMPLATE_CODE = "SMS_476455177"; +// @Value("${sms.sign-name}") +// private String SIGN_NAME; - // 阿里云的Access Key ID和Access Key Secret - @Value("${sms.access-key}") - private static String ACCESS_KEY_ID; - @Value("${sms.secret-key}") - private static String ACCESS_KEY_SECRET; - // 短信签名和模板ID - // 短信签名,替换成你在阿里云控制台设置的签名 - @Value("${sms.sign-name}") - private static String SIGN_NAME; - @Value("${sms.template-id1}") - private static String TEMPLATE_CODE; // 短信模板ID,替换成你在阿里云控制台设置的模板ID - + private final Client client; - public static void sendSms(String phoneNumber, String name) throws ClientException { - try { - // 设置发送短信的相关参数 - IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET); - DefaultAcsClient client = new DefaultAcsClient(profile); - - CommonRequest request = new CommonRequest(); - // 设置短信模板的参数 - Map templateParam = new HashMap<>(); - templateParam.put("name", name); // 这里的 "code" 是短信模板中的占位符 +// @Value("${sms.template-id1}") +// private String TEMPLATE_CODE; - request.putQueryParameter("PhoneNumbers", phoneNumber); //手机号 - request.putQueryParameter("SignName", ""); //申请的签名名称 - request.putQueryParameter("TemplateCode", ""); //模板名称 - request.putQueryParameter("TemplateParam", JSONObject.toJSONString(templateParam)); //验证码 + public void sendSms(String phoneNumber, String name) throws ClientException { + String templateCode = "SMS_476455177"; + SendSmsRequest request = new SendSmsRequest(); + // 设置短信模板的参数 + Map templateParam = new HashMap<>(); + templateParam.put("name", name); // 这里的 "name" 是短信模板中的占位符 - // 发送短信请求 - CommonResponse response = client.getCommonResponse(request); + request.setPhoneNumbers(phoneNumber); //手机号 +// request.putQueryParameter("SignName", SIGN_NAME); + request.setTemplateCode(templateCode); //模板名称 + request.setTemplateParam(JSONObject.toJSONString(templateParam)); + request.setSignName("实验室运维系统"); - if (response.getHttpResponse().isSuccess()) { - System.out.println("短信发送成功!"); + // 发送短信请求 + try { + SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(request, new RuntimeOptions()); + SendSmsResponseBody body = sendSmsResponse.getBody(); + String code = body.getCode(); + String message = body.getMessage(); + if (StrUtil.equals("OK", code) && StrUtil.equals("OK", message)) { + System.out.println(String.format("向 手机号:%s 用户发送短信成功!", phoneNumber)); } else { - System.out.println("短信发送失败."); + System.out.println(String.format("向 手机号:%s 用户发送短信失败!", phoneNumber)); } - - } catch (ClientException e) { - e.printStackTrace(); - System.out.println("短信发送异常: " + e.getMessage()); + } catch (Exception e) { + throw new RuntimeException(e); } } } \ No newline at end of file diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 2f93a42..3d186f6 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -23,7 +23,7 @@ sms: template-id: SMS_154950909 template-id1: SMS_476455177 # sms审核通过的短信签名 - sign-name: 阿里云短信测试 + sign-name: 实验室运维系统 # sms提供的accesskey access-key: LTAI5tHsaR8S4nFH3nhchQ3Z # sms提供的secretkey diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 03b9b4c..8f47a51 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -38,6 +38,8 @@ spring: exclusions: '*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*' session-stat-enable: true session-stat-max-count: 10 + profiles: + include: dev # mybatis mybatis-plus: @@ -269,4 +271,4 @@ blade: - blade_user #分库分表配置 sharding: - enabled: false + enabled: false \ No newline at end of file