From a40c0e273de44a6115d8479450789ca74dfcf6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B2=90?= Date: Sat, 20 Aug 2022 22:39:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/maku/message/enums/SmsPlatformEnum.java | 2 +- .../net/maku/message/sms/QcloudSmsStrategy.java | 76 ---------------------- .../main/java/net/maku/message/sms/SmsContext.java | 4 +- .../net/maku/message/sms/TencentSmsStrategy.java | 76 ++++++++++++++++++++++ 4 files changed, 79 insertions(+), 79 deletions(-) delete mode 100644 fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/QcloudSmsStrategy.java create mode 100644 fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java diff --git a/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/enums/SmsPlatformEnum.java b/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/enums/SmsPlatformEnum.java index c3d8326..c58ddec 100644 --- a/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/enums/SmsPlatformEnum.java +++ b/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/enums/SmsPlatformEnum.java @@ -18,7 +18,7 @@ public enum SmsPlatformEnum { /** * 腾讯云 */ - QCLOUD(1), + TENCENT(1), /** * 七牛云 */ diff --git a/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/QcloudSmsStrategy.java b/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/QcloudSmsStrategy.java deleted file mode 100644 index 4faffb3..0000000 --- a/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/QcloudSmsStrategy.java +++ /dev/null @@ -1,76 +0,0 @@ -package net.maku.message.sms; - -import cn.hutool.core.map.MapUtil; -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.SendSmsResponse; -import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; -import com.tencentcloudapi.sms.v20210111.models.SendStatus; -import net.maku.framework.common.constant.Constant; -import net.maku.framework.common.exception.FastException; -import net.maku.message.sms.config.SmsConfig; - -import java.util.Map; - -/** - * 腾讯云短信 - * - * @author 阿沐 babamu@126.com - */ -public class QcloudSmsStrategy implements SmsStrategy { - private final SmsConfig smsConfig; - private SmsClient client; - - public QcloudSmsStrategy(SmsConfig smsConfig){ - this.smsConfig = smsConfig; - - try { - HttpProfile httpProfile = new HttpProfile(); - httpProfile.setReqMethod("POST"); - httpProfile.setEndpoint("sms.tencentcloudapi.com"); - - ClientProfile clientProfile = new ClientProfile(); - clientProfile.setHttpProfile(httpProfile); - - Credential cred = new Credential(smsConfig.getAccessKey(), smsConfig.getSecretKey()); - this.client = new SmsClient(cred, "ap-guangzhou", clientProfile); - }catch (Exception e) { - e.printStackTrace(); - } - } - - @Override - public void send(String mobile, Map params) { - SendSmsRequest request = new SendSmsRequest(); - request.setSmsSdkAppId(smsConfig.getAppId()); - request.setSignName(smsConfig.getSignName()); - request.setTemplateId(smsConfig.getTemplateId()); - - // 有参数则设置 - if(MapUtil.isNotEmpty(params)){ - request.setTemplateParamSet(params.values().toArray(new String[0])); - } - - // 手机号 - String[] phoneNumberSet = { "+86" + mobile }; - request.setPhoneNumberSet(phoneNumberSet); - - // 国际、港澳台短信,需要添加SenderId,国内短信填空,默认未开通 - request.setSenderId(smsConfig.getSenderId()); - - try { - // 发送短信 - SendSmsResponse response = client.SendSms(request); - SendStatus sendStatus = response.getSendStatusSet()[0]; - - // 发送失败 - if(!Constant.OK.equalsIgnoreCase(sendStatus.getCode())) { - throw new FastException(sendStatus.getMessage()); - } - } catch (Exception e) { - throw new FastException("短信发送失败:", e); - } - } -} diff --git a/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/SmsContext.java b/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/SmsContext.java index 5ce726a..ba2479f 100644 --- a/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/SmsContext.java +++ b/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/SmsContext.java @@ -17,8 +17,8 @@ public class SmsContext { public SmsContext(SmsConfig config) { if (config.getPlatform() == SmsPlatformEnum.ALIYUN.getValue()) { this.smsStrategy = new AliyunSmsStrategy(config); - } else if (config.getPlatform() == SmsPlatformEnum.QCLOUD.getValue()) { - this.smsStrategy = new QcloudSmsStrategy(config); + } else if (config.getPlatform() == SmsPlatformEnum.TENCENT.getValue()) { + this.smsStrategy = new TencentSmsStrategy(config); } else if (config.getPlatform() == SmsPlatformEnum.QINIU.getValue()) { this.smsStrategy = new QiniuSmsStrategy(config); } else if (config.getPlatform() == SmsPlatformEnum.HUAWEI.getValue()) { diff --git a/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java b/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java new file mode 100644 index 0000000..a91b933 --- /dev/null +++ b/fast-boot-module/fast-boot-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java @@ -0,0 +1,76 @@ +package net.maku.message.sms; + +import cn.hutool.core.map.MapUtil; +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.SendSmsRequest; +import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; +import com.tencentcloudapi.sms.v20210111.models.SendStatus; +import net.maku.framework.common.constant.Constant; +import net.maku.framework.common.exception.FastException; +import net.maku.message.sms.config.SmsConfig; + +import java.util.Map; + +/** + * 腾讯云短信 + * + * @author 阿沐 babamu@126.com + */ +public class TencentSmsStrategy implements SmsStrategy { + private final SmsConfig smsConfig; + private SmsClient client; + + public TencentSmsStrategy(SmsConfig smsConfig) { + this.smsConfig = smsConfig; + + try { + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setReqMethod("POST"); + httpProfile.setEndpoint("sms.tencentcloudapi.com"); + + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + + Credential cred = new Credential(smsConfig.getAccessKey(), smsConfig.getSecretKey()); + this.client = new SmsClient(cred, "ap-guangzhou", clientProfile); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void send(String mobile, Map params) { + SendSmsRequest request = new SendSmsRequest(); + request.setSmsSdkAppId(smsConfig.getAppId()); + request.setSignName(smsConfig.getSignName()); + request.setTemplateId(smsConfig.getTemplateId()); + + // 有参数则设置 + if (MapUtil.isNotEmpty(params)) { + request.setTemplateParamSet(params.values().toArray(new String[0])); + } + + // 手机号 + String[] phoneNumberSet = {"+86" + mobile}; + request.setPhoneNumberSet(phoneNumberSet); + + // 国际、港澳台短信,需要添加SenderId,国内短信填空,默认未开通 + request.setSenderId(smsConfig.getSenderId()); + + try { + // 发送短信 + SendSmsResponse response = client.SendSms(request); + SendStatus sendStatus = response.getSendStatusSet()[0]; + + // 发送失败 + if (!Constant.OK.equalsIgnoreCase(sendStatus.getCode())) { + throw new FastException(sendStatus.getMessage()); + } + } catch (Exception e) { + throw new FastException("短信发送失败:", e); + } + } +}