AdminUtils.java 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. *
  3. *
  4. *
  5. */
  6. package com.szwl.model.utils;
  7. /**
  8. * 处理id
  9. */
  10. public final class AdminUtils {
  11. private static final Long base1 = 2452l;
  12. private static final Long base2 = 13l;
  13. private static final String prefix = "u";
  14. /**
  15. * 将id转成加密样式
  16. */
  17. public static String encrypt(boolean isPrefix , Long id) {
  18. id = id * base2 + base1;
  19. if(isPrefix){
  20. return prefix + id.toString();
  21. }else{
  22. return id.toString();
  23. }
  24. }
  25. /**
  26. * 解密id
  27. */
  28. public static Long decrypt(boolean isPrefix , String uid) {
  29. try {
  30. if(isPrefix){
  31. uid = uid.substring(1, uid.length());
  32. }
  33. Long id = Long.valueOf(uid);
  34. id = id - base1;
  35. id = id / base2;
  36. return id;
  37. } catch (Exception e) {
  38. return null;
  39. }
  40. }
  41. public static void main(String[] args) {
  42. System.out.println(encrypt(false, 3L));
  43. System.out.println(decrypt(false, "2478"));
  44. }
  45. }