package com.wpg.meter.common.interceptor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.wpg.meter.common.entity.OrgUser;
import com.wpg.meter.common.entity.api.OptLog;
import com.wpg.meter.common.mapper.OptLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@Aspect
@Configuration
@Slf4j
@Profile({“test”, “dev”, “pro”})
public class OptAspect {
@Autowired
private OptLogMapper optLogMapper;
//execution(public * com.wpg.admin.controller..*(..)) &&
@Pointcut("@annotation(com.wpg.meter.common.interceptor.OptAnnotation)")
public void addOpt() {
}
@Around(value = "addOpt()")
public Object addOptInfo(ProceedingJoinPoint joinPoint) throws Throwable {
Object result;
long startTime;
long endTime;
OptLog optLog = new OptLog();
startTime = System.currentTimeMillis();
try {
result = joinPoint.proceed();
} catch (Throwable t) {
//保存potLog日志,抛出异常
try {
endTime = System.currentTimeMillis();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
OptAnnotation optAnnotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(OptAnnotation.class);
Object[] args = joinPoint.getArgs();
optLog.setOptName(optAnnotation.optName());
optLog.setOptResult("2");
OrgUser user = (OrgUser) request.getAttribute("orgUser");
if ("登录模块".equals(optLog.getOptName())) {
optLog.setUserName(JSON.parseObject(JSON.toJSONString(args[0])).getString("loginname"));
}
if (!"export".equals(optAnnotation.optMethod()) || !"login".equals(optAnnotation.optMethod())) {
optLog.setReqParam(JSON.toJSONString(args));
}
setOptLog(optLog, optAnnotation, request, startTime, endTime, user);
optLog.setErrInfo(t.toString());
optLog.setId(IdWorker.get32UUID());
optLogMapper.myInsert(optLog);
} catch (Exception e) {
log.error("操作日志保存失败:{}", e.getMessage());
}
throw t;
}
endTime = System.currentTimeMillis();
try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
OptAnnotation optAnnotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(OptAnnotation.class);
Object[] args = joinPoint.getArgs();
optLog.setOptName(optAnnotation.optName());
if ("export".equals(optAnnotation.optMethod())) {
optLog.setOptResult("1");
} else {
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(result));
// optLog.setReqParam(JSON.toJSONString(args));
optLog.setResParam(JSON.toJSONString(result));
if ("complete".equals(jsonObject.getString("status"))) {
optLog.setOptResult("1");
} else {
optLog.setOptResult("0");
}
// if (“登录模块”.equals(optLog.getOptName())) {
// optLog.setUserName(JSON.parseObject(JSON.toJSONString(args[0])).getString(“loginname”));
// }
}
if (“登录”.equals(optLog.getOptName())) {
optLog.setUserName(JSON.parseObject(JSON.toJSONString(args[0])).getString(“loginname”));
}
if (!“export”.equals(optAnnotation.optMethod()) && !“login”.equals(optAnnotation.optMethod())) {
optLog.setReqParam(JSON.toJSONString(args));
}
OrgUser user = (OrgUser) request.getAttribute(“orgUser”);
setOptLog(optLog, optAnnotation, request, startTime, endTime, user);
optLogMapper.myInsert(optLog);
} catch (Exception e) {
log.error("操作日志保存失败:{}", e.getMessage());
}
return result;
}
private void setOptLog(OptLog optLog, OptAnnotation optAnnotation, HttpServletRequest request, long startTime, long endTime, OrgUser user) {
optLog.setOptTypeName(optAnnotation.optTypeName().getName());
optLog.setOptType(request.getMethod());
optLog.setOptDes(request.getRequestURI());
optLog.setCreateTime(new Date(startTime));
optLog.setEndTime(new Date(endTime));
optLog.setOptTime(endTime - startTime);
optLog.setSysSource(optAnnotation.sysSource().getName());
if (user != null) {
optLog.setUserId(user.getId());
optLog.setUserName(user.getLoginName());
}
}
}
package com.wpg.meter.common.interceptor;
import com.wpg.meter.common.enums.OptModuleEnum;
import com.wpg.meter.common.enums.OptTypeEnum;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface OptAnnotation {
String optMethod() default “normal”;
String optName();
OptModuleEnum sysSource() default OptModuleEnum.AREA;
OptTypeEnum optTypeName();
}
package com.wpg.meter.common.enums;
/**
-
@author menghm.
-
@description功能名称(所属菜单)
-
@date 2020/6/2.
*/
public enum OptModuleEnum {LOGIN(“login”, “登录模块”),
/**- 数据管理
*/
AREA(“area”, “区域管理”),
CONCENTRATOR(“concentrator”, “集中器管理”),
COLLECTOR(“collector”, “采集器管理”),
METER(“meter”, “水表管理”),
FACTORY(“factory”, “厂商管理”),
METERTYPE(“metertype”, “水表类型管理”),
REVENUE(“revenue”, “水表营收关系绑定”),
CYCLEWATER(“cyclewater”, “周期水量管理”),
SETTLE(“settle”, “设置结算时间”),
/**
- 系统管理
*/
USER(“user”, “人员管理”),
ROLE(“role”, “角色权限管理”),
AUTH(“auth”, “区域权限管理”),
PARAM(“param”, “系统参数管理”),
DICT(“dict”, “数据字典管理”),
OPT(“opt”, “系统操作记录查询”),
/**
- 数据分析
*/
PART(“part”, “小区水量分析”),
MEMBER(“member”, “户表水量分析”),
WORK(“work”, “综合工况统计”),
UASGE(“usage”, “综合水量分析”),
/**
- 异常分析
*/
ABNORMALCON(“abnormalcon”, “异常集中器分析”),
NOREAD(“noread”, “无法抄读水表统计”),
ABNORMALUSE(“abnormaluse”, “用水量异常统计”),;
private final String code;
private final String name;
OptModuleEnum(String code, String name) {
this.code = code;
this.name = name;
}public String getName() {
return name;
}public String getCode() {
return code;
}public static String getStateEnum(String code) {
OptModuleEnum[] values = OptModuleEnum.values();
for (OptModuleEnum stateEnum : values) {
if (stateEnum.getCode().equals(code)) {
return stateEnum.getName();
}
}
return null;
}
} - 数据管理
package com.wpg.meter.common.enums;
/**
-
状态枚举
*/
public enum OptTypeEnum {
DELETE(“D”, “删除记录”),
UPDATE(“U”, “修改记录”),
INSERT(“I”, “添加记录”),
SELECT(“S”, “查询记录”),
ADD_TEMP(“add_temp”, “添加临时表”),
SUBMIT_TEMP(“submit_temp”, “提交临时表”),
DEL_TEMP(“del_temp”, “删除临时表”),
LOGIN(“L”, “登录”),
LOGOUT(“O”, “退出”),
EXPORT(“E”, “导出”),
READ(“R”, “读取”),
DOWN(“D”, “下控指令”),;
private final String code;
private final String name;OptTypeEnum(String code, String name) {
this.code = code;
this.name = name;
}public String getName() {
return name;
}public String getCode() {
return code;
}public static String getStateEnum(String code) {
OptTypeEnum[] values = OptTypeEnum.values();
for (OptTypeEnum stateEnum : values
) {
if (stateEnum.getCode().equals(code)) {
return stateEnum.getName();
}
}
return null;
}
}
@PostMapping("/getAllOpt")
@ApiOperation(“获取所有操作名称”)
@OptAnnotation(optName = “获取所有操作名称”, optTypeName = OptTypeEnum.SELECT, sysSource = OptModuleEnum.OPT, optMethod = “getAllOpt”)
@ApiImplicitParam(name = “Authorization”, value = “token”, required = true, dataType = “string”, paramType = “header”)
public RestResult<List> getAllOpt() {
return sysLogService.getAllOpt();
}