新增:个人中心新增查询操作日志功能,优化日志表结构,并支持关闭记录内网 IP 操作

This commit is contained in:
2023-01-16 00:18:53 +08:00
parent 79c741b68e
commit f4ea2d44d6
36 changed files with 1272 additions and 119 deletions

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import cn.hutool.core.util.ReflectUtil;
/**
* 反射工具类
*
* @author Charles7c
* @since 2023/1/15 22:05
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ReflectUtils {
/**
* 获得一个类中所有非静态字段名列表,包括其父类中的字段<br>
* 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。
*
* @param beanClass
* 类
* @return 非静态字段名列表
* @throws SecurityException
* 安全检查异常
*/
public static String[] getNonStaticFieldsName(Class<?> beanClass) throws SecurityException {
Field[] nonStaticFields = getNonStaticFields(beanClass);
return Arrays.stream(nonStaticFields).map(Field::getName).toArray(String[]::new);
}
/**
* 获得一个类中所有非静态字段列表,包括其父类中的字段<br>
* 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。
*
* @param beanClass
* 类
* @return 非静态字段列表
* @throws SecurityException
* 安全检查异常
*/
public static Field[] getNonStaticFields(Class<?> beanClass) throws SecurityException {
Field[] fields = ReflectUtil.getFields(beanClass);
return Arrays.stream(fields).filter(f -> !Modifier.isStatic(f.getModifiers())).toArray(Field[]::new);
}
}

View File

@@ -0,0 +1,216 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util.helper;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.annotation.Query;
/**
* 查询助手
*
* @author Charles7c
* @since 2023/1/15 18:17
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class QueryHelper {
/**
* 根据查询条件构建 MyBatis Plus 查询条件封装对象
*
* @param query
* 查询条件
* @param <Q>
* 查询条件数据类型
* @param <R>
* 查询数据类型
* @return MyBatis Plus 查询条件封装对象
*/
public static <Q, R> QueryWrapper<R> build(Q query) {
QueryWrapper<R> queryWrapper = Wrappers.query();
// 没有查询条件,直接返回
if (query == null) {
return queryWrapper;
}
// 获取查询条件中所有的属性(包括私有的和父类的)
List<Field> fieldList = getFieldList(query.getClass(), new ArrayList<>());
fieldList.forEach(field -> buildQuery(query, field, queryWrapper));
return queryWrapper;
}
/**
* 获取指定类的所有属性(包括私有的和父类的)
*
* @param clazz
* 指定类
* @param fieldList
* 属性列表
* @param <Q>
* 查询条件数据类型
* @return 属性列表(包括私有的和父类的)
*/
public static <Q> List<Field> getFieldList(Class<Q> clazz, List<Field> fieldList) {
if (clazz != null) {
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
getFieldList(clazz.getSuperclass(), fieldList);
}
return fieldList;
}
/**
* 构建 MyBatis Plus 查询条件封装对象
*
* @param query
* 查询条件
* @param field
* 属性
* @param queryWrapper
* MyBatis Plus 查询条件封装对象
* @param <Q>
* 查询条件数据类型
* @param <R>
* 查询数据类型
*/
private static <Q, R> void buildQuery(Q query, Field field, QueryWrapper<R> queryWrapper) {
boolean accessible = field.isAccessible();
try {
field.setAccessible(true);
// 没有 @Query直接返回
Query queryAnnotation = field.getAnnotation(Query.class);
if (queryAnnotation == null) {
return;
}
// 如果属性值为空,直接返回
Object fieldValue = field.get(query);
if (ObjectUtil.isEmpty(fieldValue)) {
return;
}
// 解析查询条件
parse(queryAnnotation, field.getName(), fieldValue, queryWrapper);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
field.setAccessible(accessible);
}
}
/**
* 解析查询条件
*
* @param queryAnnotation
* 查询注解
* @param fieldName
* 属性名
* @param fieldValue
* 属性值
* @param queryWrapper
* MyBatis Plus 查询条件封装对象
* @param <R>
* 查询数据类型
*/
private static <R> void parse(Query queryAnnotation, String fieldName, Object fieldValue,
QueryWrapper<R> queryWrapper) {
// 解析多属性模糊查询
// 如果设置了多属性模糊查询,分割属性进行条件拼接
String blurry = queryAnnotation.blurry();
if (StrUtil.isNotBlank(blurry)) {
String[] propertyArr = blurry.split(",");
queryWrapper.and(wrapper -> {
for (String property : propertyArr) {
wrapper.or().like(StrUtil.toUnderlineCase(property), fieldValue);
}
});
return;
}
// 解析单个属性查询
// 如果没有单独指定属性名,就和使用该注解的属性的名称一致
// 注意:数据库规范中列采用下划线连接法命名,程序规范中变量采用驼峰法命名
String property = queryAnnotation.property();
fieldName = StrUtil.isNotBlank(property) ? property : fieldName;
String columnName = StrUtil.toUnderlineCase(fieldName);
switch (queryAnnotation.type()) {
case EQUAL:
queryWrapper.eq(columnName, fieldValue);
break;
case NOT_EQUAL:
queryWrapper.ne(columnName, fieldValue);
break;
case GREATER_THAN:
queryWrapper.gt(columnName, fieldValue);
break;
case LESS_THAN:
queryWrapper.lt(columnName, fieldValue);
break;
case GREATER_THAN_OR_EQUAL:
queryWrapper.ge(columnName, fieldValue);
break;
case LESS_THAN_OR_EQUAL:
queryWrapper.le(columnName, fieldValue);
break;
case BETWEEN:
List<Object> between = new ArrayList<>((List<Object>)fieldValue);
queryWrapper.between(columnName, between.get(0), between.get(1));
break;
case LEFT_LIKE:
queryWrapper.likeLeft(columnName, fieldValue);
break;
case INNER_LIKE:
queryWrapper.like(columnName, fieldValue);
break;
case RIGHT_LIKE:
queryWrapper.likeRight(columnName, fieldValue);
break;
case IN:
if (CollUtil.isNotEmpty((List<Object>)fieldValue)) {
queryWrapper.in(columnName, (List<Object>)fieldValue);
}
break;
case NOT_IN:
if (CollUtil.isNotEmpty((List<Object>)fieldValue)) {
queryWrapper.notIn(columnName, (List<Object>)fieldValue);
}
break;
case IS_NULL:
queryWrapper.isNull(columnName);
break;
case NOT_NULL:
queryWrapper.isNotNull(columnName);
break;
default:
break;
}
}
}

View File

@@ -19,10 +19,10 @@ package top.charles7c.cnadmin.common.util.holder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import top.charles7c.cnadmin.common.model.dto.OperationLog;
import top.charles7c.cnadmin.common.model.dto.LogContext;
/**
* 操作日志上下文持有者
* 系统日志上下文持有者
*
* @author Charles7c
* @since 2022/12/25 8:55
@@ -30,29 +30,29 @@ import top.charles7c.cnadmin.common.model.dto.OperationLog;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class LogContextHolder {
private static final ThreadLocal<OperationLog> LOG_THREAD_LOCAL = new ThreadLocal<>();
private static final ThreadLocal<LogContext> LOG_THREAD_LOCAL = new ThreadLocal<>();
/**
* 存储操作日志
* 存储系统日志上下文
*
* @param operationLog
* 操作日志信息
* @param logContext
* 系统日志上下文信息
*/
public static void set(OperationLog operationLog) {
LOG_THREAD_LOCAL.set(operationLog);
public static void set(LogContext logContext) {
LOG_THREAD_LOCAL.set(logContext);
}
/**
* 获取操作日志
* 获取系统日志上下文
*
* @return 操作日志信息
* @return 系统日志上下文信息
*/
public static OperationLog get() {
public static LogContext get() {
return LOG_THREAD_LOCAL.get();
}
/**
* 移除操作日志
* 移除系统日志上下文
*/
public static void remove() {
LOG_THREAD_LOCAL.remove();