package  com. atguigu. jedis ; import  redis. clients. jedis.  Jedis ; import  java. util.  Random ; public  class  PhoneCode  { public  static  void  main ( String [ ]  args)  { verifyCode ( "13678765435" ) ; } public  static  void  getRedisCode ( String  phone, String  code)  { Jedis  jedis =  new  Jedis ( "39.103.193.185" , 6379 ) ; String  codeKey =  "VerifyCode" + phone+ ":code" ; String  redisCode =  jedis. get ( codeKey) ; if ( redisCode. equals ( code) )  { System . out. println ( "成功" ) ; } else  { System . out. println ( "失败" ) ; } jedis. close ( ) ; } public  static  void  verifyCode ( String  phone)  { Jedis  jedis =  new  Jedis ( "39.103.193.185" , 6379 ) ; String  countKey =  "VerifyCode" + phone+ ":count" ; String  codeKey =  "VerifyCode" + phone+ ":code" ; String  count =  jedis. get ( countKey) ; if ( count ==  null )  { jedis. setex ( countKey, 24 * 60 * 60 , "1" ) ; }  else  if ( Integer . parseInt ( count) <= 2 )  { jedis. incr ( countKey) ; }  else  if ( Integer . parseInt ( count) > 2 )  { System . out. println ( "今天发送次数已经超过三次" ) ; jedis. close ( ) ; } String  vcode =  getCode ( ) ; jedis. setex ( codeKey, 120 , vcode) ; jedis. close ( ) ; } public  static  String  getCode ( )  { Random  random =  new  Random ( ) ; String  code =  "" ; for ( int  i= 0 ; i< 6 ; i++ )  { int  rand =  random. nextInt ( 10 ) ; code +=  rand; } return  code; } 
}