From 9356c5b9835aa1e015189b364af294d454ec891f Mon Sep 17 00:00:00 2001 From: WenG <450292408@qq.com> Date: Wed, 4 May 2022 15:37:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=85=BE=E8=AE=AF=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E9=AA=8C=E8=AF=81=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module-sms/pom.xml | 6 +-- .../factory/sms/impl/TencentSmsSendImpl.java | 43 ++++++++++++------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/module-sms/pom.xml b/module-sms/pom.xml index 2a5ecab2..72be86ab 100644 --- a/module-sms/pom.xml +++ b/module-sms/pom.xml @@ -29,9 +29,9 @@ - com.github.qcloudsms - qcloudsms - 1.0.6 + com.tencentcloudapi + tencentcloud-sdk-java + 3.1.501 diff --git a/module-sms/src/main/java/ink/wgink/module/sms/factory/sms/impl/TencentSmsSendImpl.java b/module-sms/src/main/java/ink/wgink/module/sms/factory/sms/impl/TencentSmsSendImpl.java index 457da478..944070b8 100644 --- a/module-sms/src/main/java/ink/wgink/module/sms/factory/sms/impl/TencentSmsSendImpl.java +++ b/module-sms/src/main/java/ink/wgink/module/sms/factory/sms/impl/TencentSmsSendImpl.java @@ -1,16 +1,16 @@ package ink.wgink.module.sms.factory.sms.impl; -import com.github.qcloudsms.SmsMultiSender; -import com.github.qcloudsms.SmsMultiSenderResult; -import com.github.qcloudsms.httpclient.HTTPException; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.sms.v20210111.SmsClient; +import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; +import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; import ink.wgink.exceptions.base.SystemException; import ink.wgink.module.sms.factory.sms.ISmsSend; import ink.wgink.properties.sms.SmsTencentProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; - /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 @@ -32,22 +32,33 @@ public class TencentSmsSendImpl implements ISmsSend { @Override public void code(String phone, String code) { - String[] params = {code}; - SmsMultiSender sender = new SmsMultiSender(Integer.parseInt(smsTencentProperties.getAppId()), smsTencentProperties.getAppKey()); try { - SmsMultiSenderResult result = sender.sendWithParam("86", new String[]{phone}, Integer.parseInt(smsTencentProperties.getVerificationCodeTemplateId()), params, smsTencentProperties.getSmsSign(), "", ""); - LOG.debug("Tencent sms result: {}", result); - } catch (HTTPException e) { - LOG.error(e.getMessage(), e); - throw new SystemException("发送验证码失败"); - } catch (IOException e) { - LOG.error(e.getMessage(), e); - throw new SystemException("发送验证码失败"); + Credential credential = new Credential(smsTencentProperties.getSecretId(), smsTencentProperties.getSecretKey()); + SmsClient client = new SmsClient(credential, "ap-guangzhou"); + SendSmsRequest sendSmsRequest = new SendSmsRequest(); + // 设置短信应用ID + sendSmsRequest.setSmsSdkAppId(smsTencentProperties.getSdkAppId()); + // 设置签名 + sendSmsRequest.setSignName(smsTencentProperties.getSignName()); + // 设置短信模板ID + sendSmsRequest.setTemplateId(smsTencentProperties.getVerificationCodeTemplateId()); + // 设置模板参数 + String[] templateParamSet = {code, "2"}; + sendSmsRequest.setTemplateParamSet(templateParamSet); + // 设置接受猴急 + String[] phoneNumberSet = {"+86" + phone}; + sendSmsRequest.setPhoneNumberSet(phoneNumberSet); + SendSmsResponse response = client.SendSms(sendSmsRequest); + // 获取返回结果 + String responseJsonString = SendSmsResponse.toJsonString(response); + LOG.debug("Tencent sms response: {}", responseJsonString); + } catch (TencentCloudSDKException e) { + throw new SystemException(String.format("ErrorCode: %s, Message: %s", e.getErrorCode(), e.getMessage()), e); } } @Override public void content(String phone, String content) { - throw new SystemException("短信发送失败,未配置腾讯短信服务"); + throw new SystemException("短信发送失败,腾讯短信未实现该功能"); } }