1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.leyou.sms.util;
import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.leyou.sms.config.SmsProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.stereotype.Component;
@Slf4j @Component @EnableConfigurationProperties(SmsProperties.class) public class SmsUtil {
@Autowired private SmsProperties smsProperties;
public CommonResponse sendSms(String phoneNumbers, String signName, String templateCode, String templateParam) { try { DefaultProfile profile = DefaultProfile .getProfile("default", smsProperties.getAccessKeyID(), smsProperties.getAccessKeySecret()); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("PhoneNumbers", phoneNumbers); request.putQueryParameter("SignName", signName); request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateParam", templateParam); CommonResponse response = client.getCommonResponse(request); log.info("[短信微服务] 短信发送。发送结果:{}", response.getData()); return response; } catch (ClientException e) { log.error("[短信微服务] 发送短信失败, phone = [{}]", phoneNumbers, e); } return null; } }
|