From 37da2b0bc9420ed27d7022ebb46e10a93c7be2a6 Mon Sep 17 00:00:00 2001 From: "sin-ning@aliyun.com" Date: Sun, 26 May 2019 11:31:42 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=B7=BB=E5=8A=A0=20aliyun=20=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E5=8F=91=E9=80=81=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/iocoder/mall/admin/api/SmsService.java | 4 +- .../mall/admin/client/SmsAliYunClient.java | 25 ++++++++---- .../iocoder/mall/admin/client/SmsClient.java | 20 ++++++++-- .../mall/admin/client/SmsYunPianClient.java | 39 ++++--------------- .../mall/admin/dataobject/SmsTemplateDO.java | 5 ++- .../mall/admin/service/SmsServiceImpl.java | 29 +++++++------- .../main/resources/config/application.yaml | 8 ++-- .../admin/client/SmsAliYunClientTest.java | 33 ++++++++++++++++ .../admin/client/SmsYunPianClientTest.java | 16 ++------ .../admin/service/SmsServiceImplTest.java | 20 ++++++---- 10 files changed, 113 insertions(+), 86 deletions(-) create mode 100644 system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsAliYunClientTest.java diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java index 73561b6a0..633527355 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java @@ -68,10 +68,11 @@ public interface SmsService { * 模板 - 创建 * * @param smsSignId 选用的哪个签名 + * @param templateCode 模板code * @param template 模板内容 * @param platform 平台 */ - void createTemplate(Integer smsSignId, String template, Integer platform, Integer smsType); + void createTemplate(Integer smsSignId, String templateCode, String template, Integer platform, Integer smsType); /** * 模板 - 获取 @@ -97,7 +98,6 @@ public interface SmsService { */ void deleteTemplate(Integer id); - /** * 短信发送 - 单个 * diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsAliYunClient.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsAliYunClient.java index e9e6bc77e..54e879c6b 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsAliYunClient.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsAliYunClient.java @@ -1,16 +1,18 @@ package cn.iocoder.mall.admin.client; +import com.alibaba.fastjson.JSON; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; -import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -23,21 +25,26 @@ import java.util.Map; @Component public class SmsAliYunClient implements SmsClient { - @Value("sms.aliYun.accessKeyId") + @Value("${sms.aliYun.accessKeyId}") private String accessKeyId; - @Value("sms.aliYun.accessSecret") + @Value("${sms.aliYun.accessSecret}") private String accessSecret; private static final String DOMAIN = "dysmsapi.aliyuncs.com"; @Override - public SendResult singleSend(String mobile, String sign, String template, Map params) { + public SendResult singleSend(String mobile, String sign, String templateCode, + String template, Map templateParams) { IAcsClient client = getClient(); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain(DOMAIN); request.setVersion("2017-05-25"); request.setAction("SendSms"); + request.putQueryParameter("PhoneNumbers", mobile); + request.putQueryParameter("SignName", sign); + request.putQueryParameter("TemplateCode", templateCode); + request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParams)); try { CommonResponse response = client.getCommonResponse(request); @@ -49,8 +56,8 @@ public class SmsAliYunClient implements SmsClient { } @Override - public SendResult batchSend(List mobileList, String sign, String template, Map params) { - + public SendResult batchSend(List mobileList, String sign, String templateCode, + String template, Map templateParams) { // 获取 client IAcsClient client = getClient(); @@ -60,12 +67,14 @@ public class SmsAliYunClient implements SmsClient { request.setDomain(DOMAIN); request.setVersion("2017-05-25"); request.setAction("SendBatchSms"); + request.putQueryParameter("PhoneNumberJson", JSON.toJSONString(mobileList)); + request.putQueryParameter("SignNameJson", JSON.toJSONString(Collections.singletonList(sign))); + request.putQueryParameter("TemplateCode", templateCode); + request.putQueryParameter("TemplateParamJson", JSON.toJSONString(Collections.singletonList(templateParams))); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); - } catch (ServerException e) { - e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsClient.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsClient.java index ddecc399e..0d888c59a 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsClient.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsClient.java @@ -28,14 +28,26 @@ public interface SmsClient { /** * 短信发送 - 单个 * - * @return + * @param mobile 手机号 + * @param sign 签名 + * @param templateCode 短信模板code + * @param template 短信模板 + * @param templateParams 短信模板 params + * @return 发送后信息 */ - SendResult singleSend(String mobile, String sign, String template, Map params); + SendResult singleSend(String mobile, String sign, String templateCode, + String template, Map templateParams); /** * 短信发送 - 批量 * - * @return + * @param mobileList 手机号 + * @param sign 签名 + * @param templateCode 短信模板 code + * @param template 短信模板 + * @param templateParams 短信模板params + * @return 发送后信息 */ - SendResult batchSend(List mobileList, String sign, String template, Map params); + SendResult batchSend(List mobileList, String sign, String templateCode, + String template, Map templateParams); } diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsYunPianClient.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsYunPianClient.java index 3e10746cc..98f857c90 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsYunPianClient.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/client/SmsYunPianClient.java @@ -2,7 +2,6 @@ package cn.iocoder.mall.admin.client; import cn.iocoder.common.framework.exception.ServiceException; import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum; -import cn.iocoder.mall.admin.api.constant.SmsApplyStatusEnum; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; @@ -96,8 +95,7 @@ public class SmsYunPianClient implements SmsClient { private String apiKey; @Override - public SendResult singleSend(String mobile, String sign, String template, Map templateParams) { - + public SendResult singleSend(String mobile, String sign, String templateCode, String template, Map templateParams) { // build 模板 template = buildTemplate(sign, template, templateParams); @@ -123,8 +121,7 @@ public class SmsYunPianClient implements SmsClient { } @Override - public SendResult batchSend(List mobileList, String sign, String template, Map templateParams) { - + public SendResult batchSend(List mobileList, String sign, String templateCode, String template, Map templateParams) { // build 模板 template = buildTemplate(sign, template, templateParams); @@ -174,17 +171,19 @@ public class SmsYunPianClient implements SmsClient { * * @param sign * @param template - * @param params + * @param templateParams * @return */ - private static String buildTemplate(String sign, String template, Map params) { - if (CollectionUtils.isEmpty(params)) { + private static String buildTemplate(String sign, String template, + Map templateParams) { + + if (CollectionUtils.isEmpty(templateParams)) { return template; } LOGGER.debug("模板构建 before -> {}", template); - for (Map.Entry entry : params.entrySet()) { + for (Map.Entry entry : templateParams.entrySet()) { String paramsKey = entry.getKey(); String value = entry.getValue(); String paramPlace = String.format(PARAM_TEMPLATE, paramsKey); @@ -196,28 +195,6 @@ public class SmsYunPianClient implements SmsClient { return template; } - /** - * 短信 status 和 云片状态 映射关系 - * - * @param checkStatus - * @return - */ - private static Integer smsStatusMapping(String checkStatus) { - Integer applyStatus; - switch (checkStatus) { - case "SUCCESS": - applyStatus = SmsApplyStatusEnum.SUCCESS.getValue(); - break; - case "FAIL": - applyStatus = SmsApplyStatusEnum.FAIL.getValue(); - break; - default: - applyStatus = SmsApplyStatusEnum.CHECKING.getValue(); - break; - } - return applyStatus; - } - /** * 基于HttpClient 4.3的通用POST方法 * diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java index 1975e7338..9adf0d981 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java @@ -20,11 +20,14 @@ public class SmsTemplateDO extends DeletableDO { * 编号 */ private Integer id; - // TODO FROM 芋艿 TO 小范,短信类型,分成,验证码类、通知类、营销类。 /** * 模板编号 (第三方的) */ private Integer smsSignId; + /** + * 模板 code(第三方平台 code) + */ + private String templateCode; /** * 短信签名 id */ diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java index 24e4ab926..3ea112f78 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java @@ -2,18 +2,17 @@ package cn.iocoder.mall.admin.service; import cn.iocoder.common.framework.constant.DeletedStatusEnum; import cn.iocoder.common.framework.exception.ServiceException; -import cn.iocoder.mall.admin.api.DataDictService; +import cn.iocoder.mall.admin.api.SmsService; +import cn.iocoder.mall.admin.api.bo.sms.PageSmsSignBO; +import cn.iocoder.mall.admin.api.bo.sms.PageSmsTemplateBO; +import cn.iocoder.mall.admin.api.bo.sms.SmsSignBO; +import cn.iocoder.mall.admin.api.bo.sms.SmsTemplateBO; +import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum; import cn.iocoder.mall.admin.api.constant.SmsApplyStatusEnum; import cn.iocoder.mall.admin.api.constant.SmsPlatformEnum; -import cn.iocoder.mall.admin.client.SmsClient; -import cn.iocoder.mall.admin.api.SmsService; -import cn.iocoder.mall.admin.api.bo.sms.SmsSignBO; -import cn.iocoder.mall.admin.api.bo.sms.PageSmsSignBO; -import cn.iocoder.mall.admin.api.bo.sms.SmsTemplateBO; -import cn.iocoder.mall.admin.api.bo.sms.PageSmsTemplateBO; -import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum; import cn.iocoder.mall.admin.api.dto.sms.PageQuerySmsSignDTO; import cn.iocoder.mall.admin.api.dto.sms.PageQuerySmsTemplateDTO; +import cn.iocoder.mall.admin.client.SmsClient; import cn.iocoder.mall.admin.convert.SmsSignConvert; import cn.iocoder.mall.admin.convert.SmsTemplateConvert; import cn.iocoder.mall.admin.dao.SmsSignMapper; @@ -44,8 +43,6 @@ import java.util.Map; @org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.SmsService.version}") public class SmsServiceImpl implements SmsService { - private static final String SMS_TEMPLATE = "【%s】%s"; - @Autowired private SmsSignMapper smsSignMapper; @Autowired @@ -57,8 +54,6 @@ public class SmsServiceImpl implements SmsService { @Autowired @Qualifier("smsAliYunClient") private SmsClient smsAliYunClient; - @Autowired - private DataDictService dataDictService; @Override public PageSmsSignBO pageSmsSign(PageQuerySmsSignDTO queryDTO) { @@ -201,7 +196,8 @@ public class SmsServiceImpl implements SmsService { @Override @Transactional - public void createTemplate(Integer smsSignId, String template, Integer platform, Integer smsType) { + public void createTemplate(Integer smsSignId, String templateCode, + String template, Integer platform, Integer smsType) { SmsSignDO smsSignDO = smsSignMapper.selectOne( new QueryWrapper().eq("id", smsSignId)); @@ -216,6 +212,7 @@ public class SmsServiceImpl implements SmsService { (SmsTemplateDO) new SmsTemplateDO() .setId(null) .setSmsSignId(smsSignId) + .setTemplateCode(templateCode) .setTemplate(template) .setPlatform(platform) .setSmsType(smsType) @@ -314,7 +311,8 @@ public class SmsServiceImpl implements SmsService { // 获取 client SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform()); // 发送短信 - smsClient.singleSend(mobile, smsSignDO.getSign(), smsTemplateDO.getTemplate(), params); + smsClient.singleSend(mobile, smsSignDO.getSign(), + smsTemplateDO.getTemplateCode(), smsTemplateDO.getTemplate(), params); } @Override @@ -339,7 +337,8 @@ public class SmsServiceImpl implements SmsService { // 获取 client SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform()); // 发送短信 - smsClient.batchSend(mobileList, smsSignDO.getSign(), smsTemplateDO.getTemplate(), params); + smsClient.batchSend(mobileList, smsSignDO.getSign(), + smsTemplateDO.getTemplateCode(), smsTemplateDO.getTemplate(), params); } /** diff --git a/system/system-service-impl/src/main/resources/config/application.yaml b/system/system-service-impl/src/main/resources/config/application.yaml index 6f12e70b0..2d0313463 100644 --- a/system/system-service-impl/src/main/resources/config/application.yaml +++ b/system/system-service-impl/src/main/resources/config/application.yaml @@ -27,12 +27,10 @@ mybatis-plus: # sms sms: yunPian: - apiKey: d4705399e71e822fe3a90f801ed95bd9 + apiKey: ${YUN_PIAN} aliYun: - accessKeyId: d4705399e71e822fe3a90f801ed95bd9 - accessSecret: d4705399e71e822fe3a90f801ed95bd9 - - + accessKeyId: ${ALI_YUN_KEY_ID} + accessSecret: ${ALI_YUN_SECRET} # dubbo dubbo: diff --git a/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsAliYunClientTest.java b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsAliYunClientTest.java new file mode 100644 index 000000000..66e9badd2 --- /dev/null +++ b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsAliYunClientTest.java @@ -0,0 +1,33 @@ +package cn.iocoder.mall.admin.client; + +import cn.iocoder.mall.admin.SystemApplicationTest; +import com.google.common.collect.ImmutableMap; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * 阿里云 短信 test + * + * @author Sin + * @time 2019/5/26 10:08 AM + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SystemApplicationTest.class) +public class SmsAliYunClientTest { + + @Autowired + private SmsAliYunClient smsAliYunClient; + + @Test + public void singleSendTest() { + String sign = "阿里云短信测试专用"; + String mobile = "13302926050"; + String templateCode = "SMS_137110043"; + String template = "验证码#code#,您正在进行身份验证,打死不要告诉别人哦!"; + smsAliYunClient.singleSend(mobile, sign, templateCode, + template, ImmutableMap.of("code", "8888")); + } +} diff --git a/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsYunPianClientTest.java b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsYunPianClientTest.java index 846f0df77..36b604586 100644 --- a/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsYunPianClientTest.java +++ b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/client/SmsYunPianClientTest.java @@ -1,14 +1,6 @@ package cn.iocoder.mall.admin.client; import cn.iocoder.mall.admin.SystemApplicationTest; -import com.aliyuncs.CommonRequest; -import com.aliyuncs.CommonResponse; -import com.aliyuncs.DefaultAcsClient; -import com.aliyuncs.IAcsClient; -import com.aliyuncs.exceptions.ClientException; -import com.aliyuncs.exceptions.ServerException; -import com.aliyuncs.http.MethodType; -import com.aliyuncs.profile.DefaultProfile; import com.google.common.collect.ImmutableMap; import org.assertj.core.util.Lists; import org.junit.Before; @@ -18,8 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import java.util.Map; - /** * 短信 sms client test * @@ -44,13 +34,15 @@ public class SmsYunPianClientTest { public void sendMobileTest() { String mobile = "13302926050"; String template = "您的验证码是#code#,打死也不告诉别人哦。"; - smsYunPianClient.singleSend(mobile, sign, template, ImmutableMap.of("code", "1111")); + smsYunPianClient.singleSend(mobile, sign, null, + template, ImmutableMap.of("code", "1111")); } @Test public void batchSendTest() { String mobile = "13302926050"; String template = "您的验证码是#code#,打死也不告诉别人哦。"; - smsYunPianClient.batchSend(Lists.newArrayList(mobile), sign, template, ImmutableMap.of("code", "2222")); + smsYunPianClient.batchSend(Lists.newArrayList(mobile), sign, null, + template, ImmutableMap.of("code", "2222")); } } diff --git a/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java index 29f94c775..9e107fe6c 100644 --- a/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java +++ b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java @@ -31,8 +31,8 @@ public class SmsServiceImplTest { @Test public void createSignTest() { - smsService.createSign("悦跑运动", SmsPlatformEnum.YunPian.getValue()); - +// smsService.createSign("悦跑运动", SmsPlatformEnum.YunPian.getValue()); + smsService.createSign("登录确认验证码", SmsPlatformEnum.AliYun.getValue()); } @Test @@ -50,7 +50,6 @@ public class SmsServiceImplTest { Assert.assertTrue("更新不成功!", smsSignBO.getSign().equals(newSign)); } - @Test public void deletedSignTest() { smsService.deleteSign(3); @@ -61,23 +60,28 @@ public class SmsServiceImplTest { @Test public void createTemplateTest() { - String template = "您的验证码是#code#,打死也不告诉别人哦。"; - smsService.createTemplate(3, template, - SmsPlatformEnum.YunPian.getValue(), + Integer sign = 4; + String templateCode = "SMS_137110043"; + String template = "验证码#code#,您正在登录,若非本人操作,请勿泄露。"; + smsService.createTemplate( + sign, + templateCode, + template, + SmsPlatformEnum.AliYun.getValue(), SmsTypeEnum.VERIFICATION_CODE.getValue()); } @Test public void singleSendTest() { String mobile = "13302926050"; - Integer templateId = 5; + Integer templateId = 7; smsService.singleSend(mobile, templateId, ImmutableMap.of("code", "8888")); } @Test public void batchSendTest() { String mobile = "13302926050"; - Integer templateId = 5; + Integer templateId = 7; smsService.batchSend(Lists.newArrayList(mobile), templateId, ImmutableMap.of("code", "8888")); } }