From b7d3e07d1639e94283d7c2fbf855c04444a5c410 Mon Sep 17 00:00:00 2001 From: MichaelWin Date: Tue, 25 Nov 2025 16:33:56 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=94=B9=E8=BF=9B=E3=80=91=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E7=B1=BB=E5=9E=8B=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../corewing/app/dto/api/SendCodeRequest.java | 2 +- .../service/impl/VerifyCodeServiceImpl.java | 24 +++---------------- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/corewing/app/dto/api/SendCodeRequest.java b/src/main/java/com/corewing/app/dto/api/SendCodeRequest.java index 808eaf7..22e72f8 100644 --- a/src/main/java/com/corewing/app/dto/api/SendCodeRequest.java +++ b/src/main/java/com/corewing/app/dto/api/SendCodeRequest.java @@ -17,7 +17,7 @@ public class SendCodeRequest { private String account; /** - * 验证码类型:register-注册, login-登录, reset-重置密码 + * 验证码类型:register-注册, login-登录, reset-重置密码, forget-忘记密码 */ @NotBlank(message = "验证码类型不能为空") private String type; diff --git a/src/main/java/com/corewing/app/service/impl/VerifyCodeServiceImpl.java b/src/main/java/com/corewing/app/service/impl/VerifyCodeServiceImpl.java index 4b7981a..ff3cf3d 100644 --- a/src/main/java/com/corewing/app/service/impl/VerifyCodeServiceImpl.java +++ b/src/main/java/com/corewing/app/service/impl/VerifyCodeServiceImpl.java @@ -1,16 +1,12 @@ package com.corewing.app.service.impl; import com.corewing.app.service.VerifyCodeService; -import com.corewing.app.util.EmailUtil; -import com.corewing.app.util.I18nUtil; -import com.corewing.app.util.RedisUtil; -import com.corewing.app.util.SmsBaoUtil; +import com.corewing.app.util.*; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.util.Random; -import java.util.regex.Pattern; /** * 验证码服务实现类 @@ -33,16 +29,6 @@ public class VerifyCodeServiceImpl implements VerifyCodeService { */ private static final int CODE_LENGTH = 6; - /** - * 手机号正则 - */ - private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$"); - - /** - * 邮箱正则 - */ - private static final Pattern EMAIL_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"); - public VerifyCodeServiceImpl(RedisUtil redisUtil, SmsBaoUtil smsBaoUtil, EmailUtil emailUtil) { this.redisUtil = redisUtil; this.smsBaoUtil = smsBaoUtil; @@ -59,11 +45,7 @@ public class VerifyCodeServiceImpl implements VerifyCodeService { throw new RuntimeException(I18nUtil.getMessage("error.verify.type.empty")); } - // 判断是手机号还是邮箱 - boolean isPhone = PHONE_PATTERN.matcher(account).matches(); - boolean isEmail = EMAIL_PATTERN.matcher(account).matches(); - - if (!isPhone && !isEmail) { + if (!AccountUtil.isPhone(account) && !AccountUtil.isEmail(account)) { throw new RuntimeException(I18nUtil.getMessage("error.account.format.invalid")); } @@ -77,7 +59,7 @@ public class VerifyCodeServiceImpl implements VerifyCodeService { log.info("验证码已生成: account={}, type={}, code={}", account, type, code); // 发送验证码 - if (isPhone) { + if (AccountUtil.isPhone(account)) { // 发送短信验证码 boolean success = smsBaoUtil.sendVerifyCode(account, code); if (!success) {