MyMetaObjectHandler.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.dk.common.infrastructure.util;
  2. import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
  3. import com.dk.common.util.oauth.AESSecurityUtil;
  4. import com.dk.common.util.oauth.JwtUtil;
  5. import groovy.util.logging.Slf4j;
  6. import lombok.SneakyThrows;
  7. import org.apache.ibatis.reflection.MetaObject;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import javax.servlet.http.HttpServletRequest;
  12. @Slf4j
  13. @Component
  14. public class MyMetaObjectHandler implements MetaObjectHandler {
  15. @Value("${aes-key}")
  16. private String AESKey;
  17. @Autowired
  18. private HttpServletRequest httpServletRequest;
  19. @Override
  20. @SneakyThrows
  21. public void insertFill(MetaObject metaObject) {
  22. if(httpServletRequest!=null){
  23. String authorization = httpServletRequest.getHeader("Authorization");
  24. if(authorization != null){
  25. String[] tokens = authorization.split(" ");
  26. String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
  27. String cpIdString = JwtUtil.getCpId(decrypt);
  28. if (cpIdString!=null){
  29. //设置企业Id
  30. Integer cpId = Integer.valueOf(cpIdString);
  31. this.strictInsertFill(metaObject, "cpId", Integer.class, cpId); // 起始版本 3.3.0(推荐使用)
  32. }
  33. // // 或者
  34. // this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
  35. // // 或者
  36. // this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)
  37. }
  38. }
  39. }
  40. @Override
  41. public void updateFill(MetaObject metaObject) {
  42. // this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
  43. // // 或者
  44. // this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
  45. // // 或者
  46. // this.fillStrategy(metaObject, "updateTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)
  47. }
  48. }