|
|
package com.xmomen.module.sms.model;
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
import java.util.GregorianCalendar;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
|
// 定义一个验证码模型类
|
|
|
public @Data class IdentifyCodeModel {
|
|
|
|
|
|
// 验证码
|
|
|
private String identifyCode;
|
|
|
// 创建时间
|
|
|
private Date createTime;
|
|
|
// 过期时间
|
|
|
private Date expiredTime;
|
|
|
|
|
|
// 构造函数,传入验证码、有效时间和时间单位
|
|
|
public IdentifyCodeModel(String identifyCode, Long validTime, TimeUnit timeUnit) {
|
|
|
this.identifyCode = identifyCode;
|
|
|
Calendar calendar = new GregorianCalendar();
|
|
|
createTime = calendar.getTime();
|
|
|
if(validTime != null) {
|
|
|
if(validTime > 0) {
|
|
|
switch(timeUnit) {
|
|
|
case SECONDS: {
|
|
|
expiredTime = new Date(createTime.getTime() + validTime * 1000);
|
|
|
break;
|
|
|
}
|
|
|
case MINUTES: {
|
|
|
expiredTime = new Date(createTime.getTime() + validTime * 1000 * 60);
|
|
|
break;
|
|
|
}
|
|
|
default: {
|
|
|
throw new IllegalArgumentException("不支持其他时间类型");
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
throw new IllegalArgumentException("不合法有效时间值");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 构造函数,传入验证码和有效时间,默认时间单位为秒
|
|
|
public IdentifyCodeModel(String identifyCode, Long validTime) {
|
|
|
this(identifyCode, validTime, TimeUnit.SECONDS);
|
|
|
}
|
|
|
// 构造函数,传入验证码,默认有效时间为null
|
|
|
public IdentifyCodeModel(String identifyCode) {
|
|
|
this(identifyCode, null);
|
|
|
}
|
|
|
|
|
|
// 判断验证码是否过期
|
|
|
public boolean isExpired() {
|
|
|
Date now = new Date();
|
|
|
if(expiredTime == null) return false;
|
|
|
if(now.getTime() > expiredTime.getTime()) {
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
}
|