【改进】账号类型校验

This commit is contained in:
2025-11-25 16:33:56 +08:00
parent 35ac70e82d
commit b7d3e07d16
2 changed files with 4 additions and 22 deletions

View File

@@ -17,7 +17,7 @@ public class SendCodeRequest {
private String account; private String account;
/** /**
* 验证码类型register-注册, login-登录, reset-重置密码 * 验证码类型register-注册, login-登录, reset-重置密码, forget-忘记密码
*/ */
@NotBlank(message = "验证码类型不能为空") @NotBlank(message = "验证码类型不能为空")
private String type; private String type;

View File

@@ -1,16 +1,12 @@
package com.corewing.app.service.impl; package com.corewing.app.service.impl;
import com.corewing.app.service.VerifyCodeService; import com.corewing.app.service.VerifyCodeService;
import com.corewing.app.util.EmailUtil; import com.corewing.app.util.*;
import com.corewing.app.util.I18nUtil;
import com.corewing.app.util.RedisUtil;
import com.corewing.app.util.SmsBaoUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.Random; 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 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) { public VerifyCodeServiceImpl(RedisUtil redisUtil, SmsBaoUtil smsBaoUtil, EmailUtil emailUtil) {
this.redisUtil = redisUtil; this.redisUtil = redisUtil;
this.smsBaoUtil = smsBaoUtil; this.smsBaoUtil = smsBaoUtil;
@@ -59,11 +45,7 @@ public class VerifyCodeServiceImpl implements VerifyCodeService {
throw new RuntimeException(I18nUtil.getMessage("error.verify.type.empty")); throw new RuntimeException(I18nUtil.getMessage("error.verify.type.empty"));
} }
// 判断是手机号还是邮箱 if (!AccountUtil.isPhone(account) && !AccountUtil.isEmail(account)) {
boolean isPhone = PHONE_PATTERN.matcher(account).matches();
boolean isEmail = EMAIL_PATTERN.matcher(account).matches();
if (!isPhone && !isEmail) {
throw new RuntimeException(I18nUtil.getMessage("error.account.format.invalid")); 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); log.info("验证码已生成: account={}, type={}, code={}", account, type, code);
// 发送验证码 // 发送验证码
if (isPhone) { if (AccountUtil.isPhone(account)) {
// 发送短信验证码 // 发送短信验证码
boolean success = smsBaoUtil.sendVerifyCode(account, code); boolean success = smsBaoUtil.sendVerifyCode(account, code);
if (!success) { if (!success) {