共通字段自动填充

This commit is contained in:
AlanScipio
2024-02-06 14:16:16 +08:00
parent 6b6658f9a1
commit 09b96b5cfa
25 changed files with 599 additions and 334 deletions

View File

@@ -0,0 +1,19 @@
package com.ruoyi.common.core.exception.auth;
import java.io.Serial;
/**
* 登录过期异常
*
* @author Alan Scipio
* created on 2024/2/6
*/
public class LoginExpiredException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;
public LoginExpiredException(String message) {
super(message);
}
}

View File

@@ -1,16 +1,17 @@
package com.ruoyi.common.core.exception.auth;
import java.io.Serial;
/**
* 未能通过的登录认证异常
*
*
* @author ruoyi
*/
public class NotLoginException extends RuntimeException
{
public class NotLoginException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;
public NotLoginException(String message)
{
public NotLoginException(String message) {
super(message);
}
}

View File

@@ -1,79 +1,72 @@
package com.ruoyi.common.core.exception.base;
import java.io.Serial;
/**
* 基础异常
*
*
* @author ruoyi
*/
public class BaseException extends RuntimeException
{
public class BaseException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;
/**
* 所属模块
*/
private String module;
private final String module;
/**
* 错误码
*/
private String code;
private final String code;
/**
* 错误码对应的参数
*/
private Object[] args;
private final Object[] args;
/**
* 错误消息
*/
private String defaultMessage;
private final String defaultMessage;
public BaseException(String module, String code, Object[] args, String defaultMessage)
{
public BaseException(String module, String code, Object[] args, String defaultMessage) {
this.module = module;
this.code = code;
this.args = args;
this.defaultMessage = defaultMessage;
}
public BaseException(String module, String code, Object[] args)
{
public BaseException(String module, String code, Object[] args) {
this(module, code, args, null);
}
public BaseException(String module, String defaultMessage)
{
public BaseException(String module, String defaultMessage) {
this(module, null, null, defaultMessage);
}
public BaseException(String code, Object[] args)
{
public BaseException(String code, Object[] args) {
this(null, code, args, null);
}
public BaseException(String defaultMessage)
{
public BaseException(String defaultMessage) {
this(null, null, null, defaultMessage);
}
public String getModule()
{
public String getModule() {
return module;
}
public String getCode()
{
public String getCode() {
return code;
}
public Object[] getArgs()
{
public Object[] getArgs() {
return args;
}
public String getDefaultMessage()
{
public String getDefaultMessage() {
return defaultMessage;
}
}

View File

@@ -2,17 +2,19 @@ package com.ruoyi.common.core.exception.user;
import com.ruoyi.common.core.exception.base.BaseException;
import java.io.Serial;
/**
* 用户信息异常类
*
*
* @author ruoyi
*/
public class UserException extends BaseException
{
public class UserException extends BaseException {
@Serial
private static final long serialVersionUID = 1L;
public UserException(String code, Object[] args)
{
public UserException(String code, Object[] args) {
super("user", code, args, null);
}
}

View File

@@ -1,13 +1,9 @@
package com.ruoyi.common.core.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.text.Convert;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@@ -21,64 +17,62 @@ import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.text.Convert;
import reactor.core.publisher.Mono;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* 客户端工具类
*
*
* @author ruoyi
*/
public class ServletUtils
{
public class ServletUtils {
/**
* 获取String参数
*/
public static String getParameter(String name)
{
public static String getParameter(String name) {
return getRequest().getParameter(name);
}
/**
* 获取String参数
*/
public static String getParameter(String name, String defaultValue)
{
public static String getParameter(String name, String defaultValue) {
return Convert.toStr(getRequest().getParameter(name), defaultValue);
}
/**
* 获取Integer参数
*/
public static Integer getParameterToInt(String name)
{
public static Integer getParameterToInt(String name) {
return Convert.toInt(getRequest().getParameter(name));
}
/**
* 获取Integer参数
*/
public static Integer getParameterToInt(String name, Integer defaultValue)
{
public static Integer getParameterToInt(String name, Integer defaultValue) {
return Convert.toInt(getRequest().getParameter(name), defaultValue);
}
/**
* 获取Boolean参数
*/
public static Boolean getParameterToBool(String name)
{
public static Boolean getParameterToBool(String name) {
return Convert.toBool(getRequest().getParameter(name));
}
/**
* 获取Boolean参数
*/
public static Boolean getParameterToBool(String name, Boolean defaultValue)
{
public static Boolean getParameterToBool(String name, Boolean defaultValue) {
return Convert.toBool(getRequest().getParameter(name), defaultValue);
}
@@ -88,8 +82,7 @@ public class ServletUtils
* @param request 请求对象{@link ServletRequest}
* @return Map
*/
public static Map<String, String[]> getParams(ServletRequest request)
{
public static Map<String, String[]> getParams(ServletRequest request) {
final Map<String, String[]> map = request.getParameterMap();
return Collections.unmodifiableMap(map);
}
@@ -100,11 +93,9 @@ public class ServletUtils
* @param request 请求对象{@link ServletRequest}
* @return Map
*/
public static Map<String, String> getParamMap(ServletRequest request)
{
public static Map<String, String> getParamMap(ServletRequest request) {
Map<String, String> params = new HashMap<>();
for (Map.Entry<String, String[]> entry : getParams(request).entrySet())
{
for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) {
params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
}
return params;
@@ -113,14 +104,10 @@ public class ServletUtils
/**
* 获取request
*/
public static HttpServletRequest getRequest()
{
try
{
public static HttpServletRequest getRequest() {
try {
return getRequestAttributes().getRequest();
}
catch (Exception e)
{
} catch (Exception e) {
return null;
}
}
@@ -128,14 +115,10 @@ public class ServletUtils
/**
* 获取response
*/
public static HttpServletResponse getResponse()
{
try
{
public static HttpServletResponse getResponse() {
try {
return getRequestAttributes().getResponse();
}
catch (Exception e)
{
} catch (Exception e) {
return null;
}
}
@@ -143,42 +126,32 @@ public class ServletUtils
/**
* 获取session
*/
public static HttpSession getSession()
{
public static HttpSession getSession() {
return getRequest().getSession();
}
public static ServletRequestAttributes getRequestAttributes()
{
try
{
public static ServletRequestAttributes getRequestAttributes() {
try {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
}
catch (Exception e)
{
} catch (Exception e) {
return null;
}
}
public static String getHeader(HttpServletRequest request, String name)
{
public static String getHeader(HttpServletRequest request, String name) {
String value = request.getHeader(name);
if (StringUtils.isEmpty(value))
{
if (StringUtils.isEmpty(value)) {
return StringUtils.EMPTY;
}
return urlDecode(value);
}
public static Map<String, String> getHeaders(HttpServletRequest request)
{
public static Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> map = new LinkedCaseInsensitiveMap<>();
Enumeration<String> enumeration = request.getHeaderNames();
if (enumeration != null)
{
while (enumeration.hasMoreElements())
{
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = request.getHeader(key);
map.put(key, value);
@@ -189,47 +162,37 @@ public class ServletUtils
/**
* 将字符串渲染到客户端
*
*
* @param response 渲染对象
* @param string 待渲染的字符串
* @param string 待渲染的字符串
*/
public static void renderString(HttpServletResponse response, String string)
{
try
{
public static void renderString(HttpServletResponse response, String string) {
try {
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
}
catch (IOException e)
{
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 是否是Ajax异步请求
*
* @param request
*/
public static boolean isAjaxRequest(HttpServletRequest request)
{
public static boolean isAjaxRequest(HttpServletRequest request) {
String accept = request.getHeader("accept");
if (accept != null && accept.contains("application/json"))
{
if (accept != null && accept.contains("application/json")) {
return true;
}
String xRequestedWith = request.getHeader("X-Requested-With");
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
{
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
return true;
}
String uri = request.getRequestURI();
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
{
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
return true;
}
@@ -239,36 +202,28 @@ public class ServletUtils
/**
* 内容编码
*
*
* @param str 内容
* @return 编码后的内容
*/
public static String urlEncode(String str)
{
try
{
public static String urlEncode(String str) {
try {
return URLEncoder.encode(str, Constants.UTF8);
}
catch (UnsupportedEncodingException e)
{
} catch (UnsupportedEncodingException e) {
return StringUtils.EMPTY;
}
}
/**
* 内容解码
*
*
* @param str 内容
* @return 解码后的内容
*/
public static String urlDecode(String str)
{
try
{
public static String urlDecode(String str) {
try {
return URLDecoder.decode(str, Constants.UTF8);
}
catch (UnsupportedEncodingException e)
{
} catch (UnsupportedEncodingException e) {
return StringUtils.EMPTY;
}
}
@@ -277,11 +232,10 @@ public class ServletUtils
* 设置webflux模型响应
*
* @param response ServerHttpResponse
* @param value 响应内容
* @param value 响应内容
* @return Mono<Void>
*/
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value)
{
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value) {
return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL);
}
@@ -289,12 +243,11 @@ public class ServletUtils
* 设置webflux模型响应
*
* @param response ServerHttpResponse
* @param code 响应状态码
* @param value 响应内容
* @param code 响应状态码
* @param value 响应内容
* @return Mono<Void>
*/
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code)
{
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code) {
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
}
@@ -302,28 +255,26 @@ public class ServletUtils
* 设置webflux模型响应
*
* @param response ServerHttpResponse
* @param status http状态码
* @param code 响应状态码
* @param value 响应内容
* @param status http状态码
* @param code 响应状态码
* @param value 响应内容
* @return Mono<Void>
*/
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code)
{
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code) {
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
}
/**
* 设置webflux模型响应
*
* @param response ServerHttpResponse
* @param response ServerHttpResponse
* @param contentType content-type
* @param status http状态码
* @param code 响应状态码
* @param value 响应内容
* @param status http状态码
* @param code 响应状态码
* @param value 响应内容
* @return Mono<Void>
*/
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code)
{
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) {
response.setStatusCode(status);
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
R<?> result = R.fail(code, value.toString());

View File

@@ -58,6 +58,35 @@ public class BaseEntity implements Serializable {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Map<String, Object> params;
@JsonIgnore
public void setCommonForInsert(String createBy, Date createTime) {
this.createBy = createBy;
this.createTime = createTime;
setCommonForUpdate(createBy, createTime);
}
@JsonIgnore
public void setCommonForInsert(String createBy) {
setCommonForInsert(createBy, new Date());
}
@JsonIgnore
public void setCommonForUpdate(String updateBy, Date updateTime) {
this.updateBy = updateBy;
this.updateTime = updateTime;
if (this instanceof ExtBaseEntity ext) {
if (ext.getUpdateCount() == null) {
ext.setUpdateCount(0);
} else {
ext.setUpdateCount(ext.getUpdateCount() + 1);
}
}
}
public void setCommonForUpdate(String updateBy) {
setCommonForUpdate(updateBy, new Date());
}
public String getSearchValue() {
return searchValue;
}