- 添加日志

This commit is contained in:
sin-ning@aliyun.com 2019-05-30 00:20:16 +08:00
parent 47816160d5
commit de44f2d527
6 changed files with 173 additions and 38 deletions

View File

@ -8,10 +8,14 @@ import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType; import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.DefaultProfile;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -25,17 +29,48 @@ import java.util.Map;
@Component @Component
public class SmsAliYunClient implements SmsClient { public class SmsAliYunClient implements SmsClient {
private static final Logger LOGGER = LoggerFactory.getLogger(SmsAliYunClient.class);
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
private static final String SUCCESS_CODE = "OK";
private static final String SUCCESS_MESSAGE = "OK";
/**
* 阿里云短信 - 批量推送最大数 500支持 1000
*/
private static final int MAX_BATCH_SIZE = 500;
@Value("${sms.aliYun.accessKeyId?:'default_value'}") @Value("${sms.aliYun.accessKeyId?:'default_value'}")
private String accessKeyId; private String accessKeyId;
@Value("${sms.aliYun.accessSecret?:'default_value'}") @Value("${sms.aliYun.accessSecret?:'default_value'}")
private String accessSecret; private String accessSecret;
private static final String DOMAIN = "dysmsapi.aliyuncs.com"; @Data
@Accessors(chain = true)
public static class Result {
/**
* 发送回执ID可根据该ID在接口QuerySendDetails中查询具体的发送状态
*/
private String BizId;
/**
* 请求状态码
*
* - OK 蔡成功
*/
private String Code;
/**
* 状态码的描述
*/
private String Message;
/**
* 请求ID
*/
private String RequestId;
}
@Override @Override
public SendResult singleSend(String mobile, String sign, String templateCode, public SendResult singleSend(String mobile, String sign, String templateCode,
String template, Map<String, String> templateParams) { String template, Map<String, String> templateParams) {
IAcsClient client = getClient(); // params
CommonRequest request = new CommonRequest(); CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST); request.setMethod(MethodType.POST);
request.setDomain(DOMAIN); request.setDomain(DOMAIN);
@ -46,20 +81,25 @@ public class SmsAliYunClient implements SmsClient {
request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParams)); request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParams));
try { // 发送请求
CommonResponse response = client.getCommonResponse(request); return doSend(request);
System.out.println(response.getData());
} catch (ClientException e) {
e.printStackTrace();
}
return null;
} }
@Override @Override
public SendResult batchSend(List<String> mobileList, String sign, String templateCode, public SendResult batchSend(List<String> mobileList, String sign, String templateCode,
String template, Map<String, String> templateParams) { String template, Map<String, String> templateParams) {
// 获取 client
IAcsClient client = getClient(); // 最大发送数为 1000我们设置为 500 , 分段发送
int maxSendSize = MAX_BATCH_SIZE;
int maxSendSizeCount = mobileList.size() % maxSendSize == 0
? mobileList.size() / maxSendSize
: mobileList.size() / maxSendSize + 1;
SendResult sendResult = null;
for (int i = 0; i < maxSendSizeCount; i++) {
// 分批发送
List<String> batchSendMobile = mobileList
.subList(i * maxSendSize, (i + 1) * maxSendSize);
// params // params
CommonRequest request = new CommonRequest(); CommonRequest request = new CommonRequest();
@ -67,18 +107,48 @@ public class SmsAliYunClient implements SmsClient {
request.setDomain(DOMAIN); request.setDomain(DOMAIN);
request.setVersion("2017-05-25"); request.setVersion("2017-05-25");
request.setAction("SendBatchSms"); request.setAction("SendBatchSms");
request.putQueryParameter("PhoneNumberJson", JSON.toJSONString(mobileList)); request.putQueryParameter("PhoneNumberJson", JSON.toJSONString(batchSendMobile));
request.putQueryParameter("SignNameJson", JSON.toJSONString(Collections.singletonList(sign))); request.putQueryParameter("SignNameJson", JSON.toJSONString(Collections.singletonList(sign)));
request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParamJson", JSON.toJSONString(Collections.singletonList(templateParams))); request.putQueryParameter("TemplateParamJson", JSON.toJSONString(Collections.singletonList(templateParams)));
// 发送请求
sendResult = doSend(request);
}
return sendResult;
}
private SendResult doSend(CommonRequest request) {
// 获取 client
IAcsClient client = getClient();
try { try {
CommonResponse response = client.getCommonResponse(request); CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData()); Result result = JSON.parseObject(response.getData(), Result.class);
} catch (ClientException e) { if (!SUCCESS_CODE.equals(result.getCode())) {
e.printStackTrace();
LOGGER.info("发送验证码失败 params {} res {}", JSON.toJSON(request), JSON.toJSON(result));
// 错误发送失败
return new SendResult()
.setIsSuccess(false)
.setCode(SendResult.ERROR_CODE)
.setMessage(result.getMessage());
} else {
LOGGER.info("发送验证码失败 params {} res", JSON.toJSON(request), JSON.toJSON(result));
// 发送成功
return new SendResult()
.setIsSuccess(true)
.setCode(SendResult.SUCCESS_CODE)
.setMessage(result.getMessage());
}
} catch (ClientException e) {
LOGGER.error("发送验证码异常 {}", ExceptionUtils.getMessage(e));
return new SendResult()
.setIsSuccess(false)
.setCode(SendResult.ERROR_CODE)
.setMessage(ExceptionUtils.getMessage(e));
} }
return null;
} }
/** /**

View File

@ -14,15 +14,28 @@ import java.util.Map;
*/ */
public interface SmsClient { public interface SmsClient {
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
class SendResult { class SendResult {
private Boolean isSuccess; public static final int SUCCESS_CODE = 0;
public static final int ERROR_CODE = 1;
public static final String SUCCESS_MESSAGE = "SUCCESS";
/**
* 错误码
*/
private Integer code; private Integer code;
/**
* 错误信息
*/
private String message; private String message;
/**
* 是否成功
*/
private Boolean isSuccess;
} }
/** /**

View File

@ -36,6 +36,7 @@ public class SmsYunPianClient implements SmsClient {
protected static final Logger LOGGER = LoggerFactory.getLogger(SmsYunPianClient.class); protected static final Logger LOGGER = LoggerFactory.getLogger(SmsYunPianClient.class);
private static final int SUCCESS_CODE = 0; private static final int SUCCESS_CODE = 0;
private static final String SUCCESS_MESSAGE = "SUCCESS";
/** /**
* 云片短信 - 批量推送最大数 500支持 1000 * 云片短信 - 批量推送最大数 500支持 1000
@ -121,13 +122,17 @@ public class SmsYunPianClient implements SmsClient {
} }
@Override @Override
public SendResult batchSend(List<String> mobileList, String sign, String templateCode, String template, Map<String, String> templateParams) { public SendResult batchSend(List<String> mobileList, String sign,
String templateCode, String template,
Map<String, String> templateParams) {
// build 模板 // build 模板
template = buildTemplate(sign, template, templateParams); template = buildTemplate(sign, template, templateParams);
// 最大发送数为 1000我们设置为 500 , 分段发送 // 最大发送数为 1000我们设置为 500 , 分段发送
int maxSendSize = MAX_BATCH_SIZE; int maxSendSize = MAX_BATCH_SIZE;
int maxSendSizeCount = mobileList.size() % maxSendSize; int maxSendSizeCount = mobileList.size() % maxSendSize == 0
? mobileList.size() / maxSendSize
: mobileList.size() / maxSendSize + 1;
int j = 0; int j = 0;
int j2 = mobileList.size(); int j2 = mobileList.size();
@ -163,7 +168,7 @@ public class SmsYunPianClient implements SmsClient {
return new SendResult() return new SendResult()
.setIsSuccess(true) .setIsSuccess(true)
.setCode(SUCCESS_CODE) .setCode(SUCCESS_CODE)
.setMessage(null); .setMessage(SUCCESS_MESSAGE);
} }
/** /**
@ -181,8 +186,6 @@ public class SmsYunPianClient implements SmsClient {
return template; return template;
} }
LOGGER.debug("模板构建 before -> {}", template);
for (Map.Entry<String, String> entry : templateParams.entrySet()) { for (Map.Entry<String, String> entry : templateParams.entrySet()) {
String paramsKey = entry.getKey(); String paramsKey = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();
@ -191,7 +194,6 @@ public class SmsYunPianClient implements SmsClient {
} }
template = String.format(SIGN_TEMPLATE, sign, template); template = String.format(SIGN_TEMPLATE, sign, template);
LOGGER.debug("模板构建 after -> {}", template);
return template; return template;
} }
@ -204,8 +206,6 @@ public class SmsYunPianClient implements SmsClient {
*/ */
public static String post(String url, Map<String, String> paramsMap) { public static String post(String url, Map<String, String> paramsMap) {
// TODO: 2019/5/25 Sin 这个地方需要 记录日志
CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpClient client = HttpClients.createDefault();
String responseText = ""; String responseText = "";
CloseableHttpResponse response = null; CloseableHttpResponse response = null;

View File

@ -0,0 +1,15 @@
package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.SmsSendLogDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
/**
* 短信
*
* @author Sin
* @time 2019/5/16 6:18 PM
*/
@Repository
public interface SmsSendMapper extends BaseMapper<SmsSendLogDO> {
}

View File

@ -12,7 +12,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class SmsClientLog extends BaseDO { public class SmsSendLogDO extends BaseDO {
/** /**
* 编号 * 编号
@ -26,6 +26,10 @@ public class SmsClientLog extends BaseDO {
* 短信 * 短信
*/ */
private String template; private String template;
/**
* 参数
*/
private String params;
/** /**
* 发送信息 * 发送信息
*/ */

View File

@ -12,11 +12,14 @@ import cn.iocoder.mall.admin.api.constant.SmsApplyStatusEnum;
import cn.iocoder.mall.admin.api.constant.SmsPlatformEnum; import cn.iocoder.mall.admin.api.constant.SmsPlatformEnum;
import cn.iocoder.mall.admin.api.dto.sms.PageQuerySmsSignDTO; import cn.iocoder.mall.admin.api.dto.sms.PageQuerySmsSignDTO;
import cn.iocoder.mall.admin.api.dto.sms.PageQuerySmsTemplateDTO; import cn.iocoder.mall.admin.api.dto.sms.PageQuerySmsTemplateDTO;
import cn.iocoder.mall.admin.client.SmsAliYunClient;
import cn.iocoder.mall.admin.client.SmsClient; import cn.iocoder.mall.admin.client.SmsClient;
import cn.iocoder.mall.admin.convert.SmsSignConvert; import cn.iocoder.mall.admin.convert.SmsSignConvert;
import cn.iocoder.mall.admin.convert.SmsTemplateConvert; import cn.iocoder.mall.admin.convert.SmsTemplateConvert;
import cn.iocoder.mall.admin.dao.SmsSendMapper;
import cn.iocoder.mall.admin.dao.SmsSignMapper; import cn.iocoder.mall.admin.dao.SmsSignMapper;
import cn.iocoder.mall.admin.dao.SmsTemplateMapper; import cn.iocoder.mall.admin.dao.SmsTemplateMapper;
import cn.iocoder.mall.admin.dataobject.SmsSendLogDO;
import cn.iocoder.mall.admin.dataobject.SmsSignDO; import cn.iocoder.mall.admin.dataobject.SmsSignDO;
import cn.iocoder.mall.admin.dataobject.SmsTemplateDO; import cn.iocoder.mall.admin.dataobject.SmsTemplateDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -47,6 +50,8 @@ public class SmsServiceImpl implements SmsService {
private SmsSignMapper smsSignMapper; private SmsSignMapper smsSignMapper;
@Autowired @Autowired
private SmsTemplateMapper smsTemplateMapper; private SmsTemplateMapper smsTemplateMapper;
@Autowired
private SmsSendMapper smsSendMapper;
@Autowired @Autowired
@Qualifier("smsYunPianClient") @Qualifier("smsYunPianClient")
@ -349,8 +354,17 @@ public class SmsServiceImpl implements SmsService {
// 获取 client // 获取 client
SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform()); SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform());
// 发送短信 // 发送短信
smsClient.singleSend(mobile, smsSignDO.getSign(), SmsClient.SendResult sendResult = smsClient.singleSend(mobile, smsSignDO.getSign(),
smsTemplateDO.getTemplateCode(), smsTemplateDO.getTemplate(), params); smsTemplateDO.getTemplateCode(), smsTemplateDO.getTemplate(), params);
// 添加日志
smsSendMapper.insert(
(SmsSendLogDO) new SmsSendLogDO()
.setTemplateId(smsTemplateDO.getId())
.setTemplate(smsTemplateDO.getTemplate())
.setMessage(sendResult.getMessage())
.setCreateTime(new Date())
);
} }
@Override @Override
@ -368,15 +382,34 @@ public class SmsServiceImpl implements SmsService {
new QueryWrapper<SmsSignDO>().eq("id", smsTemplateDO.getSmsSignId())); new QueryWrapper<SmsSignDO>().eq("id", smsTemplateDO.getSmsSignId()));
if (smsSignDO == null) { if (smsSignDO == null) {
// 添加日志
smsSendMapper.insert(
(SmsSendLogDO) new SmsSendLogDO()
.setTemplateId(smsTemplateDO.getId())
.setTemplate(smsTemplateDO.getTemplate())
.setMessage("发送成功!")
.setCreateTime(new Date())
);
throw new ServiceException(AdminErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(), throw new ServiceException(AdminErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
AdminErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage()); AdminErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
} }
// 获取 client // 获取 client
SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform()); SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform());
// 发送短信 // 发送短信
smsClient.batchSend(mobileList, smsSignDO.getSign(), SmsClient.SendResult sendResult = smsClient.batchSend(mobileList, smsSignDO.getSign(),
smsTemplateDO.getTemplateCode(), smsTemplateDO.getTemplate(), params); smsTemplateDO.getTemplateCode(), smsTemplateDO.getTemplate(), params);
// 添加日志
smsSendMapper.insert(
(SmsSendLogDO) new SmsSendLogDO()
.setTemplateId(smsTemplateDO.getId())
.setTemplate(smsTemplateDO.getTemplate())
.setMessage(sendResult.getMessage())
.setCreateTime(new Date())
);
} }
/** /**