package com.dk.common.infrastructure.util; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.dk.common.util.oauth.AESSecurityUtil; import com.dk.common.util.oauth.JwtUtil; import groovy.util.logging.Slf4j; import lombok.SneakyThrows; import org.apache.ibatis.reflection.MetaObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Slf4j @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Value("${aes-key}") private String AESKey; @Autowired private HttpServletRequest httpServletRequest; @Override @SneakyThrows public void insertFill(MetaObject metaObject) { if(httpServletRequest!=null){ String authorization = httpServletRequest.getHeader("Authorization"); if(authorization != null){ String[] tokens = authorization.split(" "); String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]); String cpIdString = JwtUtil.getCpId(decrypt); if (cpIdString!=null){ //设置企业Id Integer cpId = Integer.valueOf(cpIdString); this.strictInsertFill(metaObject, "cpId", Integer.class, cpId); // 起始版本 3.3.0(推荐使用) } // // 或者 // this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐) // // 或者 // this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug) } } } @Override public void updateFill(MetaObject metaObject) { // this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐) // // 或者 // this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐) // // 或者 // this.fillStrategy(metaObject, "updateTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug) } }