package com.ruoyi.common.utils;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.ruoyi.common.core.exception.ServiceException;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import com.aliyun.dysmsapi20170525.Client;
|
import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
|
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
import com.aliyun.teaopenapi.models.Config;
|
|
import java.util.*;
|
|
|
@Slf4j
|
public class AliSmsUtil {
|
private static final String ACCESS_KEY_ID = "LTAI5tHYSpwifc3rqLYJoETo";
|
private static final String ACCESS_KEY_SECRET = "xrDyQ89h8P0alWW7rrLIW2D2rt7Eig";
|
private static final String SIGN_NAME = "一证成";
|
// 模板
|
private static final String TEMPLATE_CODE = "SMS_489660108";
|
|
|
public static void sendSuccessMessage(String phone,String code) {
|
Map<String, String> param = new HashMap<>(3);
|
param.put("code",code);
|
sendSms(phone,SIGN_NAME,TEMPLATE_CODE,param);
|
}
|
|
public static void main(String[] args) {
|
sendSuccessMessage("17828262728","123456");
|
}
|
|
/**
|
* 阿里发送短信
|
*
|
* @param phone
|
* @param signName
|
* @param templateCode
|
* @param param
|
* @return
|
*/
|
private static boolean sendSms(String phone, String signName, String templateCode, Map<String, String> param) {
|
// 可自助调整超时时间
|
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
|
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
|
try {
|
|
// 初始化请求客户端
|
Client client = createClient();
|
|
// 构造请求对象,请填入请求参数值
|
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
.setPhoneNumbers(phone)
|
.setSignName(signName)
|
.setTemplateCode(templateCode)
|
.setTemplateParam(JSON.toJSONString(param));
|
//if (aliSmsConfig.isDebug()) {
|
// log.info("短信 DEBUG code= {}", param);
|
// return true;
|
//}
|
// 获取响应对象
|
SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
|
// hint 此处可能会抛出异常,注意catch
|
if (StringUtils.equalsIgnoreCase("ok", sendSmsResponse.getBody().getCode())) {
|
log.info("{}短信发送成功:{}", phone, sendSmsResponse.getBody().getMessage());
|
return true;
|
} else {
|
log.error("{}短信发送失败:{}", phone, sendSmsResponse.getBody().getMessage());
|
}
|
} catch (Exception e) {
|
log.error("{}短信发送失败:{}", phone, e.getMessage(),e);
|
throw new ServiceException("短信发送失败:"+e.getMessage());
|
}
|
return false;
|
}
|
private static boolean sendBatchSms(List<String> phones, String signName, String templateCode, Map<String, String> param) {
|
// 可自助调整超时时间
|
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
|
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
|
try {
|
|
// 初始化请求客户端
|
Client client = createClient();
|
|
// 构造请求对象,请填入请求参数值
|
SendBatchSmsRequest request = new SendBatchSmsRequest()
|
.setPhoneNumberJson(JSON.toJSONString(phones)) // 手机号数组JSON
|
.setSignNameJson(JSON.toJSONString(Collections.nCopies(phones.size(), SIGN_NAME)))
|
.setTemplateCode(templateCode)
|
.setTemplateParamJson(JSON.toJSONString(Collections.nCopies(phones.size(), param))); // 参数数组JSON
|
//if (aliSmsConfig.isDebug()) {
|
// log.info("短信 DEBUG code= {}", param);
|
// return true;
|
//}
|
// 获取响应对象
|
SendBatchSmsResponse sendBatchSmsResponse = client.sendBatchSms(request);
|
// hint 此处可能会抛出异常,注意catch
|
if (StringUtils.equalsIgnoreCase("ok", sendBatchSmsResponse.getBody().getCode())) {
|
return true;
|
} else {
|
log.error("{}短信发送失败:{}", JSON.toJSONString(phones), sendBatchSmsResponse.getBody().getMessage());
|
}
|
} catch (Exception e) {
|
log.error("{}短信发送失败:{}", JSON.toJSONString(phones), e.getMessage(),e);
|
throw new RuntimeException("短信发送失败", e);
|
}
|
return false;
|
}
|
public static Client createClient() throws Exception {
|
Config config = new Config()
|
// 配置 AccessKey ID
|
.setAccessKeyId(ACCESS_KEY_ID)
|
// 配置 AccessKey Secret
|
.setAccessKeySecret(ACCESS_KEY_SECRET);
|
|
// 配置 Endpoint
|
config.endpoint = "dysmsapi.aliyuncs.com";
|
|
return new Client(config);
|
}
|
|
|
}
|