mirror of
https://gitee.com/y_project/RuoYi-Vue.git
synced 2025-12-08 15:02:29 +08:00
优化代码
This commit is contained in:
@ -34,7 +34,7 @@ public class CommonController
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ServerConfig serverConfig;
|
private ServerConfig serverConfig;
|
||||||
|
|
||||||
private static final String FILE_DELIMETER = ",";
|
private static final String FILE_DELIMITER = ",";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用下载请求
|
* 通用下载请求
|
||||||
@ -119,10 +119,10 @@ public class CommonController
|
|||||||
originalFilenames.add(file.getOriginalFilename());
|
originalFilenames.add(file.getOriginalFilename());
|
||||||
}
|
}
|
||||||
AjaxResult ajax = AjaxResult.success();
|
AjaxResult ajax = AjaxResult.success();
|
||||||
ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
|
ajax.put("urls", StringUtils.join(urls, FILE_DELIMITER));
|
||||||
ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
|
ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMITER));
|
||||||
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
|
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMITER));
|
||||||
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
|
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMITER));
|
||||||
return ajax;
|
return ajax;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
@ -83,12 +83,12 @@ public class Constants
|
|||||||
/**
|
/**
|
||||||
* 角色权限分隔符
|
* 角色权限分隔符
|
||||||
*/
|
*/
|
||||||
public static final String ROLE_DELIMETER = ",";
|
public static final String ROLE_DELIMITER = ",";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 权限标识分隔符
|
* 权限标识分隔符
|
||||||
*/
|
*/
|
||||||
public static final String PERMISSION_DELIMETER = ",";
|
public static final String PERMISSION_DELIMITER = ",";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码有效期(分钟)
|
* 验证码有效期(分钟)
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
package com.ruoyi.common.utils;
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.ruoyi.common.constant.CacheConstants;
|
import com.ruoyi.common.constant.CacheConstants;
|
||||||
import com.ruoyi.common.core.domain.entity.SysDictData;
|
import com.ruoyi.common.core.domain.entity.SysDictData;
|
||||||
@ -89,37 +91,25 @@ public class DictUtils
|
|||||||
*/
|
*/
|
||||||
public static String getDictLabel(String dictType, String dictValue, String separator)
|
public static String getDictLabel(String dictType, String dictValue, String separator)
|
||||||
{
|
{
|
||||||
StringBuilder propertyString = new StringBuilder();
|
|
||||||
List<SysDictData> datas = getDictCache(dictType);
|
List<SysDictData> datas = getDictCache(dictType);
|
||||||
if (StringUtils.isNull(datas))
|
if (StringUtils.isNull(datas) || StringUtils.isEmpty(dictValue))
|
||||||
{
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
if (StringUtils.containsAny(separator, dictValue))
|
Map<String, String> dictMap = datas.stream().collect(HashMap::new, (map, dict) -> map.put(dict.getDictValue(), dict.getDictLabel()), Map::putAll);
|
||||||
|
if (!StringUtils.contains(dictValue, separator))
|
||||||
{
|
{
|
||||||
for (SysDictData dict : datas)
|
return dictMap.getOrDefault(dictValue, StringUtils.EMPTY);
|
||||||
|
}
|
||||||
|
StringBuilder labelBuilder = new StringBuilder();
|
||||||
|
for (String seperatedValue : dictValue.split(separator))
|
||||||
|
{
|
||||||
|
if (dictMap.containsKey(seperatedValue))
|
||||||
{
|
{
|
||||||
for (String value : dictValue.split(separator))
|
labelBuilder.append(dictMap.get(seperatedValue)).append(separator);
|
||||||
{
|
|
||||||
if (value.equals(dict.getDictValue()))
|
|
||||||
{
|
|
||||||
propertyString.append(dict.getDictLabel()).append(separator);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
return StringUtils.removeEnd(labelBuilder.toString(), separator);
|
||||||
{
|
|
||||||
for (SysDictData dict : datas)
|
|
||||||
{
|
|
||||||
if (dictValue.equals(dict.getDictValue()))
|
|
||||||
{
|
|
||||||
return dict.getDictLabel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return StringUtils.stripEnd(propertyString.toString(), separator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -132,37 +122,25 @@ public class DictUtils
|
|||||||
*/
|
*/
|
||||||
public static String getDictValue(String dictType, String dictLabel, String separator)
|
public static String getDictValue(String dictType, String dictLabel, String separator)
|
||||||
{
|
{
|
||||||
StringBuilder propertyString = new StringBuilder();
|
|
||||||
List<SysDictData> datas = getDictCache(dictType);
|
List<SysDictData> datas = getDictCache(dictType);
|
||||||
if (StringUtils.isNull(datas))
|
if (StringUtils.isNull(datas) || StringUtils.isEmpty(dictLabel))
|
||||||
{
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
if (StringUtils.containsAny(separator, dictLabel))
|
Map<String, String> dictMap = datas.stream().collect(HashMap::new, (map, dict) -> map.put(dict.getDictLabel(), dict.getDictValue()), Map::putAll);
|
||||||
|
if (!StringUtils.contains(dictLabel, separator))
|
||||||
{
|
{
|
||||||
for (SysDictData dict : datas)
|
return dictMap.getOrDefault(dictLabel, StringUtils.EMPTY);
|
||||||
|
}
|
||||||
|
StringBuilder valueBuilder = new StringBuilder();
|
||||||
|
for (String seperatedValue : dictLabel.split(separator))
|
||||||
|
{
|
||||||
|
if (dictMap.containsKey(seperatedValue))
|
||||||
{
|
{
|
||||||
for (String label : dictLabel.split(separator))
|
valueBuilder.append(dictMap.get(seperatedValue)).append(separator);
|
||||||
{
|
|
||||||
if (label.equals(dict.getDictLabel()))
|
|
||||||
{
|
|
||||||
propertyString.append(dict.getDictValue()).append(separator);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
return StringUtils.removeEnd(valueBuilder.toString(), separator);
|
||||||
{
|
|
||||||
for (SysDictData dict : datas)
|
|
||||||
{
|
|
||||||
if (dictLabel.equals(dict.getDictLabel()))
|
|
||||||
{
|
|
||||||
return dict.getDictValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return StringUtils.stripEnd(propertyString.toString(), separator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -50,6 +50,9 @@ public class LogAspect
|
|||||||
/** 计算操作消耗时间 */
|
/** 计算操作消耗时间 */
|
||||||
private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<Long>("Cost Time");
|
private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<Long>("Cost Time");
|
||||||
|
|
||||||
|
/** 参数最大长度限制 */
|
||||||
|
private static final int PARAM_MAX_LENGTH = 2000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理请求前执行
|
* 处理请求前执行
|
||||||
*/
|
*/
|
||||||
@ -109,7 +112,7 @@ public class LogAspect
|
|||||||
if (e != null)
|
if (e != null)
|
||||||
{
|
{
|
||||||
operLog.setStatus(BusinessStatus.FAIL.ordinal());
|
operLog.setStatus(BusinessStatus.FAIL.ordinal());
|
||||||
operLog.setErrorMsg(StringUtils.substring(Convert.toStr(e.getMessage(), ExceptionUtil.getExceptionMessage(e)), 0, 2000));
|
operLog.setErrorMsg(StringUtils.substring(Convert.toStr(e.getMessage(), ExceptionUtil.getExceptionMessage(e)), 0, PARAM_MAX_LENGTH));
|
||||||
}
|
}
|
||||||
// 设置方法名称
|
// 设置方法名称
|
||||||
String className = joinPoint.getTarget().getClass().getName();
|
String className = joinPoint.getTarget().getClass().getName();
|
||||||
@ -160,7 +163,7 @@ public class LogAspect
|
|||||||
// 是否需要保存response,参数和值
|
// 是否需要保存response,参数和值
|
||||||
if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult))
|
if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult))
|
||||||
{
|
{
|
||||||
operLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, 2000));
|
operLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, PARAM_MAX_LENGTH));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,16 +175,16 @@ public class LogAspect
|
|||||||
*/
|
*/
|
||||||
private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog, String[] excludeParamNames) throws Exception
|
private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog, String[] excludeParamNames) throws Exception
|
||||||
{
|
{
|
||||||
Map<?, ?> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
|
|
||||||
String requestMethod = operLog.getRequestMethod();
|
String requestMethod = operLog.getRequestMethod();
|
||||||
|
Map<?, ?> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
|
||||||
if (StringUtils.isEmpty(paramsMap) && StringUtils.equalsAny(requestMethod, HttpMethod.PUT.name(), HttpMethod.POST.name(), HttpMethod.DELETE.name()))
|
if (StringUtils.isEmpty(paramsMap) && StringUtils.equalsAny(requestMethod, HttpMethod.PUT.name(), HttpMethod.POST.name(), HttpMethod.DELETE.name()))
|
||||||
{
|
{
|
||||||
String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames);
|
String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames);
|
||||||
operLog.setOperParam(StringUtils.substring(params, 0, 2000));
|
operLog.setOperParam(params);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
operLog.setOperParam(StringUtils.substring(JSON.toJSONString(paramsMap, excludePropertyPreFilter(excludeParamNames)), 0, 2000));
|
operLog.setOperParam(StringUtils.substring(JSON.toJSONString(paramsMap, excludePropertyPreFilter(excludeParamNames)), 0, PARAM_MAX_LENGTH));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +193,7 @@ public class LogAspect
|
|||||||
*/
|
*/
|
||||||
private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames)
|
private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames)
|
||||||
{
|
{
|
||||||
String params = "";
|
StringBuilder params = new StringBuilder();
|
||||||
if (paramsArray != null && paramsArray.length > 0)
|
if (paramsArray != null && paramsArray.length > 0)
|
||||||
{
|
{
|
||||||
for (Object o : paramsArray)
|
for (Object o : paramsArray)
|
||||||
@ -200,15 +203,20 @@ public class LogAspect
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter(excludeParamNames));
|
String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter(excludeParamNames));
|
||||||
params += jsonObj.toString() + " ";
|
params.append(jsonObj).append(" ");
|
||||||
|
if (params.length() >= PARAM_MAX_LENGTH)
|
||||||
|
{
|
||||||
|
return StringUtils.substring(params.toString(), 0, PARAM_MAX_LENGTH);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
log.error("请求参数拼装异常 msg:{}, 参数:{}", e.getMessage(), paramsArray, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return params.trim();
|
return params.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -53,7 +53,7 @@ public class PermissionService
|
|||||||
/**
|
/**
|
||||||
* 验证用户是否具有以下任意一个权限
|
* 验证用户是否具有以下任意一个权限
|
||||||
*
|
*
|
||||||
* @param permissions 以 PERMISSION_DELIMETER 为分隔符的权限列表
|
* @param permissions 以 PERMISSION_DELIMITER 为分隔符的权限列表
|
||||||
* @return 用户是否具有以下任意一个权限
|
* @return 用户是否具有以下任意一个权限
|
||||||
*/
|
*/
|
||||||
public boolean hasAnyPermi(String permissions)
|
public boolean hasAnyPermi(String permissions)
|
||||||
@ -69,7 +69,7 @@ public class PermissionService
|
|||||||
}
|
}
|
||||||
PermissionContextHolder.setContext(permissions);
|
PermissionContextHolder.setContext(permissions);
|
||||||
Set<String> authorities = loginUser.getPermissions();
|
Set<String> authorities = loginUser.getPermissions();
|
||||||
for (String permission : permissions.split(Constants.PERMISSION_DELIMETER))
|
for (String permission : permissions.split(Constants.PERMISSION_DELIMITER))
|
||||||
{
|
{
|
||||||
if (permission != null && hasPermissions(authorities, permission))
|
if (permission != null && hasPermissions(authorities, permission))
|
||||||
{
|
{
|
||||||
@ -121,7 +121,7 @@ public class PermissionService
|
|||||||
/**
|
/**
|
||||||
* 验证用户是否具有以下任意一个角色
|
* 验证用户是否具有以下任意一个角色
|
||||||
*
|
*
|
||||||
* @param roles 以 ROLE_NAMES_DELIMETER 为分隔符的角色列表
|
* @param roles 以 ROLE_DELIMITER 为分隔符的角色列表
|
||||||
* @return 用户是否具有以下任意一个角色
|
* @return 用户是否具有以下任意一个角色
|
||||||
*/
|
*/
|
||||||
public boolean hasAnyRoles(String roles)
|
public boolean hasAnyRoles(String roles)
|
||||||
@ -135,7 +135,7 @@ public class PermissionService
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (String role : roles.split(Constants.ROLE_DELIMETER))
|
for (String role : roles.split(Constants.ROLE_DELIMITER))
|
||||||
{
|
{
|
||||||
if (hasRole(role))
|
if (hasRole(role))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import java.util.Set;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.constant.UserConstants;
|
import com.ruoyi.common.constant.UserConstants;
|
||||||
import com.ruoyi.common.core.domain.entity.SysRole;
|
import com.ruoyi.common.core.domain.entity.SysRole;
|
||||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||||
@ -39,7 +40,7 @@ public class SysPermissionService
|
|||||||
// 管理员拥有所有权限
|
// 管理员拥有所有权限
|
||||||
if (user.isAdmin())
|
if (user.isAdmin())
|
||||||
{
|
{
|
||||||
roles.add("admin");
|
roles.add(Constants.SUPER_ADMIN);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -60,7 +61,7 @@ public class SysPermissionService
|
|||||||
// 管理员拥有所有权限
|
// 管理员拥有所有权限
|
||||||
if (user.isAdmin())
|
if (user.isAdmin())
|
||||||
{
|
{
|
||||||
perms.add("*:*:*");
|
perms.add(Constants.ALL_PERMISSION);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user