package com.jtgh.yjpt.common; import java.lang.reflect.Method; import java.util.Date; import javax.servlet.http.HttpSession; import org.apache.commons.lang.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.web.context.support.WebApplicationContextUtils; import com.jtgh.yjpt.anon.Log; import com.jtgh.yjpt.entity.common.LogEntity; import com.jtgh.yjpt.entity.sys.MenuEntity; import com.jtgh.yjpt.service.common.LogService; import com.jtgh.yjpt.service.sys.MenuService; @Aspect public class LogAop { @AfterReturning(pointcut = "execution(* com.jtgh.yjpt.controller..*.*(..))") public void doLogAfterReturn(JoinPoint jp) { Class c = jp.getTarget().getClass(); Method m = ((MethodSignature) jp.getSignature()).getMethod(); if (m.isAnnotationPresent(Log.class)) { doLog(c, m, jp.getArgs(), Utils.getSession(), true); } } @AfterThrowing(pointcut = "execution(* com.jtgh.yjpt.controller..*.*(..))") public void doLogAfterThrow(JoinPoint jp) { Class c = jp.getTarget().getClass(); Method m = ((MethodSignature) jp.getSignature()).getMethod(); if (m.isAnnotationPresent(Log.class)) { doLog(c, m, jp.getArgs(), Utils.getSession(), false); } } /** * 记录LOG * * @param controller * @param funcName * @param result */ private void doLog(Class c, Method m, Object[] args, HttpSession session, boolean result) { if (session == null || session.getAttribute(GlobalData.USER_SESSION_KEY) == null) { return; } LogService logService = WebApplicationContextUtils .getWebApplicationContext(session.getServletContext()).getBean( LogService.class); MenuService menuService = WebApplicationContextUtils .getWebApplicationContext(session.getServletContext()).getBean( MenuService.class); LogEntity log = new LogEntity(); if (c.isAnnotationPresent(Log.class)) { Log ca = c.getAnnotation(Log.class); Log ma = m.getAnnotation(Log.class); String menuCode = ca.menuCode(); if (Utils.getCurrentUser() != null && Constants.NO.equals(Utils.getCurrentUser().getSfjyr())) { if (!StringUtils.isEmpty(ma.menuCode())) menuCode = ma.menuCode(); } else { if (!StringUtils.isEmpty(ca.menuCodeJyr())) menuCode = ca.menuCodeJyr(); if (!StringUtils.isEmpty(ma.menuCodeJyr())) menuCode = ma.menuCodeJyr(); } MenuEntity menu = menuService.findByCode(menuCode); log.setMenu(menu); } log.setLogUser(Utils.getCurrentUser()); log.setSessionId(session.getId()); Integer step = (Integer) session .getAttribute(GlobalData.USER_SESSION_STEP); if (step == null) { step = 1; } session.setAttribute(GlobalData.USER_SESSION_STEP, step + 1); log.setStep(step); log.setLogTime(new Date()); log.setLogMethod(m.getAnnotation(Log.class).value().name()); if (Log.Type.QLYG_VIEW.name().equals(log.getLogMethod())) { if (args != null && args.length > 0) { String businessId = args[0].toString(); log.setBusinessId(businessId); } } log.setLogResult(result ? Constants.LOG_RESULT_SUCCESS : Constants.LOG_RESULT_FAIL); log.setAddDate(new Date()); log.setAddUser(Utils.getCurrentUser()); logService.save(log); } }