|
@@ -3,6 +3,10 @@ package com.cyksj.common.util;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.Method;
|
|
import cn.hutool.http.Method;
|
|
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.tencentcloudapi.common.Credential;
|
|
import com.tencentcloudapi.common.Credential;
|
|
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
|
@@ -13,6 +17,8 @@ import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @author chan
|
|
* @author chan
|
|
|
* @date 2022/10/13 11:29 AM
|
|
* @date 2022/10/13 11:29 AM
|
|
@@ -33,12 +39,42 @@ public class SmsUtil {
|
|
|
|
|
|
|
|
private static final String TENCENT_SMS_TEMPLATE_ID = "1705927";
|
|
private static final String TENCENT_SMS_TEMPLATE_ID = "1705927";
|
|
|
|
|
|
|
|
|
|
+ private static final String ALIYUN_SMS_TEMPLATE_ID = "X";
|
|
|
|
|
+
|
|
|
|
|
+ private static String ALIYUN_SMS_ACCESS_KEY_ID = "X";
|
|
|
|
|
+
|
|
|
|
|
+ private static String ALIYUN_SMS_ACCESS_KEY_SECRET = "X";
|
|
|
|
|
+
|
|
|
|
|
+ private static String ALIYUN_SMS_SIGN_NAME = "崇宇信息科技";
|
|
|
|
|
+
|
|
|
private static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
|
|
private static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
|
|
|
|
|
|
|
|
|
|
+ private static final Integer initial;
|
|
|
|
|
+
|
|
|
|
|
+ private static final AtomicInteger INDEX;
|
|
|
|
|
+
|
|
|
|
|
+ static {
|
|
|
|
|
+ initial = 2;
|
|
|
|
|
+ INDEX = new AtomicInteger(0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
|
send("15720916464", "尊敬的用户,您的验证码为123456,此验证码仅用于银河录像。【银河录像局】");
|
|
send("15720916464", "尊敬的用户,您的验证码为123456,此验证码仅用于银河录像。【银河录像局】");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static void sendSms(String mobile, String msg) {
|
|
|
|
|
+ int i = INDEX.getAndIncrement();
|
|
|
|
|
+ if (i >= initial) {
|
|
|
|
|
+ i = 0;
|
|
|
|
|
+ INDEX.set(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (i == 0) {
|
|
|
|
|
+ System.out.println("xx1");
|
|
|
|
|
+ } else if (i == 1) {
|
|
|
|
|
+ System.out.println("xxx2");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static void send(String mobile, String msg) {
|
|
public static void send(String mobile, String msg) {
|
|
|
HttpRequest request = new HttpRequest(URL);
|
|
HttpRequest request = new HttpRequest(URL);
|
|
|
request.setMethod(Method.POST);
|
|
request.setMethod(Method.POST);
|
|
@@ -64,8 +100,8 @@ public class SmsUtil {
|
|
|
* 如有需要请在代码中查阅以获取最新的默认值 */
|
|
* 如有需要请在代码中查阅以获取最新的默认值 */
|
|
|
httpProfile.setConnTimeout(60);
|
|
httpProfile.setConnTimeout(60);
|
|
|
/* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com
|
|
/* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com
|
|
|
- * 南京地域
|
|
|
|
|
- * */
|
|
|
|
|
|
|
+ * 南京地域
|
|
|
|
|
+ * */
|
|
|
httpProfile.setEndpoint("sms.ap-nanjing.tencentcloudapi.com");
|
|
httpProfile.setEndpoint("sms.ap-nanjing.tencentcloudapi.com");
|
|
|
|
|
|
|
|
/* 非必要步骤:
|
|
/* 非必要步骤:
|
|
@@ -126,6 +162,34 @@ public class SmsUtil {
|
|
|
|
|
|
|
|
} catch (TencentCloudSDKException e) {
|
|
} catch (TencentCloudSDKException e) {
|
|
|
log.error("发送腾讯云短信错误,e:{}", e);
|
|
log.error("发送腾讯云短信错误,e:{}", e);
|
|
|
|
|
+ //发送失败使用阿里云短信
|
|
|
|
|
+ sendAliyun(mobile, msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param mobile 手机号
|
|
|
|
|
+ * @param code 验证码
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void sendAliyun(String mobile, String code) {
|
|
|
|
|
+
|
|
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ALIYUN_SMS_ACCESS_KEY_ID, ALIYUN_SMS_ACCESS_KEY_SECRET);
|
|
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
|
+
|
|
|
|
|
+ com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest request = new com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest();
|
|
|
|
|
+ request.setPhoneNumbers(mobile);//接收短信的手机号码
|
|
|
|
|
+ request.setSignName(ALIYUN_SMS_SIGN_NAME);//短信签名名称
|
|
|
|
|
+ request.setTemplateCode(ALIYUN_SMS_TEMPLATE_ID);//短信模板CODE
|
|
|
|
|
+ request.setTemplateParam(String.format("{\"code\": \"%s\"}", code));//短信模板变量对应的实际值
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse response = client.getAcsResponse(request);
|
|
|
|
|
+ System.out.println(new Gson().toJson(response));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送阿里云短信错误,e:{}", e);
|
|
|
|
|
+ //发送失败使用腾讯云短信
|
|
|
|
|
+ sendTencent(mobile, code);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|