!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,6 +30,7 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
baseEntity.setCreateTime(current); baseEntity.setCreateTime(current);
baseEntity.setUpdateTime(current); baseEntity.setUpdateTime(current);
LoginUser loginUser = getLoginUser(); LoginUser loginUser = getLoginUser();
if (ObjectUtil.isNotNull(loginUser)) {
Long userId = ObjectUtil.isNotNull(baseEntity.getCreateBy()) Long userId = ObjectUtil.isNotNull(baseEntity.getCreateBy())
? baseEntity.getCreateBy() : loginUser.getUserId(); ? baseEntity.getCreateBy() : loginUser.getUserId();
// 当前已登录 且 创建人为空 则填充 // 当前已登录 且 创建人为空 则填充
@ -39,6 +40,7 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
baseEntity.setCreateDept(ObjectUtil.isNotNull(baseEntity.getCreateDept()) baseEntity.setCreateDept(ObjectUtil.isNotNull(baseEntity.getCreateDept())
? baseEntity.getCreateDept() : loginUser.getDeptId()); ? 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