榛子云整合springboot发送短信

榛子云整合springboot发送短信,第1张

榛子云整合springboot发送短信

先导入榛子云依赖


    com.zhenzikj
    zhenzisms

然后再配置文件中添加配置(包括自定义的榛子云配置)

#配置端口号
server:
  port: 8031
spring:
  #配置服务名称
  application:
    name: serviceSms
    #配置nacos服务
  cloud:
    nacos:
      server-addr: 192.168.233.120:8848
  redis:
    host: 192.168.233.120
    port: 6379
    database: 1  #要连接的数据库,默认是0
    timeout: 30000   # 连接超时时间(毫秒)
    lettuce:
        pool:
          max-wait: -1 #最大阻塞等待时间 (负数表示没限制)
          max-active: 20  # 连接池最大连接数(使用负值表示没有限制)
          max-idle: 5   # 连接池中的最大空闲连接
          min-idle: 0  #  连接池中的最小空闲连接
zhenziyun:
  sms:
    apiUrl: http://sms_developer.zhenzikj.com/
    appId: 110400
    appSecret: 2c0f148f-23be-4116-bc06-9e24150179be
    templateId: 7362

这时我们通过configurationProperties注解来自定义榛子云配置及类

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "zhenziyun.sms")
public class SmsProperties implements InitializingBean {
    private String apiUrl;
    private String appId;
    private String appSecret;
    private String templateId;

    public static String API_URL;
    public static String APP_ID;
    public static String APP_SECRET;
    public static String TEMPLATE_ID;
    @Override
    public void afterPropertiesSet() throws Exception {
        API_URL=this.apiUrl;
        APP_ID=this.appId;
        APP_SECRET=this.appSecret;
        TEMPLATE_ID=this.templateId;
    }

业务方法中,首先获得yml文件中自定义的榛子云配置的值,然后创建一个map集合添加从前端传过来的电话号码和模板id,然后创建一个字符串数组来存储模板参数,然后再将数组添加到map集合中。最后通过创建榛子云客户端对象来发送信息。

@Service
public class SmsServiceImpl implements SmsService {
    @Override
    public boolean sendShortMessage(String telephone, String code) {
        //整合榛子云短信服务
        String apiUrl = SmsProperties.API_URL;
        String appId = SmsProperties.APP_ID;
        String appSecret = SmsProperties.APP_SECRET;
        String templateId  = SmsProperties.TEMPLATE_ID;
        //然后创建一个map集合
        Map map = new HashMap<>();
        map.put("number",telephone);
        map.put("templateId",templateId);
        //然后创建一个字符串类型的数组用来存放模板参数
        String[] templateParams = new String[2];
        templateParams[0] = code;
        templateParams[1] = "2";
        map.put("templateParams",templateParams);
        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
        try{
            String send = client.send(map);
            log.info("发送成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

前端controller里面,传过来电话号码后,显示判断传过来的参数是否为空,如果为空直接返回为空的结果,然后在判断号码是不是符合正则匹配,如果不符合,同样返回号码错误的结果,然后随机生成一个4位数字的验证码,最后调用业务方法发送短信,如果短信发送成功,便将生成的验证码存到redis缓存中,并设置有效时间。

@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信接受验证")
public class SmsController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SmsService smsService;
    @ApiOperation(value = "发送短信验证码")
    @GetMapping("/send/{telephone}")
    public Result sendCode(@ApiParam(value = "电话号码",required = true) @PathVariable String telephone){
        //首先判断手机号是否为空
        if (null==telephone){
            return Result.setResult(ResponseEnum.MOBILE_NULL_ERROR);
        }
        //判断手机号是否符合格式
        if (!RegexValidateUtil.checkphone(telephone)){
            return Result.setResult(ResponseEnum.MOBILE_ERROR);
        }
        //随机生成一个四位数字的验证码
        String code = RandomUtil.generatevalidateCode(4);
        //然后调用业务方法发送短信
        boolean b = smsService.sendShortMessage(telephone, code);
        //判断短信是否发送成功,(true为成功,false为失败)
        if (b){
            //发送成功,将验证存入到redis缓存中,并设置有效期
            String key = "ymjr:sms:code:"+code;
            redisUtil.set(key,code,120);
            return Result.ok().message("短信发送成功");
        }
        return Result.error().message("短信发送失败");

    }

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5693117.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存