From 5412968209ba2eeb111c76d91464827a6bc62e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B2=90?= Date: Tue, 25 Jun 2024 17:16:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/maku/message/sms/TencentSmsStrategy.java | 77 ---------------------- .../main/java/net/maku/sms/TencentSmsStrategy.java | 77 ++++++++++++++++++++++ 2 files changed, 77 insertions(+), 77 deletions(-) delete mode 100644 maku-boot-module/maku-module-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java create mode 100644 maku-boot-system/src/main/java/net/maku/sms/TencentSmsStrategy.java diff --git a/maku-boot-module/maku-module-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java b/maku-boot-module/maku-module-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java deleted file mode 100644 index 1386b09..0000000 --- a/maku-boot-module/maku-module-message/src/main/java/net/maku/message/sms/TencentSmsStrategy.java +++ /dev/null @@ -1,77 +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.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.ServerException; -import net.maku.message.sms.config.SmsConfig; - -import java.util.Map; - -/** - * 腾讯云短信 - * - * @author 阿沐 babamu@126.com - * MAKU - */ -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 ServerException(sendStatus.getMessage()); - } - } catch (Exception e) { - throw new ServerException(e.getMessage()); - } - } -} diff --git a/maku-boot-system/src/main/java/net/maku/sms/TencentSmsStrategy.java b/maku-boot-system/src/main/java/net/maku/sms/TencentSmsStrategy.java new file mode 100644 index 0000000..c939afb --- /dev/null +++ b/maku-boot-system/src/main/java/net/maku/sms/TencentSmsStrategy.java @@ -0,0 +1,77 @@ +package net.maku.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.ServerException; +import net.maku.sms.config.SmsConfig; + +import java.util.Map; + +/** + * 腾讯云短信 + * + * @author 阿沐 babamu@126.com + * MAKU + */ +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 ServerException(sendStatus.getMessage()); + } + } catch (Exception e) { + throw new ServerException(e.getMessage()); + } + } +}