- 优化:增加注释,优化格式
This commit is contained in:
parent
20e65bf2f7
commit
0c198e8f08
|
@ -80,7 +80,6 @@ public class AliYunSmsClient implements SmsClient {
|
||||||
request.putQueryParameter("SignName", sign);
|
request.putQueryParameter("SignName", sign);
|
||||||
request.putQueryParameter("TemplateCode", templateCode);
|
request.putQueryParameter("TemplateCode", templateCode);
|
||||||
request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParams));
|
request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParams));
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
return doSend(request);
|
return doSend(request);
|
||||||
}
|
}
|
||||||
|
@ -88,19 +87,17 @@ public class AliYunSmsClient implements SmsClient {
|
||||||
@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) {
|
||||||
|
|
||||||
// 最大发送数为 1000,我们设置为 500 个, 分段发送
|
// 最大发送数为 1000,我们设置为 500 个, 分段发送
|
||||||
int maxSendSize = MAX_BATCH_SIZE;
|
int maxSendSize = MAX_BATCH_SIZE;
|
||||||
int maxSendSizeCount = mobileList.size() % maxSendSize == 0
|
int maxSendSizeCount = mobileList.size() % maxSendSize == 0
|
||||||
? mobileList.size() / maxSendSize
|
? mobileList.size() / maxSendSize
|
||||||
: mobileList.size() / maxSendSize + 1;
|
: mobileList.size() / maxSendSize + 1;
|
||||||
|
// 处理批量
|
||||||
SendResult sendResult = null;
|
SendResult sendResult = null;
|
||||||
for (int i = 0; i < maxSendSizeCount; i++) {
|
for (int i = 0; i < maxSendSizeCount; i++) {
|
||||||
// 分批发送
|
// 分批发送
|
||||||
List<String> batchSendMobile = mobileList
|
List<String> batchSendMobile = mobileList
|
||||||
.subList(i * maxSendSize, (i + 1) * maxSendSize);
|
.subList(i * maxSendSize, (i + 1) * maxSendSize);
|
||||||
|
|
||||||
// params
|
// params
|
||||||
CommonRequest request = new CommonRequest();
|
CommonRequest request = new CommonRequest();
|
||||||
request.setMethod(MethodType.POST);
|
request.setMethod(MethodType.POST);
|
||||||
|
@ -111,7 +108,6 @@ public class AliYunSmsClient implements SmsClient {
|
||||||
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);
|
sendResult = doSend(request);
|
||||||
}
|
}
|
||||||
|
@ -125,9 +121,7 @@ public class AliYunSmsClient implements SmsClient {
|
||||||
CommonResponse response = client.getCommonResponse(request);
|
CommonResponse response = client.getCommonResponse(request);
|
||||||
Result result = JSON.parseObject(response.getData(), Result.class);
|
Result result = JSON.parseObject(response.getData(), Result.class);
|
||||||
if (!SUCCESS_CODE.equals(result.getCode())) {
|
if (!SUCCESS_CODE.equals(result.getCode())) {
|
||||||
|
|
||||||
LOGGER.info("发送验证码失败 params {} res {}", JSON.toJSON(request), JSON.toJSON(result));
|
LOGGER.info("发送验证码失败 params {} res {}", JSON.toJSON(request), JSON.toJSON(result));
|
||||||
|
|
||||||
// 错误发送失败
|
// 错误发送失败
|
||||||
return new SendResult()
|
return new SendResult()
|
||||||
.setIsSuccess(false)
|
.setIsSuccess(false)
|
||||||
|
@ -135,7 +129,6 @@ public class AliYunSmsClient implements SmsClient {
|
||||||
.setMessage(result.getMessage());
|
.setMessage(result.getMessage());
|
||||||
} else {
|
} else {
|
||||||
LOGGER.info("发送验证码失败 params {} res", JSON.toJSON(request), JSON.toJSON(result));
|
LOGGER.info("发送验证码失败 params {} res", JSON.toJSON(request), JSON.toJSON(result));
|
||||||
|
|
||||||
// 发送成功
|
// 发送成功
|
||||||
return new SendResult()
|
return new SendResult()
|
||||||
.setIsSuccess(true)
|
.setIsSuccess(true)
|
||||||
|
|
|
@ -23,6 +23,7 @@ import cn.iocoder.mall.system.biz.dto.smsTemplate.ListSmsTemplateDTO;
|
||||||
import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum;
|
import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum;
|
||||||
import cn.iocoder.mall.system.biz.enums.sms.SmsApplyStatusEnum;
|
import cn.iocoder.mall.system.biz.enums.sms.SmsApplyStatusEnum;
|
||||||
import cn.iocoder.mall.system.biz.enums.sms.SmsPlatformEnum;
|
import cn.iocoder.mall.system.biz.enums.sms.SmsPlatformEnum;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
@ -70,16 +71,14 @@ public class SmsServiceImpl implements SmsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<ListSmsTemplateBO> listSmsTemplate(ListSmsTemplateDTO listSmsTemplateDTO) {
|
public PageResult<ListSmsTemplateBO> listSmsTemplate(ListSmsTemplateDTO listSmsTemplateDTO) {
|
||||||
|
// 获取 template
|
||||||
IPage<SmsTemplateDO> signPage = smsTemplateMapper.listSmsTemplate(listSmsTemplateDTO);
|
IPage<SmsTemplateDO> signPage = smsTemplateMapper.listSmsTemplate(listSmsTemplateDTO);
|
||||||
|
if (CollectionUtils.isEmpty(signPage.getRecords())) {
|
||||||
List<ListSmsTemplateBO> templateList
|
|
||||||
= SmsTemplateConvert.INSTANCE.convert(signPage.getRecords());
|
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(templateList)) {
|
|
||||||
// TODO DOME FROM 芋艿 to 小范,Collections.EMPTY_LIST =》Collections.emptyList();另外,可以考虑直接 Convert 哈
|
// TODO DOME FROM 芋艿 to 小范,Collections.EMPTY_LIST =》Collections.emptyList();另外,可以考虑直接 Convert 哈
|
||||||
return new PageResult<ListSmsTemplateBO>().setList(Collections.emptyList()).setTotal(signPage.getTotal());
|
return new PageResult<ListSmsTemplateBO>().setList(Collections.emptyList()).setTotal(signPage.getTotal());
|
||||||
}
|
}
|
||||||
|
// 转换bo
|
||||||
|
List<ListSmsTemplateBO> templateList = SmsTemplateConvert.INSTANCE.convert(signPage.getRecords());
|
||||||
// 获取 sign
|
// 获取 sign
|
||||||
Set<Integer> smsSignIds = templateList.stream().map(
|
Set<Integer> smsSignIds = templateList.stream().map(
|
||||||
ListSmsTemplateBO::getSmsSignId).collect(Collectors.toSet());
|
ListSmsTemplateBO::getSmsSignId).collect(Collectors.toSet());
|
||||||
|
@ -88,37 +87,32 @@ public class SmsServiceImpl implements SmsService {
|
||||||
new QueryWrapper<SmsSignDO>().in("id", smsSignIds));
|
new QueryWrapper<SmsSignDO>().in("id", smsSignIds));
|
||||||
|
|
||||||
List<ListSmsTemplateBO.Sign> signList = SmsTemplateConvert.INSTANCE.convertTemplateSign(smsSignDOList);
|
List<ListSmsTemplateBO.Sign> signList = SmsTemplateConvert.INSTANCE.convertTemplateSign(smsSignDOList);
|
||||||
|
// sign 转换为 map
|
||||||
Map<Integer, ListSmsTemplateBO.Sign> smsSignDOMap = signList
|
Map<Integer, ListSmsTemplateBO.Sign> smsSignDOMap = signList
|
||||||
.stream().collect(Collectors.toMap(ListSmsTemplateBO.Sign::getId, o -> o));
|
.stream().collect(Collectors.toMap(ListSmsTemplateBO.Sign::getId, o -> o));
|
||||||
|
|
||||||
// 设置 sign
|
// 设置 sign
|
||||||
|
|
||||||
templateList.forEach(template -> {
|
templateList.forEach(template -> {
|
||||||
if (smsSignDOMap.containsKey(template.getSmsSignId())) {
|
if (smsSignDOMap.containsKey(template.getSmsSignId())) {
|
||||||
template.setSign(smsSignDOMap.get(template.getSmsSignId()));
|
template.setSign(smsSignDOMap.get(template.getSmsSignId()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return new PageResult<ListSmsTemplateBO>().setList(templateList).setTotal(signPage.getTotal());
|
return new PageResult<ListSmsTemplateBO>().setList(templateList).setTotal(signPage.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void addSign(AddSignDTO addSignDTO) {
|
public void addSign(AddSignDTO addSignDTO) {
|
||||||
|
|
||||||
// 避免重复
|
// 避免重复
|
||||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
||||||
new QueryWrapper<SmsSignDO>()
|
new QueryWrapper<SmsSignDO>()
|
||||||
.eq("platform", addSignDTO.getPlatform())
|
.eq("platform", addSignDTO.getPlatform())
|
||||||
.eq("sign", addSignDTO.getSign())
|
.eq("sign", addSignDTO.getSign())
|
||||||
);
|
);
|
||||||
|
// 处理 null 情况
|
||||||
if (smsSignDO != null) {
|
if (smsSignDO != null) {
|
||||||
// TODO DOME FROM 芋艿 to 小范:可以使用 ServiceExceptionUtil.exception(SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT);
|
// TODO DOME FROM 芋艿 to 小范:可以使用 ServiceExceptionUtil.exception(SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT);
|
||||||
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT);
|
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存数据库
|
// 保存数据库
|
||||||
smsSignMapper.insert(
|
smsSignMapper.insert(
|
||||||
(SmsSignDO) new SmsSignDO()
|
(SmsSignDO) new SmsSignDO()
|
||||||
|
@ -133,16 +127,17 @@ public class SmsServiceImpl implements SmsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SmsSignBO getSign(Integer signId) {
|
public SmsSignBO getSign(Integer signId) {
|
||||||
|
// 查询数据库
|
||||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
||||||
new QueryWrapper<SmsSignDO>()
|
new QueryWrapper<SmsSignDO>()
|
||||||
.eq("id", signId)
|
.eq("id", signId)
|
||||||
.eq("deleted", DeletedStatusEnum.DELETED_NO.getValue()));
|
.eq("deleted", DeletedStatusEnum.DELETED_NO.getValue()));
|
||||||
|
// 处理 null
|
||||||
if (smsSignDO == null) {
|
if (smsSignDO == null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
// 转换vo返回
|
||||||
return SmsSignConvert.INSTANCE.convert(smsSignDO);
|
return SmsSignConvert.INSTANCE.convert(smsSignDO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,12 +149,11 @@ public class SmsServiceImpl implements SmsService {
|
||||||
new QueryWrapper<SmsSignDO>()
|
new QueryWrapper<SmsSignDO>()
|
||||||
.eq("sign", updateSignDTO.getSign())
|
.eq("sign", updateSignDTO.getSign())
|
||||||
.eq("platform", updateSignDTO.getPlatform()));
|
.eq("platform", updateSignDTO.getPlatform()));
|
||||||
|
// 处理 null
|
||||||
if (smsSignDO != null) {
|
if (smsSignDO != null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_SIGN_IS_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新
|
// 更新
|
||||||
smsSignMapper.update(
|
smsSignMapper.update(
|
||||||
(SmsSignDO) new SmsSignDO()
|
(SmsSignDO) new SmsSignDO()
|
||||||
|
@ -172,15 +166,13 @@ public class SmsServiceImpl implements SmsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteSign(Integer id) {
|
public void deleteSign(Integer id) {
|
||||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
// 根据id查询
|
||||||
new QueryWrapper<SmsSignDO>()
|
SmsSignDO smsSignDO = smsSignMapper.selectById(id);
|
||||||
.eq("id", id));
|
// 处理 null
|
||||||
|
|
||||||
if (smsSignDO == null) {
|
if (smsSignDO == null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新 deleted 为 YES
|
// 更新 deleted 为 YES
|
||||||
smsSignMapper.delete(new UpdateWrapper<SmsSignDO>()
|
smsSignMapper.delete(new UpdateWrapper<SmsSignDO>()
|
||||||
.set("deleted", DeletedStatusEnum.DELETED_YES.getName())
|
.set("deleted", DeletedStatusEnum.DELETED_YES.getName())
|
||||||
|
@ -192,15 +184,13 @@ public class SmsServiceImpl implements SmsService {
|
||||||
@Transactional
|
@Transactional
|
||||||
public void addTemplate(Integer smsSignId, String templateCode,
|
public void addTemplate(Integer smsSignId, String templateCode,
|
||||||
String template, Integer platform, Integer smsType) {
|
String template, Integer platform, Integer smsType) {
|
||||||
|
// 根据id查询
|
||||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
SmsSignDO smsSignDO = smsSignMapper.selectById(smsSignId);
|
||||||
new QueryWrapper<SmsSignDO>().eq("id", smsSignId));
|
// 处理 null
|
||||||
|
|
||||||
if (smsSignDO == null) {
|
if (smsSignDO == null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存数据库
|
// 保存数据库
|
||||||
smsTemplateMapper.insert(
|
smsTemplateMapper.insert(
|
||||||
(SmsTemplateDO) new SmsTemplateDO()
|
(SmsTemplateDO) new SmsTemplateDO()
|
||||||
|
@ -219,16 +209,17 @@ public class SmsServiceImpl implements SmsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SmsTemplateBO getTemplate(Integer id, Integer platform) {
|
public SmsTemplateBO getTemplate(Integer id, Integer platform) {
|
||||||
|
// 获取数据
|
||||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
||||||
new QueryWrapper<SmsTemplateDO>()
|
new QueryWrapper<SmsTemplateDO>()
|
||||||
.eq("platform", platform)
|
.eq("platform", platform)
|
||||||
.eq("id", id));
|
.eq("id", id));
|
||||||
|
// 处理 null
|
||||||
if (smsTemplateDO == null) {
|
if (smsTemplateDO == null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
// 转换bo返回
|
||||||
return SmsTemplateConvert.INSTANCE.convert(smsTemplateDO);
|
return SmsTemplateConvert.INSTANCE.convert(smsTemplateDO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,22 +227,20 @@ public class SmsServiceImpl implements SmsService {
|
||||||
@Transactional
|
@Transactional
|
||||||
public void updateTemplate(Integer id, Integer smsSignId, String templateCode,
|
public void updateTemplate(Integer id, Integer smsSignId, String templateCode,
|
||||||
String template, Integer platform, Integer smsType) {
|
String template, Integer platform, Integer smsType) {
|
||||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
// 获取 template
|
||||||
new QueryWrapper<SmsTemplateDO>().eq("id", id));
|
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectById(id);
|
||||||
|
|
||||||
if (smsTemplateDO == null) {
|
if (smsTemplateDO == null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
// 获取 sign
|
||||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
||||||
new QueryWrapper<SmsSignDO>().eq("id", smsTemplateDO.getSmsSignId()));
|
new QueryWrapper<SmsSignDO>().eq("id", smsTemplateDO.getSmsSignId()));
|
||||||
|
|
||||||
if (smsSignDO == null) {
|
if (smsSignDO == null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
// 更新数据库
|
||||||
smsTemplateMapper.update(
|
smsTemplateMapper.update(
|
||||||
(SmsTemplateDO) new SmsTemplateDO()
|
(SmsTemplateDO) new SmsTemplateDO()
|
||||||
.setSmsSignId(smsSignId)
|
.setSmsSignId(smsSignId)
|
||||||
|
@ -269,13 +258,12 @@ public class SmsServiceImpl implements SmsService {
|
||||||
public void deleteTemplate(Integer id) {
|
public void deleteTemplate(Integer id) {
|
||||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
||||||
new QueryWrapper<SmsTemplateDO>().eq("id", id));
|
new QueryWrapper<SmsTemplateDO>().eq("id", id));
|
||||||
|
// 处理不存在情况
|
||||||
if (smsTemplateDO == null
|
if (smsTemplateDO == null
|
||||||
|| smsTemplateDO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
|
|| smsTemplateDO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除 数据库模板
|
// 删除 数据库模板
|
||||||
SmsTemplateDO updateTemplate =new SmsTemplateDO();
|
SmsTemplateDO updateTemplate =new SmsTemplateDO();
|
||||||
updateTemplate.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
|
updateTemplate.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
|
||||||
|
@ -287,29 +275,24 @@ public class SmsServiceImpl implements SmsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void singleSend(String mobile, Integer smsTemplateId, Map<String, String> params) {
|
public void singleSend(String mobile, Integer smsTemplateId, Map<String, String> params) {
|
||||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
// 获取 template
|
||||||
new QueryWrapper<SmsTemplateDO>().eq("id", smsTemplateId));
|
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectById(smsTemplateId);
|
||||||
|
|
||||||
if (smsTemplateDO == null
|
if (smsTemplateDO == null
|
||||||
|| smsTemplateDO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
|
|| smsTemplateDO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
// 获取 sign
|
||||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
SmsSignDO smsSignDO = smsSignMapper.selectById(smsTemplateDO.getSmsSignId());
|
||||||
new QueryWrapper<SmsSignDO>().eq("id", smsTemplateDO.getSmsSignId()));
|
|
||||||
|
|
||||||
if (smsSignDO == null) {
|
if (smsSignDO == null) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取 client
|
// 获取 client
|
||||||
SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform());
|
SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform());
|
||||||
// 发送短信
|
// 发送短信
|
||||||
SmsClient.SendResult sendResult = 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(
|
smsSendMapper.insert(
|
||||||
(SmsSendLogDO) new SmsSendLogDO()
|
(SmsSendLogDO) new SmsSendLogDO()
|
||||||
|
@ -322,18 +305,15 @@ public class SmsServiceImpl implements SmsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void batchSend(List<String> mobileList, Integer smsTemplateId, Map<String, String> params) {
|
public void batchSend(List<String> mobileList, Integer smsTemplateId, Map<String, String> params) {
|
||||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
// 获取 template
|
||||||
new QueryWrapper<SmsTemplateDO>().eq("id", smsTemplateId));
|
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectById(smsTemplateId);
|
||||||
|
|
||||||
if (smsTemplateDO == null
|
if (smsTemplateDO == null
|
||||||
|| smsTemplateDO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
|
|| smsTemplateDO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
// 获取 sign
|
||||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
SmsSignDO smsSignDO = smsSignMapper.selectById(smsTemplateDO.getSmsSignId());
|
||||||
new QueryWrapper<SmsSignDO>().eq("id", smsTemplateDO.getSmsSignId()));
|
|
||||||
|
|
||||||
if (smsSignDO == null) {
|
if (smsSignDO == null) {
|
||||||
// 添加日志
|
// 添加日志
|
||||||
smsSendMapper.insert(
|
smsSendMapper.insert(
|
||||||
|
@ -347,14 +327,11 @@ public class SmsServiceImpl implements SmsService {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
SystemErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取 client
|
// 获取 client
|
||||||
SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform());
|
SmsClient smsClient = getSmsClient(smsTemplateDO.getPlatform());
|
||||||
|
|
||||||
// 发送短信
|
// 发送短信
|
||||||
SmsClient.SendResult sendResult = 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(
|
smsSendMapper.insert(
|
||||||
(SmsSendLogDO) new SmsSendLogDO()
|
(SmsSendLogDO) new SmsSendLogDO()
|
||||||
|
@ -373,18 +350,18 @@ public class SmsServiceImpl implements SmsService {
|
||||||
*/
|
*/
|
||||||
private SmsClient getSmsClient(Integer platform) {
|
private SmsClient getSmsClient(Integer platform) {
|
||||||
SmsClient smsClient = null;
|
SmsClient smsClient = null;
|
||||||
|
// 阿里云和云片
|
||||||
if (SmsPlatformEnum.YunPian.getValue().equals(platform)) {
|
if (SmsPlatformEnum.YunPian.getValue().equals(platform)) {
|
||||||
smsClient = smsYunPianClient;
|
smsClient = smsYunPianClient;
|
||||||
} else if (SmsPlatformEnum.AliYun.getValue().equals(platform)) {
|
} else if (SmsPlatformEnum.AliYun.getValue().equals(platform)) {
|
||||||
smsClient = smsAliYunClient;
|
smsClient = smsAliYunClient;
|
||||||
}
|
}
|
||||||
|
// 没有支持的平台
|
||||||
if (smsClient == null) {
|
if (smsClient == null) {
|
||||||
throw new ServiceException(
|
throw new ServiceException(
|
||||||
SystemErrorCodeEnum.SMS_NOT_SEND_CLIENT.getCode(),
|
SystemErrorCodeEnum.SMS_NOT_SEND_CLIENT.getCode(),
|
||||||
SystemErrorCodeEnum.SMS_NOT_SEND_CLIENT.getMessage());
|
SystemErrorCodeEnum.SMS_NOT_SEND_CLIENT.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return smsClient;
|
return smsClient;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,6 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
*/
|
*/
|
||||||
private static final String URL_SEND_BATCH = "https://sms.yunpian.com/v2/sms/batch_send.json";
|
private static final String URL_SEND_BATCH = "https://sms.yunpian.com/v2/sms/batch_send.json";
|
||||||
|
|
||||||
|
|
||||||
//编码格式。发送编码格式统一用UTF-8
|
//编码格式。发送编码格式统一用UTF-8
|
||||||
private static String ENCODING = "UTF-8";
|
private static String ENCODING = "UTF-8";
|
||||||
|
|
||||||
|
@ -99,7 +98,6 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
public SendResult singleSend(String mobile, String sign, String templateCode, String template, Map<String, String> templateParams) {
|
public SendResult singleSend(String mobile, String sign, String templateCode, String template, Map<String, String> templateParams) {
|
||||||
// build 模板
|
// build 模板
|
||||||
template = buildTemplate(sign, template, templateParams);
|
template = buildTemplate(sign, template, templateParams);
|
||||||
|
|
||||||
// 请求参数
|
// 请求参数
|
||||||
Map<String, String> params = new LinkedHashMap<>();
|
Map<String, String> params = new LinkedHashMap<>();
|
||||||
params.put("apikey", apiKey);
|
params.put("apikey", apiKey);
|
||||||
|
@ -114,7 +112,7 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||||
jsonObject.getString("detail"));
|
jsonObject.getString("detail"));
|
||||||
}
|
}
|
||||||
|
// 转换 result
|
||||||
return new SendResult()
|
return new SendResult()
|
||||||
.setIsSuccess(SUCCESS_CODE == jsonObject.getInteger("code"))
|
.setIsSuccess(SUCCESS_CODE == jsonObject.getInteger("code"))
|
||||||
.setCode(jsonObject.getInteger("code"))
|
.setCode(jsonObject.getInteger("code"))
|
||||||
|
@ -127,7 +125,6 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
Map<String, String> templateParams) {
|
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 == 0
|
int maxSendSizeCount = mobileList.size() % maxSendSize == 0
|
||||||
|
@ -142,9 +139,7 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
sendMobileStr.append(",");
|
sendMobileStr.append(",");
|
||||||
sendMobileStr.append(mobileList.get(k));
|
sendMobileStr.append(mobileList.get(k));
|
||||||
}
|
}
|
||||||
|
|
||||||
String dividedMobile = sendMobileStr.toString().substring(1);
|
String dividedMobile = sendMobileStr.toString().substring(1);
|
||||||
|
|
||||||
// 发送手机号
|
// 发送手机号
|
||||||
Map<String, String> params = new LinkedHashMap<>();
|
Map<String, String> params = new LinkedHashMap<>();
|
||||||
params.put("apikey", apiKey);
|
params.put("apikey", apiKey);
|
||||||
|
@ -159,12 +154,10 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
throw new ServiceException(SystemErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||||
jsonObject.getString("detail"));
|
jsonObject.getString("detail"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用于递增 maxSendSize
|
// 用于递增 maxSendSize
|
||||||
j = j2;
|
j = j2;
|
||||||
j2 = j + maxSendSize;
|
j2 = j + maxSendSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SendResult()
|
return new SendResult()
|
||||||
.setIsSuccess(true)
|
.setIsSuccess(true)
|
||||||
.setCode(SUCCESS_CODE)
|
.setCode(SUCCESS_CODE)
|
||||||
|
@ -181,18 +174,17 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
*/
|
*/
|
||||||
private static String buildTemplate(String sign, String template,
|
private static String buildTemplate(String sign, String template,
|
||||||
Map<String, String> templateParams) {
|
Map<String, String> templateParams) {
|
||||||
|
// 不处理 empty 数据
|
||||||
if (CollectionUtils.isEmpty(templateParams)) {
|
if (CollectionUtils.isEmpty(templateParams)) {
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
// 处理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();
|
||||||
String paramPlace = String.format(PARAM_TEMPLATE, paramsKey);
|
String paramPlace = String.format(PARAM_TEMPLATE, paramsKey);
|
||||||
template = template.replaceAll(paramPlace, value);
|
template = template.replaceAll(paramPlace, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template = String.format(SIGN_TEMPLATE, sign, template);
|
template = String.format(SIGN_TEMPLATE, sign, template);
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
@ -234,7 +226,6 @@ public class YunPianSmsClient implements SmsClient {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER.debug("云片短信平台 res: {}", responseText);
|
LOGGER.debug("云片短信平台 res: {}", responseText);
|
||||||
return responseText;
|
return responseText;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue