Hashids.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace app\utils;
  3. use app\utils\Hashids\Hashids as HashidsBase;
  4. use think\Env;
  5. use think\Config;
  6. class Hashids
  7. {
  8. private static $hashids;
  9. /**
  10. * 单列模型实例化
  11. * @param $salt
  12. * @param $hashLength
  13. * @return HashidsBase
  14. */
  15. public static function getInstanceHashids($salt, $hashLength)
  16. {
  17. if (!self::$hashids instanceof HashidsBase) {
  18. self::$hashids = new HashidsBase($salt, $hashLength, Env::get('hashids.alphabet', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'));
  19. }
  20. return self::$hashids;
  21. }
  22. public static function encodeHex($str, $hashLength = 8)
  23. {
  24. $salt = Env::get('hashids.salt', Config::get('token.key'));
  25. $hashids = self::getInstanceHashids($salt, $hashLength);
  26. return $hashids->encodeHex($str);
  27. }
  28. public static function decodeHex($str, $hashLength = 8)
  29. {
  30. $salt = Env::get('hashids.salt', Config::get('token.key'));
  31. $hashids = self::getInstanceHashids($salt, $hashLength);
  32. return $hashids->decodeHex($str);
  33. }
  34. }