SmsController.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.config.Constants;
  3. import com.izouma.nineth.exception.BusinessException;
  4. import com.izouma.nineth.service.CaptchaService;
  5. import com.izouma.nineth.service.sms.SmsService;
  6. import lombok.AllArgsConstructor;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import java.util.regex.Pattern;
  12. @RestController
  13. @RequestMapping("/sms")
  14. @AllArgsConstructor
  15. public class SmsController {
  16. private SmsService smsService;
  17. private CaptchaService captchaService;
  18. @GetMapping("/sendVerify")
  19. public String sendVerify(@RequestParam String phone) {
  20. if (!Pattern.matches(Constants.Regex.PHONE, phone)) {
  21. throw new BusinessException("请输入正确的手机号");
  22. }
  23. return smsService.sendVerify(phone);
  24. }
  25. @GetMapping("/sendCaptchaVerify")
  26. public String sendVerify(@RequestParam String phone, @RequestParam String captcha, @RequestParam String captchaKey) {
  27. if (!Pattern.matches(Constants.Regex.PHONE, phone)) {
  28. throw new BusinessException("请输入正确的手机号");
  29. }
  30. boolean verify = captchaService.verify(captchaKey, captcha);
  31. if (!verify) {
  32. throw new BusinessException("验证码错误");
  33. }
  34. return smsService.sendVerify(phone);
  35. }
  36. @GetMapping("/verify")
  37. public String verify(@RequestParam String phone, @RequestParam String code) throws SmsService.SmsVerifyException {
  38. return smsService.verify(phone, code);
  39. }
  40. }