|
|
@@ -0,0 +1,102 @@
|
|
|
+package common.utils.jdcloud;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.jdcloud.sdk.auth.CredentialsProvider;
|
|
|
+import com.jdcloud.sdk.auth.StaticCredentialsProvider;
|
|
|
+import com.jdcloud.sdk.http.HttpRequestConfig;
|
|
|
+import com.jdcloud.sdk.http.Protocol;
|
|
|
+import com.jdcloud.sdk.service.sms.client.SmsClient;
|
|
|
+import com.jdcloud.sdk.service.sms.model.*;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class SMS {
|
|
|
+ private static SmsClient smsClient;
|
|
|
+ // 地域信息不用修改
|
|
|
+ private static final String REGION = "cn-north-1";
|
|
|
+
|
|
|
+ public static void init() {
|
|
|
+ // 请填写AccessKey ID
|
|
|
+ String accessKeyId = System.getenv("JD_ACCESS_KEY");
|
|
|
+ // 请填写AccessKey Secret
|
|
|
+ String secretAccessKey = System.getenv("JD_SECRET_KEY");
|
|
|
+ CredentialsProvider credentialsProvider = new StaticCredentialsProvider(accessKeyId, secretAccessKey);
|
|
|
+ smsClient = SmsClient.builder()
|
|
|
+ .credentialsProvider(credentialsProvider)
|
|
|
+ .httpRequestConfig(new HttpRequestConfig.Builder().protocol(Protocol.HTTP).build()) //默认为HTTPS
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发短信
|
|
|
+ */
|
|
|
+ public static String sendVerifyCodeByMobileNumber(String mobileNumber, String verifyCode) {
|
|
|
+ if (SMS.smsClient == null) {
|
|
|
+ SMS.init();
|
|
|
+ }
|
|
|
+
|
|
|
+ BatchSendRequest request = new BatchSendRequest();
|
|
|
+ request.setRegionId(REGION);
|
|
|
+ // 设置模板ID 应用管理-文本短信-短信模板 页面可以查看模板ID
|
|
|
+ request.setTemplateId(System.getenv("JD_TEMPLATE_ID"));
|
|
|
+ // 设置签名ID 应用管理-文本短信-短信签名 页面可以查看签名ID
|
|
|
+ request.setSignId(System.getenv("JD_SIGN_ID"));
|
|
|
+ // 设置下发手机号list
|
|
|
+ List<String> phoneList = new ArrayList<>();
|
|
|
+ phoneList.add(mobileNumber);
|
|
|
+ request.setPhoneList(phoneList);
|
|
|
+ // 设置模板参数,非必传,如果模板中包含变量请填写对应参数,否则变量信息将不做替换。
|
|
|
+ List<String> params = new ArrayList<>();
|
|
|
+ params.add(verifyCode);
|
|
|
+ request.setParams(params);
|
|
|
+ BatchSendResponse response = smsClient.batchSend(request);
|
|
|
+
|
|
|
+ return new Gson().toJson(response);
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 短信发送回执接口
|
|
|
+// */
|
|
|
+// public static void testStatusReport() {
|
|
|
+// StatusReportRequest statusReportRequest = new StatusReportRequest();
|
|
|
+// statusReportRequest.setRegionId(REGION);
|
|
|
+// // 设置序列号
|
|
|
+// // 序列号从下发接口response中获取。response.getResult().getData().getSequenceNumber();
|
|
|
+// statusReportRequest.setSequenceNumber("");
|
|
|
+// // 设置要查询的手机号列表
|
|
|
+// List<String> phoneList = new ArrayList<>();
|
|
|
+// phoneList.add("");
|
|
|
+// statusReportRequest.setPhoneList(phoneList);
|
|
|
+// StatusReportResponse response = smsClient.statusReport(statusReportRequest);
|
|
|
+// System.out.println(new Gson().toJson(response));
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 短信回复接口
|
|
|
+// */
|
|
|
+// public static void testReply() {
|
|
|
+// ReplyRequest replyRequest = new ReplyRequest();
|
|
|
+// replyRequest.setRegionId(REGION);
|
|
|
+// // 设置应用ID 应用管理-文本短信-概览 页面可以查看应用ID
|
|
|
+// replyRequest.setAppId("");
|
|
|
+// // 设置查询日期 时间格式:2019-09-01
|
|
|
+// replyRequest.setDataDate("");
|
|
|
+// // 设置要查询的手机号列表 非必传
|
|
|
+// List<String> phoneList = new ArrayList<>();
|
|
|
+// phoneList.add("");
|
|
|
+// replyRequest.setPhoneList(phoneList);
|
|
|
+// ReplyResponse response = smsClient.reply(replyRequest);
|
|
|
+// System.out.println(new Gson().toJson(response));
|
|
|
+// }
|
|
|
+
|
|
|
+ public static void main(String[ ] args) {
|
|
|
+ // 初始化client
|
|
|
+ init();
|
|
|
+ System.out.println(sendVerifyCodeByMobileNumber("177****####", "3213"));
|
|
|
+ /*
|
|
|
+ // testStatusReport();
|
|
|
+ // testReply();
|
|
|
+ */
|
|
|
+ }
|
|
|
+}
|