|
@@ -0,0 +1,90 @@
|
|
|
+package com.huaxu.logAdvice;
|
|
|
+
|
|
|
+import com.huaxu.dao.OperateLogMapper;
|
|
|
+import com.huaxu.entity.OperateLogEntity;
|
|
|
+import com.huaxu.model.LoginUser;
|
|
|
+import com.huaxu.util.UserUtil;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.Signature;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description 统一日志处理 :通过自定义注解获取操作名称
|
|
|
+ * @auto wangli
|
|
|
+ * @data 2020-11-09 8:43
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class OperateLogAdvice {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OperateLogMapper operateLogMapper;
|
|
|
+
|
|
|
+ @Around(value = "@annotation(com.huaxu.logAdvice.LogAnnotation)")
|
|
|
+ public Object logSave(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
+ //参数存储
|
|
|
+ StringBuffer stringBuffer=new StringBuffer();
|
|
|
+ try{
|
|
|
+ //1.获取到所有的参数值的数组
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ Signature signature = joinPoint.getSignature();
|
|
|
+ MethodSignature methodSignature = (MethodSignature) signature;
|
|
|
+ //2.获取到方法的所有参数名称的字符串数组
|
|
|
+ String[] parameterNames = methodSignature.getParameterNames();
|
|
|
+ //拼装参数
|
|
|
+ for (int i =0 ,len=parameterNames.length;i < len ;i++){
|
|
|
+ stringBuffer.append("\r\n参数名:"+ parameterNames[i] + " = " +args[i]);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ OperateLogEntity OperateLogs = new OperateLogEntity();
|
|
|
+ LoginUser user = UserUtil.getCurrentUser();
|
|
|
+ if (user != null) {
|
|
|
+ // 设置当前登录用户
|
|
|
+ OperateLogs.setUserName(user.getUsername());
|
|
|
+ OperateLogs.setPhone(user.getPhoneNumber());
|
|
|
+ OperateLogs.setCompanyId(user.getCompanyId());
|
|
|
+ OperateLogs.setDepartmentId(user.getDepartmentId());
|
|
|
+
|
|
|
+ MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
|
|
+
|
|
|
+ String module = null;//ApiOperation注解的模块名
|
|
|
+ ApiOperation methodApiOperation = methodSignature.getMethod().getDeclaredAnnotation(ApiOperation.class);
|
|
|
+ if (methodApiOperation != null) {
|
|
|
+ module =methodApiOperation.value();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 没有指定日志module ,获取方法
|
|
|
+ if (StringUtils.isEmpty(module)) {
|
|
|
+ Method method = methodSignature.getMethod();
|
|
|
+ module= method.getDeclaringClass().getName()+method.getName();
|
|
|
+ }
|
|
|
+ OperateLogs.setOperateContent(module);
|
|
|
+
|
|
|
+ try {
|
|
|
+ return joinPoint.proceed();
|
|
|
+ } catch (Exception e) {
|
|
|
+ OperateLogs.setOperateContent(OperateLogs.getOperateContent()+"__"+e.getMessage());
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ if (OperateLogs.getUserName() != null) {
|
|
|
+ OperateLogs.setCreateTime(new Date());
|
|
|
+ operateLogMapper.insert(OperateLogs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ return joinPoint.proceed();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|