|
|
|
@ -1,12 +1,13 @@
|
|
|
|
|
package com.greateme.verification.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.greateme.contant.business.VerificationCodeConstant;
|
|
|
|
|
import com.greateme.util.StringUtil;
|
|
|
|
|
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
|
|
|
|
|
import com.greateme.verification.entity.dto.PassengerVerificationCodeRedisDTO;
|
|
|
|
|
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
|
|
|
|
|
import com.greateme.verification.service.VerificationCodeService;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
@ -32,10 +33,8 @@ public class VerificationCodeServiceImpl implements VerificationCodeService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户手机验证码redis存储的前缀
|
|
|
|
|
*/
|
|
|
|
|
private static final String PASSENGER_PHONE_VERIFICATION_CODE_PREFIX = "PASSENGER::PHONE::VERIFICATION::CODE::";
|
|
|
|
|
@Value("${business.verification-code.timeout-minute}")
|
|
|
|
|
private Integer verificationCodeTimeoutMinute;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成一个乘客的短信验证码
|
|
|
|
@ -56,11 +55,35 @@ public class VerificationCodeServiceImpl implements VerificationCodeService {
|
|
|
|
|
verificationCodeRedis.setPassengerPhone(verificationCode.getPassengerPhone());
|
|
|
|
|
verificationCodeRedis.setToken(token);
|
|
|
|
|
// 将 verificationCodeRedis 存入redis
|
|
|
|
|
this.redisTemplate.opsForValue().set(PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + token, verificationCodeRedis, 5, TimeUnit.MINUTES);
|
|
|
|
|
this.redisTemplate.opsForValue().set(VerificationCodeConstant.PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + token, verificationCodeRedis, this.verificationCodeTimeoutMinute, TimeUnit.MINUTES);
|
|
|
|
|
// 将token和code返回
|
|
|
|
|
PassengerVerificationCodeVO result = new PassengerVerificationCodeVO();
|
|
|
|
|
result.setVerificationCode(code);
|
|
|
|
|
result.setVerificationToken(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查手机验证码是否正确
|
|
|
|
|
*
|
|
|
|
|
* @param passengerVerificationCode 手机验证码的检查对象
|
|
|
|
|
* @return 检查结果,返回bool,判断是否正确
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Integer checkPhoneVerificationCode(PassengerVerificationCodeVO passengerVerificationCode) {
|
|
|
|
|
// 获取短信验证码的token并在redis当中获取正确的短信验证码
|
|
|
|
|
String verificationToken = passengerVerificationCode.getVerificationToken();
|
|
|
|
|
PassengerVerificationCodeRedisDTO verificationCodeRedis = (PassengerVerificationCodeRedisDTO) this.redisTemplate.opsForValue().get(VerificationCodeConstant.PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + verificationToken);
|
|
|
|
|
if (null == verificationCodeRedis) {
|
|
|
|
|
return VerificationCodeConstant.VERIFICATION_CODE_TIMEOUT;
|
|
|
|
|
} else {
|
|
|
|
|
String verificationCode = verificationCodeRedis.getVerificationCode();
|
|
|
|
|
if (StringUtil.equal(verificationCode, passengerVerificationCode.getVerificationCode())) {
|
|
|
|
|
this.redisTemplate.delete(VerificationCodeConstant.PASSENGER_PHONE_VERIFICATION_CODE_PREFIX + verificationToken);
|
|
|
|
|
return VerificationCodeConstant.VERIFICATION_CODE_RIGHT;
|
|
|
|
|
} else {
|
|
|
|
|
return VerificationCodeConstant.VERIFICATION_CODE_WARING;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|