diff --git a/austin-handler/src/main/java/com/java3y/austin/domain/SmsParam.java b/austin-handler/src/main/java/com/java3y/austin/domain/SmsParam.java index bc5a10a..3e4e70b 100644 --- a/austin-handler/src/main/java/com/java3y/austin/domain/SmsParam.java +++ b/austin-handler/src/main/java/com/java3y/austin/domain/SmsParam.java @@ -30,12 +30,8 @@ public class SmsParam { private String content; /** - * 渠道商Id + * 发送账号 */ - private Integer supplierId; + private Integer sendAccount; - /** - * 渠道商名字 - */ - private String supplierName; } diff --git a/austin-handler/src/main/java/com/java3y/austin/domain/TencentSmsParam.java b/austin-handler/src/main/java/com/java3y/austin/domain/TencentSmsParam.java new file mode 100644 index 0000000..b208b6b --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/domain/TencentSmsParam.java @@ -0,0 +1,47 @@ +package com.java3y.austin.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 腾讯短信参数 + * + * 参数示例: + * [{"sms_10":{"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"AKIDhDUUDfffffMEqBF1WljQq","secretKey":"B4h39yWnfffff7D2btue7JErDJ8gxyi","smsSdkAppId":"140025","templateId":"11897","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云"}}] + * + * @author 3y + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class TencentSmsParam { + + /** + * api相关 + */ + private String url; + private String region ; + + /** + * 账号相关 + */ + private String secretId; + private String secretKey; + private String smsSdkAppId; + private String templateId; + private String signName; + + /** + * 标识渠道商Id + */ + private Integer supplierId; + + /** + * 标识渠道商名字 + */ + private String supplierName; + +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/EmailHandler.java b/austin-handler/src/main/java/com/java3y/austin/handler/EmailHandler.java index ae50dfe..f612b42 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/EmailHandler.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/EmailHandler.java @@ -1,18 +1,17 @@ package com.java3y.austin.handler; -import cn.hutool.extra.mail.Mail; + import cn.hutool.extra.mail.MailAccount; import cn.hutool.extra.mail.MailUtil; + import com.google.common.base.Throwables; -import com.java3y.austin.domain.AnchorInfo; import com.java3y.austin.domain.TaskInfo; -import com.java3y.austin.dto.ContentModel; import com.java3y.austin.dto.EmailContentModel; -import com.java3y.austin.enums.AnchorState; import com.java3y.austin.enums.ChannelType; -import com.java3y.austin.utils.LogUtils; +import com.java3y.austin.utils.AccountUtils; import com.sun.mail.util.MailSSLSocketFactory; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -24,6 +23,12 @@ import org.springframework.stereotype.Component; @Slf4j public class EmailHandler extends Handler { + private static final String EMAIL_ACCOUNT_KEY = "emailAccount"; + private static final String PREFIX = "email_"; + + @Autowired + private AccountUtils accountUtils; + public EmailHandler() { channelCode = ChannelType.EMAIL.getCode(); } @@ -31,7 +36,7 @@ public class EmailHandler extends Handler { @Override public boolean handler(TaskInfo taskInfo) { EmailContentModel emailContentModel = (EmailContentModel) taskInfo.getContentModel(); - MailAccount account = getAccount(); + MailAccount account = getAccountConfig(taskInfo.getSendAccount()); try { MailUtil.send(account, taskInfo.getReceiver(), emailContentModel.getTitle(), emailContentModel.getContent(), true, null); @@ -42,27 +47,22 @@ public class EmailHandler extends Handler { return true; } - - /** - * 获取账号信息 + * 获取账号信息合配置 + * * @return */ - private MailAccount getAccount() { - MailAccount account = new MailAccount(); + private MailAccount getAccountConfig(Integer sendAccount) { + MailAccount account = accountUtils.getAccount(sendAccount, EMAIL_ACCOUNT_KEY, PREFIX, new MailAccount()); try { - account.setHost("smtp.qq.com").setPort(465); - account.setUser("403686131@qq.com").setPass("//TODO").setAuth(true); - account.setFrom("403686131@qq.com"); - MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); - account.setStarttlsEnable(true).setSslEnable(true).setCustomProperty("mail.smtp.ssl.socketFactory", sf); - + account.setAuth(true).setStarttlsEnable(true).setSslEnable(true).setCustomProperty("mail.smtp.ssl.socketFactory", sf); account.setTimeout(25000).setConnectionTimeout(25000); } catch (Exception e) { log.error("EmailHandler#getAccount fail!{}", Throwables.getStackTraceAsString(e)); } return account; } + } diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java b/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java index 2d76ff9..88c118c 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java @@ -5,15 +5,12 @@ import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.google.common.base.Throwables; import com.java3y.austin.dao.SmsRecordDao; -import com.java3y.austin.domain.AnchorInfo; import com.java3y.austin.domain.SmsParam; import com.java3y.austin.domain.SmsRecord; import com.java3y.austin.domain.TaskInfo; import com.java3y.austin.dto.SmsContentModel; -import com.java3y.austin.enums.AnchorState; import com.java3y.austin.enums.ChannelType; import com.java3y.austin.script.SmsScript; -import com.java3y.austin.utils.LogUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -46,8 +43,8 @@ public class SmsHandler extends Handler { .phones(taskInfo.getReceiver()) .content(getSmsContent(taskInfo)) .messageTemplateId(taskInfo.getMessageTemplateId()) - .supplierId(10) - .supplierName("腾讯云通知类消息渠道").build(); + .sendAccount(taskInfo.getSendAccount()) + .build(); try { List recordList = smsScript.send(smsParam); if (!CollUtil.isEmpty(recordList)) { diff --git a/austin-handler/src/main/java/com/java3y/austin/script/SmsScript.java b/austin-handler/src/main/java/com/java3y/austin/script/SmsScript.java index 0df8244..cf8ca09 100644 --- a/austin-handler/src/main/java/com/java3y/austin/script/SmsScript.java +++ b/austin-handler/src/main/java/com/java3y/austin/script/SmsScript.java @@ -1,8 +1,7 @@ package com.java3y.austin.script; -import com.java3y.austin.domain.SmsRecord; import com.java3y.austin.domain.SmsParam; -import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.java3y.austin.domain.SmsRecord; import java.util.List; @@ -15,7 +14,8 @@ public interface SmsScript { /** * 发送短信 - * @param smsParam 发送短信参数 + * + * @param smsParam * @return 渠道商接口返回值 */ List send(SmsParam smsParam) throws Exception; diff --git a/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java b/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java index 5f27282..b0ed94c 100644 --- a/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java +++ b/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java @@ -3,14 +3,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 com.alibaba.fastjson.JSON; -import com.google.common.base.Throwables; import com.java3y.austin.constant.AustinConstant; import com.java3y.austin.domain.SmsParam; import com.java3y.austin.domain.SmsRecord; +import com.java3y.austin.domain.TencentSmsParam; import com.java3y.austin.enums.SmsStatus; +import com.java3y.austin.utils.AccountUtils; import com.tencentcloudapi.common.Credential; -import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.sms.v20210111.SmsClient; @@ -18,7 +17,7 @@ import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; import com.tencentcloudapi.sms.v20210111.models.SendStatus; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -37,42 +36,24 @@ import java.util.List; public class TencentSmsScript implements SmsScript { private static final Integer PHONE_NUM = 11; + private static final String SMS_ACCOUNT_KEY = "smsAccount"; + private static final String PREFIX = "sms_"; - /** - * api相关 - */ - private static final String URL = "sms.tencentcloudapi.com"; - private static final String REGION = "ap-guangzhou"; - - /** - * 账号相关 - */ - @Value("${tencent.sms.account.secret-id}") - private String SECRET_ID; - - @Value("${tencent.sms.account.secret-key}") - private String SECRET_KEY; + @Autowired + private AccountUtils accountUtils; - @Value("${tencent.sms.account.sms-sdk-app-id}") - private String SMS_SDK_APP_ID; - - @Value("${tencent.sms.account.template-id}") - private String TEMPLATE_ID; - - @Value("${tencent.sms.account.sign_name}") - private String SIGN_NAME; @Override - public List send(SmsParam smsParam) throws TencentCloudSDKException { - - SmsClient client = init(); - SendSmsRequest request = assembleReq(smsParam); - SendSmsResponse response = client.SendSms(request); - return assembleSmsRecord(smsParam,response); - + public List send(SmsParam smsParam) throws Exception { + TencentSmsParam tencentSmsParam = accountUtils.getAccount(smsParam.getSendAccount(), SMS_ACCOUNT_KEY, PREFIX, TencentSmsParam.builder().build()); + SmsClient client = init(tencentSmsParam); + SendSmsRequest request = assembleReq(smsParam,tencentSmsParam); + SendSmsResponse response = client.SendSms(request); + return assembleSmsRecord(smsParam, response,tencentSmsParam); } - private List assembleSmsRecord(SmsParam smsParam, SendSmsResponse response) { + + private List assembleSmsRecord(SmsParam smsParam, SendSmsResponse response, TencentSmsParam tencentSmsParam) { if (response == null || ArrayUtil.isEmpty(response.getSendStatusSet())) { return null; } @@ -88,8 +69,8 @@ public class TencentSmsScript implements SmsScript { .sendDate(Integer.valueOf(DateUtil.format(new Date(), AustinConstant.YYYYMMDD))) .messageTemplateId(smsParam.getMessageTemplateId()) .phone(Long.valueOf(phone)) - .supplierId(smsParam.getSupplierId()) - .supplierName(smsParam.getSupplierName()) + .supplierId(tencentSmsParam.getSupplierId()) + .supplierName(tencentSmsParam.getSupplierName()) .seriesId(sendStatus.getSerialNo()) .chargingNum(Math.toIntExact(sendStatus.getFee())) .status(SmsStatus.SEND_SUCCESS.getCode()) @@ -106,13 +87,13 @@ public class TencentSmsScript implements SmsScript { /** * 组装发送短信参数 */ - private SendSmsRequest assembleReq(SmsParam smsParam) { + private SendSmsRequest assembleReq(SmsParam smsParam, TencentSmsParam account) { 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); + req.setSmsSdkAppId(account.getSmsSdkAppId()); + req.setSignName(account.getSignName()); + req.setTemplateId(account.getTemplateId()); String[] templateParamSet1 = {smsParam.getContent()}; req.setTemplateParamSet(templateParamSet1); req.setSessionContext(IdUtil.fastSimpleUUID()); @@ -121,14 +102,15 @@ public class TencentSmsScript implements SmsScript { /** * 初始化 client + * @param account */ - private SmsClient init() { - Credential cred = new Credential(SECRET_ID, SECRET_KEY); + private SmsClient init(TencentSmsParam account) { + Credential cred = new Credential(account.getSecretId(), account.getSecretKey()); HttpProfile httpProfile = new HttpProfile(); - httpProfile.setEndpoint(URL); + httpProfile.setEndpoint(account.getUrl()); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); - SmsClient client = new SmsClient(cred, REGION, clientProfile); + SmsClient client = new SmsClient(cred, account.getUrl(), clientProfile); return client; } diff --git a/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java b/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java index b003922..2f6641b 100644 --- a/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java +++ b/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java @@ -41,6 +41,7 @@ public class DeduplicationRuleService { private Config config; public void duplication(TaskInfo taskInfo) { + // 配置示例:{"contentDeduplication":{"num":1,"time":300},"frequencyDeduplication":{"num":5}} JSONObject property = JSON.parseObject(config.getProperty(DEDUPLICATION_RULE_KEY, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_OBJECT)); JSONObject contentDeduplication = property.getJSONObject(CONTENT_DEDUPLICATION); JSONObject frequencyDeduplication = property.getJSONObject(FREQUENCY_DEDUPLICATION); diff --git a/austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java b/austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java index ebfa541..5eb7cef 100644 --- a/austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java +++ b/austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java @@ -28,8 +28,10 @@ public class DiscardMessageService { * @return */ public boolean isDiscard(TaskInfo taskInfo) { + // 配置示例: ["1","2"] JSONArray array = JSON.parseArray(config.getProperty(DISCARD_MESSAGE_KEY, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY)); + if (array.contains(String.valueOf(taskInfo.getMessageTemplateId()))) { LogUtils.print(AnchorInfo.builder().businessId(taskInfo.getBusinessId()).ids(taskInfo.getReceiver()).state(AnchorState.DISCARD.getCode()).build()); return true; diff --git a/austin-service-api/src/main/java/com/java3y/austin/domain/SendRequest.java b/austin-service-api/src/main/java/com/java3y/austin/domain/SendRequest.java index 6d87e0c..ff55c6f 100644 --- a/austin-service-api/src/main/java/com/java3y/austin/domain/SendRequest.java +++ b/austin-service-api/src/main/java/com/java3y/austin/domain/SendRequest.java @@ -1,7 +1,9 @@ package com.java3y.austin.domain; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.experimental.Accessors; /** @@ -10,6 +12,8 @@ import lombok.experimental.Accessors; */ @Data @Accessors(chain = true) +@AllArgsConstructor +@NoArgsConstructor public class SendRequest { /** diff --git a/austin-support/src/main/java/com/java3y/austin/utils/AccountUtils.java b/austin-support/src/main/java/com/java3y/austin/utils/AccountUtils.java new file mode 100644 index 0000000..7f7afa2 --- /dev/null +++ b/austin-support/src/main/java/com/java3y/austin/utils/AccountUtils.java @@ -0,0 +1,40 @@ +package com.java3y.austin.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; +import com.java3y.austin.constant.AustinConstant; +import org.springframework.stereotype.Component; + +/** + * 获取账号信息工具类 + * + * @author 3y + */ +@Component + +public class AccountUtils { + + @ApolloConfig("boss.austin") + private Config config; + + /** + * 短信参数示例:[{"sms_10":{"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"AKIDhDUUDfffffMEqBF1WljQq","secretKey":"B4h39yWnfffff7D2btue7JErDJ8gxyi","smsSdkAppId":"140025","templateId":"11897","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云"}}] + * 邮件参数示例:[{"email_10":{"host":"smtp.qq.com","port":465,"user":"403686131@qq.com","pass":"","from":"403686131@qq.com"}}] + */ + public T getAccount(Integer sendAccount, String apolloKey, String prefix, T t) { + String accountValues = config.getProperty(apolloKey, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY); + JSONArray jsonArray = JSON.parseArray(accountValues); + for (int i = 0; i < jsonArray.size(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + Object object = jsonObject.getObject(prefix + sendAccount, t.getClass()); + if (object != null) { + return (T) object; + } + } + return null; + } + +} diff --git a/austin-web/src/main/java/com/java3y/austin/controller/SendController.java b/austin-web/src/main/java/com/java3y/austin/controller/SendController.java index d03880a..a40c72f 100644 --- a/austin-web/src/main/java/com/java3y/austin/controller/SendController.java +++ b/austin-web/src/main/java/com/java3y/austin/controller/SendController.java @@ -1,17 +1,13 @@ package com.java3y.austin.controller; -import com.java3y.austin.domain.MessageParam; import com.java3y.austin.domain.SendRequest; import com.java3y.austin.domain.SendResponse; -import com.java3y.austin.enums.BusinessCode; import com.java3y.austin.service.SendService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; -import java.util.HashMap; -import java.util.Map; - /** * @author 三歪 */ @@ -22,35 +18,14 @@ public class SendController { @Autowired private SendService sendService; + /** - * 发送 + * 发送消息接口 + * * @return */ - @GetMapping("/sendSmsTest") - public SendResponse sendSmsTest(String receiver, Long templateId) { - - /** - * - * messageTemplate Id 为1 的模板内容(普通短信) - * {"auditStatus":10,"auditor":"yyyyyyz","created":1636978066,"creator":"yyyyc","deduplicationTime":1,"expectPushTime":"0","flowId":"yyyy","id":1,"idType":30,"isDeleted":0,"isNightShield":0,"msgContent":"{\"content\":\"{$contentValue}\"}","msgStatus":10,"msgType":10,"name":"test短信","proposer":"yyyy22","sendAccount":66,"sendChannel":30,"team":"yyyt","templateType":10,"updated":1636978066,"updator":"yyyyu"} - * - * messageTemplate Id 为2 的模板内容(营销短信) - * {"auditStatus":10,"auditor":"yyyyyyz","created":1636978066,"creator":"yyyyc","deduplicationTime":1,"expectPushTime":"0","flowId":"yyyy","id":1,"idType":30,"isDeleted":0,"isNightShield":0,"msgContent":"{\"content\":\"{$contentValue}\"}","msgStatus":10,"msgType":20,"name":"test短信","proposer":"yyyy22","sendAccount":66,"sendChannel":30,"team":"yyyt","templateType":10,"updated":1636978066,"updator":"yyyyu"} - */ - // 文案参数 - Map variables = new HashMap<>(8); - variables.put("contentValue", "6666" + System.currentTimeMillis()); - variables.put("title", "yyyyyy"); - MessageParam messageParam = new MessageParam().setReceiver(receiver).setVariables(variables); - - - SendRequest sendRequest = new SendRequest().setCode(BusinessCode.COMMON_SEND.getCode()) - .setMessageTemplateId(templateId) - .setMessageParam(messageParam); - + @PostMapping("/send") + public SendResponse send(@RequestBody SendRequest sendRequest) { return sendService.send(sendRequest); - } - - } diff --git a/austin-web/src/main/resources/application.yml b/austin-web/src/main/resources/application.yml index 5532f54..ee48d7e 100644 --- a/austin-web/src/main/resources/application.yml +++ b/austin-web/src/main/resources/application.yml @@ -7,15 +7,6 @@ ok: read-timeout: 30 write-timeout: 30 -# 腾讯云账号相关的信息配置 TODO -tencent: - sms: - account: - secret-id: - secret-key: - sign_name: - sms-sdk-app-id: - template-id: # 数据库相关的信息配置 TODO spring: @@ -48,7 +39,7 @@ spring: # 消息topicName TODO austin: topic: - name: austin + name: austinTopic # 监控配置 TODO management: @@ -75,5 +66,4 @@ apollo: bootstrap: enabled: true namespaces: boss.austin - # tomcat / HikariPool(数据库连接池 配置) TODO \ No newline at end of file