123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package com.xy.annotation.aspet;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.date.TimeInterval;
- import com.xy.annotation.LogEnvents;
- import com.xy.annotation.LogOperate;
- import com.xy.collections.list.JArrayList;
- import com.xy.collections.list.JList;
- import com.xy.enums.LogEnum;
- import com.xy.utils.Emptys;
- import com.xy.utils.LogUtils;
- import com.xy.utils.R;
- import com.xy.utils.Tuple;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.RequiredArgsConstructor;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.springframework.stereotype.Component;
- import java.lang.reflect.Method;
- import java.util.Arrays;
- @Aspect
- @Component
- @RequiredArgsConstructor
- public class LogAspet {
- JList<String> adds = new JArrayList<>(Arrays.asList("新增", "添加", "保存", "创建", "申请"));
- JList<String> dels = new JArrayList<>(Arrays.asList("删除", "清除", "清空"));
- JList<String> updates = new JArrayList<>(Arrays.asList("修改", "重置", "审核", "审批", "关闭"));
- JList<String> querys = new JArrayList<>(Arrays.asList("查询", "统计"));
- JList<String> exports = new JArrayList<>(Arrays.asList("导出", "下载"));
- JList<String> leads = new JArrayList<>(Arrays.asList("导入", "上传"));
- /**
- * 操作日志
- *
- * @param joinPoint
- * @param logOperate
- * @return
- * @throws Throwable
- */
- @Around("@annotation(logOperate)")
- public Object logOperate(ProceedingJoinPoint joinPoint, LogOperate logOperate) throws Throwable {
- TimeInterval timeInterval = DateUtil.timer();
- LogEnum.LogType logType = logOperate.logType();
- LogEnum.OptType optType = logOperate.optType();
- String logContent = logOperate.logContent();
- //处理默认参数
- if (optType.getValue() == LogEnum.OptType.V100.getValue()) {
- Tuple.Tuple2<String, LogEnum.OptType> apiOperation = getApiOperation(joinPoint, optType);
- optType = apiOperation.getV2();
- }
- if (!Emptys.check(logContent)) {
- String api = getApi(joinPoint);
- Tuple.Tuple2<String, LogEnum.OptType> apiOperation = getApiOperation(joinPoint, optType);
- logContent = api + "-" + apiOperation.getV1();
- }
- //参数
- Object[] args = joinPoint.getArgs();
- Object result = null;
- try {
- result = joinPoint.proceed(args);
- } catch (Exception e) {
- throw e;
- } finally {
- if (optType.getValue() != LogEnum.OptType.V100.getValue() && Emptys.check(logContent)) {
- if (result != null && result instanceof R) {
- R r = (R) result;
- String logMsg = r.getLogMsg();
- if (Emptys.check(logMsg)) {
- logContent += "-" + logMsg;
- }
- }
- LogUtils.operate(logType, optType, logContent, (int) timeInterval.interval());
- }
- }
- return result;
- }
- /**
- * 系统事件
- *
- * @param joinPoint
- * @param logEnvents
- * @return
- * @throws Throwable
- */
- @Around("@annotation(logEnvents)")
- public Object logEnvents(ProceedingJoinPoint joinPoint, LogEnvents logEnvents) throws Throwable {
- //参数
- Object[] args = joinPoint.getArgs();
- Object result;
- try {
- result = joinPoint.proceed(args);
- } catch (Exception e) {
- String eventName = logEnvents.eventName();
- String eventDescript = logEnvents.eventDescript();
- LogEnum.EventLevel eventLevel = logEnvents.eventLevel();
- if (!Emptys.check(eventDescript)) {
- String api = getApi(joinPoint);
- Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
- ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
- if (apiOperation != null) {
- String value = apiOperation.value();
- eventDescript = api + "-" + value + "------" + e.getMessage();
- }
- }
- if (Emptys.check(eventDescript)) {
- LogUtils.envents(eventName, eventLevel, eventDescript);
- }
- throw e;
- }
- return result;
- }
- private String getApi(ProceedingJoinPoint joinPoint) {
- Class<?> classz = joinPoint.getTarget().getClass();
- Api api = classz.getAnnotation(Api.class);
- if (api != null) {
- String[] tags = api.tags();
- return tags[0];
- }
- return "";
- }
- private Tuple.Tuple2<String, LogEnum.OptType> getApiOperation(ProceedingJoinPoint joinPoint, LogEnum.OptType optType) {
- Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
- ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
- if (apiOperation != null) {
- String value = apiOperation.value();
- optType = getOptType(value);
- return new Tuple.Tuple2<>(value, optType);
- }
- return new Tuple.Tuple2<>("", optType);
- }
- private LogEnum.OptType getOptType(String value) {
- if (adds.filter().vlike(value).object() != null) {
- return LogEnum.OptType.V1;
- }
- if (dels.filter().vlike(value).object() != null) {
- return LogEnum.OptType.V4;
- }
- if (updates.filter().vlike(value).object() != null) {
- return LogEnum.OptType.V2;
- }
- if (querys.filter().vlike(value).object() != null) {
- return LogEnum.OptType.V3;
- }
- if (exports.filter().vlike(value).object() != null) {
- return LogEnum.OptType.V5;
- }
- if (leads.filter().vlike(value).object() != null) {
- return LogEnum.OptType.V6;
- }
- return LogEnum.OptType.V100;
- }
- }
|