From ae0417c77ffb9885356ec8c495ef8f305ff4163d Mon Sep 17 00:00:00 2001 From: heshunme Date: Tue, 2 Jul 2024 22:29:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthenticationController.java | 22 +++----- .../dto/VerificationCodeResponse.java | 13 +++++ .../service/VerificationCodeService.java | 51 +++++++++++++++++++ 3 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/cmh/backend/authentication/dto/VerificationCodeResponse.java create mode 100644 src/main/java/org/cmh/backend/authentication/service/VerificationCodeService.java diff --git a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java index e58b5f1..e30cd9b 100644 --- a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java +++ b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java @@ -6,20 +6,21 @@ import org.cmh.backend.Utils.JwtVerify; import org.cmh.backend.authentication.dto.*; import org.cmh.backend.authentication.model.UserHS; import org.cmh.backend.authentication.service.UserService; +import org.cmh.backend.authentication.service.VerificationCodeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.HashMap; -import java.util.Map; - @RestController class AuthenticationController { @Autowired private UserService userService; + @Autowired + private VerificationCodeService verificationCodeService; + @GetMapping("/hello") public String hello() { return "Hello SpringBoot!"; @@ -99,18 +100,9 @@ class AuthenticationController { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - @PostMapping("/sendVerificationCode") - public ResponseEntity> sendVerificationCode(@RequestBody Map request) { - String contactInfo = request.get("contactInfo"); -// TODO:发送验证码功能有待添加。 boolean isSent = userService.sendVerificationCode(contactInfo); - boolean isSent = true; - if (isSent) { - HashMap response = new HashMap<>(); - response.put("code", 1234); - return new ResponseEntity<>(response, HttpStatus.OK); - } else { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } + @GetMapping("/getVerificationCode") + public ResponseEntity getVerificationCode() { + return new ResponseEntity<>(verificationCodeService.provideVerificationCode(), HttpStatus.OK); } diff --git a/src/main/java/org/cmh/backend/authentication/dto/VerificationCodeResponse.java b/src/main/java/org/cmh/backend/authentication/dto/VerificationCodeResponse.java new file mode 100644 index 0000000..08daef3 --- /dev/null +++ b/src/main/java/org/cmh/backend/authentication/dto/VerificationCodeResponse.java @@ -0,0 +1,13 @@ +package org.cmh.backend.authentication.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class VerificationCodeResponse { + private String code; + private String path; +} diff --git a/src/main/java/org/cmh/backend/authentication/service/VerificationCodeService.java b/src/main/java/org/cmh/backend/authentication/service/VerificationCodeService.java new file mode 100644 index 0000000..f82d36d --- /dev/null +++ b/src/main/java/org/cmh/backend/authentication/service/VerificationCodeService.java @@ -0,0 +1,51 @@ +package org.cmh.backend.authentication.service; + +import jakarta.annotation.PostConstruct; +import lombok.Getter; +import org.cmh.backend.authentication.dto.VerificationCodeResponse; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.stream.Stream; + +@Service +public class VerificationCodeService { + + @Value("${verification.code.images.path}") + private String verificationCodeImagesPath; + + @Getter + private List verificationCodeList = new ArrayList<>(); + + @PostConstruct + public void init() { + loadVerificationCodeImages(); + } + + private void loadVerificationCodeImages() { + try (Stream paths = Files.list(Paths.get(verificationCodeImagesPath))) { + paths.filter(path -> path.toString().endsWith(".png")) + .forEach(path -> verificationCodeList.add(path.getFileName().toString())); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public VerificationCodeResponse provideVerificationCode() { + if (verificationCodeList.isEmpty()) { + return null; + } + Random random = new Random(); + int code = random.nextInt(random.nextInt(verificationCodeList.size())); + String selectedCode = verificationCodeList.get(code); + return new VerificationCodeResponse(selectedCode.split("\\.")[0], "/verificationCodeImages/" + selectedCode); + } + +} \ No newline at end of file