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