Fancy
2024-10-30 e487d886533bb60618864642b85f13957252f70d
pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java
@@ -1,12 +1,14 @@
package com.dy.pmsGlobal.aop;
import com.dy.common.util.ObjectToMapUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.webFilter.UserTokenContext;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@@ -23,8 +25,10 @@
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Map;
import java.lang.reflect.Array;
import java.util.Arrays;
@Slf4j
@Aspect
@Component
public class LogAspect {
@@ -32,9 +36,11 @@
    @Value("${pms.sso.curUserUrl}")
    public String SsoCurUserUrl ;
    private LogSv logSv;
    private LogService logSv;
    @Value("${pms.global.dev}")
    public String isDevStage ;//是否为开发阶段
    @Autowired
    public void setLogSv(LogSv logSv){
    public void setLogSv(LogService logSv){
        this.logSv = logSv;
    }
    private RestTemplate restTemplate;
@@ -44,22 +50,29 @@
    }
    @AfterReturning(pointcut = "@annotation(com.dy.pmsGlobal.aop.Log)", returning = "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        // 获取方法的中文描述
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Log operationDescription = methodSignature.getMethod().getAnnotation(Log.class);
        String operationName = operationDescription.value();
        //结果
        BaseResponse response = (BaseResponse)result;
        response.isSuccess();
        // 获取用户信息
        BaUser user = (BaUser)getCurUser(response);
        Long operator = user!=null?user.id: 1l;
        // 获取IP地址
        String ipAddress = getRemoteHost();
        // 记录日志
        logSv.save(operator, operationName,ipAddress);
    public void logAfterReturning(JoinPoint joinPoint, BaseResponse result) {
        if(isDevStage != null && !isDevStage.trim().equals("") && isDevStage.trim().equalsIgnoreCase("true")){
            return;
        }
        try{
            // 获取用户信息
            BaUser user = (BaUser)getCurUser(result);
            if(user!=null && user.id !=null && !StringUtils.isNullOrEmpty(user.name)){
                // 获取方法的中文描述
                MethodSignature sign = (MethodSignature) joinPoint.getSignature();
                Log logDesc = sign.getMethod().getAnnotation(Log.class);
                String operationName = logDesc.value();
                // 获取IP地址
                String ip = getRemoteHost();
                // 记录日志
                //msg  方法名 + 参数值 + 返回值
                String args = JSONObject.toJSONString(joinPoint.getArgs());
                //String args1 = Arrays.toString(joinPoint.getArgs());
                logSv.save(user.id,user.name, operationName,ip,result.getCode(),args+" "+ result.getMsg());
            }
        }catch (Exception e){
            log.error("记录日志异常:"+e.getMessage());
        }
    }
    /**
@@ -71,8 +84,10 @@
        if(!StringUtils.isNullOrEmpty(SsoCurUserUrl)){
            String token = UserTokenContext.get();
            if(StringUtils.isNullOrEmpty(token)){
                Map<String,Object> resMap = ObjectToMapUtil.objectToMap(response.getContent());
                token =resMap.containsKey("token")?resMap.get("token").toString():"";
                JSONObject res = objectToJson(response.getContent());
                if(res!=null && res.containsKey("token")){
                    token = res.get("token").toString();
                }
            }
            String url = UriComponentsBuilder.fromUriString(SsoCurUserUrl)
@@ -114,4 +129,20 @@
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
    /**
     * 将对象转换为JSONObject
     * @param obj
     * @return
     */
    public static JSONObject objectToJson(Object obj) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            JSONObject o = JSONObject.parseObject(mapper.writeValueAsString(obj));
            return o;
        } catch (Exception e) {
            log.error("对象转换为JSONObject失败:"+e.getMessage());
            return null;
        }
    }
}