增加腾讯短信验证码功能

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 --> <!-- tencent sms start -->
<dependency> <dependency>
<groupId>com.github.qcloudsms</groupId> <groupId>com.tencentcloudapi</groupId>
<artifactId>qcloudsms</artifactId> <artifactId>tencentcloud-sdk-java</artifactId>
<version>1.0.6</version> <version>3.1.501</version>
</dependency> </dependency>
<!-- tencent sms end --> <!-- tencent sms end -->

View File

@ -1,16 +1,16 @@
package ink.wgink.module.sms.factory.sms.impl; package ink.wgink.module.sms.factory.sms.impl;
import com.github.qcloudsms.SmsMultiSender; import com.tencentcloudapi.common.Credential;
import com.github.qcloudsms.SmsMultiSenderResult; import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.github.qcloudsms.httpclient.HTTPException; 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.exceptions.base.SystemException;
import ink.wgink.module.sms.factory.sms.ISmsSend; import ink.wgink.module.sms.factory.sms.ISmsSend;
import ink.wgink.properties.sms.SmsTencentProperties; import ink.wgink.properties.sms.SmsTencentProperties;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
/** /**
* When you feel like quitting. Think about why you started * When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始 * 当你想要放弃的时候想想当初你为何开始
@ -32,22 +32,33 @@ public class TencentSmsSendImpl implements ISmsSend {
@Override @Override
public void code(String phone, String code) { public void code(String phone, String code) {
String[] params = {code};
SmsMultiSender sender = new SmsMultiSender(Integer.parseInt(smsTencentProperties.getAppId()), smsTencentProperties.getAppKey());
try { try {
SmsMultiSenderResult result = sender.sendWithParam("86", new String[]{phone}, Integer.parseInt(smsTencentProperties.getVerificationCodeTemplateId()), params, smsTencentProperties.getSmsSign(), "", ""); Credential credential = new Credential(smsTencentProperties.getSecretId(), smsTencentProperties.getSecretKey());
LOG.debug("Tencent sms result: {}", result); SmsClient client = new SmsClient(credential, "ap-guangzhou");
} catch (HTTPException e) { SendSmsRequest sendSmsRequest = new SendSmsRequest();
LOG.error(e.getMessage(), e); // 设置短信应用ID
throw new SystemException("发送验证码失败"); sendSmsRequest.setSmsSdkAppId(smsTencentProperties.getSdkAppId());
} catch (IOException e) { // 设置签名
LOG.error(e.getMessage(), e); sendSmsRequest.setSignName(smsTencentProperties.getSignName());
throw new SystemException("发送验证码失败"); // 设置短信模板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 @Override
public void content(String phone, String content) { public void content(String phone, String content) {
throw new SystemException("短信发送失败,未配置腾讯短信服务"); throw new SystemException("短信发送失败,腾讯短信未实现该功能");
} }
} }