!108 修复自动填充数据loginUser为null

Merge pull request !108 from charles/2.X
2.X
charles 1 year ago committed by 疯狂的狮子Li
parent 22c1a9618a
commit 1b032774f5

@ -8,7 +8,7 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.dromara.common.core.factory.YmlPropertySourceFactory; import org.dromara.common.core.factory.YmlPropertySourceFactory;
import org.dromara.common.mybatis.handler.CreateAndUpdateMetaObjectHandler; import org.dromara.common.mybatis.handler.InjectionMetaObjectHandler;
import org.dromara.common.mybatis.interceptor.PlusDataPermissionInterceptor; import org.dromara.common.mybatis.interceptor.PlusDataPermissionInterceptor;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
@ -71,7 +71,7 @@ public class MybatisPlusConfiguration {
*/ */
@Bean @Bean
public MetaObjectHandler metaObjectHandler() { public MetaObjectHandler metaObjectHandler() {
return new CreateAndUpdateMetaObjectHandler(); return new InjectionMetaObjectHandler();
} }
/** /**

@ -18,7 +18,7 @@ import java.util.Date;
* @author Lion Li * @author Lion Li
*/ */
@Slf4j @Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler { public class InjectionMetaObjectHandler implements MetaObjectHandler {
@Override @Override
public void insertFill(MetaObject metaObject) { public void insertFill(MetaObject metaObject) {
@ -30,14 +30,16 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
baseEntity.setCreateTime(current); baseEntity.setCreateTime(current);
baseEntity.setUpdateTime(current); baseEntity.setUpdateTime(current);
LoginUser loginUser = getLoginUser(); LoginUser loginUser = getLoginUser();
Long userId = ObjectUtil.isNotNull(baseEntity.getCreateBy()) if (ObjectUtil.isNotNull(loginUser)) {
? baseEntity.getCreateBy() : loginUser.getUserId(); Long userId = ObjectUtil.isNotNull(baseEntity.getCreateBy())
// 当前已登录 且 创建人为空 则填充 ? baseEntity.getCreateBy() : loginUser.getUserId();
baseEntity.setCreateBy(userId); // 当前已登录 且 创建人为空 则填充
// 当前已登录 且 更新人为空 则填充 baseEntity.setCreateBy(userId);
baseEntity.setUpdateBy(userId); // 当前已登录 且 更新人为空 则填充
baseEntity.setCreateDept(ObjectUtil.isNotNull(baseEntity.getCreateDept()) baseEntity.setUpdateBy(userId);
? baseEntity.getCreateDept() : loginUser.getDeptId()); baseEntity.setCreateDept(ObjectUtil.isNotNull(baseEntity.getCreateDept())
? baseEntity.getCreateDept() : loginUser.getDeptId());
}
} }
} catch (Exception e) { } catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
Loading…
Cancel
Save