From 48cb0a941539d2e994cd5f96d34571253c847b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6=E6=B6=9B?= Date: Fri, 3 Mar 2023 16:44:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=20sonar=E3=80=91DySmsHelper?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/common/util/DySmsHelper.java | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DySmsHelper.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DySmsHelper.java index 69e4c15e..7ed0ca51 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DySmsHelper.java +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DySmsHelper.java @@ -1,9 +1,5 @@ package com.jero.common.util; -import com.jero.config.StaticConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.fastjson.JSONObject; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; @@ -12,6 +8,10 @@ import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; +import com.jero.common.exception.JeroBootException; +import com.jero.config.StaticConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created on 17/6/7. @@ -25,13 +25,17 @@ import com.aliyuncs.profile.IClientProfile; * 国际短信发送请勿参照此DEMO */ public class DySmsHelper { - + + private DySmsHelper(){ + + } + private final static Logger logger=LoggerFactory.getLogger(DySmsHelper.class); //产品名称:云通信短信API产品,开发者无需替换 - static final String product = "Dysmsapi"; + static final String PRODUCT = "Dysmsapi"; //产品域名,开发者无需替换 - static final String domain = "dysmsapi.aliyuncs.com"; + static final String DOMAIN = "dysmsapi.aliyuncs.com"; // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找) static String accessKeyId; @@ -52,8 +56,8 @@ public class DySmsHelper { public static String getAccessKeySecret() { return accessKeySecret; } - - + + public static boolean sendSms(String phone,JSONObject templateParamJson,DySmsEnum dySmsEnum) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); @@ -64,15 +68,16 @@ public class DySmsHelper { setAccessKeyId(staticConfig.getAccessKeyId()); setAccessKeySecret(staticConfig.getAccessKeySecret()); //update-end-author:taoyan date:20200811 for:配置类数据获取 - + + String hangzhou = "cn-hangzhou"; //初始化acsClient,暂不支持region化 - IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); - DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); + IClientProfile profile = DefaultProfile.getProfile(hangzhou, accessKeyId, accessKeySecret); + DefaultProfile.addEndpoint(hangzhou, hangzhou, PRODUCT, DOMAIN); IAcsClient acsClient = new DefaultAcsClient(profile); - + //验证json参数 validateParam(templateParamJson,dySmsEnum); - + //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 @@ -83,7 +88,7 @@ public class DySmsHelper { request.setTemplateCode(dySmsEnum.getTemplateCode()); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam(templateParamJson.toJSONString()); - + //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); @@ -95,28 +100,22 @@ public class DySmsHelper { //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); logger.info("短信接口返回的数据----------------"); - logger.info("{Code:" + sendSmsResponse.getCode()+",Message:" + sendSmsResponse.getMessage()+",RequestId:"+ sendSmsResponse.getRequestId()+",BizId:"+sendSmsResponse.getBizId()+"}"); + String str = "{Code:" + sendSmsResponse.getCode()+",Message:" + sendSmsResponse.getMessage()+",RequestId:"+ sendSmsResponse.getRequestId()+",BizId:"+sendSmsResponse.getBizId()+"}"; + logger.info(str); if ("OK".equals(sendSmsResponse.getCode())) { result = true; } return result; - + } - + private static void validateParam(JSONObject templateParamJson,DySmsEnum dySmsEnum) { String keys = dySmsEnum.getKeys(); String [] keyArr = keys.split(","); for(String item :keyArr) { if(!templateParamJson.containsKey(item)) { - throw new RuntimeException("模板缺少参数:"+item); + throw new JeroBootException("模板缺少参数:"+item); } } } - - -// public static void main(String[] args) throws ClientException, InterruptedException { -// JSONObject obj = new JSONObject(); -// obj.put("code", "1234"); -// sendSms("13800138000", obj, DySmsEnum.FORGET_PASSWORD_TEMPLATE_CODE); -// } }