@Service
@Transactional
public class LoginSmsServiceImpl implements LoginSmsService {
@Autowired
RedisTemplate redisTemplate;
/**
*
* @param phone 电话号码
*/
@Override
public void SmsVerification(String phone) {
//判断手机号是否发送过验证码
if(redisTemplate.hasKey("timeout_"+phone)){
throw new RuntimeException("已经发送验证码了,请稍后重试");
}
//随机4位验证码
String code = RandomStringUtils.randomNumeric(4);
try {
//发送验证码
// SMSUtils.sendMsg(phone, code);
//验证码存入redis
redisTemplate.opsForValue().set("phone_"+phone, code,120, TimeUnit.SECONDS);
//超时时间
redisTemplate.opsForValue().set("timeout_"+phone, "true",60, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("发送验证码失败,请稍后重试!");
}
}
}