增加基础属性

master
bynt 3 years ago
parent 26701f9b73
commit 8e5d21bf9c

@ -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";

@ -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;
}

@ -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;

@ -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<TbBlacklist, Long>, JpaSpecificationExecutor<TbBlacklist> {
}

@ -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<TbSendSms, Long>, JpaSpecificationExecutor<TbSendSms> {
/**
@ -43,8 +45,7 @@ public interface TbSendSmsRepository extends JpaRepository<TbSendSms, Long>, 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);
}

@ -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<TbTemplate, Long>, JpaSpecificationExecutor<TbTemplate> {
/**

@ -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<Object> 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<Map<String, Object>> urlCallBack(@Validated @RequestBody LinkCallBack linkCallBack) {
Map<String, Object> map = new HashMap<>(2);
Map<String, Object> 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);
}

@ -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);
}
}*/
}

@ -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;
}
}

@ -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<TbBlacklistDto, TbBlacklist> {
}

@ -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 {

Loading…
Cancel
Save