完成了验证码的openfeign调用

main
XiaoHH 2 years ago
parent 9f0532f523
commit 80e298d8fa

@ -48,6 +48,24 @@
<groupId>com.greateme</groupId> <groupId>com.greateme</groupId>
<artifactId>common-entity</artifactId> <artifactId>common-entity</artifactId>
</dependency> </dependency>
<!-- 验证码模块的远程调用 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>verification-code-client</artifactId>
</dependency>
<!-- 公共的注解模块 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>common-annotation</artifactId>
</dependency>
<!-- 验证码模块的远程调用 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>verification-code-client</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -1,7 +1,8 @@
package com.greateme.passenger; package com.greateme.passenger;
import com.greateme.springboot.EnableTaxiFeignClients;
import com.greateme.springboot.TaxiSpringBootApplication;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/** /**
@ -14,8 +15,9 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
* @date 2023-05-30 18:17:31 * @date 2023-05-30 18:17:31
* @file ApiPassengerApplication.java * @file ApiPassengerApplication.java
*/ */
@EnableTaxiFeignClients
@EnableDiscoveryClient @EnableDiscoveryClient
@SpringBootApplication @TaxiSpringBootApplication
public class ApiPassengerApplication { public class ApiPassengerApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -1,9 +1,10 @@
package com.greateme.passenger.controller; package com.greateme.passenger.controller;
import com.greateme.entity.respose.ResponseEntity; import com.greateme.entity.respose.ResponseEntity;
import com.greateme.passenger.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.passenger.service.PassengerVerificationService; import com.greateme.passenger.service.PassengerVerificationService;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping; 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.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -26,8 +27,19 @@ public class PassengerVerificationController {
@Autowired @Autowired
private PassengerVerificationService verificationService; private PassengerVerificationService verificationService;
/**
*
*
* @param verificationCode
* @return token
*/
@PostMapping("/code") @PostMapping("/code")
public ResponseEntity<String> verificationCode(@RequestBody PassengerVerificationCodeDTO verificationCode) { public ResponseEntity<String> verificationCode(@RequestBody PassengerVerificationCodeDTO verificationCode) {
return ResponseEntity.success(this.verificationService.generationPhoneVerificationCode(verificationCode)); String token = this.verificationService.generationPhoneVerificationCode(verificationCode);
if (StringUtils.hasLength(token)) {
return ResponseEntity.success(token);
} else {
return ResponseEntity.error("生成短信验证码失败");
}
} }
} }

@ -1,26 +0,0 @@
package com.greateme.passenger.entity.vo;
import java.io.Serializable;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-30 19:40:35
* @file PassengerVerificationCodeVO.java
*/
public class PassengerVerificationCodeVO implements Serializable {
/**
*
*/
private String verificationCode;
/**
*
*/
private String verificationToken;
}

@ -1,6 +1,6 @@
package com.greateme.passenger.service; package com.greateme.passenger.service;
import com.greateme.passenger.entity.dto.PassengerVerificationCodeDTO; import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
/** /**
* <p> * <p>

@ -1,11 +1,16 @@
package com.greateme.passenger.service.impl; package com.greateme.passenger.service.impl;
import com.greateme.passenger.entity.dto.PassengerVerificationCodeDTO; import com.greateme.contant.http.HttpStatus;
import com.greateme.passenger.entity.dto.PassengerVerificationCodeRedisDTO; import com.greateme.entity.respose.ResponseEntity;
import com.greateme.passenger.service.PassengerVerificationService; import com.greateme.passenger.service.PassengerVerificationService;
import com.greateme.util.StringUtil; 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.feign.VerificationCodeRemote;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.UUID; import java.util.UUID;
@ -28,6 +33,12 @@ public class PassengerVerificationServiceImpl implements PassengerVerificationSe
*/ */
private static final Logger log = LoggerFactory.getLogger(PassengerVerificationServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(PassengerVerificationServiceImpl.class);
/**
*
*/
@Autowired
private VerificationCodeRemote verificationCodeRemote;
/** /**
* *
* *
@ -36,16 +47,15 @@ public class PassengerVerificationServiceImpl implements PassengerVerificationSe
*/ */
@Override @Override
public String generationPhoneVerificationCode(PassengerVerificationCodeDTO verificationCode) { public String generationPhoneVerificationCode(PassengerVerificationCodeDTO verificationCode) {
// 生成6位的手机验证码 ResponseEntity<PassengerVerificationCodeVO> passengerVerificationCodeEntity = verificationCodeRemote.passengerPhone(verificationCode, 6);
String code = StringUtil.generationVerificationCode(6); if (HttpStatus.SUCCESS.getCode() == passengerVerificationCodeEntity.getCode()) {
// 生成对应手机验证码的token PassengerVerificationCodeVO passengerVerificationCode = passengerVerificationCodeEntity.getDate();
String token = UUID.randomUUID().toString(); if (null != passengerVerificationCode) {
// 初始化存入redis当中的对象 log.info("成功生成验证码:{}验证码的token为{}", passengerVerificationCode.getVerificationCode(), passengerVerificationCode.getVerificationToken());
PassengerVerificationCodeRedisDTO verificationCodeRedis = new PassengerVerificationCodeRedisDTO(); return passengerVerificationCode.getVerificationToken();
verificationCodeRedis.setVerificationCode(code); }
verificationCodeRedis.setPassengerPhone(verificationCode.getPassengerPhone()); }
verificationCodeRedis.setToken(token); log.error("获取验证码的时候发生了错误,错误码:{},错误信息:{}", passengerVerificationCodeEntity.getCode(), passengerVerificationCodeEntity.getMessage());
log.info("成功生成短信验证码{},手机号码:{}token{}即将将验证码传入redis存储", verificationCodeRedis.getVerificationCode(), verificationCodeRedis.getPassengerPhone(), verificationCodeRedis.getToken()); return null;
return token;
} }
} }

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greateme</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<description>公共的注解模块</description>
<artifactId>common-annotation</artifactId>
<dependencies>
<!-- SpringBoot的相关依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 公共常量类模块 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>common-contant</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,25 @@
package com.greateme.springboot;
import com.greateme.contant.PackageContent;
import org.springframework.cloud.openfeign.EnableFeignClients;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>
* feign
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 14:54:30
* @file EnableTaxiFeignClients.java
*/
@EnableFeignClients(basePackages = PackageContent.SPRING_BOOT_SCAN_PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface EnableTaxiFeignClients {
}

@ -0,0 +1,25 @@
package com.greateme.springboot;
import com.greateme.contant.PackageContent;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>
* SpringBoot
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 14:18:59
* @file TaxiSpringBootApplication.java
*/
@SpringBootApplication(scanBasePackages = PackageContent.SPRING_BOOT_SCAN_PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface TaxiSpringBootApplication {
}

@ -0,0 +1,25 @@
package com.greateme.contant;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 14:16:08
* @file PackageContent.java
*/
public class PackageContent {
/**
*
*/
private PackageContent() {
}
/**
* SpringBoot
*/
public static final String SPRING_BOOT_SCAN_PACKAGE = "com.greateme";
}

@ -2,7 +2,7 @@ package com.greateme.contant.http;
/** /**
* <p> * <p>
* http * http
* </p> * </p>
* *
* @author XiaoHH * @author XiaoHH
@ -10,21 +10,37 @@ package com.greateme.contant.http;
* @date 2023-05-30 20:19:15 * @date 2023-05-30 20:19:15
* @file HttpStatus.java * @file HttpStatus.java
*/ */
public class HttpStatus { public enum HttpStatus {
/**
*
*/
SUCCESS(200, "Success"),
/** /**
* *
*/ */
private HttpStatus() { ERROR(500, "Error");
HttpStatus(int code, String message) {
this.code = code;
this.message = message;
} }
/** /**
* * http
*/ */
public static final int SUCCESS = 200; private final int code;
/** /**
* * http
*/ */
public static final String SUCCESS_MESSAGE = "Success"; private final String message;
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
} }

@ -0,0 +1,30 @@
package com.greateme.contant.http;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023_05_31 14:28:04
* @file ServiceNameConstant.java
*/
public class ServiceNameConstant {
/**
*
*/
private ServiceNameConstant() {
}
/**
* api_passenger
*/
public static final String API_PASSENGER = "api-passenger";
/**
* service-verification-code
*/
public static final String SERVICE_VERIFICATION_CODE = "service-verification-code";
}

@ -33,6 +33,39 @@ public class ResponseEntity<T> implements Serializable {
this.message = message; this.message = message;
} }
/**
* HttpStatus
*
* @param httpStatus http
* @param date
*/
public ResponseEntity(HttpStatus httpStatus, T date) {
this.code = httpStatus.getCode();
this.message = httpStatus.getMessage();
this.date = date;
}
/**
*
*
* @param code
* @param message
*/
public ResponseEntity(int code, String message) {
this.code = code;
this.message = message;
}
/**
* HttpStatus
*
* @param httpStatus http
*/
public ResponseEntity(HttpStatus httpStatus) {
this.code = httpStatus.getCode();
this.message = httpStatus.getMessage();
}
/** /**
* *
* 200 => * 200 =>
@ -58,7 +91,28 @@ public class ResponseEntity<T> implements Serializable {
* @return * @return
*/ */
public static <T> ResponseEntity<T> success(T data) { public static <T> ResponseEntity<T> success(T data) {
return new ResponseEntity<>(HttpStatus.SUCCESS, data, HttpStatus.SUCCESS_MESSAGE); return new ResponseEntity<>(HttpStatus.SUCCESS, data);
}
/**
*
*
* @param <T>
* @return
*/
public static <T> ResponseEntity<T> error() {
return new ResponseEntity<>(HttpStatus.ERROR);
}
/**
*
*
* @param message
* @param <T>
* @return
*/
public static <T> ResponseEntity<T> error(String message) {
return new ResponseEntity<>(HttpStatus.ERROR.getCode(), message);
} }
public int getCode() { public int getCode() {

@ -25,11 +25,6 @@ public class StringUtil {
* @return * @return
*/ */
public static String generationVerificationCode(int length) { public static String generationVerificationCode(int length) {
StringBuilder result = new StringBuilder(length); return String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, length -1)));
for (int i = 0; i < length; i++) {
int num = (int) (Math.random() * 10) + 1;
result.append(num % 10);
}
return result.toString();
} }
} }

@ -15,6 +15,7 @@
<module>common-entity</module> <module>common-entity</module>
<module>common-util</module> <module>common-util</module>
<module>common-contant</module> <module>common-contant</module>
<module>common-annotation</module>
</modules> </modules>
<artifactId>common</artifactId> <artifactId>common</artifactId>

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greateme</groupId>
<artifactId>online-taxi-public</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<description>远程调用模块聚合项目</description>
<artifactId>feign-client</artifactId>
<packaging>pom</packaging>
<modules>
<module>verification-code-client</module>
</modules>
</project>

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greateme</groupId>
<artifactId>feign-client</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<description>验证码模块的远程调用</description>
<artifactId>verification-code-client</artifactId>
<dependencies>
<!-- 远程调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 负载均衡器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- 公共实体类模块 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>common-entity</artifactId>
</dependency>
</dependencies>
</project>

@ -1,4 +1,4 @@
package com.greateme.passenger.entity.dto; package com.greateme.verification.entity.dto;
import com.greateme.util.JsonUtil; import com.greateme.util.JsonUtil;

@ -1,4 +1,4 @@
package com.greateme.passenger.entity.dto; package com.greateme.verification.entity.dto;
import com.greateme.util.JsonUtil; import com.greateme.util.JsonUtil;

@ -0,0 +1,49 @@
package com.greateme.verification.entity.vo;
import com.greateme.util.JsonUtil;
import java.io.Serializable;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-30 19:40:35
* @file PassengerVerificationCodeVO.java
*/
public class PassengerVerificationCodeVO implements Serializable {
/**
*
*/
private String verificationCode;
/**
* token
*/
private String verificationToken;
public String getVerificationCode() {
return verificationCode;
}
public void setVerificationCode(String verificationCode) {
this.verificationCode = verificationCode;
}
public String getVerificationToken() {
return verificationToken;
}
public void setVerificationToken(String verificationToken) {
this.verificationToken = verificationToken;
}
@Override
public String toString() {
return JsonUtil.toJson(this);
}
}

@ -0,0 +1,35 @@
package com.greateme.verification.feign;
import com.greateme.contant.http.ServiceNameConstant;
import com.greateme.entity.respose.ResponseEntity;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
import com.greateme.verification.feign.fallback.VerificationCodeFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 14:32:59
* @file VerificationCodeRemote.java
*/
@FeignClient(contextId = "verificationCodeRemote", value = ServiceNameConstant.SERVICE_VERIFICATION_CODE, fallback = VerificationCodeFallback.class)
public interface VerificationCodeRemote {
/**
*
*
* @param verificationCode
* @param length
* @return
*/
@PostMapping("/verificationCode/passengerPhone/{length}")
ResponseEntity<PassengerVerificationCodeVO> passengerPhone(@RequestBody PassengerVerificationCodeDTO verificationCode, @PathVariable("length") int length);
}

@ -0,0 +1,48 @@
package com.greateme.verification.feign.fallback;
import com.greateme.entity.respose.ResponseEntity;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
import com.greateme.verification.feign.VerificationCodeRemote;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 14:43:10
* @file VerificationCodeFallback.java
*/
@Component
public class VerificationCodeFallback implements FallbackFactory<VerificationCodeRemote> {
/**
*
*/
private static final Logger log = LoggerFactory.getLogger(VerificationCodeFallback.class);
@Override
public VerificationCodeRemote create(Throwable cause) {
return new VerificationCodeRemote() {
/**
*
*
* @param verificationCode
* @param length
* @return
*/
@Override
public ResponseEntity<PassengerVerificationCodeVO> passengerPhone(PassengerVerificationCodeDTO verificationCode, int length) {
log.error("调用远程发送验证码的接口发生了错误,手机号码:{},验证码长度:{}", verificationCode.getPassengerPhone(), length);
return ResponseEntity.error();
}
};
}
}

@ -12,6 +12,7 @@
<module>api</module> <module>api</module>
<module>common</module> <module>common</module>
<module>service</module> <module>service</module>
<module>feign-client</module>
</modules> </modules>
<description>马士兵的滴约打车项目</description> <description>马士兵的滴约打车项目</description>
@ -83,6 +84,20 @@
<artifactId>common-util</artifactId> <artifactId>common-util</artifactId>
<version>${online-taxi-public.version}</version> <version>${online-taxi-public.version}</version>
</dependency> </dependency>
<!-- 验证码模块的远程调用 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>verification-code-client</artifactId>
<version>${online-taxi-public.version}</version>
</dependency>
<!-- 公共的注解模块 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>common-annotation</artifactId>
<version>${online-taxi-public.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
</project> </project>

@ -8,15 +8,11 @@
<artifactId>service</artifactId> <artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>
<packaging>jar</packaging>
<description>验证码服务模块</description>
<artifactId>service-verification-code</artifactId> <artifactId>service-verification-code</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies> <dependencies>
<!-- Spring Web --> <!-- Spring Web -->
<dependency> <dependency>
@ -52,6 +48,18 @@
<groupId>com.greateme</groupId> <groupId>com.greateme</groupId>
<artifactId>common-entity</artifactId> <artifactId>common-entity</artifactId>
</dependency> </dependency>
<!-- 验证码模块的远程调用 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>verification-code-client</artifactId>
</dependency>
<!-- 公共的注解模块 -->
<dependency>
<groupId>com.greateme</groupId>
<artifactId>common-annotation</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -1,7 +1,7 @@
package com.greateme.verification; package com.greateme.verification;
import com.greateme.springboot.TaxiSpringBootApplication;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/** /**
@ -15,7 +15,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
* @file VerificationCodeApplication.java * @file VerificationCodeApplication.java
*/ */
@EnableDiscoveryClient @EnableDiscoveryClient
@SpringBootApplication @TaxiSpringBootApplication
public class VerificationCodeApplication { public class VerificationCodeApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(VerificationCodeApplication.class, args); SpringApplication.run(VerificationCodeApplication.class, args);

@ -0,0 +1,38 @@
package com.greateme.verification.controller;
import com.greateme.entity.respose.ResponseEntity;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
import com.greateme.verification.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 11:49:00
* @file VerificationCodeController.java
*/
@RequestMapping("/verificationCode")
@RestController
public class VerificationCodeController {
@Autowired
private VerificationCodeService verificationCodeService;
/**
*
*
* @param verificationCode
* @param length
* @return
*/
@PostMapping("/passengerPhone/{length}")
public ResponseEntity<PassengerVerificationCodeVO> passengerPhone(@RequestBody PassengerVerificationCodeDTO verificationCode, @PathVariable("length") int length) {
return ResponseEntity.success(this.verificationCodeService.generationPassengerPhone(verificationCode, length));
}
}

@ -0,0 +1,26 @@
package com.greateme.verification.service;
import com.greateme.verification.entity.dto.PassengerVerificationCodeDTO;
import com.greateme.verification.entity.vo.PassengerVerificationCodeVO;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 13:56:40
* @file VerificationCodeServiceImpl.java
*/
public interface VerificationCodeService {
/**
*
*
* @param verificationCode
* @param length
* @return
*/
PassengerVerificationCodeVO generationPassengerPhone(PassengerVerificationCodeDTO verificationCode, int length);
}

@ -0,0 +1,50 @@
package com.greateme.verification.service.impl;
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.springframework.stereotype.Service;
import java.util.UUID;
/**
* <p>
*
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-05-31 13:58:24
* @file VerificationCodeServiceImpl.java
*/
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {
/**
*
*
* @param verificationCode
* @param length
* @return
*/
@Override
public PassengerVerificationCodeVO generationPassengerPhone(PassengerVerificationCodeDTO verificationCode, int length) {
// 生成6位的手机验证码
String code = StringUtil.generationVerificationCode(length);
// 生成对应手机验证码的token
String token = UUID.randomUUID().toString();
// 初始化存入redis当中的对象
PassengerVerificationCodeRedisDTO verificationCodeRedis = new PassengerVerificationCodeRedisDTO();
verificationCodeRedis.setVerificationCode(code);
verificationCodeRedis.setPassengerPhone(verificationCode.getPassengerPhone());
verificationCodeRedis.setToken(token);
// TODO 将 verificationCodeRedis 存入redis
// 将token和code返回
PassengerVerificationCodeVO result = new PassengerVerificationCodeVO();
result.setVerificationCode(code);
result.setVerificationToken(token);
return result;
}
}
Loading…
Cancel
Save