Prechádzať zdrojové kódy

update 优化自动注入器 异常检查更完善

疯狂的狮子li 3 rokov pred
rodič
commit
210b5af1c7

+ 19 - 6
ruoyi-framework/src/main/java/com/ruoyi/framework/mybatisplus/CreateAndUpdateMetaObjectHandler.java

@@ -1,7 +1,9 @@
 package com.ruoyi.framework.mybatisplus;
 
+import cn.hutool.core.lang.Validator;
 import cn.hutool.http.HttpStatus;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.exception.CustomException;
 import com.ruoyi.common.utils.SecurityUtils;
 import org.apache.ibatis.reflection.MetaObject;
@@ -21,13 +23,13 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
 		try {
 			//根据属性名字设置要填充的值
 			if (metaObject.hasGetter("createTime")) {
-				if (metaObject.getValue("createTime") == null) {
+				if (Validator.isEmpty(metaObject.getValue("createTime"))) {
 					this.setFieldValByName("createTime", new Date(), metaObject);
 				}
 			}
 			if (metaObject.hasGetter("createBy")) {
-				if (metaObject.getValue("createBy") == null) {
-					this.setFieldValByName("createBy", SecurityUtils.getUsername(), metaObject);
+				if (Validator.isEmpty(metaObject.getValue("createBy"))) {
+					this.setFieldValByName("createBy", getLoginUsername(), metaObject);
 				}
 			}
 		} catch (Exception e) {
@@ -39,12 +41,12 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
 	public void updateFill(MetaObject metaObject) {
 		try {
 			if (metaObject.hasGetter("updateBy")) {
-				if (metaObject.getValue("updateBy") == null) {
-					this.setFieldValByName("updateBy", SecurityUtils.getUsername(), metaObject);
+				if (Validator.isEmpty(metaObject.getValue("updateBy"))) {
+					this.setFieldValByName("updateBy", getLoginUsername(), metaObject);
 				}
 			}
 			if (metaObject.hasGetter("updateTime")) {
-				if (metaObject.getValue("updateTime") == null) {
+				if (Validator.isEmpty(metaObject.getValue("updateTime"))) {
 					this.setFieldValByName("updateTime", new Date(), metaObject);
 				}
 			}
@@ -53,4 +55,15 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
 		}
 	}
 
+	/**
+	 * 获取登录用户名
+	 */
+	private String getLoginUsername() {
+		LoginUser loginUser = SecurityUtils.getLoginUser();
+		if (Validator.isEmpty(loginUser)) {
+			throw new CustomException("用户未登录 => 无法获取用户信息");
+		}
+		return loginUser.getUsername();
+	}
+
 }