增加腾讯短信验证码功能

This commit is contained in:
WenG 2022-05-04 15:37:20 +08:00
parent 7e5bb16657
commit 9356c5b983
2 changed files with 30 additions and 19 deletions

View File

@ -29,9 +29,9 @@
<!-- tencent sms start -->
<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>1.0.6</version>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.501</version>
</dependency>
<!-- tencent sms end -->

View File

@ -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("短信发送失败,腾讯短信未实现该功能");
}
}