Skip to content

Commit

Permalink
fix: 调整代码位置,更改代码逻辑,还有点待做明天搞
Browse files Browse the repository at this point in the history
  • Loading branch information
BanTanger committed Jan 17, 2024
1 parent 2eef345 commit f880b38
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum GlobalServiceStatusCode {
PARAM_IS_BLANK(1002, "参数为空"),
PARAM_TYPE_ERROR(1003, "参数类型错误"),
PARAM_NOT_COMPLETE(1004, "参数缺失"),
PARAM_FAILED_VALIDATE(1005, "参数未通过验证"),

/* 用户错误 2001-3000 */
USER_NOT_LOGIN(2001, "用户未登录"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void sendIdentifyingCode(String email, String code) {
public void checkIdentifyingCode(String email, String code) {
String redisKey = IdentifyingCodeValidator.REDIS_EMAIL_IDENTIFYING_CODE + email;
Object data = emailRepository.getIdentifyingCode(redisKey).orElseGet(() -> {
// todo 这些都可以写 枚举,然后直接 Enum.getMessage 就行了
throw new EmailIdentifyingException("邮箱不存在/用户未获取验证码/验证码过期/用户已验证");
});
// 取出验证码和过期时间点
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
* Time: 12:14
*/
public class EmailIdentifyingException extends RuntimeException {

// todo 待修改
public EmailIdentifyingException(String message) {
super(message);
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/achobeta/handler/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import com.achobeta.common.SystemJsonResponse;
import com.achobeta.exception.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;
import java.util.stream.Collectors;

import static com.achobeta.common.constants.GlobalServiceStatusCode.*;

/**
Expand Down Expand Up @@ -71,4 +76,17 @@ public SystemJsonResponse handleIllegalEmailException(IllegalEmailException e, H
return SystemJsonResponse.CUSTOMIZE_MSG_ERROR(USER_NO_EMAIL_VALIDATION_FAIL, "邮箱有效性校验不通过");
}

/**
* 自定义验证异常
*/
@ExceptionHandler(ConstraintViolationException.class)
public SystemJsonResponse constraintViolationException(ConstraintViolationException e) {
log.error("自定义验证异常'{}'", e.getMessage());
String message = e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.filter(Objects::nonNull)
.collect(Collectors.joining("\n"));
return SystemJsonResponse.CUSTOMIZE_MSG_ERROR(PARAM_FAILED_VALIDATE, message);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.achobeta.domain.excel.util;
package com.achobeta.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
Expand All @@ -17,20 +17,6 @@ public class ExcelUtil {

private static final String DEFAULT_SUFFIX = ".xlsx";

private static String tryCreateFile(String filePath, String fileName) throws IOException {
// 文件夹是否存在,若没有对应文件夹直接根据路径生成文件会报错
File directory = new File(filePath);
if (!directory.exists() && !directory.isDirectory()) {
directory.mkdirs();
}
// 文件是否存在
String path = filePath + fileName + DEFAULT_SUFFIX;
File file = new File(path);
if (!file.exists()){
file.createNewFile();
}
return path;
}
/**
* 打印表格
* @param title 表格标题
Expand Down Expand Up @@ -63,4 +49,20 @@ public static <T> void exportXlsxFile(String title, String sheetName,
log.warn(e.getMessage());
}
}

private static String tryCreateFile(String filePath, String fileName) throws IOException {
// 文件夹是否存在,若没有对应文件夹直接根据路径生成文件会报错
File directory = new File(filePath);
if (!directory.exists() && !directory.isDirectory()) {
directory.mkdirs();
}
// 文件是否存在
String path = filePath + fileName + DEFAULT_SUFFIX;
File file = new File(path);
if (!file.exists()){
file.createNewFile();
}
return path;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.achobeta.domain.email.component;
package com.achobeta.util;

import cn.hutool.extra.spring.SpringUtil;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validator;

import java.util.Set;
Expand All @@ -10,12 +11,11 @@ public class ValidatorUtils {

private static final Validator validator = SpringUtil.getBean(Validator.class);

public static <T> String validateEntity(T object, Class<?>... groups) {
public static <T> void validate(T object, Class<?>... groups) {
Set<ConstraintViolation<T>> validate = validator.validate(object, groups);
StringBuilder stringBuilder = new StringBuilder();
for (ConstraintViolation<T> o : validate) {
stringBuilder.append(o.getMessage() + "\n");
if (!validate.isEmpty()) {
throw new ConstraintViolationException("参数校验异常", validate);
}
return stringBuilder.toString();
}

}

0 comments on commit f880b38

Please sign in to comment.