|
@@ -11,7 +11,6 @@ import com.qmth.teachcloud.common.entity.BasicOperationLog;
|
|
|
import com.qmth.teachcloud.common.entity.SysPrivilege;
|
|
|
import com.qmth.teachcloud.common.entity.SysUser;
|
|
|
import com.qmth.teachcloud.common.enums.PrivilegeEnum;
|
|
|
-import com.qmth.teachcloud.common.enums.log.CustomizedOperationTypeEnum;
|
|
|
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
|
|
|
import com.qmth.teachcloud.common.service.BasicOperationLogService;
|
|
|
import com.qmth.teachcloud.common.service.SysPrivilegeService;
|
|
@@ -66,17 +65,10 @@ public class LogAspect {
|
|
|
*/
|
|
|
@Around(value = "operationLog()")
|
|
|
public Object aroundOperationLogPoint(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
- Object res = null;
|
|
|
- long beginTime = System.currentTimeMillis();
|
|
|
- long endTime = 0;
|
|
|
String runStatus = "成功";
|
|
|
try {
|
|
|
- res = joinPoint.proceed();
|
|
|
- endTime = System.currentTimeMillis();
|
|
|
- return res;
|
|
|
+ return joinPoint.proceed();
|
|
|
} catch (Exception e) {
|
|
|
- endTime = System.currentTimeMillis();
|
|
|
- res = "Exception: " + e.getMessage();
|
|
|
runStatus = "失败";
|
|
|
log.error(SystemConstant.LOG_ERROR, e);
|
|
|
if (e instanceof ApiException) {
|
|
@@ -87,7 +79,7 @@ public class LogAspect {
|
|
|
} finally {
|
|
|
try {
|
|
|
//方法执行完成后增加日志
|
|
|
- addOperationLog(joinPoint, res, endTime - beginTime, runStatus);
|
|
|
+ addOperationLog(joinPoint, runStatus);
|
|
|
} catch (Exception e) {
|
|
|
log.error("LogAspect 操作失败:" + e.getMessage());
|
|
|
e.printStackTrace();
|
|
@@ -100,18 +92,13 @@ public class LogAspect {
|
|
|
* 记录日志操作
|
|
|
*
|
|
|
* @param joinPoint joinPoint
|
|
|
- * @param res 返回值
|
|
|
- * @param time 方法执行时间
|
|
|
* @param runStatus 方法执行状态
|
|
|
*/
|
|
|
- private void addOperationLog(JoinPoint joinPoint, Object res, long time, String runStatus) throws IllegalAccessException {
|
|
|
- SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
- String userName = requestUser.getLoginName();
|
|
|
+ private void addOperationLog(JoinPoint joinPoint, String runStatus) throws IllegalAccessException {
|
|
|
+ Long userId = ServletUtil.getRequestUserId();
|
|
|
HttpServletRequest request = ServletUtil.getRequest();
|
|
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
Long privilegeId = ServletUtil.getCurrentPrivilegeId();
|
|
|
- String clazzName = signature.getDeclaringTypeName();
|
|
|
- String methodName = signature.getName();
|
|
|
Object[] args = joinPoint.getArgs();
|
|
|
String[] paramsName = signature.getParameterNames();
|
|
|
|
|
@@ -120,127 +107,99 @@ public class LogAspect {
|
|
|
// 方法中的信息
|
|
|
operationLog.setId(SystemConstant.getDbUuid());
|
|
|
operationLog.setSchoolId(Objects.nonNull(ServletUtil.getRequestHeaderSchoolIdByNotVaild()) ? Long.valueOf(ServletUtil.getRequestHeaderSchoolIdByNotVaild().toString()) : null);
|
|
|
+ operationLog.setUserId(userId);
|
|
|
+ operationLog.setIp(ServletUtil.getIP());
|
|
|
operationLog.setUrl(request.getServletPath());
|
|
|
- operationLog.setClazzName(clazzName);
|
|
|
- operationLog.setMethod(methodName);
|
|
|
- operationLog.setArgs(this.getTranslateArgs(paramsName, args));
|
|
|
- operationLog.setRunTime(time);
|
|
|
+ operationLog.setArgs(JSON.toJSONString(getTranslateArgs(paramsName, args)));
|
|
|
operationLog.setRunStatus(runStatus);
|
|
|
- operationLog.setReturnValue(JSON.toJSONString(res));
|
|
|
- operationLog.setUserId(requestUser.getId());
|
|
|
- operationLog.setUserName(userName);
|
|
|
- operationLog.setIp(ServletUtil.getIP());
|
|
|
operationLog.setCreateTime(System.currentTimeMillis());
|
|
|
- operationLog.setOrgId(requestUser.getOrgId());
|
|
|
- operationLog.setPrivilegeId(privilegeId);
|
|
|
|
|
|
// 注解中的信息
|
|
|
OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);
|
|
|
if (annotation != null) {
|
|
|
- String detail = annotation.detail();
|
|
|
- if (SystemConstant.strNotNull(detail)) {
|
|
|
- operationLog.setDetail(getDetail(((MethodSignature) joinPoint.getSignature()).getParameterNames(), joinPoint.getArgs(), annotation));
|
|
|
- } else {
|
|
|
- operationLog.setDetail(autoCreateOperationLogDetailByUrlAndPrivilegeId(request.getServletPath(),privilegeId));
|
|
|
+ OperationTypeEnum operationType = annotation.operationType();
|
|
|
+ operationLog.setOperationType(operationType);
|
|
|
+
|
|
|
+ if (OperationTypeEnum.SAVE.equals(operationType)) {
|
|
|
+ operationType = this.getAddOrUpdateByParam(args);
|
|
|
}
|
|
|
- operationLog.setLevel(annotation.level());
|
|
|
- CustomizedOperationTypeEnum customizedOperationType = annotation.customizedOperationType();
|
|
|
- if (CustomizedOperationTypeEnum.EDIT.equals(customizedOperationType)) {
|
|
|
- customizedOperationType = this.getAddOrUpdateByParam(args);
|
|
|
+ // 退出登录,不绑定权限ID
|
|
|
+ if (!OperationTypeEnum.LOGOUT.equals(operationType)) {
|
|
|
+ operationLog.setPrivilegeId(privilegeId);
|
|
|
}
|
|
|
|
|
|
- operationLog.setCustomizedOperationType(customizedOperationType);
|
|
|
-
|
|
|
+ // functionName
|
|
|
+ String functionName = annotation.functionName();
|
|
|
+ if (SystemConstant.strNotNull(functionName)) {
|
|
|
+ operationLog.setFunctionName(functionName);
|
|
|
+ } else {
|
|
|
+ operationLog.setFunctionName(getUrlName(request.getServletPath()));
|
|
|
+ }
|
|
|
|
|
|
- OperationTypeEnum operationType = annotation.operationType();
|
|
|
- if (OperationTypeEnum.UN_KNOW.equals(operationType)) {
|
|
|
- operationType = customizedOperationType.getOperationType();
|
|
|
+ // detail
|
|
|
+ String detail = annotation.detail();
|
|
|
+ if (SystemConstant.strNotNull(detail)) {
|
|
|
+ operationLog.setDetail(getDetail(paramsName, args, detail));
|
|
|
+ } else {
|
|
|
+ operationLog.setDetail(autoCreateOperationLogDetailByUrlAndPrivilegeId(request.getServletPath(), privilegeId));
|
|
|
}
|
|
|
- operationLog.setOperationType(operationType);
|
|
|
- operationLog.setOperationUnit(annotation.operationUnit());
|
|
|
- operationLog.setSystemType(annotation.system());
|
|
|
}
|
|
|
// 保存操作日志
|
|
|
basicOperationLogService.save(operationLog);
|
|
|
}
|
|
|
|
|
|
- private String getTranslateArgs(String[] paramsName, Object[] args) {
|
|
|
- List<LogArgsDto> logArgsDtoList = new ArrayList<>();
|
|
|
+ private Map<Object, Object> getTranslateArgs(String[] paramsName, Object[] args) {
|
|
|
+ Map<Object, Object> map = new HashMap<>();
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
- if (Objects.nonNull(args[i]) && (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse || args[i] instanceof CommonsMultipartFile || args[i] instanceof MultipartFile || args[i] instanceof BeanPropertyBindingResult)) {
|
|
|
+ if (Objects.isNull(args[i]) || (Objects.nonNull(args[i]) && (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse || args[i] instanceof CommonsMultipartFile || args[i] instanceof MultipartFile || args[i] instanceof BeanPropertyBindingResult))) {
|
|
|
continue;
|
|
|
}
|
|
|
- LogArgsDto logArgsDto = new LogArgsDto();
|
|
|
- logArgsDto.setArg(args[i]);
|
|
|
- logArgsDto.setParamName(paramsName[i]);
|
|
|
- logArgsDtoList.add(logArgsDto);
|
|
|
+ map.put(paramsName[i], args[i]);
|
|
|
}
|
|
|
- return JSON.toJSONString(logArgsDtoList);
|
|
|
+ return map;
|
|
|
}
|
|
|
|
|
|
- private CustomizedOperationTypeEnum getAddOrUpdateByParam(Object[] args) throws IllegalAccessException {
|
|
|
+ /**
|
|
|
+ * 处理SAVE类型方法,解析出新增或者修改
|
|
|
+ *
|
|
|
+ * @param args 请求参数
|
|
|
+ */
|
|
|
+ private OperationTypeEnum getAddOrUpdateByParam(Object[] args) throws IllegalAccessException {
|
|
|
+ boolean isAdd = false;
|
|
|
for (Object arg : args) {
|
|
|
if (Objects.nonNull(arg) && (arg instanceof HttpServletRequest || arg instanceof HttpServletResponse || arg instanceof MultipartFile || arg instanceof BeanPropertyBindingResult)) {
|
|
|
continue;
|
|
|
}
|
|
|
- Class<?> clazz = arg.getClass();
|
|
|
- Field[] fields = clazz.getDeclaredFields();
|
|
|
+ Field[] fields = arg.getClass().getFields();
|
|
|
for (Field field : fields) {
|
|
|
EditKey editKey = field.getAnnotation(EditKey.class);
|
|
|
if (editKey != null) {
|
|
|
// 捕捉到编辑对象的id
|
|
|
field.setAccessible(true);
|
|
|
Object obj = field.get(arg);
|
|
|
- if (Objects.nonNull(obj)) {
|
|
|
- return CustomizedOperationTypeEnum.UPDATE;
|
|
|
- } else {
|
|
|
- return CustomizedOperationTypeEnum.ADD;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- Class<?> supperClazz = clazz.getSuperclass();
|
|
|
- if (Objects.nonNull(supperClazz)) {
|
|
|
- Field[] supperFields = supperClazz.getDeclaredFields();
|
|
|
- for (Field field : supperFields) {
|
|
|
- EditKey editKey = field.getAnnotation(EditKey.class);
|
|
|
- if (editKey != null) {
|
|
|
- // 捕捉到编辑对象的id
|
|
|
- field.setAccessible(true);
|
|
|
- Object obj = field.get(arg);
|
|
|
- if (Objects.nonNull(obj)) {
|
|
|
- return CustomizedOperationTypeEnum.UPDATE;
|
|
|
- } else {
|
|
|
- return CustomizedOperationTypeEnum.ADD;
|
|
|
- }
|
|
|
+ if (Objects.isNull(obj)) {
|
|
|
+ isAdd = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return CustomizedOperationTypeEnum.UPDATE;
|
|
|
+ return isAdd ? OperationTypeEnum.ADD : OperationTypeEnum.UPDATE;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对当前登录用户和占位符处理
|
|
|
*
|
|
|
- * @param argNames 方法参数名称数组
|
|
|
+ * @param paramsName 方法参数名称数组
|
|
|
* @param args 方法参数数组
|
|
|
- * @param annotation 注解信息
|
|
|
+ * @param detail 注解信息
|
|
|
* @return 返回处理后的描述
|
|
|
*/
|
|
|
- private String getDetail(String[] argNames, Object[] args, OperationLogDetail annotation) {
|
|
|
+ private String getDetail(String[] paramsName, Object[] args, String detail) {
|
|
|
SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
|
|
|
- Map<Object, Object> map = new HashMap<>();
|
|
|
- for (int i = 0; i < argNames.length; i++) {
|
|
|
- if (Objects.nonNull(args[i]) && (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse || args[i] instanceof CommonsMultipartFile || args[i] instanceof MultipartFile || args[i] instanceof BeanPropertyBindingResult)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- map.put(argNames[i], args[i]);
|
|
|
- }
|
|
|
-
|
|
|
- String detail = annotation.detail();
|
|
|
+ Map<Object, Object> map = getTranslateArgs(paramsName, args);
|
|
|
try {
|
|
|
- detail = "'" + requestUser.getRealName() + "'=》" + annotation.detail();
|
|
|
+ detail = "'" + requestUser.getRealName() + "'=》" + detail;
|
|
|
for (Map.Entry<Object, Object> entry : map.entrySet()) {
|
|
|
Object k = entry.getKey();
|
|
|
Object v = entry.getValue();
|
|
@@ -252,34 +211,6 @@ public class LogAspect {
|
|
|
return detail;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据权限url自动创建操作日志详情
|
|
|
- *
|
|
|
- * @param url url
|
|
|
- * @return 操作日志详情
|
|
|
- */
|
|
|
- private String autoCreateOperationLogDetailByUrl(String url) {
|
|
|
- String detail = "";
|
|
|
- List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(new QueryWrapper<SysPrivilege>()
|
|
|
- .lambda()
|
|
|
- .eq(SysPrivilege::getUrl, url)
|
|
|
- .eq(SysPrivilege::getType, PrivilegeEnum.URL)
|
|
|
- .orderByAsc(SysPrivilege::getId));
|
|
|
- if (sysPrivilegeList.size() > 0) {
|
|
|
- SysPrivilege sysPrivilege = sysPrivilegeList.get(0);
|
|
|
- Long parentId = sysPrivilege.getParentId();
|
|
|
- String operationName = sysPrivilege.getName();
|
|
|
- String path = sysPrivilegeService.findPrivilegeUrlCatalog(parentId);
|
|
|
- if (SystemConstant.strNotNull(path)) {
|
|
|
- path = path.substring(0, path.length() - SystemConstant.CATALOG_LINK.length());
|
|
|
- detail = path + SystemConstant.OPERATE_LINK + operationName;
|
|
|
- } else {
|
|
|
- detail = operationName;
|
|
|
- }
|
|
|
- }
|
|
|
- return detail;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 根据权限url和所在菜单的权限id自动创建操作日志详情
|
|
|
*
|
|
@@ -305,6 +236,20 @@ public class LogAspect {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据url获取url名称
|
|
|
+ *
|
|
|
+ * @param url url
|
|
|
+ * @return url名称
|
|
|
+ */
|
|
|
+ private String getUrlName(String url) {
|
|
|
+ SysPrivilege sysPrivilege = sysPrivilegeService.getOne(new QueryWrapper<SysPrivilege>()
|
|
|
+ .lambda()
|
|
|
+ .eq(SysPrivilege::getUrl, url)
|
|
|
+ .eq(SysPrivilege::getType, PrivilegeEnum.URL));
|
|
|
+ return sysPrivilege == null ? null : sysPrivilege.getName();
|
|
|
+ }
|
|
|
+
|
|
|
@Before(value = "operationLog()")
|
|
|
public void doBeforeAdvice(JoinPoint joinPoint) {
|
|
|
log.info("进入方法前执行.....");
|