LogAop.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.jtgh.yjpt.common;
  2. import java.lang.reflect.Method;
  3. import java.util.Date;
  4. import javax.servlet.http.HttpSession;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.aspectj.lang.JoinPoint;
  7. import org.aspectj.lang.annotation.AfterReturning;
  8. import org.aspectj.lang.annotation.AfterThrowing;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.reflect.MethodSignature;
  11. import org.springframework.web.context.support.WebApplicationContextUtils;
  12. import com.jtgh.yjpt.anon.Log;
  13. import com.jtgh.yjpt.entity.common.LogEntity;
  14. import com.jtgh.yjpt.entity.sys.MenuEntity;
  15. import com.jtgh.yjpt.service.common.LogService;
  16. import com.jtgh.yjpt.service.sys.MenuService;
  17. @Aspect
  18. public class LogAop {
  19. @AfterReturning(pointcut = "execution(* com.jtgh.yjpt.controller..*.*(..))")
  20. public void doLogAfterReturn(JoinPoint jp) {
  21. Class<?> c = jp.getTarget().getClass();
  22. Method m = ((MethodSignature) jp.getSignature()).getMethod();
  23. if (m.isAnnotationPresent(Log.class)) {
  24. doLog(c, m, jp.getArgs(), Utils.getSession(), true);
  25. }
  26. }
  27. @AfterThrowing(pointcut = "execution(* com.jtgh.yjpt.controller..*.*(..))")
  28. public void doLogAfterThrow(JoinPoint jp) {
  29. Class<?> c = jp.getTarget().getClass();
  30. Method m = ((MethodSignature) jp.getSignature()).getMethod();
  31. if (m.isAnnotationPresent(Log.class)) {
  32. doLog(c, m, jp.getArgs(), Utils.getSession(), false);
  33. }
  34. }
  35. /**
  36. * 记录LOG
  37. *
  38. * @param controller
  39. * @param funcName
  40. * @param result
  41. */
  42. private void doLog(Class<?> c, Method m, Object[] args,
  43. HttpSession session, boolean result) {
  44. if (session == null
  45. || session.getAttribute(GlobalData.USER_SESSION_KEY) == null) {
  46. return;
  47. }
  48. LogService logService = WebApplicationContextUtils
  49. .getWebApplicationContext(session.getServletContext()).getBean(
  50. LogService.class);
  51. MenuService menuService = WebApplicationContextUtils
  52. .getWebApplicationContext(session.getServletContext()).getBean(
  53. MenuService.class);
  54. LogEntity log = new LogEntity();
  55. if (c.isAnnotationPresent(Log.class)) {
  56. Log ca = c.getAnnotation(Log.class);
  57. Log ma = m.getAnnotation(Log.class);
  58. String menuCode = ca.menuCode();
  59. if (Utils.getCurrentUser() != null
  60. && Constants.NO.equals(Utils.getCurrentUser().getSfjyr())) {
  61. if (!StringUtils.isEmpty(ma.menuCode()))
  62. menuCode = ma.menuCode();
  63. } else {
  64. if (!StringUtils.isEmpty(ca.menuCodeJyr()))
  65. menuCode = ca.menuCodeJyr();
  66. if (!StringUtils.isEmpty(ma.menuCodeJyr()))
  67. menuCode = ma.menuCodeJyr();
  68. }
  69. MenuEntity menu = menuService.findByCode(menuCode);
  70. log.setMenu(menu);
  71. }
  72. log.setLogUser(Utils.getCurrentUser());
  73. log.setSessionId(session.getId());
  74. Integer step = (Integer) session
  75. .getAttribute(GlobalData.USER_SESSION_STEP);
  76. if (step == null) {
  77. step = 1;
  78. }
  79. session.setAttribute(GlobalData.USER_SESSION_STEP, step + 1);
  80. log.setStep(step);
  81. log.setLogTime(new Date());
  82. log.setLogMethod(m.getAnnotation(Log.class).value().name());
  83. if (Log.Type.QLYG_VIEW.name().equals(log.getLogMethod())) {
  84. if (args != null && args.length > 0) {
  85. String businessId = args[0].toString();
  86. log.setBusinessId(businessId);
  87. }
  88. }
  89. log.setLogResult(result ? Constants.LOG_RESULT_SUCCESS
  90. : Constants.LOG_RESULT_FAIL);
  91. log.setAddDate(new Date());
  92. log.setAddUser(Utils.getCurrentUser());
  93. logService.save(log);
  94. }
  95. }