Unverified Commit 7b38967f authored by ZackYoung's avatar ZackYoung Committed by GitHub

[Optimize]optimize_some_code (#2198)

* optimize_some_code * optimize_some_code * optimize_some_code
parent 6fdf8577
......@@ -28,6 +28,7 @@ import org.dinky.utils.I18nMsgUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
......@@ -46,6 +47,7 @@ import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
/**
......@@ -67,6 +69,17 @@ public class WebExceptionHandler {
return Result.failed(e.getMsg());
}
private static final Map<String, Status> ERR_CODE_MAPPING =
MapUtil.<String, Status>builder()
.put(NotLoginException.NOT_TOKEN, Status.NOT_TOKEN)
.put(NotLoginException.INVALID_TOKEN, Status.INVALID_TOKEN)
.put(NotLoginException.TOKEN_TIMEOUT, Status.EXPIRED_TOKEN)
.put(NotLoginException.BE_REPLACED, Status.BE_REPLACED)
.put(NotLoginException.KICK_OUT, Status.KICK_OUT)
.put(NotLoginException.TOKEN_FREEZE, Status.TOKEN_FREEZED)
.put(NotLoginException.NO_PREFIX, Status.NO_PREFIX)
.build();
@ExceptionHandler
public Result<Void> notLoginException(NotLoginException notLoginException) {
ServletRequestAttributes servletRequestAttributes =
......@@ -75,23 +88,10 @@ public class WebExceptionHandler {
if (response != null) {
response.setStatus(CodeEnum.NOTLOGIN.getCode());
}
if (notLoginException.getType().equals(NotLoginException.NOT_TOKEN)) {
return Result.failed(Status.NOT_TOKEN);
} else if (notLoginException.getType().equals(NotLoginException.INVALID_TOKEN)) {
return Result.failed(Status.INVALID_TOKEN);
} else if (notLoginException.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
return Result.failed(Status.EXPIRED_TOKEN);
} else if (notLoginException.getType().equals(NotLoginException.BE_REPLACED)) {
return Result.failed(Status.BE_REPLACED);
} else if (notLoginException.getType().equals(NotLoginException.KICK_OUT)) {
return Result.failed(Status.KICK_OUT);
} else if (notLoginException.getType().equals(NotLoginException.TOKEN_FREEZE)) {
return Result.failed(Status.TOKEN_FREEZED);
} else if (notLoginException.getType().equals(NotLoginException.NO_PREFIX)) {
return Result.failed(Status.NO_PREFIX);
} else {
return Result.failed(Status.USER_NOT_LOGIN);
}
String type = notLoginException.getType();
Status status = ERR_CODE_MAPPING.getOrDefault(type, Status.NOT_TOKEN);
return Result.failed(status);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment