parent
49cb10bd8e
commit
1b2462a55e
6 changed files with 71 additions and 153 deletions
@ -1,39 +0,0 @@ |
|||||||
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); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@ -1,72 +0,0 @@ |
|||||||
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); |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,27 @@ |
|||||||
|
package org.springblade.modules.business.sms; |
||||||
|
|
||||||
|
|
||||||
|
import com.aliyun.dysmsapi20170525.Client; |
||||||
|
import com.aliyun.teaopenapi.models.Config; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class SmsConfig { |
||||||
|
|
||||||
|
@Value("${sms.access-key}") |
||||||
|
private String ACCESS_KEY_ID; |
||||||
|
|
||||||
|
@Value("${sms.secret-key}") |
||||||
|
private String ACCESS_KEY_SECRET; |
||||||
|
|
||||||
|
|
||||||
|
@Bean |
||||||
|
public Client ClientConfig() throws Exception { |
||||||
|
|
||||||
|
Config config = new Config().setAccessKeyId(ACCESS_KEY_ID) |
||||||
|
.setAccessKeySecret(ACCESS_KEY_SECRET); |
||||||
|
return new Client(config); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue