司机 校验 验证码代码实现

main
topsun 2 years ago
parent 7fec52991a
commit fd788ecf6d

@ -6,6 +6,7 @@ import com.taxi.apidriver.service.VerficationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@ -22,4 +23,11 @@ public class VerficationCodeController {
log.info("司机的号码"+driverPhone);
return verficationCodeService.checkSendVerficationCode(driverPhone);
}
@PostMapping("/verification-code-check")
public ResponseResult verificationCodeCheck(@RequestBody VerificationCodeDTO verificationCodeDTO){
String driverPhone = verificationCodeDTO.getDriverPhone();
String verificationCode = verificationCodeDTO.getVerificationCode();
return verficationCodeService.checkCode(driverPhone,verificationCode);
}
}

@ -1,11 +1,10 @@
package com.taxi.apidriver.remote;
import com.internal.dto.ResponseResult;
import com.internal.request.VerificationCodeDTO;
import com.internal.response.NumberResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.*;
@FeignClient("service-verificationcode")
public interface ServiceVerificationcodeClient {

@ -3,9 +3,13 @@ package com.taxi.apidriver.service;
import com.internal.contant.CommonStatusEnum;
import com.internal.contant.DriverCarConstant;
import com.internal.contant.IdentityConstant;
import com.internal.contant.TokenConstant;
import com.internal.dto.ResponseResult;
import com.internal.request.VerificationCodeDTO;
import com.internal.response.DriverUserExistsResponse;
import com.internal.response.NumberResponse;
import com.internal.response.TokenResponse;
import com.internal.util.JwtUtils;
import com.internal.util.RedisPrefixUtils;
import com.taxi.apidriver.remote.ServiceDriverUserClient;
import com.taxi.apidriver.remote.ServiceVerificationcodeClient;
@ -55,4 +59,70 @@ public class VerficationCodeService {
5, TimeUnit.MINUTES);
return ResponseResult.success();
}
/**
* @param driverPhone
* @param verificationCode
* @return
*/
public ResponseResult checkCode(String driverPhone, String verificationCode) {
//一、根据手机号reids-key规则查询验证码
String key = RedisPrefixUtils.getVerificationCodePrefixKey(driverPhone
,IdentityConstant.DRIVER_IDENTITY);
String codeRedis = stringRedisTemplate.opsForValue().get(key);
System.out.println("redis中的code:" + codeRedis);
//二、判断验证码是否正确
if (codeRedis != null && verificationCode != null) {
verificationCode = verificationCode.trim();
if (!codeRedis.trim().equals(verificationCode)) {
CommonStatusEnum.VERIFICATION_CODE_ERROR.setMessage("验证码输入有误!");
return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR);
} else {
// //三、判断这个手机号是否存在用户,并进行对应的处理
// VerificationCodeDTO verificationCodeDTO = new VerificationCodeDTO();
// verificationCodeDTO.setPassengerPhone(driverPhone);
// serviceDriverUserClient.getUser(verificationCodeDTO);
/**
* tokenredis
*/
TokenResponse tokenResponse = proDoubleTokenAndSaveRedis(stringRedisTemplate,
driverPhone);
return ResponseResult.success(tokenResponse);
}
} else {
CommonStatusEnum.VERIFICATION_CODE_ERROR.setMessage("验证码错误!");
return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR);
}
}
/**
* tokenredis
*
*/
public TokenResponse proDoubleTokenAndSaveRedis(StringRedisTemplate stringRedisTemplate,
String passenegerPhone){
//四、颁发令牌
String accessToken = JwtUtils.generatorToken(passenegerPhone,
IdentityConstant.DRIVER_IDENTITY, TokenConstant.ACCESS_TOKEN_TYPE);
String refreshToken = JwtUtils.generatorToken(passenegerPhone,
IdentityConstant.DRIVER_IDENTITY,TokenConstant.REFRESH_TOKEN_TYPE);
//将accesstoken存入redis
String accessTokenKey = RedisPrefixUtils.getTokenPrefixKey(passenegerPhone,
IdentityConstant.DRIVER_IDENTITY,TokenConstant.ACCESS_TOKEN_TYPE);
stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30, TimeUnit.DAYS);
//将refreshToken存入redis
String refreshTokenKey = RedisPrefixUtils.getTokenPrefixKey(passenegerPhone,
IdentityConstant.DRIVER_IDENTITY,TokenConstant.REFRESH_TOKEN_TYPE);
stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);
TokenResponse checkCodeResponse = new TokenResponse();
checkCodeResponse.setAccessToken(accessToken);
checkCodeResponse.setRefreshToken(refreshToken);
return checkCodeResponse;
}
}

Loading…
Cancel
Save