IntCode.php 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace addons\cms\library;
  3. use Hashids\Hashids;
  4. class IntCode
  5. {
  6. private static $hasids = null;
  7. /**
  8. * 初始化
  9. * @access public
  10. * @return Hashids
  11. */
  12. public static function hashids()
  13. {
  14. if (is_null(self::$hasids)) {
  15. $config = get_addon_config('cms');
  16. $key = $config['hashids_key'];
  17. $length = $config['hashids_key_length'] ?? 10;
  18. $key = $key ? $key : config('token.key');
  19. self::$hasids = new Hashids($key, $length);
  20. }
  21. return self::$hasids;
  22. }
  23. /**
  24. * 加密
  25. * @param $int
  26. * @return string
  27. */
  28. public static function encode($int)
  29. {
  30. return self::hashids()->encode($int);
  31. }
  32. /**
  33. * 解密
  34. * @param $str
  35. * @return string
  36. */
  37. public static function decode($str)
  38. {
  39. $data = self::hashids()->decode($str);
  40. if (isset($data[0])) {
  41. return $data[0];
  42. }
  43. return null;
  44. }
  45. }