package com.ruoyi.framework.aspectj;
|
|
import com.ruoyi.common.annotation.AutoInit;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Component;
|
|
import java.lang.reflect.Method;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* 自动填充orgid切面
|
*
|
* @author smartor
|
*/
|
@Aspect
|
@Component
|
public class AutoInitAspect {
|
|
private static final Logger log = LoggerFactory.getLogger(AutoInit.class);
|
|
/**
|
* 定义切点 - 拦截所有带有@AutoInit注解的方法
|
*/
|
@Pointcut("@annotation(com.ruoyi.common.annotation.AutoInit)")
|
public void autoInitPointCut() {
|
}
|
|
/**
|
* 在方法执行前自动初始化
|
*
|
* @param joinPoint 切点
|
* @param autoInit 注解对象
|
*/
|
@Before("autoInitPointCut() && @annotation(autoInit)")
|
public void doBefore(JoinPoint joinPoint, AutoInit autoInit) {
|
try {
|
// 获取当前登录用户的orgid
|
String orgid = SecurityUtils.getLoginUser().getUser().getOrgid();
|
|
// 如果orgid为空,则不初始化
|
if (StringUtils.isEmpty(orgid)) {
|
log.warn("当前用户orgid为空,跳过自动初始化");
|
return;
|
}
|
|
// 获取方法参数
|
Object[] args = joinPoint.getArgs();
|
if (args == null || args.length == 0) {
|
return;
|
}
|
|
// 获取方法签名
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
Method method = signature.getMethod();
|
String[] parameterNames = signature.getParameterNames();
|
|
// 获取注解配置的参数名列表
|
String[] configuredParamNames = autoInit.paramNames();
|
List<String> targetParamNames = configuredParamNames.length > 0
|
? Arrays.asList(configuredParamNames)
|
: null;
|
|
// 遍历所有参数
|
for (int i = 0; i < args.length; i++) {
|
Object arg = args[i];
|
if (arg == null) {
|
continue;
|
}
|
|
// 如果指定了参数名,检查当前参数是否在指定列表中
|
if (targetParamNames != null && !targetParamNames.contains(parameterNames[i])) {
|
continue;
|
}
|
|
// 尝试填充orgid
|
setOrgId(arg, orgid, autoInit.override());
|
}
|
|
} catch (Exception e) {
|
log.error("自动填充orgid失败", e);
|
}
|
}
|
|
/**
|
* 填充orgid到目标对象
|
*
|
* @param target 目标对象
|
* @param orgid 要填充的orgid值
|
* @param override 是否覆盖已有值
|
*/
|
private void setOrgId(Object target, String orgid, boolean override) {
|
try {
|
Class<?> clazz = target.getClass();
|
|
// 尝试获取getOrgid方法
|
Method getOrgidMethod = null;
|
try {
|
getOrgidMethod = clazz.getMethod("getOrgid");
|
} catch (NoSuchMethodException e) {
|
// 对象没有getOrgid方法,跳过
|
return;
|
}
|
|
// 检查是否需要覆盖
|
if (!override) {
|
Object existingOrgid = getOrgidMethod.invoke(target);
|
if (existingOrgid != null && StringUtils.isNotEmpty(existingOrgid.toString())) {
|
log.debug("对象已有orgid值: {},跳过填充", existingOrgid);
|
return;
|
}
|
}
|
|
// 获取setOrgid方法并填充值
|
Method setOrgidMethod = clazz.getMethod("setOrgid", String.class);
|
setOrgidMethod.invoke(target, orgid);
|
|
log.debug("成功为对象 {} 填充orgid: {}", clazz.getSimpleName(), orgid);
|
|
} catch (NoSuchMethodException e) {
|
// 对象没有setOrgid方法,跳过
|
log.debug("对象 {} 没有setOrgid方法,跳过填充", target.getClass().getSimpleName());
|
} catch (Exception e) {
|
log.error("填充orgid时发生异常: {}", e.getMessage());
|
}
|
}
|
}
|