Hashids.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/1/7
  6. * Time: 9:05 下午
  7. */
  8. namespace addons\unishop\extend;
  9. use addons\unishop\model\Config;
  10. class Hashids
  11. {
  12. private static $hashids;
  13. /**
  14. * 单列模型实例化
  15. * @param $salt
  16. * @param $hashLength
  17. * @return \Hashids\Hashids
  18. */
  19. public static function getInstanceHashids($salt, $hashLength)
  20. {
  21. if (!self::$hashids instanceof \Hashids\Hashids) {
  22. self::$hashids = new \Hashids\Hashids($salt, $hashLength);
  23. }
  24. return self::$hashids;
  25. }
  26. public static function encodeHex($str, $hashLength = 5)
  27. {
  28. $salt = Config::getByName('salt')['value'];
  29. $hashids = self::getInstanceHashids($salt, $hashLength);
  30. return $hashids->encodeHex($str);
  31. }
  32. public static function decodeHex($str, $hashLength = 5)
  33. {
  34. $salt = Config::getByName('salt')['value'];
  35. $hashids = self::getInstanceHashids($salt, $hashLength);
  36. return $hashids->decodeHex($str);
  37. }
  38. }