package cn.besteam.modules.system.biz;
|
|
import cn.besteam.config.AliSmsConfig;
|
import com.alibaba.fastjson.JSON;
|
import com.aliyuncs.DefaultAcsClient;
|
import com.aliyuncs.IAcsClient;
|
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 lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* @author: lizhiheng
|
* @date: 2020-06-18
|
* @since v1.0.0
|
*/
|
@Service
|
@Slf4j
|
public class AliSmsService {
|
|
/**
|
* 产品名称:云通信短信API产品,开发者无需替换
|
*/
|
private static final String PRODUCT = "Dysmsapi";
|
|
/**
|
* 产品域名,开发者无需替换
|
*/
|
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
|
|
@Resource
|
private AliSmsConfig aliSmsConfig;
|
|
/**
|
* 发送验证码短信
|
*
|
* @param phone 手机号
|
* @param code 验证码
|
* @return
|
*/
|
public boolean sendLoginCode(String phone, String code) {
|
|
Map<String, String> param = new HashMap<>(3);
|
param.put("code", code);
|
return sendSms(phone, aliSmsConfig.getSignName(), aliSmsConfig.getLoginTemplateCode(), param);
|
}
|
|
/**
|
* 发送通知短信
|
*
|
* @param phone 手机号
|
* @param param 参数
|
* @return
|
*/
|
public boolean sendMessage(String phone, Map<String, String> param) {
|
return sendSms(phone, aliSmsConfig.getSignName(), aliSmsConfig.getLoginTemplateCode(), param);
|
}
|
|
|
/**
|
* 发送月报通知短信
|
* @param phone
|
* @param param
|
* @return
|
*/
|
public boolean sendMonthlySMS(String phone, Map<String, String> param){
|
return sendSms(phone, aliSmsConfig.getSignName(), "SMS_246010544", param);
|
}
|
|
|
/**
|
* 阿里发送短信
|
*
|
* @param phone
|
* @param signName
|
* @param templateCode
|
* @param param
|
* @return
|
*/
|
private boolean sendSms(String phone, String signName, String templateCode, Map<String, String> param) {
|
// 可自助调整超时时间
|
System.setProperty("sun.net.client.defaultConnectTimeout", aliSmsConfig.getConnectTimeout());
|
System.setProperty("sun.net.client.defaultReadTimeout", aliSmsConfig.getReadTimeout());
|
try {
|
// 初始化acsClient,暂不支持region化
|
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliSmsConfig.getAccessKeyId(), aliSmsConfig.getAccessKeySecret());
|
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
|
IAcsClient acsClient = new DefaultAcsClient(profile);
|
// 组装请求对象-具体描述见控制台-文档部分内容
|
SendSmsRequest request = new SendSmsRequest();
|
// 必填:待发送手机号
|
request.setPhoneNumbers(phone);
|
// 必填:短信签名-可在短信控制台中找到
|
request.setSignName(signName);
|
// 必填:短信模板-可在短信控制台中找到
|
request.setTemplateCode(templateCode);
|
// 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
request.setTemplateParam(JSON.toJSONString(param));
|
if (aliSmsConfig.isDebug()) {
|
log.info("短信 DEBUG code= {}", param);
|
return true;
|
}
|
// hint 此处可能会抛出异常,注意catch
|
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
if (StringUtils.equalsIgnoreCase("ok", sendSmsResponse.getCode())) {
|
return true;
|
} else {
|
log.error("{}短信发送失败:{}", phone, sendSmsResponse.getMessage());
|
}
|
} catch (ClientException e) {
|
throw new RuntimeException("短信发送失败", e);
|
}
|
return false;
|
}
|
|
}
|