diff --git a/eladmin-common/src/main/java/me/zhengjie/constant/SmsConstant.java b/eladmin-common/src/main/java/me/zhengjie/constant/SmsConstant.java index c65a049..3b99e98 100644 --- a/eladmin-common/src/main/java/me/zhengjie/constant/SmsConstant.java +++ b/eladmin-common/src/main/java/me/zhengjie/constant/SmsConstant.java @@ -7,29 +7,25 @@ package me.zhengjie.constant; * 短信发送常量 */ public class SmsConstant { - /** - * APP ID - */ + /** APP ID*/ public static final String APP_ID = "app1"; + /** "|"分割*/ public static final String VERTICAL_LINE = "\\|"; + /** 时间格式*/ public static final String FORMATE_TIMESTAMP = "yyyyMMddHHmmss"; /** TOKEN*/ public static final String TOKEN = "f625d0a23493cd8aeb8ada97da7a7b65"; + /** 服务前缀*/ public static final String SHORT_CHAIN_PREFIX = "t.p.tuoz.net/s/"; - /** - * 生成短链 - */ - public static final String SHORT_GENERATION_LINK = "http://t.p.tuoz.net/trans"; - + /** 生成短链*/ + public static final String SHORT_GENERATION_LINK = "http://t.p.tuoz.net/trans"; - /** - * 短信url - */ + /*** 短信url*/ public static final String SMS_LINK = "http://api.hzdaba.cn/v2/Accounts/baiyekeji_label/Sms/send"; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/constant/DefaultConstant.java b/eladmin-system/src/main/java/me/zhengjie/modules/constant/DefaultConstant.java new file mode 100644 index 0000000..757d7c7 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/constant/DefaultConstant.java @@ -0,0 +1,39 @@ +package me.zhengjie.modules.constant; + +/** + * @author E + * @date + */ +public class DefaultConstant { + + + + /** + * 0 + */ + public static final int ZERO_NUMBER = 0; + + + /** + * 1 + */ + public static final int ONE_NUMBER = 1; + + /** + * 2 + */ + public static final int TWO_NUMBER = 2; + + + /** + * 5 + */ + public static final int FIVE_NUMBER = 5; + + /** + * 10 + */ + public static final int TEN_NUMBER = 10; + + +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/dto/ShortLinkUrlDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/dto/ShortLinkUrlDto.java index f736576..f02ec5d 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/dto/ShortLinkUrlDto.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/dto/ShortLinkUrlDto.java @@ -1,6 +1,5 @@ package me.zhengjie.modules.sms.dto; -import com.alipay.api.domain.Datas; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbBlacklistRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbBlacklistRepository.java index 15079ea..c9e2129 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbBlacklistRepository.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbBlacklistRepository.java @@ -19,11 +19,13 @@ package me.zhengjie.modules.sms.repository; import me.zhengjie.modules.sms.domain.TbBlacklist; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; /** * @website https://el-admin.vip * @author Enzo * @date 2021-04-14 **/ +@Repository public interface TbBlacklistRepository extends JpaRepository, JpaSpecificationExecutor { } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbSendSmsRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbSendSmsRepository.java index 1538c23..ff4cd17 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbSendSmsRepository.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbSendSmsRepository.java @@ -20,6 +20,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; import javax.transaction.Transactional; @@ -28,6 +29,7 @@ import javax.transaction.Transactional; * @author Enzo * @date 2021-04-14 **/ +@Repository public interface TbSendSmsRepository extends JpaRepository, JpaSpecificationExecutor { /** @@ -43,8 +45,7 @@ public interface TbSendSmsRepository extends JpaRepository, Jpa * @param linkUrl 短链接 * @return */ - @Transactional(rollbackOn = Exception.class) @Modifying(clearAutomatically = true) - @Query(value = "update tb_send_sms set is_link_callback = 1 where link_url = ?1",nativeQuery = true) + @Query(value = "update TbSendSms set isLinkCallback = 1 where linkUrl = ?1") int updateStatusSendStatus(String linkUrl); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbTemplateRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbTemplateRepository.java index 6e1fe68..eee4257 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbTemplateRepository.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/repository/TbTemplateRepository.java @@ -19,12 +19,14 @@ package me.zhengjie.modules.sms.repository; import me.zhengjie.modules.sms.domain.TbTemplate; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; /** * @website https://el-admin.vip * @author Enzo * @date 2021-04-16 **/ +@Repository public interface TbTemplateRepository extends JpaRepository, JpaSpecificationExecutor { /** diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbSendSmsController.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbSendSmsController.java index 12929e5..916829d 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbSendSmsController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbSendSmsController.java @@ -15,34 +15,39 @@ */ package me.zhengjie.modules.sms.rest; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import me.zhengjie.annotation.Log; import me.zhengjie.common.http.ResponseCode; -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.service.TbSendSmsService; import me.zhengjie.modules.sms.service.dto.TbSendSmsQueryCriteria; import me.zhengjie.modules.sms.vo.LinkCallBack; import me.zhengjie.modules.sms.vo.SendVo; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.data.domain.Pageable; -import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.*; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; -import javax.servlet.http.HttpServletResponse; /** * @author Enzo * @website https://el-admin.vip * @date 2021-04-14 **/ +@Slf4j @RestController @RequiredArgsConstructor @Api(tags = "SendSmsController管理") @@ -98,18 +103,23 @@ public class TbSendSmsController { @ApiOperation("发送SendSmsController") @PostMapping("/url/send") public ResponseEntity send(@RequestBody SendVo vo) { + if (vo == null || CollectionUtils.isEmpty(vo.getData())) { + log.info("the send Sms fail Information parameter error"); + throw new BadRequestException("传入参数错误"); + } CompletableFuture.runAsync(() -> tbSendSmsService.sendSmsBySendVo(vo)); return new ResponseEntity<>(HttpStatus.OK); } + @Log("点击短链回调") @ApiOperation("点击短链回调") @PostMapping("/url/callback") public ResponseEntity> urlCallBack(@Validated @RequestBody LinkCallBack linkCallBack) { - Map map = new HashMap<>(2); + Map map = new HashMap<>(DefaultConstant.TWO_NUMBER); boolean updateSmsStatusByLinkUrl = tbSendSmsService.updateSmsStatusByLinkUrl(linkCallBack.getShortUrl()); - map.put("status", updateSmsStatusByLinkUrl ? 0 : 1); - map.put("data", updateSmsStatusByLinkUrl ? ResponseCode.SUCCESS.getDesc():ResponseCode.FAILURE.getDesc()); + map.put("status", updateSmsStatusByLinkUrl ? ResponseCode.SUCCESS.getCode() : ResponseCode.FAILURE.getCode()); + map.put("data", updateSmsStatusByLinkUrl ? ResponseCode.SUCCESS.getDesc() : ResponseCode.FAILURE.getDesc()); return new ResponseEntity<>(map, HttpStatus.OK); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java index 9910698..d66c9ce 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java @@ -94,12 +94,12 @@ public class TbTemplateController { return new ResponseEntity<>(HttpStatus.OK); } - /** + /* *//** * 接收大数据传过来的短信内容 * * @param sendVo 发送任务对应的VO映射 * @return 返回建立任务完成的状态 - */ + *//* @Log("接收短信内容和url") @ApiOperation("接收短信内容和url") // @PreAuthorize("@el.check('taskRecord:list')") @@ -122,5 +122,5 @@ public class TbTemplateController { return new ResponseEntity<>(CommonResponse.createBySuccess(), HttpStatus.OK); } return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_WORD_INPUT), HttpStatus.OK); - } + }*/ } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/impl/TbSendSmsServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/impl/TbSendSmsServiceImpl.java index 0151a0f..3e44b21 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/impl/TbSendSmsServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/impl/TbSendSmsServiceImpl.java @@ -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 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 map = new HashMap<>(2); + Map 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; } } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/mapstruct/TbBlacklistMapper.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/mapstruct/TbBlacklistMapper.java new file mode 100644 index 0000000..8fd0c44 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/mapstruct/TbBlacklistMapper.java @@ -0,0 +1,33 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package me.zhengjie.modules.sms.service.mapstruct; + + +import me.zhengjie.base.BaseMapper; +import me.zhengjie.modules.sms.domain.TbBlacklist; +import me.zhengjie.modules.sms.service.dto.TbBlacklistDto; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; + +/** +* @website https://el-admin.vip +* @author Enzo +* @date 2021-04-14 +**/ +@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface TbBlacklistMapper extends BaseMapper { + +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/util/SmsUtil.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/util/SmsUtil.java index d0f599a..a215a3b 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/util/SmsUtil.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/util/SmsUtil.java @@ -11,7 +11,6 @@ import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; @@ -20,7 +19,6 @@ import java.util.Date; * @author aliyunsms * @date : 2021-04-19 */ -@Component public class SmsUtil {