LogAspet.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.xy.annotation.aspet;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.date.TimeInterval;
  4. import com.xy.annotation.LogEnvents;
  5. import com.xy.annotation.LogOperate;
  6. import com.xy.collections.list.JArrayList;
  7. import com.xy.collections.list.JList;
  8. import com.xy.enums.LogEnum;
  9. import com.xy.utils.Emptys;
  10. import com.xy.utils.LogUtils;
  11. import com.xy.utils.R;
  12. import com.xy.utils.Tuple;
  13. import io.swagger.annotations.Api;
  14. import io.swagger.annotations.ApiOperation;
  15. import lombok.RequiredArgsConstructor;
  16. import org.aspectj.lang.ProceedingJoinPoint;
  17. import org.aspectj.lang.annotation.Around;
  18. import org.aspectj.lang.annotation.Aspect;
  19. import org.aspectj.lang.reflect.MethodSignature;
  20. import org.springframework.stereotype.Component;
  21. import java.lang.reflect.Method;
  22. import java.util.Arrays;
  23. @Aspect
  24. @Component
  25. @RequiredArgsConstructor
  26. public class LogAspet {
  27. JList<String> adds = new JArrayList<>(Arrays.asList("新增", "添加", "保存", "创建", "申请"));
  28. JList<String> dels = new JArrayList<>(Arrays.asList("删除", "清除", "清空"));
  29. JList<String> updates = new JArrayList<>(Arrays.asList("修改", "重置", "审核", "审批", "关闭"));
  30. JList<String> querys = new JArrayList<>(Arrays.asList("查询", "统计"));
  31. JList<String> exports = new JArrayList<>(Arrays.asList("导出", "下载"));
  32. JList<String> leads = new JArrayList<>(Arrays.asList("导入", "上传"));
  33. /**
  34. * 操作日志
  35. *
  36. * @param joinPoint
  37. * @param logOperate
  38. * @return
  39. * @throws Throwable
  40. */
  41. @Around("@annotation(logOperate)")
  42. public Object logOperate(ProceedingJoinPoint joinPoint, LogOperate logOperate) throws Throwable {
  43. TimeInterval timeInterval = DateUtil.timer();
  44. LogEnum.LogType logType = logOperate.logType();
  45. LogEnum.OptType optType = logOperate.optType();
  46. String logContent = logOperate.logContent();
  47. //处理默认参数
  48. if (optType.getValue() == LogEnum.OptType.V100.getValue()) {
  49. Tuple.Tuple2<String, LogEnum.OptType> apiOperation = getApiOperation(joinPoint, optType);
  50. optType = apiOperation.getV2();
  51. }
  52. if (!Emptys.check(logContent)) {
  53. String api = getApi(joinPoint);
  54. Tuple.Tuple2<String, LogEnum.OptType> apiOperation = getApiOperation(joinPoint, optType);
  55. logContent = api + "-" + apiOperation.getV1();
  56. }
  57. //参数
  58. Object[] args = joinPoint.getArgs();
  59. Object result = null;
  60. try {
  61. result = joinPoint.proceed(args);
  62. } catch (Exception e) {
  63. throw e;
  64. } finally {
  65. if (optType.getValue() != LogEnum.OptType.V100.getValue() && Emptys.check(logContent)) {
  66. if (result != null && result instanceof R) {
  67. R r = (R) result;
  68. String logMsg = r.getLogMsg();
  69. if (Emptys.check(logMsg)) {
  70. logContent += "-" + logMsg;
  71. }
  72. }
  73. LogUtils.operate(logType, optType, logContent, (int) timeInterval.interval());
  74. }
  75. }
  76. return result;
  77. }
  78. /**
  79. * 系统事件
  80. *
  81. * @param joinPoint
  82. * @param logEnvents
  83. * @return
  84. * @throws Throwable
  85. */
  86. @Around("@annotation(logEnvents)")
  87. public Object logEnvents(ProceedingJoinPoint joinPoint, LogEnvents logEnvents) throws Throwable {
  88. //参数
  89. Object[] args = joinPoint.getArgs();
  90. Object result;
  91. try {
  92. result = joinPoint.proceed(args);
  93. } catch (Exception e) {
  94. String eventName = logEnvents.eventName();
  95. String eventDescript = logEnvents.eventDescript();
  96. LogEnum.EventLevel eventLevel = logEnvents.eventLevel();
  97. if (!Emptys.check(eventDescript)) {
  98. String api = getApi(joinPoint);
  99. Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
  100. ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
  101. if (apiOperation != null) {
  102. String value = apiOperation.value();
  103. eventDescript = api + "-" + value + "------" + e.getMessage();
  104. }
  105. }
  106. if (Emptys.check(eventDescript)) {
  107. LogUtils.envents(eventName, eventLevel, eventDescript);
  108. }
  109. throw e;
  110. }
  111. return result;
  112. }
  113. private String getApi(ProceedingJoinPoint joinPoint) {
  114. Class<?> classz = joinPoint.getTarget().getClass();
  115. Api api = classz.getAnnotation(Api.class);
  116. if (api != null) {
  117. String[] tags = api.tags();
  118. return tags[0];
  119. }
  120. return "";
  121. }
  122. private Tuple.Tuple2<String, LogEnum.OptType> getApiOperation(ProceedingJoinPoint joinPoint, LogEnum.OptType optType) {
  123. Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
  124. ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
  125. if (apiOperation != null) {
  126. String value = apiOperation.value();
  127. optType = getOptType(value);
  128. return new Tuple.Tuple2<>(value, optType);
  129. }
  130. return new Tuple.Tuple2<>("", optType);
  131. }
  132. private LogEnum.OptType getOptType(String value) {
  133. if (adds.filter().vlike(value).object() != null) {
  134. return LogEnum.OptType.V1;
  135. }
  136. if (dels.filter().vlike(value).object() != null) {
  137. return LogEnum.OptType.V4;
  138. }
  139. if (updates.filter().vlike(value).object() != null) {
  140. return LogEnum.OptType.V2;
  141. }
  142. if (querys.filter().vlike(value).object() != null) {
  143. return LogEnum.OptType.V3;
  144. }
  145. if (exports.filter().vlike(value).object() != null) {
  146. return LogEnum.OptType.V5;
  147. }
  148. if (leads.filter().vlike(value).object() != null) {
  149. return LogEnum.OptType.V6;
  150. }
  151. return LogEnum.OptType.V100;
  152. }
  153. }