78 lines
2.7 KiB
Java
78 lines
2.7 KiB
Java
package com.ruoyi.common.utils;
|
|
|
|
import com.tencentcloudapi.common.Credential;
|
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
|
import com.tencentcloudapi.common.profile.HttpProfile;
|
|
import com.tencentcloudapi.cvm.v20170312.CvmClient;
|
|
import com.tencentcloudapi.cvm.v20170312.models.DescribeRegionsRequest;
|
|
import com.tencentcloudapi.cvm.v20170312.models.DescribeRegionsResponse;
|
|
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
|
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
|
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
|
|
import lombok.Data;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.var;
|
|
|
|
import java.util.Objects;
|
|
|
|
@Slf4j
|
|
public class SmsUtils {
|
|
//应用id
|
|
private static final String SDK_APP_ID = "1400854852";
|
|
//API的SecretId
|
|
private static final String SECRET_ID = "AKIDeEf2A8uX1HSainvvnXAc3X9ZlhtyvkMp";
|
|
//API的SecretKey
|
|
private static final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V";
|
|
//签名内容
|
|
private static final String SIGN_NAME = "乙巢智慧仲裁网";
|
|
|
|
public static Boolean sendSms(SendSmsRequest request) {
|
|
Credential cred = new Credential(SECRET_ID, SECRET_KEY );
|
|
|
|
SmsClient client = new SmsClient(cred, "ap-guangzhou");
|
|
|
|
final var req = new com.tencentcloudapi.sms.v20210111.models.SendSmsRequest();
|
|
req.setPhoneNumberSet(new String[]{"+86" + request.getPhone()});
|
|
req.setSmsSdkAppId(SDK_APP_ID );
|
|
req.setSignName(SIGN_NAME);
|
|
req.setTemplateId(request.getTemplateId());
|
|
req.setTemplateParamSet(request.getTemplateParamSet());
|
|
SendSmsResponse res = null;
|
|
try {
|
|
res = client.SendSms(req);
|
|
} catch (TencentCloudSDKException e) {
|
|
log.error("发送短信出错:", e);
|
|
return Boolean.FALSE;
|
|
}
|
|
SendStatus sendStatus = res.getSendStatusSet()[0];
|
|
log.info("发送短信结果:Code={}, Message={}", sendStatus.getCode(), sendStatus.getMessage());
|
|
|
|
if (Objects.nonNull(res.getSendStatusSet()) && res.getSendStatusSet().length > 0 && "Ok".equals(res.getSendStatusSet()[0].getCode())){
|
|
return Boolean.TRUE;
|
|
}
|
|
return Boolean.FALSE;
|
|
}
|
|
/**
|
|
* 参数对象
|
|
*/
|
|
@Data
|
|
public static class SendSmsRequest {
|
|
/**
|
|
* 电话
|
|
*/
|
|
private String phone;
|
|
|
|
/**
|
|
* 模板 ID: 必须填写已审核通过的模板 ID
|
|
*/
|
|
private String templateId;
|
|
|
|
/**
|
|
* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空
|
|
*/
|
|
private String[] templateParamSet;
|
|
|
|
}
|
|
}
|