Aliyun Sms Utils

Config

SmsClient

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author 梦欣
 * Sms 短信服务客户端
 */
@Component
public class SmsClient {

    @Autowired
    private SmsProperties smsProperties;

    /**
     * 将短信服务客户端加入到IoC中
     */
    @Bean
    public Client createClient() throws Exception{
        // 获取阿里云短信服务配置对象
        Config aliyunConfig = new Config()
                // 设置AccessKeyId;
                .setAccessKeyId(smsProperties.getAccessKeyId())
                // 设置AccessKeySecret
                .setAccessKeySecret(smsProperties.getAccessKeySecret())
                .setEndpoint(smsProperties.getEndpoint());

        // 将短信服务配置信息加入到客户端中, 并返回
        return new Client(aliyunConfig);
    }

}

yaml

leyou:
  sms:
    accessKeyId: 
    accessKeySecret: 
    endpoint: 
    signName: 
    templateCode: 
    code: 

Properties

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author 梦欣
 * Sms 短信服务属性
 */
@Data
@Component
@ConfigurationProperties(prefix = "meng.sms")
public class SmsProperties {

    @ApiModelProperty(value = "Ali yun AccessKey")
    private String accessKeyId;

    @ApiModelProperty(value = "Ali yun AccessKeySecret")
    private String accessKeySecret;

    @ApiModelProperty(value = "Ali yun SMS 服务域名")
    private String endpoint;

    @ApiModelProperty(value = "短信服务签名")
    private String signName;

    @ApiModelProperty(value = "短信服务模板编码")
    private String templateCode;

    @ApiModelProperty(value = "短信模板中验证码的占位符")
    private String code;

}

Utils(逐步完善)

发送短信(非群发)

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teautil.models.RuntimeOptions;
import com.meng.common.constant.SmsConstants;
import com.meng.common.exception.constant.ExceptionEnum;
import com.meng.common.exception.entity.LeyouException;
import com.meng.sms.config.SmsClient;
import com.meng.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 梦欣
 * 短信服务工具类
 */
@Slf4j
@Component
public class SmsUtils {

    @Autowired
    private SmsClient smsClient;

    @Autowired
    private SmsProperties smsProperties;

    /**
     * 发送短信 (非群发)
     */
    public void sendSmsMessage(String phoneNumber, String smsCode) {
        // 获取发送短信请求对象
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName(smsProperties.getSignName())
                .setTemplateCode(smsProperties.getTemplateCode())
                .setTemplateParam("{\"" + smsProperties.getCode() + "\": \"" + smsCode + "\"}");

        // 获取运行时间对象
        RuntimeOptions runtimeOptions = new RuntimeOptions();

        try {
            // 发送短信
            SendSmsResponse sendSmsResponse = smsClient.createClient().sendSmsWithOptions(sendSmsRequest, runtimeOptions);

            // 获取短信发送结果
            SendSmsResponseBody sendSmsResponseBody = sendSmsResponse.getBody();

            // 判断短信是否发送成功
            if (sendSmsResponseBody.getCode().equals(SmsConstants.SMS_RESPONSE_STATUS)) {
                log.info("阿里云短信发送成功(非群发), Message: {}", sendSmsResponseBody.getMessage());
            }else {
                log.error("阿里云短信发送失败(非群发), Message: {}", sendSmsResponseBody.getMessage());
            }
        } catch (Exception e) {
            log.error("阿里云短信发送失败(非群发), 原因: {}",e.getMessage());
            throw new LeyouException(ExceptionEnum.SEND_MESSAGE_ERROR);
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cherish Xin And Meng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值