|  |  | 
 |  |  | 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; | 
 |  |  | 
 |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
 |  |  | import org.springframework.web.util.UriComponentsBuilder; | 
 |  |  |  | 
 |  |  | import java.util.Map; | 
 |  |  |  | 
 |  |  | @Slf4j | 
 |  |  | @Aspect | 
 |  |  | @Component | 
 |  |  | public class LogAspect { | 
 |  |  | 
 |  |  |     @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; | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @AfterReturning(pointcut = "@annotation(com.dy.pmsGlobal.aop.Log)", returning = "result") | 
 |  |  |     public void logAfterReturning(JoinPoint joinPoint, Object result) { | 
 |  |  |         //结果 | 
 |  |  |         BaseResponse res = (BaseResponse)result; | 
 |  |  |         res.isSuccess(); | 
 |  |  |         // 获取用户信息 | 
 |  |  |         BaUser user = (BaUser)getCurUser(res); | 
 |  |  |         if(user!=null){ | 
 |  |  |             Long operator = user.id; | 
 |  |  |             // 获取方法的中文描述 | 
 |  |  |             MethodSignature sign = (MethodSignature) joinPoint.getSignature(); | 
 |  |  |             Log logDesc = sign.getMethod().getAnnotation(Log.class); | 
 |  |  |             String operationName = logDesc.value(); | 
 |  |  |             // 获取IP地址 | 
 |  |  |             String ip = getRemoteHost(); | 
 |  |  |             // 记录日志 | 
 |  |  |             logSv.save(operator, operationName,ip); | 
 |  |  |     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(); | 
 |  |  |                 // 记录日志 | 
 |  |  |                 logSv.save(user.id,user.name, operationName,ip,result.getCode(),result.getMsg()); | 
 |  |  |             } | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             log.error("记录日志异常:"+e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |         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) | 
 |  |  | 
 |  |  |         } | 
 |  |  |         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; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } |