视频直播系统源码,java 随机验证码 、10 分钟有效

 1.首先创建一个vo类存放我们验证码相关信息。

 

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.io.Serializable;
import java.util.Date;
 
/**
 * @Author Lxq
 * @Date 2020/12/7 17:30
 * @Version 1.0
 * <p>
 * 存放验证码相关内容,这个对象是放在redis中
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class VerifyCodeVO implements Serializable {
    /**
     * 验证码
     */
    private Integer verifyCode;
    /**
     * 发送验证码的手机号
     */
    private String phoneNumber;
    /**
     * 最近成功发送时间
     */
    private Date lastSendTime;
    /**
     * 可用次数
     */
    private Integer availableTimes;
}


2.当前时间与晚上十二点的秒差

 

/**
     * 当前时间与晚上十二点的秒差
     *
     * @return
     */
    private Long nowTo12ClockTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
 
        //当前时间与晚上十二点的秒差
        return (calendar.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

 

3.发送验证码的具体方法

 

@Override
    public void sendVerificationCode(String phoneNumber) {
        if (phoneNumber == null) {
            throw new CustomException("手机号码不能为空!");
        }
        // 从redis中获取手机号码对应对象信息
        VerifyCodeVO verifyCodeVO = redisUtils.get(phoneNumber, VerifyCodeVO.class);
        if (verifyCodeVO != null) {
            long lastSendTime = verifyCodeVO.getLastSendTime().getTime();
            long now = System.currentTimeMillis();
            if ((now - lastSendTime) / 1000 < 60) {
                throw new CustomException("请不要频繁获取验证码!");
            }
            Integer availableTimes = verifyCodeVO.getAvailableTimes();
            if (availableTimes.equals(0)) {
                throw new CustomException("今日验证码次数已使用完毕!");
            }
            // 次数减一
            verifyCodeVO.setAvailableTimes(verifyCodeVO.getAvailableTimes() - 1);
            // 重新生成新验证码
            int random = (int) ((Math.random() * 9 + 1) * 100000);
            verifyCodeVO.setVerifyCode(random);
            verifyCodeVO.setLastSendTime(new Date());
            redisUtils.set(phoneNumber, verifyCodeVO, nowTo12ClockTime());
            // 异步发送短信
            String id = ArpSmsTemplateEnum.APP_DATA_VERIFICATION_CODE.getId();
            Integer parameters = ArpSmsTemplateEnum.APP_DATA_VERIFICATION_CODE.getParameters();
            AsyncManager.me().ttlExecute(AsyncMessageFactory.sendBusinessSms(id, new String[]{"+86" + phoneNumber}, new String[]{String.valueOf(random)}, parameters));
 
        } else {
            int random = (int) ((Math.random() * 9 + 1) * 100000);
            VerifyCodeVO vc = new VerifyCodeVO();
            vc.setPhoneNumber(phoneNumber);
            vc.setVerifyCode(random);
            vc.setLastSendTime(new Date());
            vc.setAvailableTimes(9);
            redisUtils.set(phoneNumber, vc, nowTo12ClockTime());
            // 模板ID
            String id = ArpSmsTemplateEnum.APP_DATA_VERIFICATION_CODE.getId();
            Integer parameters = ArpSmsTemplateEnum.APP_DATA_VERIFICATION_CODE.getParameters();
            AsyncManager.me().ttlExecute(AsyncMessageFactory.sendBusinessSms(id, new String[]{"+86" + phoneNumber}, new String[]{String.valueOf(random)}, parameters));
        }
    }

 

备注:短信接口是调用腾讯云接口,自行解决

 

4.接收验证码接口

 

@Override
    public Integer getVerificationCode(String phoneNumber) {
        VerifyCodeVO verifyCodeVO = redisUtils.get(phoneNumber, VerifyCodeVO.class);
        if (verifyCodeVO == null) {
            throw new CustomException("请先获取手机验证码!");
        }
        long currentTimeMillis = System.currentTimeMillis();
        long lastTime = verifyCodeVO.getLastSendTime().getTime();
        // 10 分钟有效期
        if ((lastTime + (10 * 60 * 1000)) < currentTimeMillis) {
            throw new CustomException("验证码已过期,请重新获取!");
        }
        return verifyCodeVO.getVerifyCode();
    }

 

 以上就是视频直播系统源码,java 随机验证码 、10 分钟有效, 更多内容欢迎关注之后的文章