parent
203646760e
commit
899ee964b9
6 changed files with 135 additions and 18 deletions
@ -0,0 +1,39 @@ |
||||
package org.springblade.modules.business.sms; |
||||
|
||||
import com.aliyun.teaopenapi.models.*; |
||||
import lombok.Data; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* @author xueyaoxuan |
||||
*/ |
||||
@Data |
||||
@Component |
||||
@ConfigurationProperties(prefix = "sms.aliyun") |
||||
public class AliYunSmsConfig { |
||||
|
||||
private String accessKeyId; |
||||
private String accessKeySecret; |
||||
private String signName; |
||||
private boolean isPushSms; |
||||
|
||||
/** |
||||
* 使用AK&SK初始化账号Client |
||||
* |
||||
* @return Client |
||||
* @throws Exception |
||||
*/ |
||||
public com.aliyun.dysmsapi20170525.Client createClient() throws Exception { |
||||
Config config = new Config() |
||||
// AccessKey ID
|
||||
.setAccessKeyId(this.getAccessKeyId()) |
||||
// AccessKey Secret
|
||||
.setAccessKeySecret(this.getAccessKeySecret()); |
||||
// 访问的域名
|
||||
config.endpoint = "dysmsapi.aliyuncs.com"; |
||||
return new com.aliyun.dysmsapi20170525.Client(config); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,72 @@ |
||||
package org.springblade.modules.business.sms; |
||||
|
||||
import cn.hutool.core.util.StrUtil; |
||||
import com.aliyun.dysmsapi20170525.Client; |
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest; |
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.core.log.exception.ServiceException; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
public class AliYunSmsManager { |
||||
|
||||
@Autowired |
||||
private AliYunSmsConfig aliYunSmsConfig; |
||||
|
||||
public String sendSms(List<String> mobiles, String message, String code) { |
||||
SendSmsResponse sendSmsResponse = null; |
||||
try{ |
||||
//调用阿里云api手机号上限1000
|
||||
if (mobiles.size()>1000){ |
||||
throw new ServiceException("发送短信手机号上限!"); |
||||
} |
||||
//检验手机号格式
|
||||
mobiles.forEach(mobile->{ |
||||
if (StrUtil.isAllEmpty(mobile)){ |
||||
throw new ServiceException("手机号格式错误!"); |
||||
} |
||||
}); |
||||
sendSmsResponse = sendALiYunSms(mobiles.stream().collect(Collectors.joining(",")), |
||||
message, code); |
||||
//
|
||||
if (200 == sendSmsResponse.getStatusCode()) { |
||||
// 发送短信成功,返回短信码进行redis存储
|
||||
return message; |
||||
} |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 发送阿里云短信 |
||||
* |
||||
* @param mobiles 手机号列表 |
||||
* @param message json格式的模板参数 |
||||
* @param code 阿里云短信模板code |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
private SendSmsResponse sendALiYunSms(String mobiles, String message, String code) throws Exception { |
||||
|
||||
//初始化Client对象
|
||||
Client client = aliYunSmsConfig.createClient(); |
||||
|
||||
//构建请求参数
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest() |
||||
.setPhoneNumbers(mobiles) |
||||
.setSignName(aliYunSmsConfig.getSignName()) |
||||
.setTemplateCode(code) |
||||
.setTemplateParam(message); |
||||
//发送短信
|
||||
return client.sendSms(sendSmsRequest); |
||||
|
||||
} |
||||
} |
||||
@ -1,5 +0,0 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
<mapper namespace="org.springblade.modules.system.mapper.TopMenuSettingMapper"> |
||||
|
||||
</mapper> |
||||
Loading…
Reference in new issue