IntCode.php 802 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace app\common\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('shop');
  16. $key = $config['coupon_key'];
  17. $key = $key ? $key : config('token.key');
  18. self::$hasids = new Hashids($key, 4);
  19. }
  20. return self::$hasids;
  21. }
  22. public static function encode($int)
  23. {
  24. return self::hashids()->encode($int);
  25. }
  26. public static function decode($str)
  27. {
  28. $data = self::hashids()->decode($str);
  29. if (isset($data[0])) {
  30. return $data[0];
  31. }
  32. return null;
  33. }
  34. }