public class Demo1 {
public static void main(String[] args) {
// vertifyCode("15022588756");
checkCode("240329","15022588756");
}
//①随机生成一个六位数字的随机数
public static String getCode(){
String code="";
Random r = new Random();
for (int i = 0; i <6 ; i++) {
code+= r.nextInt(10);
}
return code;
}
//②每个手机每天只能发送三次验证码,保存到redis中,设置过期时间120s
public static void vertifyCode(String phone){
String keyCount="Vertify"+phone+":KeyCount";
String keyCode="Vertify"+phone+":KeyCode";
Jedis jedis = new Jedis("49.232.145.5", 6379);
String count = jedis.get(keyCount);
//count==null表示一次都没有发送
if(count==null){
jedis.setex(keyCount,24*60*60,"1");
//第二次或第三次发送
}else if(Integer.parseInt(count)<=2){
jedis.incr(keyCount);
//第四次发送时,失败
}else if(Integer.parseInt(count)>2){
System.out.println("您今天的次数已经用完了!");
jedis.close();
}
//验证码保存到redis里
String code = getCode();
System.out.println(code);
jedis.setex(keyCode,120,code);
jedis.close();
}
//③验证码校验功能
public static void checkCode(String code,String phone){
String keyCode="Vertify"+phone+":KeyCode";
Jedis jedis = new Jedis("49.232.145.5", 6379);
if (code.equals(jedis.get(keyCode))){
System.out.println("验证码正确!");
}else{
System.out.println("验证码输入错误");
}
jedis.close();
}
}