疯狂的狮子li
2023-01-13 f3c6513d5ae0d5dc8f4bf2c8625d7edb85b7521d
ruoyi-framework/src/main/java/com/ruoyi/framework/handler/CreateAndUpdateMetaObjectHandler.java
@@ -6,7 +6,7 @@
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.LoginUtils;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
@@ -27,26 +27,16 @@
        try {
            if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
                BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
                Date current = new Date();
                // 创建时间为空 则填充
                if (ObjectUtil.isNull(baseEntity.getCreateTime())) {
                    baseEntity.setCreateTime(current);
                }
                // 更新时间为空 则填充
                if (ObjectUtil.isNull(baseEntity.getUpdateTime())) {
                    baseEntity.setUpdateTime(current);
                }
                String username = getLoginUsername();
                Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime())
                    ? baseEntity.getCreateTime() : new Date();
                baseEntity.setCreateTime(current);
                baseEntity.setUpdateTime(current);
                String username = StringUtils.isNotBlank(baseEntity.getCreateBy())
                    ? baseEntity.getCreateBy() : getLoginUsername();
                // 当前已登录 且 创建人为空 则填充
                if (StringUtils.isNotBlank(username)
                    && StringUtils.isBlank(baseEntity.getCreateBy())) {
                    baseEntity.setCreateBy(username);
                }
                baseEntity.setCreateBy(username);
                // 当前已登录 且 更新人为空 则填充
                if (StringUtils.isNotBlank(username)
                    && StringUtils.isBlank(baseEntity.getUpdateBy())) {
                    baseEntity.setUpdateBy(username);
                }
                baseEntity.setUpdateBy(username);
            }
        } catch (Exception e) {
            throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
@@ -72,18 +62,18 @@
        }
    }
   /**
    * 获取登录用户名
    */
   private String getLoginUsername() {
      LoginUser loginUser;
      try {
            loginUser = LoginUtils.getLoginUser();
      } catch (Exception e) {
         log.warn("自动注入警告 => 用户未登录");
         return null;
      }
      return loginUser.getUsername();
   }
    /**
     * 获取登录用户名
     */
    private String getLoginUsername() {
        LoginUser loginUser;
        try {
            loginUser = LoginHelper.getLoginUser();
        } catch (Exception e) {
            log.warn("自动注入警告 => 用户未登录");
            return null;
        }
        return loginUser.getUsername();
    }
}