飞滴出行网约车2022-乘客端 根据手机号请求验证码并存入redis缓存2分钟

master
yh 3 years ago
parent 0feb760919
commit 90ee01ad22

@ -45,6 +45,11 @@
<artifactId>spring-cloud-starter-loadbalancer</artifactId> <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -3,6 +3,7 @@ package com.mashibing.apipassenger.controller;
import com.mashibing.apipassenger.remote.ServiceVerificationcodeClient; import com.mashibing.apipassenger.remote.ServiceVerificationcodeClient;
import com.mashibing.apipassenger.request.VerificationCodeDTO; import com.mashibing.apipassenger.request.VerificationCodeDTO;
import com.mashibing.apipassenger.service.VerificationCodeService; import com.mashibing.apipassenger.service.VerificationCodeService;
import com.mashibing.internalcommon.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -16,10 +17,9 @@ public class VerificationController {
// 实现 乘客获取验证码 API // 实现 乘客获取验证码 API
@GetMapping("/verification-code") @GetMapping("/verification-code")
public String verificationcode(@RequestBody VerificationCodeDTO verificationCodeDTO){ public ResponseResult verificationcode(@RequestBody VerificationCodeDTO verificationCodeDTO){
String passengerPhone = verificationCodeDTO.getPassengerPhone(); String passengerPhone = verificationCodeDTO.getPassengerPhone();
System.out.println("接受到的手机验证码为"+passengerPhone);
return service.generatorCode(passengerPhone); return service.generatorCode(passengerPhone);
} }

@ -5,31 +5,35 @@ import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.NumberCodeResponse; import com.mashibing.internalcommon.response.NumberCodeResponse;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service @Service
public class VerificationCodeService { public class VerificationCodeService {
@Autowired @Autowired
private ServiceVerificationcodeClient verificationcodeClient; private ServiceVerificationcodeClient verificationcodeClient;
public String generatorCode(String passengerPhone){ @Autowired
private StringRedisTemplate redisTemplate;
private String prefixKey = "passenger-verification-code-";
// 调用验证码服务,获取验证码
System.out.println("调用验证码服务,获取验证码");
// 使用 frign 远程调用生成验证码服务接口 public ResponseResult generatorCode(String passengerPhone){
// 使用 feign 远程调用生成验证码服务接口
ResponseResult<NumberCodeResponse> numberCodeResponse = verificationcodeClient.getNumberCode(6); ResponseResult<NumberCodeResponse> numberCodeResponse = verificationcodeClient.getNumberCode(6);
String numberCode = numberCodeResponse.getData().getNumberCode(); String numberCode = numberCodeResponse.getData().getNumberCode();
System.out.println("remote number code: "+numberCode);
// 存入redis // 存入redis
System.out.println("存入redis"); String redisKey = prefixKey + passengerPhone;
JSONObject result = new JSONObject(); redisTemplate.opsForValue().set(redisKey,numberCode,2, TimeUnit.MINUTES);
result.put("code",1);
result.put("message","success"); // 返回处理结果
// 返回值 return ResponseResult.success("");
return result.toString();
} }
} }

@ -8,4 +8,8 @@ spring:
cloud: cloud:
nacos: nacos:
discovery: discovery:
server-addr: 127.0.0.1:8848 server-addr: 127.0.0.1:8848
redis:
host: 127.0.0.1
port: 6379
database: 0

@ -13,8 +13,18 @@ public class ResponseResult<T> {
private String message ; private String message ;
private T data ; private T data ;
/**
*
* @param <T>
* @return
*/
public static <T> ResponseResult success(){
return new ResponseResult().setCode(CommonStatusEnum.SUCCESS.getCode()).setMessage(CommonStatusEnum.SUCCESS.getValue()).setData("");
}
/** /**
* *
* @param data * @param data
* @param <T> * @param <T>
* @return * @return
@ -35,7 +45,7 @@ public class ResponseResult<T> {
} }
/** /**
* *
* @param data * @param data
* @param <T> * @param <T>
* @return * @return

@ -17,12 +17,12 @@ public class NumberCodeController {
// 生成指定长度验证码 // 生成指定长度验证码
double codetmp = (Math.random() *9 + 1) * (Math.pow(10,size-1));// *9 保证首位不为0+1保证最低被乘数大于1长度使用 10的size-1平方数 double codetmp = (Math.random() *9 + 1) * (Math.pow(10,size-1));// *9 保证首位不为0+1保证最低被乘数大于1长度使用 10的size-1平方数
Integer numberCode = (int)codetmp; Integer numberCode = (int)codetmp;
System.out.println("generator code"+size+" 长度验证码为:"+numberCode);
// 封装返回结果 // 封装返回结果
NumberCodeResponse data = new NumberCodeResponse(); NumberCodeResponse data = new NumberCodeResponse();
data.setNumberCode(numberCode.toString()); data.setNumberCode(numberCode.toString());
// 返回处理结果
return ResponseResult.success(data); return ResponseResult.success(data);
} }

Loading…
Cancel
Save