添加校验验证码流程和接口

master
liuyuanqiang 2 years ago
parent 585cb383bd
commit f195c94d90

@ -33,6 +33,10 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<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>
<dependency> <dependency>
<groupId>org.mashibing</groupId> <groupId>org.mashibing</groupId>

@ -2,8 +2,10 @@ package com.mashibing.apipassenger.controller;
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.common.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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -13,12 +15,23 @@ public class VerificationCodeController {
private VerificationCodeService verificationCodeService; private VerificationCodeService verificationCodeService;
@GetMapping("verification-code") @GetMapping("verification-code")
public String verificateCode(@RequestBody VerificationCodeDTO verificationCodeDTO){ public ResponseResult verificateCode(@RequestBody VerificationCodeDTO verificationCodeDTO){
String passengerPhone = verificationCodeDTO.getPassengerPhone(); String passengerPhone = verificationCodeDTO.getPassengerPhone();
System.out.println("接收到的参数:" + passengerPhone); System.out.println("接收到的参数:" + passengerPhone);
//调用短信验证码服务并存储到Redis //调用短信验证码服务并存储到Redis
return verificationCodeService.generatorCode(passengerPhone); return verificationCodeService.generatorCode(passengerPhone);
}
@PostMapping("/verification-code-check")
public ResponseResult checkVerificationCode(@RequestBody VerificationCodeDTO verificationCodeDTO){
String verificationCode = verificationCodeDTO.getVerificationCode();
String passengerPhone = verificationCodeDTO.getPassengerPhone();
System.out.println("verificationCode:" + verificationCode + ",passengerPhone:" + passengerPhone);
return verificationCodeService.checkCode(passengerPhone,verificationCode);
} }
} }

@ -1,16 +1,10 @@
package com.mashibing.apipassenger.request; package com.mashibing.apipassenger.request;
import lombok.Data;
@Data
public class VerificationCodeDTO { public class VerificationCodeDTO {
private String passengerPhone; private String passengerPhone;
private String verificationCode;
public String getPassengerPhone() {
return passengerPhone;
}
public void setPassengerPhone(String passengerPhone) {
this.passengerPhone = passengerPhone;
}
} }

@ -3,15 +3,29 @@ package com.mashibing.apipassenger.service;
import com.mashibing.apipassenger.remote.ServiceVefificationcodeClient; import com.mashibing.apipassenger.remote.ServiceVefificationcodeClient;
import com.mashibing.common.dto.ResponseResult; import com.mashibing.common.dto.ResponseResult;
import com.mashibing.common.response.NumberCodeResponse; import com.mashibing.common.response.NumberCodeResponse;
import com.mashibing.common.response.TokenResponse;
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
ServiceVefificationcodeClient serviceVefificationcodeClient; private ServiceVefificationcodeClient serviceVefificationcodeClient;
public String generatorCode(String passengerPhone){
@Autowired
private StringRedisTemplate stringRedisTemplate;
private String verificationCodePrefix = "verification-code-";
/**
*
* @param passengerPhone
* @return
*/
public ResponseResult generatorCode(String passengerPhone){
//调用验证码服务,获取验证码 //调用验证码服务,获取验证码
System.out.println("调用验证码服务,获取验证码"); System.out.println("调用验证码服务,获取验证码");
ResponseResult<NumberCodeResponse> responseResult = serviceVefificationcodeClient.getNumberCode(6); ResponseResult<NumberCodeResponse> responseResult = serviceVefificationcodeClient.getNumberCode(6);
@ -20,25 +34,33 @@ public class VerificationCodeService {
//存入redis //存入redis
System.out.println("存入redis"); System.out.println("存入redis");
String key = verificationCodePrefix + numberCode;
stringRedisTemplate.opsForValue().set(key, "" + numberCode,2, TimeUnit.MINUTES);
JSONObject jsonObject = new JSONObject(); //发送短信。。。
jsonObject.put("code",1);
jsonObject.put("message","success");
return jsonObject.toString();
return ResponseResult.success("");
}
/**
*
* @param passengerPhone
* @param verificationCode
* @return
*/
public ResponseResult checkCode(String passengerPhone, String verificationCode) {
System.out.println("根据手机号和验证码查询redis");
System.out.println("校验验证码");
System.out.println("判断是否有用户,没有则新增用户");
System.out.println("颁发token");
TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setToken("my token");
return ResponseResult.success(tokenResponse);
} }
} }

@ -8,3 +8,7 @@ spring:
nacos: nacos:
discovery: discovery:
server-addr: 127.0.0.1:8848 server-addr: 127.0.0.1:8848
redis:
port: 6379
host: 192.168.1.10
database: 0

@ -8,3 +8,7 @@ spring:
nacos: nacos:
discovery: discovery:
server-addr: 127.0.0.1:8848 server-addr: 127.0.0.1:8848
redis:
port: 6379
host: 192.168.1.10
database: 0

@ -11,6 +11,16 @@ 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.getMsg());
}
/** /**
* *
* @param data * @param data

@ -0,0 +1,8 @@
package com.mashibing.common.response;
import lombok.Data;
@Data
public class TokenResponse {
private String token;
}
Loading…
Cancel
Save