send sms with db

master
3y 4 years ago
parent eb99631e91
commit 2f7ac4d427

@ -0,0 +1,42 @@
package com.java3y.austin.enums;
/**
*
*/
public enum ChannelType {
IM(10, "IM(站内信)"),
PUSH(20, "push(通知栏)"),
SMS(30, "sms(短信)"),
EMAIL(40, "email(邮件)"),
OFFICIAL_ACCOUNT(50, "OfficialAccounts(服务号)"),
MINI_PROGRAM(60, "miniProgram(小程序)")
;
private Integer code;
private String description;
ChannelType(Integer code, String description) {
this.code = code;
this.description = description;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

@ -0,0 +1,37 @@
package com.java3y.austin.enums;
/**
* ID
*/
public enum IdType {
USER_ID(10, "userid"),
DID(20, "did"),
PHONE(30, "phone"),
OPEN_ID(40, "openId"),
EMAIL(50, "email");
IdType(Integer code, String description) {
this.code = code;
this.description = description;
}
private Integer code;
private String description;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

@ -0,0 +1,40 @@
package com.java3y.austin.enums;
/**
*
*/
public enum MessageType {
NOTICE(10,"通知类消息"),
MARKETING(20,"营销类消息"),
AUTH_CODE(30,"验证码消息")
;
/**
* `msg_type` tinyint(4)
* NOT NULL DEFAULT '0' COMMENT '10. 20. 30.',
*/
private Integer code;
private String description;
MessageType(Integer code, String description) {
this.code = code;
this.description = description;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

@ -0,0 +1,32 @@
package com.java3y.austin.enums;
public enum SmsStatus {
SEND_SUCCESS(10,"调用渠道接口发送成功"),
RECEIVE_SUCCESS(20,"用户收到短信(收到渠道短信回执,状态成功)"),
RECEIVE_FAIL(30, "用户收不到短信(收到渠道短信回执,状态失败)");
private Integer code;
private String description;
SmsStatus(Integer code, String description) {
this.code = code;
this.description = description;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

@ -0,0 +1,32 @@
package com.java3y.austin.enums;
public enum TemplateType {
OPERATION(10, "运营类的模板"),
TECHNOLOGY(20, "技术类的模板"),
;
private Integer code;
private String description;
TemplateType(Integer code, String description) {
this.code = code;
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}

@ -14,6 +14,11 @@ import java.util.Set;
@Builder
public class SmsParam {
/**
* Id
*/
private Long messageTemplateId;
/**
*
*/
@ -23,4 +28,14 @@ public class SmsParam {
*
*/
private String content;
/**
* Id
*/
private Integer supplierId;
/**
*
*/
private String supplierName;
}

@ -0,0 +1,73 @@
package com.java3y.austin.pojo;
import lombok.Builder;
import lombok.Data;
import java.util.Set;
/**
*
*/
@Data
@Builder
public class TaskInfo {
/**
* Id
*/
private Long messageTemplateId;
/**
* Id
*/
private Long businessId;
/**
*
*/
private Set<String> receiver;
/**
* Id
*/
private Integer idType;
/**
*
*/
private Integer sendChannel;
/**
*
*/
private Integer templateType;
/**
*
*/
private Integer msgType;
/**
*
*/
private String content;
/**
* ..
*/
private Integer sendAccount;
/**
*
*/
private Integer deduplicationTime;
/**
*
* 0:
* 1
*/
private Integer isNightShield;
}

@ -0,0 +1,14 @@
package com.java3y.austin.handler;
import com.java3y.austin.domain.MessageTemplate;
import com.java3y.austin.pojo.TaskInfo;
public interface Handler {
boolean doHandler(TaskInfo TaskInfo);
}

@ -0,0 +1,41 @@
package com.java3y.austin.handler;
import cn.hutool.core.collection.CollUtil;
import com.java3y.austin.dao.SmsRecordDao;
import com.java3y.austin.domain.MessageTemplate;
import com.java3y.austin.domain.SmsRecord;
import com.java3y.austin.pojo.SmsParam;
import com.java3y.austin.pojo.TaskInfo;
import com.java3y.austin.script.SmsScript;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class SmsHandler implements Handler {
@Autowired
private SmsRecordDao smsRecordDao;
@Autowired
private SmsScript smsScript;
@Override
public boolean doHandler(TaskInfo taskInfo) {
SmsParam smsParam = SmsParam.builder()
.phones(taskInfo.getReceiver())
.content(taskInfo.getContent())
.messageTemplateId(taskInfo.getMessageTemplateId())
.supplierId(10)
.supplierName("腾讯云通知类消息渠道").build();
List<SmsRecord> recordList = smsScript.send(smsParam);
if (CollUtil.isNotEmpty(recordList)) {
smsRecordDao.saveAll(recordList);
return true;
}
return false;
}
}

@ -0,0 +1,17 @@
package com.java3y.austin.script;
import com.java3y.austin.domain.SmsRecord;
import com.java3y.austin.pojo.SmsParam;
import java.util.List;
public interface SmsScript {
/**
* @param smsParam
* @return
*/
List<SmsRecord> send(SmsParam smsParam);
}

@ -1,8 +1,13 @@
package com.java3y.austin.script;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.domain.SmsRecord;
import com.java3y.austin.enums.SmsStatus;
import com.java3y.austin.pojo.SmsParam;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
@ -11,12 +16,15 @@ import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.Getter;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author 3y
* @date 2021/11/6
@ -26,7 +34,9 @@ import org.springframework.stereotype.Service;
*/
@Service
@Slf4j
public class TencentSmsScript {
public class TencentSmsScript implements SmsScript {
private static final Integer PHONE_NUM = 11;
/**
* api
@ -52,44 +62,82 @@ public class TencentSmsScript {
@Value("${tencent.sms.account.sign_name}")
private String SIGN_NAME;
public String send(SmsParam smsParam) {
@Override
public List<SmsRecord> send(SmsParam smsParam) {
try {
SmsClient client = init();
/**
* client
*/
Credential cred = new Credential(SECRET_ID, SECRET_KEY);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(URL);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(cred, REGION, clientProfile);
/**
*
*/
SendSmsRequest req = new SendSmsRequest();
String[] phoneNumberSet1 = smsParam.getPhones().toArray(new String[smsParam.getPhones().size() - 1]);
req.setPhoneNumberSet(phoneNumberSet1);
req.setSmsSdkAppId(SMS_SDK_APP_ID);
req.setSignName(SIGN_NAME);
req.setTemplateId(TEMPLATE_ID);
String[] templateParamSet1 = {smsParam.getContent()};
req.setTemplateParamSet(templateParamSet1);
req.setSessionContext(IdUtil.fastSimpleUUID());
/**
*
*/
SendSmsResponse resp = client.SendSms(req);
return SendSmsResponse.toJsonString(resp);
SendSmsRequest request = assembleReq(smsParam);
SendSmsResponse response = client.SendSms(request);
return assembleSmsRecord(smsParam,response);
} catch (TencentCloudSDKException e) {
log.error("send tencent sms fail!{},params:{}",
Throwables.getStackTraceAsString(e), JSON.toJSONString(smsParam));
return null;
}
}
private List<SmsRecord> assembleSmsRecord(SmsParam smsParam, SendSmsResponse response) {
if (response == null || ArrayUtil.isEmpty(response.getSendStatusSet())) {
return null;
}
List<SmsRecord> smsRecordList = new ArrayList<>();
for (SendStatus sendStatus : response.getSendStatusSet()) {
String phone = new StringBuilder(new StringBuilder(sendStatus.getPhoneNumber())
.reverse().substring(0, PHONE_NUM)).reverse().toString();
SmsRecord smsRecord = SmsRecord.builder()
.sendDate(Integer.valueOf(DateUtil.format(new Date(), "yyyyMMdd")))
.messageTemplateId(smsParam.getMessageTemplateId())
.phone(Long.valueOf(phone))
.supplierId(smsParam.getSupplierId())
.supplierName(smsParam.getSupplierName())
.seriesId(sendStatus.getSerialNo())
.chargingNum(Math.toIntExact(sendStatus.getFee()))
.status(SmsStatus.SEND_SUCCESS.getCode())
.reportContent(sendStatus.getCode())
.created(Math.toIntExact(DateUtil.currentSeconds()))
.updated(Math.toIntExact(DateUtil.currentSeconds()))
.build();
smsRecordList.add(smsRecord);
}
return smsRecordList;
}
/**
*
*/
private SendSmsRequest assembleReq(SmsParam smsParam) {
SendSmsRequest req = new SendSmsRequest();
String[] phoneNumberSet1 = smsParam.getPhones().toArray(new String[smsParam.getPhones().size() - 1]);
req.setPhoneNumberSet(phoneNumberSet1);
req.setSmsSdkAppId(SMS_SDK_APP_ID);
req.setSignName(SIGN_NAME);
req.setTemplateId(TEMPLATE_ID);
String[] templateParamSet1 = {smsParam.getContent()};
req.setTemplateParamSet(templateParamSet1);
req.setSessionContext(IdUtil.fastSimpleUUID());
return req;
}
/**
* client
*/
private SmsClient init() {
Credential cred = new Credential(SECRET_ID, SECRET_KEY);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(URL);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(cred, REGION, clientProfile);
return client;
}
}

@ -13,6 +13,11 @@
<dependencies>
<dependency>
<groupId>com.java3y.austin</groupId>
<artifactId>austin-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
@ -42,15 +47,11 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,8 @@
package com.java3y.austin.dao;
import com.java3y.austin.domain.MessageTemplate;
import org.springframework.data.repository.CrudRepository;
public interface MessageTemplateDao extends CrudRepository<MessageTemplate, Long> {
}

@ -0,0 +1,9 @@
package com.java3y.austin.dao;
import com.java3y.austin.domain.SmsRecord;
import org.springframework.data.repository.CrudRepository;
public interface SmsRecordDao extends CrudRepository<SmsRecord, Long> {
}

@ -0,0 +1,141 @@
package com.java3y.austin.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
/**
* @autor 3y
* DO
*/
public class MessageTemplate {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
/**
*
*/
private String name;
/**
*
*/
private Integer auditStatus;
/**
* ID
*/
private String flowId;
/**
*
*/
private Integer msgStatus;
/**
* Id
*/
private Integer idType;
/**
*
*/
private Integer sendChannel;
/**
*
*/
private Integer templateType;
/**
*
*/
private Integer msgType;
/**
*
* 0
* elsecrontab
*/
private String expectPushTime;
/**
* {$var}
*/
private String msgContent;
/**
* ..
*/
private Integer sendAccount;
/**
*
*/
private String creator;
/**
*
*/
private String updator;
/**
*
*/
private String auditor;
/**
*
*/
private String team;
/**
*
*/
private String proposer;
/**
*
* 0
* 1
*/
private Integer isDeleted;
/**
* s
*/
private Integer created;
/**
* s
*/
private Integer updated;
/**
*
*/
private Integer deduplicationTime;
/**
*
* 0:
* 1
*/
private Integer isNightShield;
}

@ -0,0 +1,81 @@
package com.java3y.austin.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
/**
*
*/
public class SmsRecord {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
/**
* Id
*/
private Long messageTemplateId;
/**
*
*/
private Long phone;
/**
* Id
*/
private Integer supplierId;
/**
*
*/
private String supplierName;
/**
* Id
*/
private String seriesId;
/**
*
*/
private Integer chargingNum;
/**
*
*/
private String reportContent;
/**
*
*/
private Integer status;
/**
*
*/
private Integer sendDate;
/**
*
*/
private Integer created;
/**
*
*/
private Integer updated;
}

@ -1,5 +1,9 @@
package com.java3y.austin;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.java3y.austin.dao.MessageTemplateDao;
import com.java3y.austin.domain.MessageTemplate;
import com.java3y.austin.pojo.SmsParam;
import com.java3y.austin.script.TencentSmsScript;
import org.springframework.beans.factory.annotation.Autowired;
@ -13,32 +17,8 @@ import java.util.HashSet;
@SpringBootApplication
@RestController
public class AustinApplication {
@Autowired
private TencentSmsScript tencentSmsScript;
public static void main(String[] args) {
SpringApplication.run(AustinApplication.class, args);
}
/**
* @param phone
* @return
*/
@GetMapping("/sendSms")
public String sendSms(String phone,String content) {
/**
* content
*/
SmsParam smsParam = SmsParam.builder()
.phones(new HashSet<>(Arrays.asList(phone)))
.content(content)
.build();
return tencentSmsScript.send(smsParam);
}
}

@ -0,0 +1,69 @@
package com.java3y.austin.controller;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.java3y.austin.dao.MessageTemplateDao;
import com.java3y.austin.domain.MessageTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
*/
@RestController
public class MessageTemplateController {
@Autowired
private MessageTemplateDao messageTemplateDao;
/**
* test insert
*/
@GetMapping("/insert")
public String insert() {
MessageTemplate messageTemplate = MessageTemplate.builder()
.name("test短信")
.auditStatus(10)
.flowId("yyyy")
.msgStatus(10)
.idType(10)
.sendChannel(10)
.templateType(10)
.msgType(10)
.expectPushTime("0")
.msgContent("3333333m")
.sendAccount(66)
.creator("yyyyc")
.updator("yyyyu")
.team("yyyt")
.proposer("yyyy22")
.auditor("yyyyyyz")
.isDeleted(0)
.created(Math.toIntExact(DateUtil.currentSeconds()))
.updated(Math.toIntExact(DateUtil.currentSeconds()))
.deduplicationTime(1)
.isNightShield(0)
.build();
MessageTemplate info = messageTemplateDao.save(messageTemplate);
return JSON.toJSONString(info);
}
/**
* test query
*/
@GetMapping("/query")
public String query() {
Iterable<MessageTemplate> all = messageTemplateDao.findAll();
for (MessageTemplate messageTemplate : all) {
return JSON.toJSONString(messageTemplate);
}
return null;
}
}

@ -0,0 +1,38 @@
package com.java3y.austin.controller;
import com.alibaba.fastjson.JSON;
import com.java3y.austin.handler.SmsHandler;
import com.java3y.austin.pojo.SmsParam;
import com.java3y.austin.pojo.TaskInfo;
import com.java3y.austin.script.TencentSmsScript;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.HashSet;
@RestController
public class SendController {
@Autowired
private SmsHandler smsHandler;
/**
*
* @param phone
* @return
*/
@GetMapping("/sendSms")
public boolean sendSms(String phone,String content,Long messageTemplateId ) {
TaskInfo taskInfo = TaskInfo.builder().receiver(new HashSet<>(Arrays.asList(phone)))
.content(content).messageTemplateId(messageTemplateId).build();
return smsHandler.doHandler(taskInfo);
}
}

@ -0,0 +1,32 @@
package com.java3y.austin.controller;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.java3y.austin.dao.SmsRecordDao;
import com.java3y.austin.domain.MessageTemplate;
import com.java3y.austin.domain.SmsRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
public class SmsRecordController {
@Autowired
private SmsRecordDao smsRecordDao;
/**
* test insert
*/
@GetMapping("/insert")
public String insert(Integer phone) {
return null;
}
/**
* test query
*/
@GetMapping("/query")
public String query() {
return null;
}
}

@ -1,18 +1,29 @@
# HTTP 连接配置 TODO
# HTTP 连接配置 T0D0
ok:
http:
connect-timeout: 30
keep-alive-duration: 300
max-idle-connections: 200
read-timeout: 30
write-timeout: 30
http:
connect-timeout: 30
keep-alive-duration: 300
max-idle-connections: 200
read-timeout: 30
write-timeout: 30
# 腾讯云账号相关的信息配置 TODO
tencent:
sms:
account:
secret-id:
secret-key:
sign_name:
sms-sdk-app-id:
template-id:
sms:
account:
secret-id:
secret-key:
sign_name:
sms-sdk-app-id:
template-id:
# 数据库相关的信息配置 TODO
spring:
datasource:
url:
username:
password:
driver-class-name:
# tomcat / HikariPool(数据库连接池 配置) TODO
Loading…
Cancel
Save