mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 优化自动注入器 异常检查更完善
This commit is contained in:
		@@ -1,7 +1,9 @@
 | 
				
			|||||||
package com.ruoyi.framework.mybatisplus;
 | 
					package com.ruoyi.framework.mybatisplus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import cn.hutool.core.lang.Validator;
 | 
				
			||||||
import cn.hutool.http.HttpStatus;
 | 
					import cn.hutool.http.HttpStatus;
 | 
				
			||||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 | 
					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.exception.CustomException;
 | 
				
			||||||
import com.ruoyi.common.utils.SecurityUtils;
 | 
					import com.ruoyi.common.utils.SecurityUtils;
 | 
				
			||||||
import org.apache.ibatis.reflection.MetaObject;
 | 
					import org.apache.ibatis.reflection.MetaObject;
 | 
				
			||||||
@@ -21,13 +23,13 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
 | 
				
			|||||||
		try {
 | 
							try {
 | 
				
			||||||
			//根据属性名字设置要填充的值
 | 
								//根据属性名字设置要填充的值
 | 
				
			||||||
			if (metaObject.hasGetter("createTime")) {
 | 
								if (metaObject.hasGetter("createTime")) {
 | 
				
			||||||
				if (metaObject.getValue("createTime") == null) {
 | 
									if (Validator.isEmpty(metaObject.getValue("createTime"))) {
 | 
				
			||||||
					this.setFieldValByName("createTime", new Date(), metaObject);
 | 
										this.setFieldValByName("createTime", new Date(), metaObject);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if (metaObject.hasGetter("createBy")) {
 | 
								if (metaObject.hasGetter("createBy")) {
 | 
				
			||||||
				if (metaObject.getValue("createBy") == null) {
 | 
									if (Validator.isEmpty(metaObject.getValue("createBy"))) {
 | 
				
			||||||
					this.setFieldValByName("createBy", SecurityUtils.getUsername(), metaObject);
 | 
										this.setFieldValByName("createBy", getLoginUsername(), metaObject);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} catch (Exception e) {
 | 
							} catch (Exception e) {
 | 
				
			||||||
@@ -39,12 +41,12 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
 | 
				
			|||||||
	public void updateFill(MetaObject metaObject) {
 | 
						public void updateFill(MetaObject metaObject) {
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			if (metaObject.hasGetter("updateBy")) {
 | 
								if (metaObject.hasGetter("updateBy")) {
 | 
				
			||||||
				if (metaObject.getValue("updateBy") == null) {
 | 
									if (Validator.isEmpty(metaObject.getValue("updateBy"))) {
 | 
				
			||||||
					this.setFieldValByName("updateBy", SecurityUtils.getUsername(), metaObject);
 | 
										this.setFieldValByName("updateBy", getLoginUsername(), metaObject);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if (metaObject.hasGetter("updateTime")) {
 | 
								if (metaObject.hasGetter("updateTime")) {
 | 
				
			||||||
				if (metaObject.getValue("updateTime") == null) {
 | 
									if (Validator.isEmpty(metaObject.getValue("updateTime"))) {
 | 
				
			||||||
					this.setFieldValByName("updateTime", new Date(), metaObject);
 | 
										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();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user