- 添加 aliyun 短信发送服务

pull/1/head
sin-ning@aliyun.com 2019-05-26 11:31:42 +08:00
parent 74724637b7
commit 37da2b0bc9
10 changed files with 113 additions and 86 deletions

View File

@ -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);
/**
* -
*

View File

@ -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<String, String> params) {
public SendResult singleSend(String mobile, String sign, String templateCode,
String template, Map<String, String> 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<String> mobileList, String sign, String template, Map<String, String> params) {
public SendResult batchSend(List<String> mobileList, String sign, String templateCode,
String template, Map<String, String> 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();
}

View File

@ -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<String, String> params);
SendResult singleSend(String mobile, String sign, String templateCode,
String template, Map<String, String> templateParams);
/**
* -
*
* @return
* @param mobileList
* @param sign
* @param templateCode code
* @param template
* @param templateParams params
* @return
*/
SendResult batchSend(List<String> mobileList, String sign, String template, Map<String, String> params);
SendResult batchSend(List<String> mobileList, String sign, String templateCode,
String template, Map<String, String> templateParams);
}

View File

@ -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<String, String> templateParams) {
public SendResult singleSend(String mobile, String sign, String templateCode, String template, Map<String, String> templateParams) {
// build 模板
template = buildTemplate(sign, template, templateParams);
@ -123,8 +121,7 @@ public class SmsYunPianClient implements SmsClient {
}
@Override
public SendResult batchSend(List<String> mobileList, String sign, String template, Map<String, String> templateParams) {
public SendResult batchSend(List<String> mobileList, String sign, String templateCode, String template, Map<String, String> 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<String, String> params) {
if (CollectionUtils.isEmpty(params)) {
private static String buildTemplate(String sign, String template,
Map<String, String> templateParams) {
if (CollectionUtils.isEmpty(templateParams)) {
return template;
}
LOGGER.debug("模板构建 before -> {}", template);
for (Map.Entry<String, String> entry : params.entrySet()) {
for (Map.Entry<String, String> 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.3POST
*

View File

@ -20,11 +20,14 @@ public class SmsTemplateDO extends DeletableDO {
*
*/
private Integer id;
// TODO FROM 芋艿 TO 小范,短信类型,分成,验证码类、通知类、营销类。
/**
* ()
*/
private Integer smsSignId;
/**
* code code
*/
private String templateCode;
/**
* id
*/

View File

@ -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<SmsSignDO>().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);
}
/**

View File

@ -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:

View File

@ -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"));
}
}

View File

@ -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"));
}
}

View File

@ -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"));
}
}