Skip to content

Commit

Permalink
chore: 忽略获取在线用户信息异常
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Nov 16, 2024
1 parent 6abb444 commit 4856366
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.extension.crud.model.entity.BaseDO;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -57,23 +56,19 @@ public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {
*/
@Override
public void insertFill(MetaObject metaObject) {
try {
if (null == metaObject) {
return;
}
Long createUser = UserContextHolder.getUserId();
LocalDateTime createTime = LocalDateTime.now();
if (metaObject.getOriginalObject() instanceof BaseDO baseDO) {
// 继承了 BaseDO 的类,填充创建信息字段
baseDO.setCreateUser(ObjectUtil.defaultIfNull(baseDO.getCreateUser(), createUser));
baseDO.setCreateTime(ObjectUtil.defaultIfNull(baseDO.getCreateTime(), createTime));
} else {
// 未继承 BaseDO 的类,如存在创建信息字段则进行填充
this.fillFieldValue(metaObject, CREATE_USER, createUser, false);
this.fillFieldValue(metaObject, CREATE_TIME, createTime, false);
}
} catch (Exception e) {
throw new BusinessException("插入数据时自动填充异常:" + e.getMessage());
if (null == metaObject) {
return;
}
Long createUser = UserContextHolder.getUserId();
LocalDateTime createTime = LocalDateTime.now();
if (metaObject.getOriginalObject() instanceof BaseDO baseDO) {
// 继承了 BaseDO 的类,填充创建信息字段
baseDO.setCreateUser(ObjectUtil.defaultIfNull(baseDO.getCreateUser(), createUser));
baseDO.setCreateTime(ObjectUtil.defaultIfNull(baseDO.getCreateTime(), createTime));
} else {
// 未继承 BaseDO 的类,如存在创建信息字段则进行填充
this.fillFieldValue(metaObject, CREATE_USER, createUser, false);
this.fillFieldValue(metaObject, CREATE_TIME, createTime, false);
}
}

Expand All @@ -84,23 +79,19 @@ public void insertFill(MetaObject metaObject) {
*/
@Override
public void updateFill(MetaObject metaObject) {
try {
if (null == metaObject) {
return;
}
Long updateUser = UserContextHolder.getUserId();
LocalDateTime updateTime = LocalDateTime.now();
if (metaObject.getOriginalObject() instanceof BaseDO baseDO) {
// 继承了 BaseDO 的类,填充修改信息
baseDO.setUpdateUser(updateUser);
baseDO.setUpdateTime(updateTime);
} else {
// 未继承 BaseDO 的类,根据类中拥有的修改信息字段进行填充,不存在修改信息字段不进行填充
this.fillFieldValue(metaObject, UPDATE_USER, updateUser, true);
this.fillFieldValue(metaObject, UPDATE_TIME, updateTime, true);
}
} catch (Exception e) {
throw new BusinessException("修改数据时自动填充异常:" + e.getMessage());
if (null == metaObject) {
return;
}
Long updateUser = UserContextHolder.getUserId();
LocalDateTime updateTime = LocalDateTime.now();
if (metaObject.getOriginalObject() instanceof BaseDO baseDO) {
// 继承了 BaseDO 的类,填充修改信息
baseDO.setUpdateUser(updateUser);
baseDO.setUpdateTime(updateTime);
} else {
// 未继承 BaseDO 的类,根据类中拥有的修改信息字段进行填充,不存在修改信息字段不进行填充
this.fillFieldValue(metaObject, UPDATE_USER, updateUser, true);
this.fillFieldValue(metaObject, UPDATE_TIME, updateTime, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import top.continew.starter.core.util.ExceptionUtils;
import top.continew.starter.extension.crud.service.CommonUserService;

import java.util.Optional;

/**
* 用户上下文 Holder
*
Expand Down Expand Up @@ -142,7 +140,7 @@ public static void clearContext() {
* @return 用户 ID
*/
public static Long getUserId() {
return Optional.ofNullable(getContext()).map(UserContext::getId).orElse(null);
return ExceptionUtils.exToNull(() -> getContext().getId());
}

/**
Expand All @@ -151,7 +149,7 @@ public static Long getUserId() {
* @return 用户名
*/
public static String getUsername() {
return Optional.ofNullable(getContext()).map(UserContext::getUsername).orElse(null);
return ExceptionUtils.exToNull(() -> getContext().getUsername());
}

/**
Expand Down

0 comments on commit 4856366

Please sign in to comment.