|
|
|
@ -30,6 +30,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import me.zhengjie.constant.SmsConstant;
|
|
|
|
|
import me.zhengjie.exception.BadRequestException;
|
|
|
|
|
import me.zhengjie.modules.constant.DefaultConstant;
|
|
|
|
|
import me.zhengjie.modules.sms.domain.TbSendSms;
|
|
|
|
|
import me.zhengjie.modules.sms.domain.TbTemplate;
|
|
|
|
|
import me.zhengjie.modules.sms.dto.ShortLinkUrlDto;
|
|
|
|
@ -39,7 +40,6 @@ import me.zhengjie.modules.sms.service.TbSendSmsService;
|
|
|
|
|
import me.zhengjie.modules.sms.service.dto.TbSendSmsDto;
|
|
|
|
|
import me.zhengjie.modules.sms.service.dto.TbSendSmsQueryCriteria;
|
|
|
|
|
import me.zhengjie.modules.sms.service.mapstruct.TbSendSmsMapper;
|
|
|
|
|
import me.zhengjie.modules.sms.util.SmsUtil;
|
|
|
|
|
import me.zhengjie.modules.sms.vo.SendVo;
|
|
|
|
|
import me.zhengjie.modules.upload.task.model.SendSmsJsonContent;
|
|
|
|
|
import me.zhengjie.utils.FileUtil;
|
|
|
|
@ -47,7 +47,7 @@ import me.zhengjie.utils.PageUtil;
|
|
|
|
|
import me.zhengjie.utils.QueryHelp;
|
|
|
|
|
import me.zhengjie.utils.ValidationUtil;
|
|
|
|
|
import me.zhengjie.utils.enums.StatusEnum;
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@ -70,7 +70,6 @@ import java.util.*;
|
|
|
|
|
public class TbSendSmsServiceImpl implements TbSendSmsService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final SmsUtil smsUtil;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* hutool 缓存
|
|
|
|
@ -149,25 +148,30 @@ public class TbSendSmsServiceImpl implements TbSendSmsService {
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void sendSmsBySendVo(SendVo vo) {
|
|
|
|
|
List<SendVo.DataInfo> infos = vo.getData();
|
|
|
|
|
if (CollectionUtils.isEmpty(infos)) {
|
|
|
|
|
throw new BadRequestException("传入信息有误");
|
|
|
|
|
}
|
|
|
|
|
for (SendVo.DataInfo info : infos) {
|
|
|
|
|
TbTemplate template = lruCache.get(info.getTaskName());
|
|
|
|
|
if (template == null) {
|
|
|
|
|
template = tbTemplateRepository.findByTaskName(info.getTaskName());
|
|
|
|
|
// 5分钟缓存
|
|
|
|
|
lruCache.put(info.getTaskName(), template, DateUnit.MINUTE.getMillis() * 5);
|
|
|
|
|
lruCache.put(info.getTaskName(), template,
|
|
|
|
|
DateUnit.MINUTE.getMillis() * DefaultConstant.FIVE_NUMBER);
|
|
|
|
|
}
|
|
|
|
|
if (template == null) {
|
|
|
|
|
log.info("send Sms fail the Template is null ");
|
|
|
|
|
throw new BadRequestException("模板信息为空");
|
|
|
|
|
}
|
|
|
|
|
Map<String, Object> map = new HashMap<>(2);
|
|
|
|
|
Map<String, Object> map = new HashMap<>(DefaultConstant.TWO_NUMBER);
|
|
|
|
|
map.put("baseUrlAddr", template.getLinkUrl());
|
|
|
|
|
map.put("variableList", info.getPhoneList());
|
|
|
|
|
// 接收生成短链参数
|
|
|
|
|
ShortLinkUrlDto urlDto = JSONUtil.toBean(HttpUtil.post(SmsConstant.SHORT_GENERATION_LINK, JSON.toJSONString(map)), ShortLinkUrlDto.class);
|
|
|
|
|
if (urlDto.getStatus() != 0) {
|
|
|
|
|
ShortLinkUrlDto urlDto =
|
|
|
|
|
JSONUtil.toBean(HttpUtil.post
|
|
|
|
|
(SmsConstant.SHORT_GENERATION_LINK,
|
|
|
|
|
JSON.toJSONString(map)),
|
|
|
|
|
ShortLinkUrlDto.class);
|
|
|
|
|
if (urlDto == null || urlDto.getData() == null ||
|
|
|
|
|
urlDto.getStatus() != DefaultConstant.ZERO_NUMBER) {
|
|
|
|
|
log.info("send Sms fail the urlDto is fail urlResponse message [{}]" , urlDto != null ? urlDto.getMsg() : null);
|
|
|
|
|
throw new BadRequestException("短链生成失败");
|
|
|
|
|
}
|
|
|
|
|
TbSendSms sendSms;
|
|
|
|
@ -177,39 +181,46 @@ public class TbSendSmsServiceImpl implements TbSendSmsService {
|
|
|
|
|
for (String resultUrl : urlDto.getData().getShortChainResult()) {
|
|
|
|
|
sendSms = new TbSendSms();
|
|
|
|
|
// StrUtil 工具类
|
|
|
|
|
String[] split = resultUrl.split(SmsConstant.VERTICAL_LINE);
|
|
|
|
|
String[] split = StringUtils.split(resultUrl, SmsConstant.VERTICAL_LINE);
|
|
|
|
|
phone = split[1];
|
|
|
|
|
linkUrl = split[0];
|
|
|
|
|
String sendMessage = message.replace("${url}", StrUtil.SPACE + SmsConstant.SHORT_CHAIN_PREFIX + linkUrl);
|
|
|
|
|
String sendMessage = message.replace
|
|
|
|
|
("${url}",
|
|
|
|
|
StrUtil.SPACE +
|
|
|
|
|
SmsConstant.SHORT_CHAIN_PREFIX + linkUrl);
|
|
|
|
|
sendSms.setPhone(phone);
|
|
|
|
|
sendSms.setLinkUrl(linkUrl);
|
|
|
|
|
sendSms.setIsLinkCallback(0);
|
|
|
|
|
sendSms.setIsLinkCallback(DefaultConstant.ZERO_NUMBER);
|
|
|
|
|
sendSms.setSendMessage(sendMessage);
|
|
|
|
|
sendSms.setTemplateId(template.getId());
|
|
|
|
|
sendSms.setSendTime(new Timestamp(System.currentTimeMillis()));
|
|
|
|
|
boolean sendResult = sendBusinessSms(phone, sendMessage);
|
|
|
|
|
sendSms.setSendStatus(sendResult ? StatusEnum.SUCCESS.getValue() : StatusEnum.UNSUCCESSFUL.getValue());
|
|
|
|
|
tbSendSmsRepository.save(sendSms);
|
|
|
|
|
}
|
|
|
|
|
log.info("======= [ sendSms send size {} ] =======", info.getPhoneList().size());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public boolean updateSmsStatusByLinkUrl(String linkUrl) {
|
|
|
|
|
TbSendSms sendSms = tbSendSmsRepository.findByLinkUrl(linkUrl);
|
|
|
|
|
if (sendSms != null) {
|
|
|
|
|
return tbSendSmsRepository.updateStatusSendStatus(linkUrl) > 0;
|
|
|
|
|
return tbSendSmsRepository.updateStatusSendStatus(linkUrl) > DefaultConstant.ZERO_NUMBER;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return Boolean.FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean sendBusinessSms(String phone, String templateMessage) {
|
|
|
|
|
SendSmsJsonContent sendSmsJsonContent = new SendSmsJsonContent();
|
|
|
|
|
sendSmsJsonContent.setMobile(phone);
|
|
|
|
|
sendSmsJsonContent.setAppId(SmsConstant.APP_ID);
|
|
|
|
|
sendSmsJsonContent.setNumberId(RandomUtil.randomString(10));
|
|
|
|
|
String signature = SmsConstant.APP_ID + SmsConstant.TOKEN + DateUtil.format(new Date(), SmsConstant.FORMATE_TIMESTAMP);
|
|
|
|
|
sendSmsJsonContent.setNumberId
|
|
|
|
|
(RandomUtil.randomString(DefaultConstant.TEN_NUMBER));
|
|
|
|
|
String signature = SmsConstant.APP_ID +
|
|
|
|
|
SmsConstant.TOKEN +
|
|
|
|
|
DateUtil.format
|
|
|
|
|
(new Date(), SmsConstant.FORMATE_TIMESTAMP);
|
|
|
|
|
signature = SecureUtil.md5(signature).toUpperCase();
|
|
|
|
|
// 配置 sign
|
|
|
|
|
sendSmsJsonContent.setSig(signature);
|
|
|
|
@ -231,6 +242,6 @@ public class TbSendSmsServiceImpl implements TbSendSmsService {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return Boolean.FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|