|
@@ -0,0 +1,161 @@
|
|
|
|
+package com.boman.web.core.utils;
|
|
|
|
+
|
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
|
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
|
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
|
+import com.aliyuncs.profile.IClientProfile;
|
|
|
|
+import com.boman.common.core.utils.DateUtils;
|
|
|
|
+import com.boman.domain.dto.AjaxResult;
|
|
|
|
+
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * @author tjf
|
|
|
|
+ * @Date: 2021/07/15/10:21
|
|
|
|
+ */
|
|
|
|
+public class SendSmsUtils {
|
|
|
|
+
|
|
|
|
+ * 产品名称:云通信短信API产品,开发者无需替换
|
|
|
|
+ */
|
|
|
|
+ static final String PRODUCT = "Dysmsapi";
|
|
|
|
+
|
|
|
|
+ * 产品域名,开发者无需替换
|
|
|
|
+ */
|
|
|
|
+ static final String DOMAIN = "dysmsapi.aliyuncs.com";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ static final String ACCESS_KEY_ID = "LTAI5tNA2fcBJH6EWRH6Pxr6";
|
|
|
|
+ static final String ACCESS_KEY_SECRET = "5WdaPEOvC3u9LC7pwy2DQ9pgmJvgUr";
|
|
|
|
+ static final String SIGN_NAME = "";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * 发送短信接口
|
|
|
|
+ * @param phone
|
|
|
|
+ * @return
|
|
|
|
+ * @throws ClientException
|
|
|
|
+ */
|
|
|
|
+ public static SendSmsResponse sendSms(String phone,String code ,String templateCode ) throws ClientException {
|
|
|
|
+
|
|
|
|
+ System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
|
|
|
+ System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
|
|
|
+ DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
|
|
|
|
+ IAcsClient acsClient = new DefaultAcsClient(profile);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SendSmsRequest request = new SendSmsRequest();
|
|
|
|
+
|
|
|
|
+ request.setPhoneNumbers(phone);
|
|
|
|
+
|
|
|
|
+ request.setSignName(SIGN_NAME);
|
|
|
|
+
|
|
|
|
+ request.setTemplateCode(templateCode);
|
|
|
|
+
|
|
|
|
+ request.setTemplateParam(code);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return acsClient.getAcsResponse(request);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * 短信查验接口
|
|
|
|
+ * @param bizId
|
|
|
|
+ * @param phone
|
|
|
|
+ * @return
|
|
|
|
+ * @throws ClientException
|
|
|
|
+ */
|
|
|
|
+ public static QuerySendDetailsResponse querySendDetails(String bizId, String phone,String accessKeyId,String accessKeySecret) throws ClientException {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
|
|
|
+ System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
|
|
|
|
+ DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
|
|
|
|
+ IAcsClient acsClient = new DefaultAcsClient(profile);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ QuerySendDetailsRequest request = new QuerySendDetailsRequest();
|
|
|
|
+
|
|
|
|
+ request.setPhoneNumber(phone);
|
|
|
|
+
|
|
|
|
+ request.setBizId(bizId);
|
|
|
|
+
|
|
|
|
+ request.setSendDate(DateUtils.getDateNow());
|
|
|
|
+
|
|
|
|
+ request.setPageSize(10L);
|
|
|
|
+
|
|
|
|
+ request.setCurrentPage(1L);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ QuerySendDetailsResponse querySendDetailsResponse = null;
|
|
|
|
+ try {
|
|
|
|
+ querySendDetailsResponse = acsClient.getAcsResponse(request);
|
|
|
|
+ } catch (ClientException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return querySendDetailsResponse;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static String getCode(Integer num) {
|
|
|
|
+ String[] codes = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
|
|
|
|
+ StringBuilder code = new StringBuilder();
|
|
|
|
+ for (int i = 0; i < num; i++) {
|
|
|
|
+ int j = (int) (Math.random() * 10);
|
|
|
|
+ code.append(codes[j]);
|
|
|
|
+ }
|
|
|
|
+ return code.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * 发送短信并且查验接口
|
|
|
|
+ * @param phone
|
|
|
|
+ * @param code 短信发送替换内容
|
|
|
|
+ * @param templateCode 模板名称
|
|
|
|
+ * @return
|
|
|
|
+ * @throws ClientException
|
|
|
|
+ */
|
|
|
|
+ public static QuerySendDetailsResponse sendSmsAndQuery(String phone,String code ,String templateCode) throws ClientException {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
|
|
|
+ System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
|
|
|
+ DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
|
|
|
|
+ IAcsClient acsClient = new DefaultAcsClient(profile);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SendSmsRequest request = new SendSmsRequest();
|
|
|
|
+
|
|
|
|
+ request.setPhoneNumbers(phone);
|
|
|
|
+
|
|
|
|
+ request.setSignName(SIGN_NAME);
|
|
|
|
+
|
|
|
|
+ request.setTemplateCode(templateCode);
|
|
|
|
+
|
|
|
|
+ request.setTemplateParam(code);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
|
|
|
+ QuerySendDetailsResponse querySendDetailsResponse = SendSmsUtils.querySendDetails(sendSmsResponse.getBizId(), phone, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
|
|
|
+ return querySendDetailsResponse;
|
|
|
|
+ }
|
|
|
|
+}
|