Skip to content

Commit

Permalink
账号信息读取数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongFuCheng3y committed Aug 24, 2022
1 parent 2af5791 commit 8f31ee9
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public class SendAccountConstant {
/**
* 短信账号code
*/
public static final Integer TENCENT_SMS_CODE = 10;
public static final Integer YUN_PIAN_SMS_CODE = 20;


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.java3y.austin.common.enums;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

/**
* 短信渠道商
* @author 3y
*/
@Getter
@ToString
@AllArgsConstructor
public enum SmsSupplier {


TENCENT(10,"腾讯渠道商"),
YUN_PAIN(20,"云片渠道商");
private Integer code;
private String description;


/**
* 根据状态获取描述信息
* @param code
* @return
*/
public static String getDescriptionByStatus(Integer code) {
for (SmsStatus value : SmsStatus.values()) {
if (value.getCode().equals(code)) {
return value.getDescription();
}
}
return "";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.java3y.austin.cron.handler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
Expand All @@ -9,14 +10,19 @@
import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.DingDingWorkNoticeAccount;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.support.utils.AccountUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* 刷新钉钉的access_token
Expand All @@ -36,7 +42,8 @@ public class RefreshDingDingAccessTokenHandler {
private StringRedisTemplate redisTemplate;

@Autowired
private AccountUtils accountUtils;
private ChannelAccountDao channelAccountDao;


/**
* 每小时请求一次接口刷新(以防失效)
Expand All @@ -45,14 +52,12 @@ public class RefreshDingDingAccessTokenHandler {
public void execute() {
log.info("refreshAccessTokenJob#execute!");
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
for (int index = SendAccountConstant.START; true; index = index + SendAccountConstant.STEP) {
DingDingWorkNoticeAccount account = accountUtils.getAccount(index, SendAccountConstant.DING_DING_WORK_NOTICE_ACCOUNT_KEY, SendAccountConstant.DING_DING_WORK_NOTICE_PREFIX, DingDingWorkNoticeAccount.class);
if (account == null) {
break;
}
List<ChannelAccount> accountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(AustinConstant.FALSE, ChannelType.DING_DING_WORK_NOTICE.getCode());
for (ChannelAccount channelAccount : accountList) {
DingDingWorkNoticeAccount account = JSON.parseObject(channelAccount.getAccountConfig(), DingDingWorkNoticeAccount.class);
String accessToken = getAccessToken(account);
if (StrUtil.isNotBlank(accessToken)) {
redisTemplate.opsForValue().set(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + index, accessToken);
redisTemplate.opsForValue().set(SendAccountConstant.DING_DING_ACCESS_TOKEN_PREFIX + channelAccount.getId(), accessToken);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.GeTuiAccount;
import com.java3y.austin.cron.dto.getui.QueryTokenParamDTO;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.cron.dto.getui.GeTuiTokenResultDTO;
import com.java3y.austin.cron.dto.getui.QueryTokenParamDTO;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.support.utils.AccountUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* 刷新个推的token
Expand All @@ -35,7 +41,8 @@ public class RefreshGeTuiAccessTokenHandler {
private StringRedisTemplate redisTemplate;

@Autowired
private AccountUtils accountUtils;
private ChannelAccountDao channelAccountDao;


/**
* 每小时请求一次接口刷新(以防失效)
Expand All @@ -44,14 +51,14 @@ public class RefreshGeTuiAccessTokenHandler {
public void execute() {
log.info("refreshGeTuiAccessTokenJob#execute!");
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
List<ChannelAccount> accountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(AustinConstant.FALSE, ChannelType.PUSH.getCode());
for (int index = SendAccountConstant.START; true; index = index + SendAccountConstant.STEP) {
GeTuiAccount account = accountUtils.getAccount(index, SendAccountConstant.GE_TUI_ACCOUNT_KEY, SendAccountConstant.GE_TUI_ACCOUNT_PREFIX, GeTuiAccount.class);
if (account == null) {
break;
}
String accessToken = getAccessToken(account);
if (StrUtil.isNotBlank(accessToken)) {
redisTemplate.opsForValue().set(SendAccountConstant.GE_TUI_ACCESS_TOKEN_PREFIX + index, accessToken);
for (ChannelAccount channelAccount : accountList) {
GeTuiAccount account = JSON.parseObject(channelAccount.getAccountConfig(), GeTuiAccount.class);
String accessToken = getAccessToken(account);
if (StrUtil.isNotBlank(accessToken)) {
redisTemplate.opsForValue().set(SendAccountConstant.GE_TUI_ACCESS_TOKEN_PREFIX + index, accessToken);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class SmsParam {
*/
private Set<String> phones;

/**
* 发送渠道账号的Id
*/
private Integer sendAccountId;


/**
* 发送文案
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public boolean handler(TaskInfo taskInfo) {
.phones(taskInfo.getReceiver())
.content(getSmsContent(taskInfo))
.messageTemplateId(taskInfo.getMessageTemplateId())
.sendAccountId(taskInfo.getSendAccount())
.build();

try {
/**
* 1、动态配置做流量负载
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package com.java3y.austin.handler.receipt;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.dto.account.TencentSmsAccount;
import com.java3y.austin.common.enums.SmsStatus;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.support.domain.SmsRecord;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.PullSmsSendStatus;
import com.tencentcloudapi.sms.v20210111.models.PullSmsSendStatusRequest;
import com.tencentcloudapi.sms.v20210111.models.PullSmsSendStatusResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 拉取短信回执信息
* 拉取回执信息
*
* @author 3y
*/
Expand All @@ -34,22 +18,18 @@
public class MessageReceipt {

@Autowired
private TencentSmsReceipt tencentSmsReceipt;

@Autowired
private YunPianSmsReceipt yunPianSmsReceipt;
private SmsPullReceipt smsPullReceipt;

@PostConstruct
private void init() {
SupportThreadPoolConfig.getPendingSingleThreadPool().execute(() -> {
while (true) {

// TODO 回执这里自行打开(免得报错)
// tencentSmsReceipt.pull();
// yunPianSmsReceipt.pull();
// smsPullReceipt.pull();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,95 @@
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.dto.account.TencentSmsAccount;
import com.java3y.austin.common.dto.account.YunPianSmsAccount;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.common.enums.SmsStatus;
import com.java3y.austin.support.config.SupportThreadPoolConfig;
import com.java3y.austin.common.enums.SmsSupplier;
import com.java3y.austin.support.dao.ChannelAccountDao;
import com.java3y.austin.support.dao.SmsRecordDao;
import com.java3y.austin.support.domain.ChannelAccount;
import com.java3y.austin.support.domain.SmsRecord;
import com.java3y.austin.support.utils.AccountUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import com.tencentcloudapi.sms.v20210111.models.PullSmsSendStatus;
import com.tencentcloudapi.sms.v20210111.models.PullSmsSendStatusRequest;
import com.tencentcloudapi.sms.v20210111.models.PullSmsSendStatusResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
* 拉取腾讯云短信回执信息
* 拉取短信回执信息
*
* @author 3y
*/
@Component
@Slf4j
public class TencentSmsReceipt {

public class SmsPullReceipt {

@Autowired
private AccountUtils accountUtils;
private ChannelAccountDao channelAccountDao;

@Autowired
private SmsRecordDao smsRecordDao;


/**
* 拉取消息并入库
* <p>
* eg accountList:[{"sms_10":{"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"234234","secretKey":"234324324","smsSdkAppId":"2343242","templateId":"234234","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云"}},{"sms_20":{"url":"https://sms.yunpian.com/v2/sms/tpl_batch_send.json","apikey":"23423432","tpl_id":"23423432","supplierId":20,"supplierName":"云片"}}]
*/
public void pull() {
List<ChannelAccount> channelAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(AustinConstant.FALSE, ChannelType.SMS.getCode());
for (ChannelAccount channelAccount : channelAccountList) {
Integer supplierId = JSON.parseObject(channelAccount.getAccountConfig()).getInteger("supplierId");
if (SmsSupplier.TENCENT.getCode().equals(supplierId)) {
TencentSmsAccount tencentSmsAccount = JSON.parseObject(channelAccount.getAccountConfig(), TencentSmsAccount.class);
pullTencent(tencentSmsAccount);
} else if (SmsSupplier.YUN_PAIN.getCode().equals(supplierId)) {
YunPianSmsAccount yunPianSmsAccount = JSON.parseObject(channelAccount.getAccountConfig(), YunPianSmsAccount.class);
pullYunPain(yunPianSmsAccount);
}
}
}

// 获取腾讯云账号信息
TencentSmsAccount account = accountUtils.getAccount(10, SendAccountConstant.SMS_ACCOUNT_KEY, SendAccountConstant.SMS_PREFIX, TencentSmsAccount.class);
/**
* 拉取腾讯的回执
*
* @param account
*/
private void pullTencent(TencentSmsAccount account) {
try {
SmsClient client = getSmsClient(account);
/**
* 初始化客户端
*/
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(account.getUrl());
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(new Credential(account.getSecretId(), account.getSecretKey()), account.getRegion(), clientProfile);

// 每次拉取10条
/**
* 每次拉取10条
*/
PullSmsSendStatusRequest req = new PullSmsSendStatusRequest();
req.setLimit(10L);
req.setSmsSdkAppId(account.getSmsSdkAppId());

/**
* 拉取回执后入库
*/
PullSmsSendStatusResponse resp = client.PullSmsSendStatus(req);
List<SmsRecord> smsRecordList = new ArrayList<>();
if (resp != null && resp.getPullSmsSendStatusSet() != null && resp.getPullSmsSendStatusSet().length > 0) {
log.debug("receipt sms:{}", JSON.toJSONString(resp.getPullSmsSendStatusSet()));
for (PullSmsSendStatus pullSmsSendStatus : resp.getPullSmsSendStatusSet()) {
SmsRecord smsRecord = SmsRecord.builder()
.sendDate(Integer.valueOf(DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN)))
Expand All @@ -89,20 +120,7 @@ public void pull() {
}
}

/**
* 构造smsClient
*
* @param account
* @return
*/
private SmsClient getSmsClient(TencentSmsAccount account) {
Credential cred = new Credential(account.getSecretId(), account.getSecretKey());
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(account.getUrl());
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
return new SmsClient(cred, account.getRegion(), clientProfile);
}

private void pullYunPain(YunPianSmsAccount yunPianSmsAccount) {

}
}
Loading

0 comments on commit 8f31ee9

Please sign in to comment.