陈昶聿
5 天以前 987e38327f849e1b13d8541246dde08d877db0e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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());
        }
    }
}