获取手机验证码

/**
 * 获取短信验证码
 * @param phone - 手机号
 * @return - 发送手机验证码结果
 * @author LiuDeTian at 2021-08-30
 */
@GetMapping(value = "/getAuthCode")
@ApiOperation(value = "获取验证码")
public ResultVO getAuthCode(@RequestParam String phone) {
    try {
    	//获取redis中是否有已发送结果
        long keyExpire = RedisTool.getExpireSeconds(RedisConstant.AUTH_CODE + phone);
        if (keyExpire > 0) {
            return ResultVO.FAIL("验证码已发送,如果未收到,请【" + keyExpire + "】秒后重新获取!", null);
        }
        String authCode = AliYunSmsTool.generateAuthCode(6);  // 生成6位结果码
        SendSmsResponseBody smsRes = AliYunSmsTool.sendSMS(phone, authCode); //发送信息
        if (!"OK".equals(smsRes.getCode())) {
            log.error("验证码发送失败!" + smsRes.getMessage());
            return ResultVO.FAIL("验证码发送失败!" + smsRes.getMessage(), null);
        }
        RedisTool.set(RedisConstant.AUTH_CODE + phone, authCode, 90);
        applicationContext.publishEvent(new SmsEvent(SysAuthCode.builder()
                .authCode(authCode)
                .mobileNum(phone)
                .smsCode(smsRes.getCode())
                .smsMessage(smsRes.getMessage())
                .createTime(LocalDateTime.now())
                .build())
        );
        return ResultVO.SUCCESS();
    } catch (Exception e) {
        e.printStackTrace();
        log.error("获取验证码异常!", e);
        return ResultVO.ERROR("获取验证码异常!", null);
    }
}


//------------------- 工具类-------------------------------------//
@Component
public class AliYunSmsTool {

// 阿里云短信客户端
private static Client client;

// 短息模板id
private static String templateCode;

// 短信签名
private static String signName;

private static final String[] CODE_EL_ARRAY = new String[] { /*"a","b","c","d","e","f","g","h","i","g","k","l","m","n","o","p","q","r",
        "s","t","u","v","w","x","y","z",*/"0","1","2","3","4","5","6","7","8","9" };

@Value("${aliYunSMS.templateCode}")
public void setTemplateCode(String value) {
    templateCode = value;
}

@Value("${aliYunSMS.signName}")
public void setSignName(String value) {
    signName = value;
}

/**
 * 注入静态变量的值
 * @param client - 阿里云短信客户端实例
 * @author liudetian at 2021-08-26
 */
@Autowired
private void setClient(Client client) {
    AliYunSmsTool.client = client;
}

/**
 * 发送短信
 * @param mobile - 手机号
 * @param message - 消息
 * @return - 发送结果
 * @author LiuDeTian at 2021-08-26
 */
public static SendSmsResponseBody sendSMS(String mobile, String message) throws Exception {
    SendSmsRequest sendSmsRequest = new SendSmsRequest()
            .setPhoneNumbers(mobile)
            .setSignName(signName)
            .setTemplateCode(templateCode)  //短信模板Id
            .setTemplateParam("{\"code\":\"" + message + "\"}");
    // 复制代码运行请自行打印 API 的返回值
    return client.sendSms(sendSmsRequest).getBody();
}

/**
 * 生成给定位数的验证码
 * @param digits - 位数
 * @return - 给定位数的验证码
 * @author LiuDeTian at 2021-08-30
 */
public static String generateAuthCode(int digits) {
    StringBuilder codeBuilder = new StringBuilder();
    for (int i = 0; i < digits;i++) {
        codeBuilder.append(CODE_EL_ARRAY[(int)(Math.random() * CODE_EL_ARRAY.length)]);
    }
    return codeBuilder.toString();
}

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值