对接多个短信平台,该如何快速完成

最近有小伙伴问有没有短信发送比较成熟的框架?

今天就为大家推荐一款可以支持阿里云、腾讯云、百度云、京东云等等多个平台的消息通道,

项目中可以随时增加或者切换,是不是很方便!

guerlab-sms

基于Spring boot的短信服务支持,通过引用不同的Starter启用不同的短信通道支持,支持多通道下的负载均衡,支持同步/异步方式发送。 目前支持类型:阿里云短信、百度云短信、华为云短信、京东云短信、网易云信短信、腾讯云短信、七牛云短信、云片网短信、又拍云短信、移动云模板短信

https://gitee.com/guerlab_net/guerlab-sms

下面说一下接入方法:

首先引入jar包

<!-- 短信模块 https://gitee.com/guerlab_net/guerlab-sms-->
<dependency>
    <groupId>net.guerlab.sms</groupId>
    <artifactId>guerlab-sms-server-starter</artifactId>
    <version>${net.guerlab.sms.version}</version>
</dependency>
<dependency>
    <groupId>net.guerlab.sms</groupId>
    <artifactId>guerlab-sms-qcloud-starter</artifactId>
    <version>${net.guerlab.sms.version}</version>
</dependency>

配置参数

sms:
  reg: ^1[0-9]{10}$ #手机号码正则表达式,为空则不做验证
  load-balancer-type: Random #负载均衡类型 可选值: Random、RoundRobin、WeightRandom、WeightRoundRobin
  web:
    enable: false #启用web端点
    base-path: /commons/sms #访问路径前缀
  verification-code:
    code-length: 6 #验证码长度
    delete-by-verify-fail: false #为true则验证失败后删除验证码
    delete-by-verify-succeed: true #为true则验证成功后删除验证码  (注意:上线后,改为true)
    retry-interval-time: 60 #重试间隔时间,单位秒
    expiration-time: 180 #验证码有效期,单位秒
    identification-code-length: 3 #识别码长度
    use-identification-code: false #是否启用识别码
  qcloud:
    enable: true #启用腾讯云的短信业务客户端
    weight: 1 #权重
    app-id: 190150612 #短信应用SDK AppID
    appkey: 65685aaf81654521dce32ae854682 #短信应用SDK AppKey
    sms-sign: 上市公司 #短信签名
    templates:
      VerificationCode: 299586 #验证码业务所使用的短信模板ID
      test: 369586 #自定义业务所使用的短信模板ID
    params-orders:
      VerificationCode: #规定验证码业务的参数顺序
        - code
        #- identificationCode

实现一个接口,主要是短信的存储操作,可存储到redis,或db中

IVerificationCodeRepository
package net.guerlab.sms.server.repository;

import net.guerlab.sms.server.entity.VerificationCode;

public interface IVerificationCodeRepository {
    VerificationCode findOne(String var1, String var2);

    void save(VerificationCode var1);

    void delete(String var1, String var2);
}
@Repository
public class VerificationCodeDBRepository implements IVerificationCodeRepository {
    ...
}

调用发送短信接口

 @Autowired
 private VerificationCodeService verificationCodeService;

...

    @WebLog(notes = "获取验证码")
    @PostMapping("/sendCode")
    public ResponseData sendVerificationCode(@RequestBody reg reg) {
        if (StrUtil.isBlank(reg.getPhone())) {
            throw new AppException(ExEnum.E20051.getCode(), "手机号验证失败");
        }

        // 一分钟之内不能重复发送短信
        Sms sms = smsService.find(reg.getPhone());
        if (ObjectUtil.isNotNull(sms) && ((System.currentTimeMillis() - sms.getCreatedAt().getTime()) / 1000) < 1 * 60) {
            throw new AppException(ExEnum.E20051.getCode(), "短信已发送");
        }
        if (ObjectUtil.isNotNull(sms)){
            smsService.delete(sms.getPhone());
        }

        verificationCodeService.send(reg.getPhone());
        return ResponseData.success("短信发送成功");
    }

是不是比起挨个去学习某云、某云的短信接口简单多了。

猜你喜欢

转载自blog.csdn.net/lizhao1226/article/details/116132287